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( "%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