infinite loop with select

  • Thread starter Andreas Mueller
  • Start date
A

Andreas Mueller

I'm having a problem using select with a Raw-Socket.
I get a packet, change the IP address (for sending it to a tun device)
and then I try to recalculate the IP-checksum.
If I do so, it loops forever with the same packet. If I don't use
either(!) ip->check = 0; or ip->check = in_cksum... the code works fine.

Does anybody see what the problem is?

thanks in advance,
Andreas


for( ;; ) {
FD_ZERO( &read_fds );
FD_SET( sUDP, &read_fds );
ret_val = select( sUDP + 1, &read_fds, NULL, NULL, NULL );
if( ret_val < 0 )
break;
if( ret_val > 0 ) {

if ( FD_ISSET( sUDP, &read_fds ) ) {
int lenInUDP = recvfrom(sUDP, &ethBufferUDP,
sizeof(ethBufferUDP), 0, 0, 0);
ip = (struct iphdr *) ethBufferUDP;

//change source address to interface address
ip->saddr = inet_addr("172.16.1.20");
//change destination address to 134.2.173.55
ip->daddr = inet_addr("172.16.1.1");

/***************PROBLEM HERE*******************************************/
ip->check = 0;
ip->check = in_cksum ((unsigned short *) ip, ip->ihl * (32 / 8));
}
}
}


here's the code for in_chksum:

unsigned short in_cksum (unsigned short *addr, int count) {
register long sum = 0;
register unsigned short checksum;

while (count > 1)
{
sum += *addr++;
count -= 2;
}
if (count > 0)
{
sum += *(unsigned char *) addr;
}
while (sum >> 16)
{
sum = (sum & 0xffff) + (sum >> 16);
}

checksum = ~sum;

return checksum;
}
 
D

Dave Vandervies

I'm having a problem using select with a Raw-Socket.
I get a packet, change the IP address (for sending it to a tun device)
and then I try to recalculate the IP-checksum.
If I do so, it loops forever with the same packet. If I don't use
either(!) ip->check = 0; or ip->check = in_cksum... the code works fine.

Does anybody see what the problem is?

For one thing, you're working with things not defined by the C language,
which puts your question beyond the scope of comp.lang.c.

Somewhere that discusses your platform and/or raw sockets would probably
be a better place to ask.


dave
 
C

Chris Torek

For one thing, you're working with things not defined by the C language,
which puts your question beyond the scope of comp.lang.c.

Somewhere that discusses your platform and/or raw sockets would probably
be a better place to ask.

Indeed. However, there is one thing (well, two really)
I can note here:

First, //-comments are C99-specific (and sometimes fail to survive
Usenet, although these did).

Second, the comment does not match the code. Why does one list
one address, and the other use another?
 
K

Kenneth Brody

Andreas said:
I'm having a problem using select with a Raw-Socket.
I get a packet, change the IP address (for sending it to a tun device)
and then I try to recalculate the IP-checksum.
If I do so, it loops forever with the same packet. If I don't use
either(!) ip->check = 0; or ip->check = in_cksum... the code works fine.

Does anybody see what the problem is?

thanks in advance,
Andreas

for( ;; ) {
FD_ZERO( &read_fds );
FD_SET( sUDP, &read_fds );
ret_val = select( sUDP + 1, &read_fds, NULL, NULL, NULL );
if( ret_val < 0 )
break;
if( ret_val > 0 ) {

if ( FD_ISSET( sUDP, &read_fds ) ) {
int lenInUDP = recvfrom(sUDP, &ethBufferUDP,
sizeof(ethBufferUDP), 0, 0, 0);
ip = (struct iphdr *) ethBufferUDP;

//change source address to interface address
ip->saddr = inet_addr("172.16.1.20");
//change destination address to 134.2.173.55
ip->daddr = inet_addr("172.16.1.1");

/***************PROBLEM HERE*******************************************/
ip->check = 0;
ip->check = in_cksum ((unsigned short *) ip, ip->ihl * (32 / 8));
}
}
}
[...]

Well, the use of sockets is beyond the scope of clc, but...

What is "ethBufferUDP"? When you point ip (a pointer to "struct
iphdr") there, are you certain that the buffer is large enough?
Is it possible that ip->check points beyond the buffer?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,531
Members
45,005
Latest member
MellisaSti

Latest Threads

Top