Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > infinite loop with select

Reply
Thread Tools

infinite loop with select

 
 
Andreas Mueller
Guest
Posts: n/a
 
      06-05-2007
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 / );
}
}
}


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;
}
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      06-05-2007
In article <f43qmj$ta5$(E-Mail Removed)-tuebingen.de>,
Andreas Mueller <(E-Mail Removed)> wrote:
>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

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
In fact, I go to considerable lengths to maintain the self-imposed delusion
that I live in a fantasy world, one in which software is easy and fun to write,
and works right the first time --Steve Summit in comp.lang.c
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      06-05-2007
>In article <f43qmj$ta5$(E-Mail Removed)-tuebingen.de>
>Andreas Mueller <(E-Mail Removed)> wrote:
>>I'm having a problem using select with a Raw-Socket. ...


In article <f440hs$f6l$(E-Mail Removed)>,
Dave Vandervies <(E-Mail Removed)> wrote:
>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:

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


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?
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      06-05-2007
Andreas Mueller wrote:
>
> 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 / );
> }
> }
> }

[...]

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: <(E-Mail Removed)>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
infinite loop unexpectly dies Vedran Vukotic Perl 0 03-02-2006 07:43 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 11 11-10-2004 08:53 AM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments