URGENT: how to get an ip address of sending interface in C
My program (in C) sends data via 4 interfaces. ppp0,ppp1,ppp2,ppp3. I have to find a way to get the IP address of each of the sending interfaces and I have to log these IP addresses. The program reads data from a file and sends it to the server. It has to also send the IP address to the server. What function can I use? and where do I put it? Any help would be appreciated.
int i; for (i = 0; i < 4; i++){ if (setsockopt(client_socket, SOL_SOCKET, SO_BINDTODEVICE, ifname[i], strlen(ifname[i]) + 1) ==-1) { printf ("Error setting interface %s \n", ifname[i]); perror("XXX "); } else{ printf("about to connect ip addr %s\n", ServerIPAddr); if (connect(client_socket, (struct sockaddr *)&server_ssin, sizeof(struct sockaddr_in)) == -1 ) { printf ("Error connecting to server from interface %s \n", ifname[i]); } else { printf("Connected to server on port %d from interface %s \n", dport, ifname[i]); break; } } } int data_read=0; while ((data_read = read(file_tosend, buffer, BUFF_SIZE)) > 0){ if ((data = send(client_socket, buffer, data_read, 0)) == -1) { printf("Error sending data to server\n"); return -1; } total_sent += data; memset(buffer, 0x0, BUFF_SIZE); } IT IS URGENTLY NEEDED Thank you in advance |
try in the programming forum :)
|
Try:
Code:
... <net/if.h> man ioctl man ioctl_list |
Hi,
I tried what you suggested doiron33, but it cant compile. It gives the following error: 'SIOCGIFCONF' undeclared. I included the <net/ifconf.h> and it still doen't work. I need to get the ip address of the interface that sends the data. So is this code included in the section where I am sending the data?? if (0 == ioctl(server_socket, SIOCGIFCONF, &ifc)) { for(i=0; i<ifc.ifc_len/sizeof(struct ifreq); i++) { /* here you can enumerate each IP f.ex. 'inet_ntoa(((struct sockaddr_in *) &ifc.ifc_req[i].ifr_addr) ->sin_addr)' gives IP in ascii xxx.xxx.xxx.xxx form */ } } thank you for your reply.... dravya |
You need also #include <sys/socket.h> but I'm wondering why you haven't included it yet since you are working with sockets.
So is this code included in the section where I am sending the data?? I doesn't matter. Read the ifconf structure definition from if.h (BTW: if.h not ifconf.h) for info which data you can get about the connected socket. And for trivial IP check you can use just getsockname(...) & getpeername(...) with gethostbyname(...) function. |
All times are GMT -5. The time now is 10:38 AM. |