LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   URGENT: how to get an ip address of sending interface in C (https://www.linuxquestions.org/questions/linux-general-1/urgent-how-to-get-an-ip-address-of-sending-interface-in-c-185696/)

dravya 05-25-2004 12:08 PM

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

wrongman 05-25-2004 01:21 PM

try in the programming forum :)

dorian33 05-25-2004 01:39 PM

Try:
Code:

...
#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

dravya 05-25-2004 02:42 PM

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

dorian33 05-25-2004 03:32 PM

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.