Hi,
I have following situation:
my Linux host has two interfaces eth0 (192.168.254.254) and eth1 (192.168.253.254). For the test purpose I want connect these two port with a cable and make some data exchange. For both the server and client socket I make bind(). The IP addresses seem to be assigned properly. When I start the test and lo interface is up everything runs successfully but not over the cable. When I make lo interface down I don't get any connection. I also tried to set route for these hosts but with the same result. What am I doing wrong?
Code:
// server init code
int open_server(char *interface, char *server_ip, struct _tester *tester)
{
int sockfd, i;
struct sockaddr_in *server;
struct ifreq ifr;
printf("Server:\nInterface: %s\nIP: %s\n", interface, server_ip);
memset(&ifr, 0, sizeof(struct ifreq));
if((sockfd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
{
perror("Socket");
return TST_ERROR;
}
i = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
{
perror("setsockopt");
return TST_ERROR;
}
server = (struct sockaddr_in *)&(ifr.ifr_addr);
server->sin_family = AF_INET;
server->sin_port = htons(SERVER_PORT);
inet_aton(server_ip, &server->sin_addr);
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if(ioctl(sockfd, SIOCSIFADDR, &ifr) < 0)
{
perror("IOCTL (SIOCSIFADDR)");
return TST_ERROR;
}
// bring interface up
ifr.ifr_flags = IFF_UP;
if(ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("IOCTL (SIOCSIFADDR)");
return TST_ERROR;
}
if(bind(sockfd, (struct sockaddr *)server, sizeof(struct sockaddr_in)) < 0)
{
perror("Bind");
return TST_ERROR;
}
if(listen(sockfd, 5) < 0)
{
perror("Listen");
return TST_ERROR;
}
printf("IP assigned %s\n", inet_ntoa(server->sin_addr));
return sockfd;
}
Code:
// client init code
int open_client(char *interface, char *client_ip, struct _tester *tester)
{
int sockfd, i;
struct sockaddr_in *client;
struct ifreq ifr;
printf("Client:\nInterface: %s\nIP: %s\n", interface, client_ip);
memset(&ifr, 0, sizeof(struct ifreq));
if((sockfd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
perror("Socket");
i = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
client = (struct sockaddr_in *)&(ifr.ifr_addr);
client->sin_family = AF_INET;
client->sin_port = htons(SERVER_PORT + 1);
inet_aton(client_ip, &client->sin_addr);
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if(ioctl(sockfd, SIOCSIFADDR, &ifr) < 0)
perror("IOCTL (SIOCSIFADDR)");
// bring interface up
ifr.ifr_flags = IFF_UP;
if(ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
perror("IOCTL (SIOCSIFADDR)");
if(bind(sockfd, (struct sockaddr *)client, sizeof(struct sockaddr_in)) < 0)
{
perror("Bind");
return TST_ERROR;
}
return sockfd;
}
Thanks in advance