infinite loop with select

Discussion in 'C Programming' started by Andreas Mueller, Jun 5, 2007.

  1. 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;
    }
    Andreas Mueller, Jun 5, 2007
    #1
    1. Advertising

  2. In article <f43qmj$ta5$-tuebingen.de>,
    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?


    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
    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
    Dave Vandervies, Jun 5, 2007
    #2
    1. Advertising

  3. Andreas Mueller

    Chris Torek Guest

    >In article <f43qmj$ta5$-tuebingen.de>
    >Andreas Mueller <> wrote:
    >>I'm having a problem using select with a Raw-Socket. ...


    In article <f440hs$f6l$>,
    Dave Vandervies <> 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 (40°39.22'N, 111°50.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.
    Chris Torek, Jun 5, 2007
    #3
  4. 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 / 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:>
    Kenneth Brody, Jun 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vedran Vukotic

    infinite loop unexpectly dies

    Vedran Vukotic, Mar 2, 2006, in forum: Perl
    Replies:
    0
    Views:
    4,450
    Vedran Vukotic
    Mar 2, 2006
  2. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 28, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    862
    Steven Cheng[MSFT]
    Nov 10, 2004
  3. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    538
    Steven Cheng[MSFT]
    Nov 1, 2004
  4. Replies:
    5
    Views:
    586
    benben
    Jan 31, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page