1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

finding ip addr from a c prog

Discussion in 'C' started by ismitevijay, Aug 28, 2006.

  1. ismitevijay

    ismitevijay New Member

    Joined:
    Aug 26, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    hi all;
    i hav written a code in c for finding ip:

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include<sys/ioctl.h>
    #include<sys/socket.h>
    #include<linux/if.h>
    #include<arpa/inet.h>
    
    int main()
    {
    	struct ifreq ifr;
    	char buf[30];
    	int fd;
    	fd=socket(AF_INET,SOCK_DGRAM,0);
    	strcpy(&ifr.if_name,"eth0");
    	ioctl(fd,SIOCGIFADDR,&ifr);
    	strcpy(buf,inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));
    	printf(" %s",buf);
    	return 0;
    }
    but i m getting err as :
    ip.c: In function ‘main’:
    ip.c:15: error: ‘struct ifreq’ has no member named ‘if_name

    tell me wat i m missisng
     
    Last edited by a moderator: Aug 28, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    if_name is not a member of ifreq structure.

    The structure contains 128 bytes which has the following members
    Code:
    union { 
       sockaddr   ifru_addr 
       sockaddr   ifru_dstaddr 
       sockaddr   ifru_broadaddr 
       short   ifru_flags 
       int   ifru_metric 
       caddr_t   ifru_data 
    }  
     

Share This Page