URGENT: how to get an ip address of sending interface in C
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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);
}
...
#include <net/if.h>
...
int i;
struct ifconf ifc;
char ifcbf[300];
....
ifc.ifc_len=sizeof(ifcbf);
ifc.ifc_buf=ifcbf;
if (0 == ioctl(sfd, 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
*/
}
}
for more info what can you get search for SIOCGIFCONF in:
<net/if.h>
man ioctl
man ioctl_list
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
*/
}
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.