2006-06-6 (1)获取网口地址 linux GNU c ioctl

unsigned long do_ioctl_get_ipaddress(char *dev)
{
                struct ifreq ifr;
                int fd;
                int err;
                unsigned long ip;
                unsigned long myip;
                struct in_addr tmp_addr;
                int i=0;
        strcpy(ifr.ifr_name, dev);
        fd = socket(AF_INET, SOCK_DGRAM, 0);
        err = ioctl(fd, SIOCGIFADDR, &ifr);
        if (err) {
                perror("ioctl");
                return 0;
        }
        close(fd);
        memcpy(&ip,ifr.ifr_addr.sa_data + 2,4);
        myip = ip;
        tmp_addr.s_addr=myip;
        return myip;
}
 
 
 

#include <sys/socket.h>

int socket (int family, int type, int protocol);

Figure 4.2. Protocol family constants for socket function.
graphics/04fig02.gif
Figure 4.3. type of socket for socket function.
graphics/04fig03.gif
Figure 4.4. protocol of sockets for AF_INET or AF_INET6.
graphics/04fig04.gif

Not all combinations of socket family and type are valid. Figure 4.5 shows the valid combinations, along with the actual protocols that are valid for each pair. The boxes marked "Yes" are valid but do not have handy acronyms. The blank boxes are not supported.

Figure 4.5. Combinations of family and type for the socket function.
graphics/04fig05.gif
 
 
 
 

#include <unistd.h>

int ioctl(int fd, int request, ... /* void *arg */ );

graphics/17fig01.gif
 
#define IFNAMSIZ     16

struct ifreq {
    char    ifr_name[IFNAMSIZ];  /* interface name, e.g., "le0" */
    union {
        struct  sockaddr ifru_addr;
        struct  sockaddr ifru_dstaddr;
        struct  sockaddr ifru_broadaddr;
        short   ifru_flags;
        int     ifru_metric;
        caddr_t ifru_data;
    } ifr_ifru;
};套接口地址结构
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值