Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1090962
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2009-11-27 15:50:08

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>

#define err(msg) perror(msg)
#define SA struct sockaddr

int getpeermac(int sockfd, char *buf)
{
     int ret =0;
     struct arpreq arpreq;
     struct sockaddr_in dstadd_in;

     socklen_t len = sizeof(struct sockaddr_in);
     memset(&arpreq, 0, sizeof(struct arpreq));
     memset(&dstadd_in, 0, sizeof( struct sockaddr_in));

     if(getpeername(sockfd, (struct sockaddr*)&dstadd_in, &len) < 0)
         err("getpeername()");
     else
     {
         memcpy( &arpreq.arp_pa, &dstadd_in, sizeof( struct sockaddr_in ));
         strcpy(arpreq.arp_dev, "eth0");
         arpreq.arp_pa.sa_family = AF_INET;
         arpreq.arp_ha.sa_family = AF_UNSPEC;
         if(ioctl(sockfd, SIOCGARP, &arpreq) < 0)
             err("ioctl SIOCGARP");
         else
         {
             unsigned char* ptr = (unsigned char *)arpreq.arp_ha.sa_data;
             ret = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
         }
     }
     return ret;
}

int main(void)
{
        struct sockaddr_in addr;
        int fd;

        if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
                err("socket");
                goto err;
        }

        memset(&addr, '\0', sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8888);
        if (inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr) <= 0) {
                err("inet_pton");
                goto err1;
        }

        if (bind(fd, (SA *)&addr, sizeof(addr)) == -1 || listen(fd, 10) == -1) {
                err("bind or listen");
                goto err;
        }

        while (1) {
                int connfd;

                if ((connfd = accept(fd, NULL, NULL)) == -1) {
                        err("accept");
                        continue;
                }

                char mac[18];

                memset(mac, '\0', sizeof(mac));
                getpeermac(connfd, mac);
                printf("%s\n", mac);

                close(connfd);
        }


        return 0;
err1:
        close(fd);
err:
        return -1;
}


阅读(5917) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~