Linux下通过ioctl修改IP信息

通过ioctl修改和获取ip的地址、网关、掩码

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>

struct rtc_time
 {
	int tm_sec;
	int tm_min;
	int tm_hour;
	int tm_mday;
	int tm_mon;
	int tm_year;
	int tm_wday;
	int tm_yday;
	int tm_isdst;
};

volatile int g_SendFlag;
unsigned char g_SendBuff[1024];

unsigned char g_eth_name[16];
unsigned char g_macaddr[6];

int is_valid_ip(char* str)
{
    int ret = 0;
    struct in_addr inp;
    ret = inet_aton(str, &inp);
    if (0 == ret)
    {
		printf("is_valid_ip failed\n");
        return -1;
    }
    else
    {
        printf("inet_aton:ip=%lu\n",ntohl(inp.s_addr));
    }
 
    return 0;
}

int is_valid_netmask(char* netmask)
{
	if(is_valid_ip(netmask) > 0)
    {
        unsigned int b = 0, i, n[4];
        sscanf(netmask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
        for(i = 0; i < 4; ++i) //灏嗗瓙缃戞帺鐮佸瓨鍏?2浣嶆棤绗﹀彿鏁村瀷
            b += n[i] << (i * 8);
        b = ~b + 1;
        if((b & (b - 1)) == 0) //鍒ゆ柇鏄惁涓?^n
            return 0;
    }
	return -1;
}
/*
	The function can printf the LOCAL MAC addr, copy the Local MAC addr to the parm str
*/
int getLocalMacAddr(char *str)
{
	int i;
    int fd;
    struct ifreq ifr;

	memset(str, 0, strlen(str));
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == fd)
    {
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);

    if ( ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
    {
        close( fd );
        return -1;
    }
    memcpy(g_macaddr, ifr.ifr_hwaddr.sa_data, sizeof(g_macaddr));
    printf("local mac:");
    for(i=0;i<sizeof(g_macaddr)-1;i++)
    {
        printf("%.2x:", g_macaddr[i]);
    }
	sprintf(str, "%.2x", g_macaddr[i]);
    printf("%s\n", str);
	close(fd);
	return 0;
}
/*
	The function can printf the Broadcast addr, copy the Broadcast addr to the parm str
*/
int getBroadcastAddr(char* str)
{
	#if 1
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;

	memset(str, 0, strlen(str));
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == fd)
    {
		perror("socket error\n");
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);

    //get the broadcast addr
    if ( ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
    {
        close( fd );
        return -1;
    }
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
	sprintf(str,"%s",inet_ntoa(sin.sin_addr));
    printf("broadcast is :%s\n", str);
	close(fd);
	#endif
	return 0;
}

/*
	The function can printf the ip addr, copy the ip addr to the parm str
*/
int getIpAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
	
	memset(str, 0, strlen(str));
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == fd)
    {
		perror("socket\n");
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);
    //get the ip addr
    if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
    {
		perror("ioctl\n");
        close( fd );
        return -1;
    }
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
	sprintf(str,"%s",inet_ntoa(sin.sin_addr));
    printf("ip is :%s\n", str);
	
	close(fd);
	return 0;
}
/*
	The function can printf the Gateway addr, copy the Gateway addr to the parm str
*/
int getGatewayAddr(char *str)
{ 
	FILE *fp;    
	char buf[256]; // 128 is enough for linux    
	char iface[16];    
	unsigned long dest_addr, gate_addr;    
	//*p = INADDR_NONE;    
	fp = fopen("/proc/net/route", "r");    
	if (fp == NULL)    
		return -1;    
	/* Skip title line */    
	fgets(buf, sizeof(buf), fp);    
	while (fgets(buf, sizeof(buf), fp)) {    
		if (sscanf(buf, "%s\t%lX\t%lX", iface,&dest_addr, &gate_addr) != 3||dest_addr != 0)    
			continue;    
	//*p = gate_addr;   
		break;    
	}    
	sprintf(str, "%d.%d.%d.%d", 
		gate_addr&0x000000ff, (gate_addr>>8)&0x000000ff, (gate_addr>>16)&0x000000ff, (gate_addr>>24)&0x000000ff);
	printf("gateway is = %s\n", str);		
	fclose(fp);    
	return 0;
}
/*
	The function can printf the mask addr, copy the mask addr to the parm str
*/
int getMaskAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
	
	memset(str, 0, strlen(str));
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == fd)
    {
		perror("socket error\n");
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);
	
    //get the ip addr
    if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
    {
		perror("ioctl error\n");
        close( fd );
        return -1;
    }
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
	sprintf(str,"%s",inet_ntoa(sin.sin_addr));
    printf("Mask is :%s\n", str);
	close(fd);
	return 0;
}

/*
	The function can set the Broadcast addr, the parm str is want change's broadcast addr
*/
int setBroadcastAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
	

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == fd)
    {
		perror("socket error\n");
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);
	
	//set the Mask addr
	struct sockaddr_in *sin_net_broadcast;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name )-1);
    sin_net_broadcast = (struct sockaddr_in *)&ifr.ifr_addr;
    sin_net_broadcast->sin_family = AF_INET;
    inet_pton(AF_INET, str, &sin_net_broadcast->sin_addr);

    if (ioctl(fd, SIOCGIFBRDADDR, &ifr ) < 0)
    {
        close( fd );
        printf("set net Broadcast error:%s\n", strerror(errno));
        return -1;
    }
	else 
		printf("set the Broadcast(%s) successfully\n", str);
	
	close(fd);
	return 0;
}

/*
	The function can set the ip addr, the parm str is want change's ip addr
*/
int setIpAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
		

	
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == fd)
    {
		perror("socket error\n");
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);
    if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
    {
		perror(ifr.ifr_name);
        close( fd );
        return -1;
    }
	
	//set the Mash addr
	struct sockaddr_in *sin_net_ip;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name )-1);
    sin_net_ip = (struct sockaddr_in *)&ifr.ifr_addr;
    sin_net_ip->sin_family = AF_INET;
    inet_pton(AF_INET, str, &sin_net_ip->sin_addr);

    if (ioctl(fd, SIOCSIFADDR, &ifr ) < 0)
    {
        close( fd );
        printf("set net ip error:%s\n", strerror(errno));
        return -1;
    }
	else 
		printf("set the ip(%s) successfully\n", str);
	
	close(fd);
	
	return 0;
}
/*
	The function can set the gateway addr, the parm str is want change's gateway addr
*/
int setGatewayAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
    struct rtentry rt;
		
	
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == fd)
    {
		perror("socket\n");
        return -1;
    }

    memset(&rt, 0, sizeof(struct rtentry));
    memset(&sin, 0, sizeof(struct sockaddr_in));
    sin.sin_family = AF_INET;
    sin.sin_port = 0;
    if(inet_aton(str, &sin.sin_addr)<0)
    {
        perror("inet_aton error\n" );
        close(fd);
        return -1;
    }

    memcpy (&rt.rt_gateway, &sin, sizeof(struct sockaddr_in));
    ((struct sockaddr_in *)&rt.rt_dst)->sin_family=AF_INET;
    ((struct sockaddr_in *)&rt.rt_genmask)->sin_family=AF_INET;
    rt.rt_flags = RTF_GATEWAY;
    if (ioctl(fd, SIOCADDRT, &rt)<0)
    {
        perror("ioctl(SIOCADDRT) error in set_default_route\n");
        close(fd);
        return -1;
    }
	else
		printf("set the gateway(%s) successfully\n", str);
	
	close(fd);
	return 0;
}
/*
	The function can set the mask addr, the parm str is want change's mask addr
*/
int setMaskAddr(char *str)
{
    int fd;
    struct ifreq ifr;
    struct sockaddr_in sin;
	
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == fd)
    {
        return -1;
    }

    strcpy(g_eth_name, "eth0");
    strcpy(ifr.ifr_name, g_eth_name);
	
	//set the Mash addr
	struct sockaddr_in *sin_net_mask;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name )-1);
    sin_net_mask = (struct sockaddr_in *)&ifr.ifr_addr;
    sin_net_mask->sin_family = AF_INET;
    inet_pton(AF_INET, str, &sin_net_mask->sin_addr);

    if (ioctl(fd, SIOCSIFNETMASK, &ifr ) < 0)
    {
        close( fd );
        printf("set net mask error:%s\n", strerror(errno));
        return -1;
    }
	else 
		printf("set the mask(%s) successfully\n", str);
	
	close(fd);
	return 0;
}
int main()
{
	char buff[512] = {0};
	/*获取当前的IP信息*/
	getLocalMacAddr(buff);
	getBroadcastAddr(buff);
	getIpAddr(buff);
	getGatewayAddr(buff);
	getMaskAddr(buff);
	
	/*设置IP信息*/
	setBroadcastAddr("192.168.2");
	setIpAddr("192.168.1.202");
	setGatewayAddr("192.168.1.3");
	setMaskAddr("255.255.0.0");
	/*查看修改后的IP信息*/
	getLocalMacAddr(buff);
	getBroadcastAddr(buff);
	getIpAddr(buff);
	getGatewayAddr(buff);
	getMaskAddr(buff);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值