使用SIOCSIFADDR ioctl设置IP地址 [英] Set IP address using SIOCSIFADDR ioctl

查看:3412
本文介绍了使用SIOCSIFADDR ioctl设置IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Linux上的IOCTL接口来设置IP地址。
我成功地获得并设置了它。当我设置ip地址时,
ifconfig eth0 显示正确的IP地址,但随后系统断开连接。
即系统不可ping。
这是我设置IP地址的代码。如果我遗失了什么,请告诉我。

I am trying to get and set the IP address using IOCTL interface on Linux. I am successfully able to get and set it. When I set the ip address, ifconfig eth0 shows proper IP address, but then the system gets disconnected. i.e. System is not pingable. Here's my code for setting the IP address. Please let me know if I am missing something.


  struct ifreq ifr;
  in_addr_t in_addr;
  struct sockaddr_in sin;

  memset(&ifr, 0, sizeof(struct ifreq));
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  sprintf(ifr.ifr_name, "eth0");
  in_addr = inet_addr("192.168.101.17");
  sin.sin_addr.s_addr = in_addr;
  memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
  io = ioctl(sockfd, SIOCSIFADDR, (char *)&ifr); 


推荐答案

这适用于接口或别名。使用strace验证操作是否正确:

This will work for interfaces or aliases. Use "strace" to verify correct operation:

strace ./ifconfig

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 5
ioctl(5, SIOCSIFADDR, {ifr_name="eth0:8", ifr_addr={AF_INET, inet_addr("192.168.1.202")}}) = 0
ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0:8", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
ioctl(5, SIOCSIFFLAGS, {ifr_name="eth0:8", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
close(5)                                = 0

完整源代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>             /* offsetof */
#include <net/if.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_ether.h>
#endif

#define IFNAME "eth0:2"
#define HOST "192.168.1.204"
#define ifreq_offsetof(x)  offsetof(struct ifreq, x)

int main(int argc, char **argv) {

        struct ifreq ifr;
        struct sockaddr_in sai;
        int sockfd;                     /* socket fd we use to manipulate stuff with */
        int selector;
        unsigned char mask;

        char *p;


        /* Create a channel to the NET kernel. */
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        /* get interface name */
        strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);

        memset(&sai, 0, sizeof(struct sockaddr));
        sai.sin_family = AF_INET;
        sai.sin_port = 0;

        sai.sin_addr.s_addr = inet_addr(HOST);

        p = (char *) &sai;
        memcpy( (((char *)&ifr + ifreq_offsetof(ifr_addr) )),
                        p, sizeof(struct sockaddr));

        ioctl(sockfd, SIOCSIFADDR, &ifr);
        ioctl(sockfd, SIOCGIFFLAGS, &ifr);

        ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
        // ifr.ifr_flags &= ~selector;  // unset something

        ioctl(sockfd, SIOCSIFFLAGS, &ifr);
        close(sockfd);
        return 0;
}

这篇关于使用SIOCSIFADDR ioctl设置IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆