posts - 297,  comments - 15,  trackbacks - 0
  1. #include <sys/types.h>   
  2. #include <sys/socket.h>   
  3. #include <sys/ioctl.h>   
  4. #include <netinet/in.h>   
  5. #include <unistd.h>   
  6. #include <string.h>   
  7. #include <arpa/inet.h>   
  8. #include <net/if.h>   
  9. #include <errno.h>   
  10. #include <features.h>   
  11. #include <netpacket/packet.h>   
  12. #include <net/ethernet.h>   
  13. #include <net/route.h>   
  14. #include <stdio.h>   
  15.    
  16. #define   FAILURE   0   
  17. #define   SUCCESS   1   
  18. typedef unsigned char           UNS8; /* 8-bit */   
  19. typedef unsigned short          UNS16;  /* 16-bit */   
  20. typedef unsigned int            UNS32;  /* 32-bit */    
  21. typedef unsigned long long int  UNS64;  /* 64-bit */   
  22. typedef char                    S8;   /* 8-bit */   
  23. typedef short                   S16;  /* 16-bit */   
  24. typedef int                     S32;  /* 32-bit */    
  25. typedef long long int           S64;  /* 64-bit */   
  26. typedef enum {   
  27.     FALSE,   
  28.     TRUE   
  29. BOOL;   
  30. #define m_MSG(format, args...)  printf(format, ## args)   
  31. #define m_ERROR(terminal,format, args...)  fprintf(terminal,format, ## args)   
  32. #define m_DEBUG(format, args...)  printf(format, ## args)   
  33.    
  34.    
  35. typedef struct _rt {   
  36.     UNS32 dst;   
  37.     UNS32 mask;   
  38.     UNS32 gw;   
  39.     S32   flags;   
  40. }RT;   
  41. S32 setDefaultRoute( S32 fd, S8 *interface, UNS32 *route_addr )   
  42. {   
  43.     struct  rtentry     rtent;   
  44.     struct  sockaddr_in *p;   
  45.    
  46.     memset( &rtent,0,sizeofstruct rtentry ) );   
  47.     p = ( struct sockaddr_in * ) &rtent.rt_dst;   
  48.     p->sin_family = AF_INET;   
  49.     p->sin_addr.s_addr = 0;   
  50.     p = ( struct sockaddr_in * ) &rtent.rt_gateway;   
  51.     p->sin_family = AF_INET;   
  52.     p->sin_addr.s_addr = *route_addr;   
  53.     p = ( struct sockaddr_in * ) &rtent.rt_genmask;   
  54.     p->sin_family = AF_INET;   
  55.     p->sin_addr.s_addr = 0;   
  56.    
  57.     rtent.rt_dev = interface;   
  58.    
  59.     rtent.rt_metric = 1;   
  60.     rtent.rt_window = 0;   
  61.     rtent.rt_flags = RTF_UP | RTF_GATEWAY;   
  62.     if ( ioctl( fd,SIOCADDRT,&rtent ) == -1 ) {   
  63.         if ( errno == ENETUNREACH )    /* possibly gateway is over the bridge */ {   
  64.             /* try adding a route to gateway first */   
  65.             memset( &rtent,0,sizeofstruct rtentry ) );   
  66.             p = ( struct sockaddr_in * ) &rtent.rt_dst;   
  67.             p->sin_family = AF_INET;   
  68.             p->sin_addr.s_addr = *route_addr;   
  69.             p = ( struct sockaddr_in * ) &rtent.rt_gateway;   
  70.             p->sin_family = AF_INET;   
  71.             p->sin_addr.s_addr = 0;   
  72.             p = ( struct sockaddr_in * ) &rtent.rt_genmask;   
  73.             p->sin_family = AF_INET;   
  74.             p->sin_addr.s_addr = 0xffffffff;   
  75.    
  76.    
  77.             rtent.rt_dev = interface;   
  78.             rtent.rt_metric = 0;   
  79.             rtent.rt_flags = RTF_UP | RTF_HOST;   
  80.             if ( ioctl( fd,SIOCADDRT,&rtent ) == 0 ) {   
  81.                 memset( &rtent,0,sizeofstruct rtentry ) );   
  82.                 p = ( struct sockaddr_in * ) &rtent.rt_dst;   
  83.                 p->sin_family = AF_INET;   
  84.                 p->sin_addr.s_addr = 0;   
  85.                 p = ( struct sockaddr_in * ) &rtent.rt_gateway;   
  86.                 p->sin_family = AF_INET;   
  87.                 p->sin_addr.s_addr = *route_addr;   
  88.                 p = ( struct sockaddr_in * ) &rtent.rt_genmask;   
  89.                 p->sin_family = AF_INET;   
  90.                 p->sin_addr.s_addr = 0;   
  91.    
  92.    
  93.                 rtent.rt_dev = interface;   
  94.    
  95.                 rtent.rt_metric = 1;   
  96.                 rtent.rt_window = 0;   
  97.                 rtent.rt_flags = RTF_UP | RTF_GATEWAY ;   
  98.                 if ( ioctl( fd,SIOCADDRT,&rtent ) == -1 ) {   
  99.                     m_ERROR(stderr,"ioctl SIOCADDRT: %m\n");   
  100.                     return FAILURE;   
  101.                 }   
  102.             }   
  103.         } else {   
  104.             m_ERROR(stderr,"ioctl SIOCADDRT: %m\n" );   
  105.             return FAILURE;   
  106.         }   
  107.     }   
  108.     return SUCCESS;   
  109. }   
  110. //通过读取 /proc/net/route获取路由表   
  111. S32 get_route( FILE *fp,RT *rt )   
  112. {   
  113.     S8          devname[64] ;   
  114.     UNS32       d, g, m;   
  115.     S32         flgs,ref,use,metric,mtu,win,ir;   
  116.     S32         r;   
  117.    
  118.     if ( fp == NULL ) {   
  119.         m_DEBUG("I am here 1\n\n");   
  120.         return FAILURE;   
  121.     }   
  122.     r = fscanf( fp,"%63s%lx%lx%X%d%d%d%lx%d%d%d\n",devname,&d,&g,&flgs,&ref,&use,&metric,&m,&mtu,&win,&ir );   
  123.     if ( r != 11 ) {   
  124.         if ( ( r  0 ) && feof( fp ) ) {   
  125.             /* EOF with no (nonspace) chars read. */   
  126.             m_DEBUG("I am here 3\n\n");   
  127.             return FAILURE;   
  128.         }   
  129.         m_DEBUG("I am here 4\n\n");   
  130.         return FAILURE;   
  131.     }   
  132.    
  133.     if ( !( flgs & RTF_UP ) ) {   
  134.         /* Skip interfaces that are down. */   
  135.         m_DEBUG("I am here 5\n\n");   
  136.         return get_route( fp,rt );   
  137.     }   
  138.    
  139.     rt->dst = d;   
  140.     rt->mask = m;   
  141.     rt->gw = g;   
  142.     rt->flags = flgs;   
  143.    
  144.     return SUCCESS;   
  145. }   
  146. S32 read_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )   
  147. {   
  148.     S32               fd;   
  149.     struct ifreq      ifr;   
  150.     struct sockaddr_in*our_ip;   
  151.     RT                rt;   
  152.     S32               fret;   
  153.     FILE              *fp = NULL;   
  154.    
  155.     *ipaddr = 0;   
  156.     *netmaskaddr = 0;   
  157.     *gwaddr = 0;   
  158.     memset( &ifr,0,sizeofstruct ifreq ) );   
  159.     if ( ( fd = socket( AF_INET,SOCK_RAW,IPPROTO_RAW ) ) >= 0 ) {   
  160.         ifr.ifr_addr.sa_family = AF_INET;   
  161.         strcpy( ifr.ifr_name,interface );   
  162.    
  163.         if ( ipaddr ) {   
  164.                 bzero(ipaddr,sizeof(UNS32));   
  165.             if ( ioctl( fd,SIOCGIFADDR,&ifr ) == 0 ) {   
  166.                 our_ip = ( struct sockaddr_in * ) &ifr.ifr_addr;   
  167.                 *ipaddr = our_ip->sin_addr.s_addr;   
  168.             } else {   
  169.                 m_ERROR(stderr, "SIOCGIFADDR failed, is the interface up and configured?: %m" );   
  170.                 close( fd );   
  171.                 return FAILURE;   
  172.             }   
  173.         }   
  174.         if ( netmaskaddr ) {   
  175.                 bzero(netmaskaddr,sizeof(UNS32));   
  176.             if ( ioctl( fd,SIOCGIFNETMASK,&ifr ) == 0 ) {   
  177.                 our_ip = ( struct sockaddr_in * ) &ifr.ifr_netmask;   
  178.                 *netmaskaddr = our_ip->sin_addr.s_addr;   
  179.             } else {   
  180.                 m_ERROR(stderr,"SIOCGIFHWADDR failed!: %m" );   
  181.                 close( fd );   
  182.                 return FAILURE;   
  183.             }   
  184.         }   
  185.     } else {   
  186.         m_ERROR(stderr,"socket failed!: %m" );   
  187.         return FAILURE;   
  188.     }   
  189.     close( fd );   
  190.     if ( gwaddr ) {   
  191.         bzero(gwaddr,sizeof(UNS32));   
  192.         fp = fopen( "/proc/net/route","r" );   
  193.         if ( fp == NULL ) {   
  194.             return FAILURE;   
  195.         }else{   
  196.             if ( fscanf( fp,"%*[^\n]\n" )  0 ) {/* Skip the first line. */           
  197.                 /* Empty or missing line, or read error. */           
  198.                 m_DEBUG("I am here 2\n\n");   
  199.                 return FAILURE;   
  200.             }   
  201.         }   
  202.         while ( ( fret = get_route( fp,&rt ) ) == SUCCESS ) {   
  203.             m_DEBUG("DST:0x%08x,FLAGS:0x%08x,GW:0x%08x,MASK:0x%08x\n",rt.dst,rt.flags,rt.gw,rt.mask);   
  204.             if ( rt.flags & RTF_GATEWAY ) {   
  205.                 *gwaddr = rt.gw;   
  206.                 break;   
  207.             }   
  208.         }   
  209.         fclose(fp);   
  210.         if ( fret != SUCCESS ) {   
  211.             return FAILURE;   
  212.         }   
  213.     }   
  214.    
  215.     return SUCCESS;   
  216. }   
  217.    
  218. S32 write_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )   
  219. {   
  220.     S32               fd;   
  221.     struct ifreq      ifr;   
  222.     struct sockaddr_in*p  = ( struct sockaddr_in * ) &( ifr.ifr_addr );   
  223.    
  224.     memset( &ifr,0,sizeofstruct ifreq ) );   
  225.     if ( interface ) {   
  226.         strcpy( ifr.ifr_name,interface );   
  227.     } else {   
  228.         return FAILURE;   
  229.     }   
  230.    
  231.    
  232.     if ( ( fd = socket( AF_INET,SOCK_STREAM,0 ) ) >= 0 ) {   
  233.         ifr.ifr_addr.sa_family = AF_INET;   
  234.         if ( ipaddr ) {   
  235.             p->sin_addr.s_addr = *ipaddr;   
  236.             if ( ioctl( fd,SIOCSIFADDR,&ifr ) == -1 ) { /* setting IP address */    
  237.                 m_ERROR(stderr,"ioctl SIOCSIFADDR: %m\n" );   
  238.                 return FAILURE;   
  239.             }   
  240.         }   
  241.         if ( netmaskaddr ) {   
  242.             p->sin_addr.s_addr = *netmaskaddr;   
  243.             if ( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) { /* setting netmask */    
  244.                 p->sin_addr.s_addr = 0xffffffff; /* try 255.255.255.255 */   
  245.                 if ( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) {   
  246.                     m_ERROR(stderr,"ioctl SIOCSIFNETMASK: %m\n" );   
  247.                     return FAILURE;   
  248.                 }   
  249.             }   
  250.         }   
  251.         if (ipaddr && netmaskaddr){   
  252.             p->sin_addr.s_addr=*ipaddr|(~*netmaskaddr);   
  253.             if ( ioctl(fd,SIOCSIFBRDADDR,&ifr) == -1 ) {/* setting broadcast address */   
  254.                 m_ERROR(stderr,"ioctl SIOCSIFBRDADDR: %m\n");   
  255.                 return FAILURE;   
  256.             }   
  257.         }   
  258.         if ( gwaddr ) {   
  259.             return setDefaultRoute( fd,interface,gwaddr );   
  260.         }   
  261.         close( fd );   
  262.     } else {   
  263.         m_ERROR(stderr,"socket failed!: %m" );   
  264.         return FAILURE;   
  265.     }          
  266.        
  267.     return SUCCESS;   
  268. }   
  269.    
  270. int main(void)   
  271. {   
  272.     S8 *interface="eth0";   
  273.    
  274.     struct in_addr ipaddr;   
  275.     struct in_addr netmaskaddr;   
  276.     struct in_addr gwaddr;   
  277.    
  278.     inet_aton("192.168.2.18",&ipaddr);   
  279.     inet_aton("255.240.0.0",&netmaskaddr);   
  280.     inet_aton("192.168.2.3",&gwaddr);   
  281.    
  282. //  write_interface(interface,(UNS32 *)&ipaddr,(UNS32 *)&netmaskaddr,(UNS32 *)&gwaddr);   
  283.     bzero(&ipaddr,sizeof(struct in_addr));   
  284.     bzero(&netmaskaddr,sizeof(struct in_addr));   
  285.     bzero(&gwaddr,sizeof(struct in_addr));   
  286. while(1){   
  287.     read_interface(interface,(UNS32 *)&ipaddr,(UNS32 *)&netmaskaddr,(UNS32 *)&gwaddr);   
  288.     m_DEBUG("ipaddr:%s\n",inet_ntoa(ipaddr));   
  289.     m_DEBUG("netmask:%s\n",inet_ntoa(netmaskaddr));   
  290.     m_DEBUG("gateway:%s\n",inet_ntoa(gwaddr));   
  291.     usleep(500000);   
  292. }   
  293.     return SUCCESS;   
  294.        
  295. }  

from:

http://read.pudn.com/downloads86/sourcecode/unix_linux/network/332436/ifconf.c__.htm



ipget.c written by myself:
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
    int i=0;
    int sockfd;
    struct ifconf ifconf;
    unsigned char buf[512];
    struct ifreq *ifreq;

    ifconf.ifc_len = 512;
    ifconf.ifc_buf = buf;

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0) {
        perror("socket");
        exit(EXIT_FAILURE);
    }
    ioctl(sockfd, SIOCGIFCONF, &ifconf);
    ifreq = (struct ifreq *)buf;
    for(i = ifconf.ifc_len/sizeof(struct ifreq); i > 0; i--)
    {
        printf("name = [%s]\n", ifreq->ifr_name);
        printf("local addr = [%s]\n",
                inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
        ifreq++;
    }

    close(sockfd);

    exit(EXIT_SUCCESS);
}

/* Interface request structure used for socket ioctl's. All interface
    ioctl's must have parameter definitions which begin with ifr_name.
    The remainder may be interface specific. */
struct ifreq
{
#ifndef IFNAMSIZ
#define IFNAMSIZ    16
#endif
char  ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
__ulong32_t ifru_flags;
int   ifru_metric;
CADdr_t ifru_data;
u_short ifru_site6;
__ulong32_t  ifru_mtu;
int   ifru_baudrate;
} ifr_ifru;
//Following Macros are provided for convenIEnce
#define ifr_addr    ifr_ifru.ifru_addr   /* address */
#define ifr_dstaddr ifr_ifru.ifru_dstaddr  /* other end of p-to-p link */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_flags ifr_ifru.ifru_flags   /* flags */
#define ifr_metric   ifr_ifru.ifru_metric  /* metric */
#define ifr_data    ifr_ifru.ifru_data   /* for use by interface */
#define ifr_site6 ifr_ifru.ifru_site6   /* IPv6 site index */
#define ifr_mtu ifr_ifru.ifru_mtu    /* mtu of interface */
#define ifr_isno    ifr_ifru.ifru_data   /* pointer to if_netopts */
#define ifr_baudrate  ifr_ifru.ifru_baudrate /* baudrate of interface */
};
/* Structure used in SIOCGIFCONF request. Used to retrieve interface
    configuration for machine (useful for programs which must know all
    networks accessible). */
struct ifconf
{
int   ifc_len;        /* size of associated buffer */
union
{
caddr_t ifcu_buf;
struct ifreq *ifcu_req;
} ifc_ifcu;
Following macros are provided for convenience
#define ifc_buf ifc_ifcu.ifcu_buf    /* buffer address */
#define ifc_req ifc_ifcu.ifcu_req    /* array of structures returned */
};
SIOCGIFADDR:获取接口地址,检索特定接口的地址
SIOCSIFADDR:设置接口地址。ifr.ifr_addr 字段返回地址。
SIOCGIFCONF:获取所有配置接口的信息,返回系统中配置的所有接口的配置信息

posted on 2011-05-20 12:30 chatler 阅读(869) 评论(0)  编辑 收藏 引用 所属分类: Linux_Coding

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜