我想设置我的IP,子网,DHCP地址,但我需要使用c ++来实现。我已经在互联网上研究并尝试了一些代码。不幸的是,它没有用。还有一件事它认为运行它是安全的,我的意思是我不能使用需要特权的函数,比如system()等等。你有什么建议可以给我吗?
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
int main()
{
char ip_address[15];
int fd;
struct ifreq ifr;
struct sockaddr_in *addr;
/*Read IP Address*/
printf("Enter Ip Address: ");
scanf("%s",ip_address);
/*AF_INET - to define network interface IPv4*/
/*Creating soket for it.*/
fd = socket(AF_INET, SOCK_DGRAM, 0);
/*AF_INET - to define IPv4 Address type.*/
ifr.ifr_addr.sa_family = AF_INET;
/*eth0 - define the ifr_name - port name
where network attached.*/
memcpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
/*defining the sockaddr_in*/
addr=(struct sockaddr_in *)&ifr.ifr_addr;
/*convert ip address in correct format to write*/
inet_pton(AF_INET,ip_address,&addr->sin_addr);
/*Setting the Ip Address using ioctl*/
ioctl(fd, SIOCSIFADDR, &ifr);
/*closing fd*/
close(fd);
printf("IP Address updated sucessfully.\n");
/*Getting the Ip Address after Updating.*/
/*clear ip_address buffer with 0x20- space*/
memset((unsigned char*)ip_address,0x20,15);
ioctl(fd, SIOCGIFADDR, &ifr);
/*Extracting Ip Address*/
strcpy(ip_address,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
printf("Updated IP Address is: %s\n",ip_address);
return 0;
}
这段代码是我到目前为止所尝试的。运行后我检查ifconfig
但没有任何改变。