Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334371
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: LINUX

2011-09-21 10:21:39

/* tcp4_10localip.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include   // added by liyuan
#include   // added by liyuan
struct  in_addr myself, mymask;
int fd_arp;                 /* socket fd for receive packets */
struct  ifreq  ifr;            /* ifr structure */
main (int argc, char* argv[]) {
    char device[32];  /* ethernet device name */
    struct sockaddr  from, to;
    int fromlen;
    struct sockaddr_in *sin_ptr;
    u_char *ptr;
    int n;
    strcpy(device, "eth0");
    if ((fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806))) < 0) {
        perror( "arp socket error");
        exit(-1);
    }
    strcpy(ifr.ifr_name, device);
    /* ifr.ifr_addr.sa_family = AF_INET; */
    /* get ip address of my interface */
    if (ioctl(fd_arp, SIOCGIFADDR, &ifr) < 0) {
        perror("ioctl SIOCGIFADDR error");
        exit(-1);
    }
    sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
    myself  = sin_ptr->sin_addr;
    /* get network mask of my interface */
    if (ioctl(fd_arp, SIOCGIFNETMASK, &ifr) < 0) {
        perror("ioctl SIOCGIFNETMASK error");
        exit(-1);
    }
    sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
    mymask  = sin_ptr->sin_addr;
    /* get mac address of the interface */
    if (ioctl(fd_arp, SIOCGIFHWADDR, &ifr) < 0) {
        perror("ioctl SIOCGIFHWADDR error");
        exit(-1);
    }
    ptr = (u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
    printf( "request mac %02x:%02x:%02x:%02x:%02x:%02x, ",
            *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
            *(ptr + 4), *(ptr + 5) );
    printf( "request netmask  %s ", inet_ntoa(mymask));
    printf( "request IP  %s\n", inet_ntoa(myself));
}  /* end of main */
阅读(286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~