util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * AirConnect: Chromecast & UPnP to AirPlay
  3. *
  4. * (c) Philippe 2016-2017, philippe_44@outlook.com
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. */
  10. #include "platform.h"
  11. #ifdef WIN32
  12. #include <iphlpapi.h>
  13. #else
  14. #include "tcpip_adapter.h"
  15. #include <ctype.h>
  16. #endif
  17. #include <stdarg.h>
  18. #include "pthread.h"
  19. #include "util.h"
  20. #include "log_util.h"
  21. /*----------------------------------------------------------------------------*/
  22. /* globals */
  23. /*----------------------------------------------------------------------------*/
  24. extern log_level util_loglevel;
  25. /*----------------------------------------------------------------------------*/
  26. /* locals */
  27. /*----------------------------------------------------------------------------*/
  28. static log_level *loglevel = &util_loglevel;
  29. static char *ltrim(char *s);
  30. static int read_line(int fd, char *line, int maxlen, int timeout);
  31. /*----------------------------------------------------------------------------*/
  32. /* */
  33. /* NETWORKING utils */
  34. /* */
  35. /*----------------------------------------------------------------------------*/
  36. /*---------------------------------------------------------------------------*/
  37. #define MAX_INTERFACES 256
  38. #define DEFAULT_INTERFACE 1
  39. #if !defined(WIN32)
  40. #define INVALID_SOCKET (-1)
  41. #endif
  42. in_addr_t get_localhost(char **name)
  43. {
  44. #ifdef WIN32
  45. char buf[256];
  46. struct hostent *h = NULL;
  47. struct sockaddr_in LocalAddr;
  48. memset(&LocalAddr, 0, sizeof(LocalAddr));
  49. gethostname(buf, 256);
  50. h = gethostbyname(buf);
  51. if (name) *name = strdup(buf);
  52. if (h != NULL) {
  53. memcpy(&LocalAddr.sin_addr, h->h_addr_list[0], 4);
  54. return LocalAddr.sin_addr.s_addr;
  55. }
  56. else return INADDR_ANY;
  57. #else
  58. tcpip_adapter_ip_info_t ipInfo;
  59. tcpip_adapter_if_t if_type = TCPIP_ADAPTER_IF_STA;
  60. // then get IP address
  61. tcpip_adapter_get_ip_info(if_type, &ipInfo);
  62. // we might be in AP mode
  63. if (ipInfo.ip.addr == INADDR_ANY) {
  64. if_type = TCPIP_ADAPTER_IF_AP;
  65. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo);
  66. }
  67. // get hostname if required
  68. if (name) {
  69. const char *hostname;
  70. tcpip_adapter_get_hostname(if_type, &hostname);
  71. *name = strdup(hostname);
  72. }
  73. return ipInfo.ip.addr;
  74. #endif
  75. }
  76. /*----------------------------------------------------------------------------*/
  77. #ifdef WIN32
  78. void winsock_init(void) {
  79. WSADATA wsaData;
  80. WORD wVersionRequested = MAKEWORD(2, 2);
  81. int WSerr = WSAStartup(wVersionRequested, &wsaData);
  82. if (WSerr != 0) {
  83. LOG_ERROR("Bad winsock version", NULL);
  84. exit(1);
  85. }
  86. }
  87. /*----------------------------------------------------------------------------*/
  88. void winsock_close(void) {
  89. WSACleanup();
  90. }
  91. #endif
  92. /*----------------------------------------------------------------------------*/
  93. int shutdown_socket(int sd)
  94. {
  95. if (sd <= 0) return -1;
  96. #ifdef WIN32
  97. shutdown(sd, SD_BOTH);
  98. #else
  99. shutdown(sd, SHUT_RDWR);
  100. #endif
  101. LOG_DEBUG("closed socket %d", sd);
  102. return closesocket(sd);
  103. }
  104. /*----------------------------------------------------------------------------*/
  105. int bind_socket(unsigned short *port, int mode)
  106. {
  107. int sock;
  108. socklen_t len = sizeof(struct sockaddr);
  109. struct sockaddr_in addr;
  110. if ((sock = socket(AF_INET, mode, 0)) < 0) {
  111. LOG_ERROR("cannot create socket %d", sock);
  112. return sock;
  113. }
  114. /* Populate socket address structure */
  115. memset(&addr, 0, sizeof(addr));
  116. addr.sin_family = AF_INET;
  117. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  118. addr.sin_port = htons(*port);
  119. #ifdef SIN_LEN
  120. si.sin_len = sizeof(si);
  121. #endif
  122. if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
  123. closesocket(sock);
  124. LOG_ERROR("cannot bind socket %d", sock);
  125. return -1;
  126. }
  127. if (!*port) {
  128. getsockname(sock, (struct sockaddr *) &addr, &len);
  129. *port = ntohs(addr.sin_port);
  130. }
  131. LOG_DEBUG("socket binding %d on port %d", sock, *port);
  132. return sock;
  133. }
  134. /*----------------------------------------------------------------------------*/
  135. int conn_socket(unsigned short port)
  136. {
  137. struct sockaddr_in addr;
  138. int sd;
  139. sd = socket(AF_INET, SOCK_STREAM, 0);
  140. addr.sin_family = AF_INET;
  141. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  142. addr.sin_port = htons(port);
  143. if (sd < 0 || connect(sd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  144. close(sd);
  145. return -1;
  146. }
  147. LOG_DEBUG("created socket %d", sd);
  148. return sd;
  149. }
  150. /*----------------------------------------------------------------------------*/
  151. /* */
  152. /* SYSTEM utils */
  153. /* */
  154. /*----------------------------------------------------------------------------*/
  155. #ifdef WIN32
  156. /*----------------------------------------------------------------------------*/
  157. void *dlopen(const char *filename, int flag) {
  158. SetLastError(0);
  159. return LoadLibrary((LPCTSTR)filename);
  160. }
  161. /*----------------------------------------------------------------------------*/
  162. void *dlsym(void *handle, const char *symbol) {
  163. SetLastError(0);
  164. return (void *)GetProcAddress(handle, symbol);
  165. }
  166. /*----------------------------------------------------------------------------*/
  167. char *dlerror(void) {
  168. static char ret[32];
  169. int last = GetLastError();
  170. if (last) {
  171. sprintf(ret, "code: %i", last);
  172. SetLastError(0);
  173. return ret;
  174. }
  175. return NULL;
  176. }
  177. #endif
  178. /*----------------------------------------------------------------------------*/
  179. /* */
  180. /* STDLIB extensions */
  181. /* */
  182. /*----------------------------------------------------------------------------*/
  183. #ifdef WIN32
  184. /*---------------------------------------------------------------------------*/
  185. char *strcasestr(const char *haystack, const char *needle) {
  186. size_t length_needle;
  187. size_t length_haystack;
  188. size_t i;
  189. if (!haystack || !needle)
  190. return NULL;
  191. length_needle = strlen(needle);
  192. length_haystack = strlen(haystack);
  193. if (length_haystack < length_needle) return NULL;
  194. length_haystack -= length_needle - 1;
  195. for (i = 0; i < length_haystack; i++)
  196. {
  197. size_t j;
  198. for (j = 0; j < length_needle; j++)
  199. {
  200. unsigned char c1;
  201. unsigned char c2;
  202. c1 = haystack[i+j];
  203. c2 = needle[j];
  204. if (toupper(c1) != toupper(c2))
  205. goto next;
  206. }
  207. return (char *) haystack + i;
  208. next:
  209. ;
  210. }
  211. return NULL;
  212. }
  213. /*---------------------------------------------------------------------------*/
  214. char* strsep(char** stringp, const char* delim)
  215. {
  216. char* start = *stringp;
  217. char* p;
  218. p = (start != NULL) ? strpbrk(start, delim) : NULL;
  219. if (p == NULL) {
  220. *stringp = NULL;
  221. } else {
  222. *p = '\0';
  223. *stringp = p + 1;
  224. }
  225. return start;
  226. }
  227. /*---------------------------------------------------------------------------*/
  228. char *strndup(const char *s, size_t n) {
  229. char *p = malloc(n + 1);
  230. strncpy(p, s, n);
  231. p[n] = '\0';
  232. return p;
  233. }
  234. #endif
  235. /*----------------------------------------------------------------------------*/
  236. char* strextract(char *s1, char *beg, char *end)
  237. {
  238. char *p1, *p2, *res;
  239. p1 = strcasestr(s1, beg);
  240. if (!p1) return NULL;
  241. p1 += strlen(beg);
  242. p2 = strcasestr(p1, end);
  243. if (!p2) return strdup(p1);
  244. res = malloc(p2 - p1 + 1);
  245. memcpy(res, p1, p2 - p1);
  246. res[p2 - p1] = '\0';
  247. return res;
  248. }
  249. #ifdef WIN32
  250. /*----------------------------------------------------------------------------*/
  251. int asprintf(char **strp, const char *fmt, ...)
  252. {
  253. va_list args, cp;
  254. int len, ret = 0;
  255. va_start(args, fmt);
  256. len = vsnprintf(NULL, 0, fmt, args);
  257. *strp = malloc(len + 1);
  258. if (*strp) ret = vsprintf(*strp, fmt, args);
  259. va_end(args);
  260. return ret;
  261. }
  262. #endif
  263. /*---------------------------------------------------------------------------*/
  264. static char *ltrim(char *s)
  265. {
  266. while(isspace((int) *s)) s++;
  267. return s;
  268. }
  269. /*----------------------------------------------------------------------------*/
  270. /* */
  271. /* HTTP management */
  272. /* */
  273. /*----------------------------------------------------------------------------*/
  274. /*----------------------------------------------------------------------------*/
  275. bool http_parse(int sock, char *method, key_data_t *rkd, char **body, int *len)
  276. {
  277. char line[256], *dp;
  278. unsigned j;
  279. int i, timeout = 100;
  280. rkd[0].key = NULL;
  281. if ((i = read_line(sock, line, sizeof(line), timeout)) <= 0) {
  282. if (i < 0) {
  283. LOG_ERROR("cannot read method", NULL);
  284. }
  285. return false;
  286. }
  287. if (!sscanf(line, "%s", method)) {
  288. LOG_ERROR("missing method", NULL);
  289. return false;
  290. }
  291. i = *len = 0;
  292. while (read_line(sock, line, sizeof(line), timeout) > 0) {
  293. LOG_SDEBUG("sock: %u, received %s", line);
  294. // line folding should be deprecated
  295. if (i && rkd[i].key && (line[0] == ' ' || line[0] == '\t')) {
  296. for(j = 0; j < strlen(line); j++) if (line[j] != ' ' && line[j] != '\t') break;
  297. rkd[i].data = realloc(rkd[i].data, strlen(rkd[i].data) + strlen(line + j) + 1);
  298. strcat(rkd[i].data, line + j);
  299. continue;
  300. }
  301. dp = strstr(line,":");
  302. if (!dp){
  303. LOG_ERROR("Request failed, bad header", NULL);
  304. kd_free(rkd);
  305. return false;
  306. }
  307. *dp = 0;
  308. rkd[i].key = strdup(line);
  309. rkd[i].data = strdup(ltrim(dp + 1));
  310. if (!strcasecmp(rkd[i].key, "Content-Length")) *len = atol(rkd[i].data);
  311. i++;
  312. rkd[i].key = NULL;
  313. }
  314. if (*len) {
  315. int size = 0;
  316. *body = malloc(*len + 1);
  317. while (*body && size < *len) {
  318. int bytes = recv(sock, *body + size, *len - size, 0);
  319. if (bytes <= 0) break;
  320. size += bytes;
  321. }
  322. (*body)[*len] = '\0';
  323. if (!*body || size != *len) {
  324. LOG_ERROR("content length receive error %d %d", *len, size);
  325. }
  326. }
  327. return true;
  328. }
  329. /*----------------------------------------------------------------------------*/
  330. static int read_line(int fd, char *line, int maxlen, int timeout)
  331. {
  332. int i,rval;
  333. int count=0;
  334. struct pollfd pfds;
  335. char ch;
  336. *line = 0;
  337. pfds.fd = fd;
  338. pfds.events = POLLIN;
  339. for(i = 0; i < maxlen; i++){
  340. if (poll(&pfds, 1, timeout)) rval=recv(fd, &ch, 1, 0);
  341. else return 0;
  342. if (rval == -1) {
  343. if (errno == EAGAIN) return 0;
  344. LOG_ERROR("fd: %d read error: %s", fd, strerror(errno));
  345. return -1;
  346. }
  347. if (rval == 0) {
  348. LOG_INFO("disconnected on the other end %u", fd);
  349. return 0;
  350. }
  351. if (ch == '\n') {
  352. *line=0;
  353. return count;
  354. }
  355. if (ch=='\r') continue;
  356. *line++=ch;
  357. count++;
  358. if (count >= maxlen-1) break;
  359. }
  360. *line = 0;
  361. return count;
  362. }
  363. /*----------------------------------------------------------------------------*/
  364. char *http_send(int sock, char *method, key_data_t *rkd)
  365. {
  366. unsigned sent, len;
  367. char *resp = kd_dump(rkd);
  368. char *data = malloc(strlen(method) + 2 + strlen(resp) + 2 + 1);
  369. len = sprintf(data, "%s\r\n%s\r\n", method, resp);
  370. NFREE(resp);
  371. sent = send(sock, data, len, 0);
  372. if (sent != len) {
  373. LOG_ERROR("HTTP send() error:%s %u (strlen=%u)", data, sent, len);
  374. NFREE(data);
  375. }
  376. return data;
  377. }
  378. /*----------------------------------------------------------------------------*/
  379. char *kd_lookup(key_data_t *kd, char *key)
  380. {
  381. int i = 0;
  382. while (kd && kd[i].key){
  383. if (!strcasecmp(kd[i].key, key)) return kd[i].data;
  384. i++;
  385. }
  386. return NULL;
  387. }
  388. /*----------------------------------------------------------------------------*/
  389. bool kd_add(key_data_t *kd, char *key, char *data)
  390. {
  391. int i = 0;
  392. while (kd && kd[i].key) i++;
  393. kd[i].key = strdup(key);
  394. kd[i].data = strdup(data);
  395. kd[i+1].key = NULL;
  396. return NULL;
  397. }
  398. /*----------------------------------------------------------------------------*/
  399. void kd_free(key_data_t *kd)
  400. {
  401. int i = 0;
  402. while (kd && kd[i].key){
  403. free(kd[i].key);
  404. if (kd[i].data) free(kd[i].data);
  405. i++;
  406. }
  407. kd[0].key = NULL;
  408. }
  409. /*----------------------------------------------------------------------------*/
  410. char *kd_dump(key_data_t *kd)
  411. {
  412. int i = 0;
  413. int pos = 0, size = 0;
  414. char *str = NULL;
  415. if (!kd || !kd[0].key) return strdup("\r\n");
  416. while (kd && kd[i].key) {
  417. char *buf;
  418. int len;
  419. len = asprintf(&buf, "%s: %s\r\n", kd[i].key, kd[i].data);
  420. while (pos + len >= size) {
  421. void *p = realloc(str, size + 1024);
  422. size += 1024;
  423. if (!p) {
  424. free(str);
  425. return NULL;
  426. }
  427. str = p;
  428. }
  429. memcpy(str + pos, buf, len);
  430. pos += len;
  431. free(buf);
  432. i++;
  433. }
  434. str[pos] = '\0';
  435. return str;
  436. }
  437. /*--------------------------------------------------------------------------*/
  438. void free_metadata(struct metadata_s *metadata)
  439. {
  440. NFREE(metadata->artist);
  441. NFREE(metadata->album);
  442. NFREE(metadata->title);
  443. NFREE(metadata->genre);
  444. NFREE(metadata->path);
  445. NFREE(metadata->artwork);
  446. NFREE(metadata->remote_title);
  447. }
  448. /*----------------------------------------------------------------------------*/
  449. int _fprintf(FILE *file, ...)
  450. {
  451. va_list args;
  452. char *fmt;
  453. int n;
  454. va_start(args, file);
  455. fmt = va_arg(args, char*);
  456. n = vfprintf(file, fmt, args);
  457. va_end(args);
  458. return n;
  459. }