无法使用ioctl更改IP地址SIOCSIFADDR
问题描述:
我正在尝试编写一个c程序来更改网络接口的IP地址和子网掩码。但是,使用命令SIOCSIFADDR
致电ioctl
总是会返回EINVAL
。我的程序代码如下。无法使用ioctl更改IP地址SIOCSIFADDR
/* 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接口)。
答
你的问题来自于ifr
变量:它是由一个union
,所以ifr_addr
和ifr_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;
};
};
您的代码可以用一些修正工作:地址和掩码存储在在ifr
复制2层sockaddr_in
结构就在使用它之前。
/* 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;
}
+0
非常感谢。它在你改正之后起作用。 –
试着用strace调用你的程序:'strace ./inet_config wlp2s2 ...' – purplepsycho