I have two wireless adapters, one USB adapter and one built in to my laptop.
I'd like to be able to use these two connections. So, in a toy example I bind two different sockets to the two different IP addresses and port numbers and call connect on each one.
However, when I examine my network traffic in wireshark...I only see traffic from one ip!? In fact, I see both calls to connect from one IP address despite the fact that I explicitly bind each socket.
Here is the code I'm using:
Note, I'm also using non-blocking sockets and select. The code I have for this has been verified to work for one internet connection.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <net/if.h>
#include <sys/ioctl.h>
int main () {
const char * destIp = "213.112.225.102";
const char * ip1 = "192.168.43.1";//"172.31.55.111";//"198.228.228.28";
int portNumber1 = 55555;
int sockFd1 = -1;
const char * ip2 = "192.168.1.1";//"98.249.5.16";
int portNumber2 = 7777;
int sockFd2 = -1;
struct sockaddr_in serverAddress;
serverAddress.sin_addr.s_addr = inet_pton(AF_INET, "213.112.225.102", &(serverAddress.sin_addr));
serverAddress.sin_port = htons(6985);
///////////////////////////////////////////
struct sockaddr * saddr;
struct addrinfo hints, * ai, * it;
char strportnum[] = "6985";
memset(&hints, '\0', sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(destIp, strportnum, &hints, &ai);
saddr = ai->ai_addr;
saddr->sa_family = AF_INET;
it = ai;
///////////////////////////////////////////////
//char * opt;
int res;
long arg;
fd_set myset;
struct timeval tv;
int valopt;
socklen_t lon;
struct sockaddr_in clientAddress;
struct sockaddr_in clientAddress2;
printf("it fam == ||%d||, AF_INET == ||%d||\n", it->ai_family, AF_INET);
printf("ATTEMPTING SOCKET 1!\n");
//IP 1 CONNECTION----------------------------------------------------------------------------------//
if ((sockFd1 = socket(it->ai_family, it->ai_socktype, it->ai_protocol)) != -1) {
system("route add -net 213.112.225.102 netmask 255.255.255.255 gw 192.168.43.1 dev wlp10s0");
struct ifreq interface1;
memset(&interface1, 0, sizeof(interface1));
strncpy(interface1.ifr_ifrn.ifrn_name, "wlp10s0", IFNAMSIZ);
if (setsockopt(sockFd1, SOL_SOCKET, SO_BINDTODEVICE, &interface1, sizeof(interface1)) < 0) {
printf("error in set sock opt 1... errno == %d strerror == (%s)\n", errno, strerror(errno));
close(sockFd1); // Error
return 1;
}
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = inet_pton(AF_INET, ip1, &(clientAddress.sin_addr));
clientAddress.sin_port = htons(portNumber1);
if (bind(sockFd1, (struct sockaddr *) &clientAddress, sizeof(clientAddress)) < 0) {
fprintf(stderr, "Error with bind, errno == %d (%s)\n", errno, strerror(errno));
}
// Set non-blocking
if( (arg = fcntl(sockFd1, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
return 1;
}
arg |= O_NONBLOCK;
if( fcntl(sockFd1, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
return 1;
}
printf("ATTEMPTING CONNECTION 2!\n");
// Trying to connect with timeout
res = connect(sockFd1, saddr, sizeof(*saddr));
if (res < 0) {
if (errno == EINPROGRESS) {
fprintf(stderr, "EINPROGRESS in connect() - selecting\n");
do {
//Set timeouts
tv.tv_sec = 15;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sockFd1, &myset);
res = select(sockFd1 + 1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR) {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
}
else if (res > 0) {
// Socket selected for write
lon = sizeof(int);
if (getsockopt(sockFd1, SOL_SOCKET, SO_ERROR, (void *) &valopt, &lon) < 0) {
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
}
// Check the value returned...
if (valopt) {
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt));
}
break;
}
else {
fprintf(stderr, "Timeout in select() - Cancelling!\n");
break;
}
} while (1);
}
else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
}
}
}
printf("ATTEMPTING SOCKET 2!\n");
//IP 2 CONNECTION----------------------------------------------------------------------------------//
if ((sockFd2 = socket(it->ai_family, it->ai_socktype, it->ai_protocol)) != -1) {
system("route add -net 213.112.225.102 netmask 255.255.255.255 gw 192.168.1.1 dev wlp11s0u1");
struct ifreq interface2;
memset(&interface2, 0, sizeof(interface2));
strncpy(interface2.ifr_ifrn.ifrn_name, "wlp11s0u1", IFNAMSIZ);
if (setsockopt(sockFd2, SOL_SOCKET, SO_BINDTODEVICE, &interface2, sizeof(interface2)) < 0) {
printf("error in set sock opt 2... errno == %d strerror == (%s)\n", errno, strerror(errno));
close(sockFd2); // Error
return 1;
}
clientAddress2.sin_family = AF_INET;
clientAddress2.sin_addr.s_addr = inet_pton(AF_INET, ip2, &(clientAddress.sin_addr));
clientAddress2.sin_port = htons(portNumber2);
if (bind(sockFd2, (struct sockaddr *) &clientAddress2, sizeof(clientAddress2)) < 0) {
fprintf(stderr, "Error with bind (%s)\n", strerror(errno));
}
// Set non-blocking
if( (arg = fcntl(sockFd2, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
return 1;
}
arg |= O_NONBLOCK;
if( fcntl(sockFd2, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
return 1;
}
printf("ATTEMPTING CONNECTION 2!\n");
// Trying to connect with timeout
res = connect(sockFd2, saddr, sizeof(*saddr));
if (res < 0) {
if (errno == EINPROGRESS) {
fprintf(stderr, "EINPROGRESS in connect() - selecting\n");
do {
//Set timeouts
tv.tv_sec = 15;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sockFd2, &myset);
res = select(sockFd2 + 1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR) {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
}
else if (res > 0) {
// Socket selected for write
lon = sizeof(int);
if (getsockopt(sockFd2, SOL_SOCKET, SO_ERROR, (void *) &valopt, &lon) < 0) {
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
}
// Check the value returned...
if (valopt) {
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt));
}
break;
}
else {
fprintf(stderr, "Timeout in select() - Cancelling!\n");
break;
}
} while (1);
}
else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
}
}
}
return 0;
}
/*
ifreq interface;
memset(&interface, 0, sizeof(interface));
strncpy(interface.ifr_ifrn.ifrn_name, "eth1", IFNAMSIZ);
if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface)) < 0) {
close(sd); // Error
}
*/
So what gives, why is bind not binding!?
EDIT:
Okay, so thanks to this old post: Multiple Ethernet Interfaces - How to create a separate network and access from C code
I'm taking a different approach now, but I still am not utilizing both networks...