Re: help getting ip address in c code



Hello,

Study Beej's guide to network programming
http://beej.us/guide/

and send your question to
news://comp.os.linux.development.apps
( news://comp.os.linux.development )


// moma
http://www.futuredesktop.com/opportunities.html


laclac01@xxxxxxxxx wrote:
have some code that is trying to get the IP address of interface eth0,
when i complie it i get the following error
"getip.c: In function `main':
getip.c:9: error: storage size of `saddr' isn't known
getip.c:13: error: dereferencing pointer to incomplete type"

How can i get this to work? Or is there a better way to get the IP
address?

Here is the code

#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/ioctl.h>

int main()
{
int fd;
struct ifreq ifr;
struct sockaddr_in saddr;
fd=socket(PF_INET,SOCK_STREAM,0);
strcpy(ifr.ifr_name,"eth0");
ioctl(fd, SIOCGIFADDR,&ifr);
saddr=*((struct sockaddr_in *)(&(ifr.ifr_addr)));



return 0;
}

.



Relevant Pages

  • help getting IP address in c code
    ... when i complie it i get the following error ... getip.c:9: error: storage size of `saddr' isn't known ... int main ...
    (comp.os.linux.misc)
  • help getting ip address in c code
    ... when i complie it i get the following error ... getip.c:9: error: storage size of `saddr' isn't known ... int main ...
    (alt.os.linux)
  • Re: help getting IP address in c code
    ... getip.c:9: error: storage size of `saddr' isn't known ... int main ...
    (comp.os.linux.misc)
  • Re: How to get my own IP address without DNS
    ... you can use system call ioctl with SIOCGIFADDR as shown below ... struct ifreq ifr; struct sockaddr_in saddr; ... saddr.sin_addr.s_addr is the address of the interface in integer ...
    (comp.os.linux.networking)