关闭

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

1013人阅读 评论(0) 收藏 举报
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;
};套接口地址结构
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:304790次
    • 积分:4934
    • 等级:
    • 排名:第5750名
    • 原创:181篇
    • 转载:0篇
    • 译文:0篇
    • 评论:21条
    文章分类