LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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



Reply
 
Search this Thread
Old 05-25-2004, 01:08 PM   #1
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
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
 
Old 05-25-2004, 02:21 PM   #2
wrongman
Member
 
Registered: May 2004
Location: Italy
Distribution: Debian Unstable 64bit
Posts: 99

Rep: Reputation: 15
try in the programming forum
 
Old 05-25-2004, 02:39 PM   #3
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 587

Rep: Reputation: 32
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
 
Old 05-25-2004, 03:42 PM   #4
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 05-25-2004, 04:32 PM   #5
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 587

Rep: Reputation: 32
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.

Last edited by dorian33; 05-25-2004 at 04:36 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending out going traffic to the loopback interface with iptables hypexr Linux - Networking 1 09-22-2005 08:36 PM
Interface card(Ethernet) not sending DHCP requests vishalsethia Linux - Newbie 5 05-08-2005 02:12 AM
Sending, receiving through the USB interface The_Nerd Programming 5 02-13-2005 03:49 AM
sysklogd sending logs out the wrong interface slizadel Linux - Networking 1 06-10-2004 08:13 PM
Library versioning interface - urgent rflowers Linux - Software 0 09-04-2003 10:27 PM


All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration