Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752451
  • 博文数量: 119
  • 博客积分: 137
  • 博客等级: 少校
  • 技术积分: 1582
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 16:39
文章分类

全部博文(119)

文章存档

2017年(3)

2016年(7)

2014年(1)

2013年(8)

2012年(20)

2011年(27)

2010年(53)

分类: 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;
}

阅读(3387) | 评论(0) | 转发(0) |
0

上一篇:OSI七层与TCP/IP五层

下一篇:VB教材下载

给主人留下些什么吧!~~