#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;
}
|