forked from CaptainDouche/esp_smartcfg_linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.c
306 lines (261 loc) · 6.45 KB
/
sockets.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "sockets.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if (defined _WIN32)
#include <Windows.h>
#include <winsock.h>
#include <winsock2.h>
// # pragma comment(lib, "wsock32.lib")
#elif (defined _LINUX)
#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <linux/if_link.h>
#include <linux/wireless.h>
#endif
bool parse_ipstr(const char* str, uint8_t* ip)
{
int n = sscanf(str, "%hhu.%hhu.%hhu.%hhu", ip+0, ip+1, ip+2, ip+3);
return (n == 4);
}
bool parse_macstr(const char* str, uint8_t* mac)
{
int n = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac+0, mac+1, mac+2, mac+3, mac+4, mac+5);
return (n == 6);
}
void setup_addr_in(sockaddr_in_t* addr, const char* addrstr, unsigned short port)
{
memset(addr, 0, sizeof(sockaddr_in_t));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(addrstr); // INADDR_ANY / INADDR_BROADCAST
}
void print_socketerror(const char* msg)
{
#if (defined _WIN32)
int e = WSAGetLastError();
char *s = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
e,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&s,
0,
NULL
);
eprintf("%s: %s (%d)\n", msg, s, e);
LocalFree(s);
#elif (defined _LINUX)
int e = errno;
eprintf("%s: %s (%d)\n", msg, strerror(e), e);
#endif
}
int get_ifc_addr(const char* ifname, char* ipstr)
{
#if (defined _WIN32)
// TODO ...
#elif (defined _LINUX)
// http://man7.org/linux/man-pages/man3/getifaddrs.3.html
*ipstr = '\0';
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
return -1;
}
int res = -1;
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL) continue;
if ((ifa->ifa_addr->sa_family == AF_INET) && STREQU(ifa->ifa_name, ifname))
{
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ipstr, sizeof("111.222.333.444"), NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
}
else
{
//printf("%s: address: <%s>\n", ifa->ifa_name, ipstr);
res = 0;
}
break;
}
}
freeifaddrs(ifaddr);
return res;
#endif
}
int dump_own_addrs(void)
{
#if (defined _WIN32)
char name[HOSTNAME_MAX + 1];
if (gethostname(name, sizeof(name)) == 0)
{
int i = 0;
struct hostent* hostinfo;
printf("IP addresses of %s:\n", name);
if ((hostinfo = gethostbyname(name)) != NULL)
{
while (hostinfo->h_addr_list[i])
{
char* ip = inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[i]);
printf("%s\n", ip);
i++;
}
}
return i;
}
else
{
print_socketerror("gethostname()");
return -1;
}
#elif (defined _LINUX)
// http://man7.org/linux/man-pages/man3/getifaddrs.3.html
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
return -1;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL) continue;
int family = ifa->ifa_addr->sa_family;
char host[NI_MAXHOST];
if (family == AF_INET)
{
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
break;
}
printf("%-8s %s\n", ifa->ifa_name, host);
}
else
if (family == AF_INET6)
{
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
break;
}
printf("%-8s %s\n", ifa->ifa_name, host);
}
}
freeifaddrs(ifaddr);
return 0;
#endif
}
bool sockets_init(void)
{
#if (defined _WIN32)
WSADATA wsaData;
WORD wVerReq = MAKEWORD(1, 1);
WSAStartup(wVerReq, &wsaData);
#endif
return true;
}
void sockets_deinit(void)
{
#if (defined _WIN32)
WSACleanup();
#endif
}
bool sockopt_broadcast(SOCKET sock)
{
int yes=1;
int res = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes));
if (res != 0)
{
print_socketerror("setsockopt(SO_BROADCAST)");
closesocket(sock);
return false;
}
return true;
}
bool sockopt_blocking(SOCKET sock, bool blocking)
{
#if (defined _WIN32)
u_long flags = blocking ? 0 : 1;
bool res = (NO_ERROR == ioctlsocket(sock, FIONBIO, &flags));
#elif (defined _LINUX)
const int flags = fcntl(sock, F_GETFL, 0);
bool res = (0 == fcntl(sock, F_SETFL, blocking ? flags ^ O_NONBLOCK : flags | O_NONBLOCK));
#endif
if (!res)
{
print_socketerror("sockopt_blocking");
}
return res;
}
SOCKET udpsock(void)
{
SOCKET sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
print_socketerror("socket() failed");
return INVALID_SOCKET;
}
return sock;
}
int wifi_get_ssid(const char* ifname, char* essid, uint8_t* bssid)
{
#if (defined _WIN32)
// TODO ...
#elif (defined _LINUX)
int res = -1;
struct iwreq req;
strncpy(req.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ);
int fd, status;
fd = socket(AF_INET, SOCK_DGRAM, 0);
char* buffer;
buffer = calloc(32, sizeof(char));
req.u.essid.pointer = buffer;
req.u.essid.length = 32;
if (ioctl(fd, SIOCGIWESSID, &req) < 0)
{
eprintf(stderr, "Failed get ESSID on interface %s: %s\n", ifname, strerror(errno));
}
else
{
//printf("%s ESSID: %s", ifname, (char*)req.u.essid.pointer);
strncpy(essid, (char*)req.u.essid.pointer, IW_ESSID_MAX_SIZE);
essid[IW_ESSID_MAX_SIZE] = '\0';
res = 0;
}
if (bssid)
{
memset(bssid, 0x00, MAC_LEN);
struct iwreq iwr;
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(fd, SIOCGIWAP, &iwr) < 0)
{
perror("ioctl[SIOCGIWAP]");
}
else
{
memcpy(bssid, iwr.u.ap_addr.sa_data, MAC_LEN);
//printf("%s BSSID: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx \n", ifname, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
}
}
free(buffer);
return res;
#endif
}