gpt4 book ai didi

c - 无法使用 ioctl SIOCSIFADDR 更改 IP 地址

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:57 26 4
gpt4 key购买 nike

我正在尝试编写一个 c 程序来更改网络接口(interface)的 IP 地址和子网掩码。但是,使用命令 SIOCSIFADDR 调用 ioctl 总是返回 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接口(interface)。)

最佳答案

你的问题来自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;
}

关于c - 无法使用 ioctl SIOCSIFADDR 更改 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39832427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com