码迷,mamicode.com
首页 > 系统相关 > 详细

Linux下获取和设置IP

时间:2015-05-13 21:32:43      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作。

那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体。

/* 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
  {
# define IFHWADDRLEN    6
# define IFNAMSIZ       IF_NAMESIZE
    union
      {
        char ifrn_name[IFNAMSIZ];       /* Interface name, e.g. "en0".  */
      } ifr_ifrn;

    union
      {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        struct sockaddr ifru_netmask;
        struct sockaddr ifru_hwaddr;
        short int ifru_flags;
        int ifru_ivalue;
        int ifru_mtu;
        struct ifmap ifru_map;
        char ifru_slave[IFNAMSIZ];      /* Just fits the size */
        char ifru_newname[IFNAMSIZ];
        __caddr_t ifru_data;
      } ifr_ifru;
  };
# define ifr_name       ifr_ifrn.ifrn_name      /* interface name       */
# define ifr_hwaddr     ifr_ifru.ifru_hwaddr    /* MAC address          */
# define ifr_addr       ifr_ifru.ifru_addr      /* address              */
# define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */
# define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */
# define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */
# define ifr_flags      ifr_ifru.ifru_flags     /* flags                */
# define ifr_metric     ifr_ifru.ifru_ivalue    /* metric               */
# define ifr_mtu        ifr_ifru.ifru_mtu       /* mtu                  */
# define ifr_map        ifr_ifru.ifru_map       /* device map           */
# define ifr_slave      ifr_ifru.ifru_slave     /* slave device         */
# define ifr_data       ifr_ifru.ifru_data      /* for use by interface */
# define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */
# define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth       */
# define ifr_qlen       ifr_ifru.ifru_ivalue    /* queue length         */
# define ifr_newname    ifr_ifru.ifru_newname   /* New name             */
# define _IOT_ifreq     _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

看完这个,结构体的内容一目了然

1.获取ip

int get_local_ip(char * dev_name, char * addr) 
{
    int ret;
    int sockfd;
    struct sockaddr_in sin;
    struct ifreq req;
    
    if(dev_name == NULL)
    {
        error_exit("dev_name");
    }
    
    if((sockfd = socket(AF_INET, SOCK_DGRAM ,0)) < 0)
    {
        error_exit("ip socket");
    }
    strcpy(req.ifr_name, dev_name);
    
    ret = ioctl(sockfd,SIOCGIFADDR,&req);
    if(ret != -1)
    {
        memcpy(&sin, &req.ifr_addr,sizeof(sin));
        printf("local IP:%s\n",inet_ntoa(sin.sin_addr));
    }
    
    close(sockfd);
    return 0;
}

2.设置ip

#define error_exit(_errmsg_)     \
    error(EXIT_FAILURE, errno, _errmsg_)

#define MAC_ADDR_LEN 6

int set_local_ip(char * dev_name, const char * ip_addr)
{
    int ret;
    int sockfd;
    struct sockaddr_in set_ip;
    struct ifreq req;
    
    if(ip_addr == NULL)
    {
        error_exit("ip_addr invalid");
    }
    if((sockfd = socket(AF_INET, SOCK_DGRAM ,0)) < 0)
    {
        error_exit("ip socket");
    }
    bzero(&req, sizeof(req));
    strncpy(req.ifr_name, dev_name, strlen(dev_name) );
    
    bzero(&set_ip, sizeof(set_ip));
    set_ip.sin_family = AF_INET;
    set_ip.sin_addr.s_addr = inet_addr(ip_addr);
    
    memcpy(&req.ifr_addr, &set_ip, sizeof(set_ip));
    
    
    ret = ioctl(sockfd,SIOCSIFADDR,&req);
    if(ret < 0)
    {
        error_exit("set IP...");
    }
    
    close(sockfd);
    return 0;
}

整个demo为:

int main()
{
    char * dev_name = "eth1";
    char ip_addr[15] = "192.168.123.100";
    set_local_ip(dev_name, ip_addr);
    return 0;
}    

 

Linux下获取和设置IP

标签:

原文地址:http://www.cnblogs.com/youthshouting/p/4501540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!