分类: LINUX
2011-06-28 14:30:07
linux用指令来设置IP,MASK,GATEWAY:
//终端指令实现
//ifconfig eth0 192.168.0.0 netmask 255.255.0.0
//route add default gw 10.240.0.0
//C代码实现
char IP[24] = "192.168.1.0";
char Mask[24] = "255.255.0.0“;
char gateway[24] = "10.240.0.0";
char cmd_IP_Mask[64] = {0};
char cmd_GW[64] = {0};
sprintf(cmd_IP_Mask, "ifconfig eth0 %s netmask %s", IP, Mask);
sprintf(cmd_GW, "route add default gw %s netmask %s", gateway);
unsigned int IP_Address = inet_addr(IP),将点间隔的IP地址转换成长整形的数据
将长整形的IP地址以点阵形式输出:
printf("GetHostIp:IP...%s\n",inet_ntoa( IP_Address ));
//如果应用传入的是长整形的ip ,mask ,gw,用以下C代码来实现转换---可以直接拿来用的,已经测试通过
#define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
#define SET_SA_FAMILY(addr, family) \
memset((char*)&(addr), '\0', sizeof(addr)); \
addr.sa_family = (family)
char ipaddr[20];
char netmask[20];
char gateway[20];
sprintf(ipaddr, "%d.%d.%d.%d", (ip&0xff000000)>>24,
(ip&0xff0000)>>16, (ip&0xff00)>>8, (ip&0xff));
sprintf(netmask, "%d.%d.%d.%d", (mask&0xff000000)>>24,
(mask&0xff0000)>>16, (mask&0xff00)>>8,
(mask&0xff));
sprintf(gateway, "%d.%d.%d.%d", (gw&0xff000000)>>24,
(gw&0xff0000)>>16, (gw&0xff00)>>8,
(gw&0xff));
static int set_ipaddr(char *ipaddr)
{
int s;
if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
return FAIL;
}
struct ifreq ifr;
bzero((char *)&ifr, sizeof(ifr));
strcpy(ifr.ifr_name,"eth0");
struct sockaddr_in addr;
bzero(&addr, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
inet_aton(ipaddr,&addr.sin_addr);//将输入字符转成网络地址
memcpy((char*)&ifr.ifr_ifru.ifru_addr, (char*)&addr, sizeof(struct sockaddr_in));
if(ioctl(s, SIOCSIFADDR, &ifr) < 0)
{
close(s);
return FAIL;
}
close(s);
return OK;
}
static int set_netmask(char *netmask)
{
int s;
if((s = socket(AF_INET,SOCK_DGRAM, 0)) < 0)
{
KK_ERROR("[set_netmask]create socket failed:line:%d\n",__LINE__);
return FAIL;
}
struct ifreq ifr;
bzero((char *)&ifr, sizeof(ifr));
strcpy(ifr.ifr_name,"eth0");
struct sockaddr_in netmask_addr;
bzero(&netmask_addr, sizeof(struct sockaddr_in));
netmask_addr.sin_family = AF_INET;
inet_aton(netmask, &netmask_addr.sin_addr);
memcpy((char*)&ifr.ifr_ifru.ifru_netmask, (char*)&netmask_addr, sizeof(struct sockaddr_in));
if(ioctl(s,SIOCSIFNETMASK,&ifr) < 0)
{
close(s);
KK_ERROR("[set_netmask]ioctl failed:line:%d\n",__LINE__);
return FAIL;
}
close(s);
return OK;
}
static int set_gateway(char *gateway)
{
static int sock_fd = -1;
struct rtentry rt;
U32 dstaddr, gwaddr;
dstaddr = inet_addr("0.0.0.0");
gwaddr = inet_addr(gateway);
/* Get an internet socket for doing socket ioctls. */
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&rt, 0, sizeof(rt));
/*set Destination addr*/
SET_SA_FAMILY (rt.rt_dst, AF_INET);
SIN_ADDR(rt.rt_dst) = dstaddr;
/*set gw addr*/
SET_SA_FAMILY (rt.rt_gateway, AF_INET);
SIN_ADDR(rt.rt_gateway) = gwaddr;
/*set genmask addr*/
SET_SA_FAMILY (rt.rt_genmask, AF_INET);
SIN_ADDR(rt.rt_genmask) = 0L;
rt.rt_dev = "eth0";
rt.rt_flags = RTF_GATEWAY;
if(ioctl(sock_fd, SIOCADDRT, &rt) < 0)
{
close(sock_fd);
KK_ERROR("[set_gateway]ioctl failed:line:%d\n",__LINE__);
return FAIL;
}
return OK;
}