无法使用ioctl SIOCSIFADDR更改IP地址

时间:2016-10-03 13:07:06

标签: c sockets networking ioctl

我正在尝试编写一个c程序来更改网络接口的IP地址和子网掩码。但是,使用命令ioctl调用SIOCSIFADDR始终返回EINVAL。我的程序代码如下。

/* inet_config.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define _INTERFACE_NAME  argv[1]
#define _INET_ADDRESS    argv[2]
#define _SUBNET_MASK     argv[3]

int main(int argc, char ** argv) {
  int sockfd, inet_addr_config_result, subnet_mask_config_result, ioctl_result;
  struct ifreq ifr;
  struct sockaddr_in *inet_addr, *subnet_mask;

  /* Prepare the struct ifreq */

  bzero(ifr.ifr_name, IFNAMSIZ);
  bzero((char *)&(ifr.ifr_addr), sizeof(struct sockaddr));
  bzero((char *)&(ifr.ifr_netmask), sizeof(struct sockaddr));

  strcpy(ifr.ifr_name, _INTERFACE_NAME);

  ifr.ifr_addr.sa_family = AF_INET;
  inet_addr = (struct sockaddr_in *)&(ifr.ifr_addr);
  inet_addr_config_result = inet_pton(AF_INET, _INET_ADDRESS, &(inet_addr->sin_addr));

  ifr.ifr_netmask.sa_family = AF_INET;
  subnet_mask = (struct sockaddr_in *)&(ifr.ifr_netmask);
  subnet_mask_config_result = inet_pton(AF_INET, _SUBNET_MASK, &(subnet_mask->sin_addr));

  /* Error handling */

  if(inet_addr_config_result == 0)
  {
    fprintf(stderr, "%s: inet_pton: Invalid IPv4 address.\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  if(inet_addr_config_result < 0)
  {
    fprintf(stderr, "%s: inet_pton: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  if(subnet_mask_config_result == 0)
  {
    fprintf(stderr, "%s: inet_pton: Invalid IPv4 subnet mask.\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  if(subnet_mask_config_result < 0)
  {
    fprintf(stderr, "%s: inet_pton: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  /* Open socket for ioctl calls */

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if(sockfd < 0)
  {
    fprintf(stderr, "%s: socket: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  /* Call ioctl to configure network devices */

  ioctl_result = ioctl(sockfd, SIOCSIFADDR, &ifr);  // Set IP address
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCSIFADDR: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  ioctl_result = ioctl(sockfd, SIOCSIFNETMASK, &ifr);   // Set subnet mask
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCSIFNETMASK: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  ioctl_result = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCGIFFLAGS: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);

  ioctl(sockfd, SIOCSIFFLAGS, &ifr);
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCSIFFLAGS: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  printf("Network device configured\n");

  return 0;
}

输入命令

./inet_config wlp2s0 192.168.2.0 255.255.255.0 

我收到以下错误消息:

./inet_config: ioctl SIOCSIFADDR: Invalid argument

inet_config是从上面代码编译的二进制文件的名称,wlp2s0是我的WiFi接口。)

1 个答案:

答案 0 :(得分:0)

您的问题来自ifr变量:它由union组成,因此ifr_addrifr_netmask指向同一个内存区域:

struct ifreq {
    char ifr_name[IFNAMSIZ]; /* Interface name */
    union { // <-- all your problem comes from this union
        struct sockaddr ifr_addr;
        struct sockaddr ifr_dstaddr;
        struct sockaddr ifr_broadaddr;
        struct sockaddr ifr_netmask;
        struct sockaddr ifr_hwaddr;
        short           ifr_flags;
        int             ifr_ifindex;
        int             ifr_metric;
        int             ifr_mtu;
        struct ifmap    ifr_map;
        char            ifr_slave[IFNAMSIZ];
        char            ifr_newname[IFNAMSIZ];
        char           *ifr_data;
    };
};

您的代码可以使用一些更正:地址和掩码存储在两个sockaddr_in结构中,这些结构在使用之前复制到ifr

/* inet_config.c */
[...]

int main(int argc, char ** argv) {
  int sockfd, inet_addr_config_result, subnet_mask_config_result, ioctl_result;
  struct ifreq ifr;
  /// note: no pointer here
  struct sockaddr_in inet_addr, subnet_mask;

  /* Prepare the struct ifreq */

  bzero(ifr.ifr_name, IFNAMSIZ);
  strcpy(ifr.ifr_name, _INTERFACE_NAME);

  /// note: prepare the two struct sockaddr_in
  inet_addr.sin_family = AF_INET;
  inet_addr_config_result = inet_pton(AF_INET, _INET_ADDRESS, &(inet_addr.sin_addr));

  subnet_mask.sin_family = AF_INET;
  subnet_mask_config_result = inet_pton(AF_INET, _SUBNET_MASK, &(subnet_mask.sin_addr));

  /* Error handling */
  [...]
  /* Open socket for ioctl calls */

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if(sockfd < 0)
  {
    fprintf(stderr, "%s: socket: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  /* Call ioctl to configure network devices */

  /// put addr in ifr structure
  memcpy(&(ifr.ifr_addr), &inet_addr, sizeof (struct sockaddr));
  ioctl_result = ioctl(sockfd, SIOCSIFADDR, &ifr);  // Set IP address
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCSIFADDR: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  /// put mask in ifr structure
  memcpy(&(ifr.ifr_addr), &subnet_mask, sizeof (struct sockaddr));
  ioctl_result = ioctl(sockfd, SIOCSIFNETMASK, &ifr);   // Set subnet mask
  if(ioctl_result < 0)
  {
    fprintf(stderr, "%s: ioctl SIOCSIFNETMASK: ", argv[0]);
    perror("");
    exit(EXIT_FAILURE);
  }

  [....]

  printf("Network device configured\n");

  return 0;
}