通过IP或socket获取对方的MAC地址

http://blog.chinaunix.net/uid-17188120-id-2820866.html

1.通过已经连接的socket文件获取:

  1. int getpeermac( int sockfd, char *buf ) 
  2.     int ret =0;  
  3.     struct arpreq arpreq;  
  4.     struct sockaddr_in dstadd_in;  
  5.     socklen_t  len = sizeofstruct sockaddr_in );  
  6.     memset( &arpreq, 0, sizeofstruct arpreq ));  
  7.     memset( &dstadd_in, 0, sizeofstruct sockaddr_in ));  
  8.     if( getpeername( sockfd, (struct sockaddr*)&dstadd_in, &len ) < 0 )  
  9.         err_err("getpeername()");  
  10.     else  
  11.     {  
  12.         memcpy( &arpreq.arp_pa, &dstadd_in, sizeofstruct sockaddr_in ));  
  13.         strcpy(arpreq.arp_dev, "eth1");  
  14.         arpreq.arp_pa.sa_family = AF_INET;  
  15.         arpreq.arp_ha.sa_family = AF_UNSPEC;  
  16.         if( ioctl( sockfd, SIOCGARP, &arpreq ) < 0 )  
  17.             err_err("ioctl SIOCGARP");  
  18.         else  
  19.         {  
  20.             unsigned char* ptr = (unsigned char *)arpreq.arp_ha.sa_data;  
  21.             ret = sprintf(buf, "%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));  
  22.         }  
  23.     }  
  24.     return ret;  
  25. }   

2. 通过IP获取:

/**
* @send_arp.c
* @This software is intended to be used as a example to show how to send and receive arp request with Linux * PF_PACKET interface
* @Author:xuelei
**/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include /* 需要里面的 glibc 版本号 */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
        #include
        #include /* 链路层(L2)协议 */
#else
        #include
        #include
        #include /* 链路层协议 */
#endif
#include

#define INLEN 4
#define MAC_BCAST_ADDR  (uint8_t *) "\xff\xff\xff\xff\xff\xff"

void usage_quit(char *arg0);
int get_ifi(char *dev, char *mac, int macln, struct in_addr *lc_addr, int ipln);
void prmac(u_char *ptr);

int main(int argc, char **argv)
{
        if(argc != 2)
                usage_quit(argv[0]);

        int fd, salen, n;
        u_char *mac;
        char recv_buf[120], rep_addr[16];
        struct in_addr lc_addr, req_addr;
        struct sockaddr_ll reqsa, repsa;
        struct arp_pkt {
                struct ether_header eh;
                struct ether_arp ea;
                u_char padding[18];
        } req;

        bzero(&reqsa, sizeof(reqsa));
        reqsa.sll_family = PF_PACKET;
        reqsa.sll_ifindex = if_nametoindex("eth0");

        if((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) < 0) {
                perror("Socket error");
                exit(1);
        }

        mac = (char *)malloc(ETH_ALEN);
        bzero(&req, sizeof(req));

        if(get_ifi("eth0", mac, ETH_ALEN, &lc_addr, INLEN)) {
                fprintf(stderr, "Error: Get host’s information failed\n");
                exit(0);
        }
        printf("        Host MAC is: %02x:%02x:%02x:%02x:%02x:%02x\n",*mac,*(mac+1),*(mac+2),*(mac+3),*(mac+4),*(mac+5));
         printf("       Host IP is: %s\n", inet_ntop(AF_INET, &lc_addr, rep_addr, 1024));
        /* 填写以太网头部*/
        memcpy(req.eh.ether_dhost, MAC_BCAST_ADDR, ETH_ALEN);
        memcpy(req.eh.ether_shost, mac, ETH_ALEN);
        req.eh.ether_type = htons(ETHERTYPE_ARP);

        /* 填写arp数据 */
        req.ea.arp_hrd = htons(ARPHRD_ETHER);
        req.ea.arp_pro = htons(ETHERTYPE_IP);
        req.ea.arp_hln = ETH_ALEN;
        req.ea.arp_pln = INLEN;
        req.ea.arp_op = htons(ARPOP_REQUEST);
        memcpy(req.ea.arp_sha, mac, ETH_ALEN);
        memcpy(req.ea.arp_spa, &lc_addr, INLEN);
        inet_aton(argv[1], req.ea.arp_tpa);

        bzero(recv_buf, sizeof(recv_buf));
        bzero(&repsa, sizeof(repsa));
        salen = sizeof(struct sockaddr_ll);

        if((n = sendto(fd, &req, sizeof(req), 0, (struct sockaddr *)&reqsa, sizeof(reqsa))) <= 0) {
                perror("Sendto error");
                exit(1);
        }
        printf("Broadcast arp request of %s, %d bytes be sent\n\n", argv[1], n);



        while(1) {
                if((n = recvfrom(fd, recv_buf, sizeof(req), 0, (struct sockaddr *)&repsa, &salen)) <= 0) {
                        perror("Recvfrom error");
                        exit(1);
                }
                if(ntohs(*(__be16 *)(recv_buf + 20))==2 && !memcmp(req.ea.arp_tpa, recv_buf + 28, 4)) {
                        printf("Response from %s, %d bytes received\n", argv[1], n);
                        printf("        Peer IP is: %s\n", inet_ntop(AF_INET, (struct in_addr *)(recv_buf + 28), rep_addr, 1024));
                        prmac( (u_char *)(recv_buf + 22) ); //prmac( (u_char *)(recv_buf + 6) );
                        break;
                }
                else
                    {
                    printf("Have recive data\n");
                    break;
                    }
        }

        free(mac);
}

int get_ifi(char *dev, char * mac, int macln, struct in_addr *lc_addr, int ipln)
{
        int reqfd, n;
        struct ifreq macreq;

        reqfd = socket(AF_INET, SOCK_DGRAM, 0);
        strcpy(macreq.ifr_name, dev);

        /* 获取本地接口MAC地址*/
        if(ioctl(reqfd, SIOCGIFHWADDR, ¯eq) != 0)
                return 1;
        memcpy(mac, macreq.ifr_hwaddr.sa_data, macln);

        /* 获取本地接口IP地址*/
        if(ioctl(reqfd, SIOCGIFADDR, ¯eq) != 0)
                return 1;
        memcpy(lc_addr, &((struct sockaddr_in *)(¯eq.ifr_addr))->sin_addr, ipln);

        return 0;
}

void prmac(u_char *ptr)
{
        printf("        Peer MAC is: %02x:%02x:%02x:%02x:%02x:%02x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
}

void usage_quit(char *arg0)
{
        fprintf(stderr, "Usage: %s \n", arg0);
        exit(1);
}

你可能感兴趣的:(C语言接口,TCPIP网络协议)