-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
174 lines (133 loc) · 4.35 KB
/
socket.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifdef OSX
#include <sys/types.h>
#include <sys/sysctl.h>
#include <net/if_dl.h>
#endif
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include "socket.h"
#include <sys/select.h>
#include <errno.h>
int sock;
struct sockaddr_in sockAddr;
#ifdef LINUX
unsigned char * linux_get_mac(const char *intf) {
unsigned char * mac = (unsigned char *) malloc(sizeof(unsigned char *) * 6);
struct ifreq iface;
unsigned char i;
int tmpSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
//How to get the MAC of the Interface: http://www.geekpage.jp/en/programming/linux-network/get-macaddr.php
iface.ifr_addr.sa_family = AF_INET;
strncpy(iface.ifr_name, intf, IFNAMSIZ-1);
ioctl(tmpSock, SIOCGIFHWADDR, &iface);
close(tmpSock);
for (i = 0; i < 6; i++)
mac[i] = (unsigned char)iface.ifr_hwaddr.sa_data[i];
return mac;
}
#endif
#ifdef OSX
unsigned char * bsd_get_mac(const char *intf) {
int32_t mib[6];
size_t len;
char *buf;
unsigned char *mac;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex(intf)) == 0) {
perror("if_nametoindex error");
exit(2);
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
perror("sysctl 1 error");
exit(3);
}
if ((buf = malloc(len)) == NULL) {
perror("malloc error");
exit(4);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
perror("sysctl 2 error");
exit(5);
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
mac = (unsigned char *)LLADDR(sdl);
return mac;
}
#endif
unsigned char * get_mac(const char *intf) {
#ifdef LINUX
return linux_get_mac(intf);
#endif
#ifdef OSX
return bsd_get_mac(intf);
#endif
}
void init_socket() {
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1) {
printf("Could not create Socket\n");
exit(1);
}
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddr.sin_port = htons(63321);
if (bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) < 0) {
printf("Could not bind to socket\n");
exit(1);
}
}
int sendBroadcast(unsigned char * data, unsigned int len) {
int bcp = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &bcp, sizeof(bcp)) < 0) {
printf("Could not broadcast data\n");
exit(1);
}
struct sockaddr_in bcAddr;
bcAddr.sin_family = AF_INET;
bcAddr.sin_addr.s_addr = inet_addr("255.255.255.255");
bcAddr.sin_port = htons(63322);
return sendto(sock, data, len, 0, (struct sockaddr *) &bcAddr, sizeof(bcAddr));
}
int recvBroadcast(char * data) {
struct sockaddr_in sa_in;
socklen_t sa_len = sizeof(sa_in);
struct timeval tout;
tout.tv_sec = 0;
tout.tv_usec = 500000;
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
int s = select(sock + 1, &fds, NULL, NULL, &tout);
if (s <= 0)
return -1;
return recvfrom(sock, data, PACKET_BUFFER, 0,(struct sockaddr * )&sa_in, &sa_len);
}
int recvBroadcast_tout(char * data) {
struct sockaddr_in sa_in;
socklen_t sa_len = sizeof(sa_in);
struct timeval tout;
tout.tv_sec = 5;
tout.tv_usec = 0;
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
int s = select(sock + 1, &fds, NULL, NULL, &tout);
if (s <= 0)
return -1;
return recvfrom(sock, data, PACKET_BUFFER, 0,(struct sockaddr * )&sa_in, &sa_len);
}