-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathprotocol_pack.c
executable file
·31 lines (29 loc) · 1000 Bytes
/
protocol_pack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include"protocol_pack.h"
u_char mac_addr[6];
int get_mac()
{
int sockfd;
struct ifreq tmp;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if( sockfd < 0)
{
perror("create socket fail\n");
return -1;
}
memset(&tmp,0,sizeof(struct ifreq));
strncpy(tmp.ifr_name,"eth0",sizeof(tmp.ifr_name)-1);
if( (ioctl(sockfd,SIOCGIFHWADDR,&tmp)) < 0 )
{
printf("mac ioctl error\n");
return -1;
}
mac_addr[0] = (unsigned char)tmp.ifr_hwaddr.sa_data[0];
mac_addr[1] = (unsigned char)tmp.ifr_hwaddr.sa_data[1];
mac_addr[2] = (unsigned char)tmp.ifr_hwaddr.sa_data[2];
mac_addr[3] = (unsigned char)tmp.ifr_hwaddr.sa_data[3];
mac_addr[4] = (unsigned char)tmp.ifr_hwaddr.sa_data[4];
mac_addr[5] = (unsigned char)tmp.ifr_hwaddr.sa_data[5];
//printf("local mac:%x:%x:%x:%x:%x:%x\n", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);
close(sockfd);
return 0;
}