close

敬告遊客:
以下內容枯燥無聊,若情非得已不建議進入。
若仍執意進入,當有頭暈嘔吐現象時請盡速離開本篇文章。



int main(int argc,char **argv)
{
        struct sockaddr_in *addr;
        struct ifreq ifr;
        char *name,*address;
        int sockfd;

        if(argc != 2)
                usage();
        else
                name = argv[1];

        sockfd = socket(AF_INET,SOCK_DGRAM,0);
        strncpy(ifr.ifr_name,name,IFNAMSIZ-1);

        if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1)
                perror("ioctl error"),exit(1);
        addr = (struct sockaddr_in *)&(ifr.ifr_addr);
        address = inet_ntoa(addr->sin_addr);
        printf("inet addr: %s ",address);

        if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1)
                perror("ioctl error"),exit(1);
        addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
        address = inet_ntoa(addr->sin_addr);
        printf("broad addr: %s ",address);

        if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1)
                perror("ioctl error"),exit(1);
        addr = (struct sockaddr_in *)&ifr.ifr_addr;
        address = inet_ntoa(addr->sin_addr);
        printf("inet mask: %s ",address);

        printf("/n");
        exit(0);
}

arrow
arrow
    文章標籤
    get ip get netmask
    全站熱搜
    創作者介紹
    創作者 calories 的頭像
    calories

    卡油裡

    calories 發表在 痞客邦 留言(0) 人氣()