Reading from UDP socket on specific interface

This is a discussion on Reading from UDP socket on specific interface within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I'm currently trying to write software to test the routing functionality of a router, but whilst I can send ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Reading from UDP socket on specific interface

    Hi,

    I'm currently trying to write software to test the routing functionality of a router, but whilst I can send UDP data through the router and back to my testing machine I can't read the data at the destination interface.

    My UDP packets are traveling from 10.100.10.2 (Test Machine Eth0) -> Router -> 10.100.20.2 (Test Machine Eth1), verified by Wireshark - so my concern is the listening UDP socket. Note that this is only one possible route, so it's important that I can be certain data is arriving on the correct interface (of several) on the test machine.

    My development environment:
    Linux kernel : 2.6
    GCC : 3.4.6

    Some of my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define PORT  55443
    #define BSIZE 256
    
    void main()
    {
        struct sockaddr_in  sin;
        char   *device_ip[]      = { "10.100.10.2" };
        char   *buffer[ BSIZE ];
        int    sock;
        int    bytes_read;
        int    sin_len;
    
        /* Setup address */
        memset( &sin, 0, sizeof( sin ) );
        sin.sin_family = AF_INET;
        sin.sin_port = htons( PORT );
        sin.sin_addr.s_addr = inet_addr( device_ip );
        sin_len = sizeof( sin );
    
        /* Create socket */
        sock = socket( AF_INET, SOCK_DGRAM, 0 );
        if ( sock > 0 )
        {
            /* Display any data found */
            while( 1 )
            {
                bytes_read = recvfrom( sock, buffer, sizeof( buffer ), 0, ( struct sockaddr * ) &sin, &sin_len );
                if ( bytes_read > 0 )
                {
                    printf( "&#37;s\n", buffer );
                }
            }
        }
    }
    As you can see the code is very simple, so that why I'm so confused by this. I've also tried using bind to lock the UDP socket to the specific interface and using the sock option SO_BINDTODEVICE, both with no luck.

    There must be a way of forcing a UDP socket to read from a specific interface, without this I'm unable to automate the testing.

    Does anyone have any ideas on how to solve this?

    Thanks in advance,

    - U
    Last edited by uriel; 11-29-2007 at 03:07 PM. Reason: Woops, typo...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Code:
        char   *device_ip[]      = { "10.100.10.2" };
        char   *buffer[ BSIZE ];
    You've got an extra level of indirection. Remove stuff in red.

    gg

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    perhaps you should hae got an error at this stage, since inet_addr can just take char * but not char*[].

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. Reading from the Ethernet card.
    By guillermoh in forum C Programming
    Replies: 2
    Last Post: 02-07-2008, 06:41 PM
  3. looking for non-MFC based socket programming sample
    By George2 in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-19-2006, 06:57 AM
  4. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  5. OOP in C
    By lyx in forum C Programming
    Replies: 4
    Last Post: 11-23-2003, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21