forked from fpasteau/squeezelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
383 lines (327 loc) · 8.38 KB
/
utils.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Squeezelite - lightweight headless squeezebox emulator
*
* (c) Adrian Smith 2012-2014, [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "squeezelite.h"
#if LINUX || OSX
#include <sys/ioctl.h>
#include <net/if.h>
#include <netdb.h>
#endif
#if WIN
#include <iphlpapi.h>
#endif
#if OSX
#include <net/if_dl.h>
#include <net/if_types.h>
#include <ifaddrs.h>
#include <netdb.h>
#endif
#include <fcntl.h>
// logging functions
const char *logtime(void) {
static char buf[100];
#if WIN
SYSTEMTIME lt;
GetLocalTime(<);
sprintf(buf, "[%02d:%02d:%02d.%03d]", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
strftime(buf, sizeof(buf), "[%T.", localtime(&tv.tv_sec));
sprintf(buf+strlen(buf), "%06ld]", (long)tv.tv_usec);
#endif
return buf;
}
void logprint(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fflush(stderr);
}
// cmdline parsing
char *next_param(char *src, char c) {
static char *str = NULL;
char *ptr, *ret;
if (src) str = src;
if (str && (ptr = strchr(str, c))) {
ret = str;
*ptr = '\0';
str = ptr + 1;
} else {
ret = str;
str = NULL;
}
return ret && ret[0] ? ret : NULL;
}
// clock
u32_t gettime_ms(void) {
#if WIN
return GetTickCount();
#else
#if LINUX
struct timespec ts;
if (!clock_gettime(CLOCK_MONOTONIC, &ts)) {
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
#endif
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
// mac address
#if LINUX
// search first 4 interfaces returned by IFCONF
void get_mac(u8_t mac[]) {
struct ifconf ifc;
struct ifreq *ifr, *ifend;
struct ifreq ifreq;
struct ifreq ifs[4];
mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0;
int s = socket(AF_INET, SOCK_DGRAM, 0);
ifc.ifc_len = sizeof(ifs);
ifc.ifc_req = ifs;
if (ioctl(s, SIOCGIFCONF, &ifc) == 0) {
ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
if (ifr->ifr_addr.sa_family == AF_INET) {
strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
if (ioctl (s, SIOCGIFHWADDR, &ifreq) == 0) {
memcpy(mac, ifreq.ifr_hwaddr.sa_data, 6);
if (mac[0]+mac[1]+mac[2] != 0) {
break;
}
}
}
}
}
close(s);
}
#endif
#if OSX
void get_mac(u8_t mac[]) {
struct ifaddrs *addrs, *ptr;
const struct sockaddr_dl *dlAddr;
const unsigned char *base;
mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0;
if (getifaddrs(&addrs) == 0) {
ptr = addrs;
while (ptr) {
if (ptr->ifa_addr->sa_family == AF_LINK && ((const struct sockaddr_dl *) ptr->ifa_addr)->sdl_type == IFT_ETHER) {
dlAddr = (const struct sockaddr_dl *)ptr->ifa_addr;
base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
memcpy(mac, base, min(dlAddr->sdl_alen, 6));
break;
}
ptr = ptr->ifa_next;
}
freeifaddrs(addrs);
}
}
#endif
#if WIN
#pragma comment(lib, "IPHLPAPI.lib")
void get_mac(u8_t mac[]) {
IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);
DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0;
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_SUCCESS) {
memcpy(mac, AdapterInfo[0].Address, 6);
}
}
#endif
void set_nonblock(sockfd s) {
#if WIN
u_long iMode = 1;
ioctlsocket(s, FIONBIO, &iMode);
#else
int flags = fcntl(s, F_GETFL,0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
#endif
}
// connect for socket already set to non blocking with timeout in seconds
int connect_timeout(sockfd sock, const struct sockaddr *addr, socklen_t addrlen, int timeout) {
fd_set w, e;
struct timeval tval;
if (connect(sock, addr, addrlen) < 0) {
#if !WIN
if (last_error() != EINPROGRESS) {
#else
if (last_error() != WSAEWOULDBLOCK) {
#endif
return -1;
}
}
FD_ZERO(&w);
FD_SET(sock, &w);
e = w;
tval.tv_sec = timeout;
tval.tv_usec = 0;
// only return 0 if w set and sock error is zero, otherwise return error code
if (select(sock + 1, NULL, &w, &e, timeout ? &tval : NULL) == 1 && FD_ISSET(sock, &w)) {
int error = 0;
socklen_t len = sizeof(error);
getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&error, &len);
return error;
}
return -1;
}
void server_addr(char *server, in_addr_t *ip_ptr, unsigned *port_ptr) {
struct addrinfo *res = NULL;
struct addrinfo hints;
const char *port = NULL;
if (strtok(server, ":")) {
port = strtok(NULL, ":");
if (port) {
*port_ptr = atoi(port);
}
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
getaddrinfo(server, NULL, &hints, &res);
if (res && res->ai_addr) {
*ip_ptr = ((struct sockaddr_in*)res->ai_addr)->sin_addr.s_addr;
}
if (res) {
freeaddrinfo(res);
}
}
void set_readwake_handles(event_handle handles[], sockfd s, event_event e) {
#if WINEVENT
handles[0] = WSACreateEvent();
handles[1] = e;
WSAEventSelect(s, handles[0], FD_READ | FD_CLOSE);
#elif SELFPIPE
handles[0].fd = s;
handles[1].fd = e.fds[0];
handles[0].events = POLLIN;
handles[1].events = POLLIN;
#else
handles[0].fd = s;
handles[1].fd = e;
handles[0].events = POLLIN;
handles[1].events = POLLIN;
#endif
}
event_type wait_readwake(event_handle handles[], int timeout) {
#if WINEVENT
int wait = WSAWaitForMultipleEvents(2, handles, FALSE, timeout, FALSE);
if (wait == WSA_WAIT_EVENT_0) {
WSAResetEvent(handles[0]);
return EVENT_READ;
} else if (wait == WSA_WAIT_EVENT_0 + 1) {
return EVENT_WAKE;
} else {
return EVENT_TIMEOUT;
}
#else
if (poll(handles, 2, timeout) > 0) {
if (handles[0].revents) {
return EVENT_READ;
}
if (handles[1].revents) {
wake_clear(handles[1].fd);
return EVENT_WAKE;
}
}
return EVENT_TIMEOUT;
#endif
}
// pack/unpack to network byte order
void packN(u32_t *dest, u32_t val) {
u8_t *ptr = (u8_t *)dest;
*(ptr) = (val >> 24) & 0xFF; *(ptr+1) = (val >> 16) & 0xFF; *(ptr+2) = (val >> 8) & 0xFF; *(ptr+3) = val & 0xFF;
}
void packn(u16_t *dest, u16_t val) {
u8_t *ptr = (u8_t *)dest;
*(ptr) = (val >> 8) & 0xFF; *(ptr+1) = val & 0xFF;
}
u32_t unpackN(u32_t *src) {
u8_t *ptr = (u8_t *)src;
return *(ptr) << 24 | *(ptr+1) << 16 | *(ptr+2) << 8 | *(ptr+3);
}
u16_t unpackn(u16_t *src) {
u8_t *ptr = (u8_t *)src;
return *(ptr) << 8 | *(ptr+1);
}
#if OSX
void set_nosigpipe(sockfd s) {
int set = 1;
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
}
#endif
#if WIN
void winsock_init(void) {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
int WSerr = WSAStartup(wVersionRequested, &wsaData);
if (WSerr != 0) {
LOG_ERROR("Bad winsock version");
exit(1);
}
}
void winsock_close(void) {
WSACleanup();
}
void *dlopen(const char *filename, int flag) {
SetLastError(0);
return LoadLibrary((LPCTSTR)filename);
}
void *dlsym(void *handle, const char *symbol) {
SetLastError(0);
return (void *)GetProcAddress(handle, symbol);
}
char *dlerror(void) {
static char ret[32];
int last = GetLastError();
if (last) {
sprintf(ret, "code: %i", last);
SetLastError(0);
return ret;
}
return NULL;
}
// this only implements numfds == 1
int poll(struct pollfd *fds, unsigned long numfds, int timeout) {
fd_set r, w;
struct timeval tv;
int ret;
FD_ZERO(&r);
FD_ZERO(&w);
if (fds[0].events & POLLIN) FD_SET(fds[0].fd, &r);
if (fds[0].events & POLLOUT) FD_SET(fds[0].fd, &w);
tv.tv_sec = timeout / 1000;
tv.tv_usec = 1000 * (timeout % 1000);
ret = select(fds[0].fd + 1, &r, &w, NULL, &tv);
if (ret < 0) return ret;
fds[0].revents = 0;
if (FD_ISSET(fds[0].fd, &r)) fds[0].revents |= POLLIN;
if (FD_ISSET(fds[0].fd, &w)) fds[0].revents |= POLLOUT;
return ret;
}
#endif
#if LINUX
void touch_memory(u8_t *buf, size_t size) {
u8_t *ptr;
for (ptr = buf; ptr < buf + size; ptr += sysconf(_SC_PAGESIZE)) {
*ptr = 0;
}
}
#endif