You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

901 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* SSDP responder
  2. *
  3. * Copyright (c) 2017 Joachim Nilsson <troglobit@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.a
  16. */
  17. #include <config.h>
  18. #include <ctype.h>
  19. #include <err.h>
  20. #include <errno.h>
  21. #include <getopt.h>
  22. #include <ifaddrs.h>
  23. #include <netdb.h>
  24. #include <paths.h>
  25. #include <poll.h>
  26. #include <stdio.h>
  27. #include <signal.h>
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <arpa/inet.h>
  34. #include <netinet/ip.h>
  35. #include <netinet/udp.h>
  36. #include <sys/param.h> /* MIN() */
  37. #include <sys/socket.h>
  38. #include "ssdp.h"
  39. #include "queue.h"
  40. struct ifsock {
  41. LIST_ENTRY(ifsock) link;
  42. int stale;
  43. int mod;
  44. /*
  45. * Sockets for inbound and outbound
  46. *
  47. * - The inbound is the multicast socket, shared between all ifaces
  48. * - The outbound is bound to the iface address and a random port
  49. */
  50. int in, out;
  51. /* Interface address and netmask */
  52. struct sockaddr_in addr;
  53. struct sockaddr_in mask;
  54. void (*cb)(int);
  55. };
  56. LIST_HEAD(, ifsock) il = LIST_HEAD_INITIALIZER();
  57. static char *supported_types[] = {
  58. SSDP_ST_ALL,
  59. "upnp:rootdevice",
  60. "urn:schemas-upnp-org:device:InternetGatewayDevice:1",
  61. uuid,
  62. NULL
  63. };
  64. int debug = 0;
  65. int running = 1;
  66. char uuid[42];
  67. char hostname[64];
  68. char *os = NULL, *ver = NULL;
  69. char server_string[64] = "POSIX UPnP/1.0 " PACKAGE_NAME "/" PACKAGE_VERSION;
  70. /* Find interface in same subnet as sa */
  71. static struct ifsock *find_outbound(struct sockaddr *sa)
  72. {
  73. in_addr_t cand;
  74. struct ifsock *ifs;
  75. struct sockaddr_in *addr = (struct sockaddr_in *)sa;
  76. cand = addr->sin_addr.s_addr;
  77. LIST_FOREACH(ifs, &il, link) {
  78. in_addr_t a, m;
  79. a = ifs->addr.sin_addr.s_addr;
  80. m = ifs->mask.sin_addr.s_addr;
  81. if (a == htonl(INADDR_ANY) || m == htonl(INADDR_ANY))
  82. continue;
  83. if ((a & m) == (cand & m))
  84. return ifs;
  85. }
  86. return NULL;
  87. }
  88. /* Exact match, must be same ifaddr as sa */
  89. static struct ifsock *find_iface(struct sockaddr *sa)
  90. {
  91. struct ifsock *ifs;
  92. struct sockaddr_in *addr = (struct sockaddr_in *)sa;
  93. if (!sa)
  94. return NULL;
  95. LIST_FOREACH(ifs, &il, link) {
  96. if (ifs->addr.sin_addr.s_addr == addr->sin_addr.s_addr)
  97. return ifs;
  98. }
  99. return NULL;
  100. }
  101. int register_socket(int in, int out, struct sockaddr *addr, struct sockaddr *mask, void (*cb)(int sd))
  102. {
  103. struct ifsock *ifs;
  104. struct sockaddr_in *address = (struct sockaddr_in *)addr;
  105. struct sockaddr_in *netmask = (struct sockaddr_in *)mask;
  106. ifs = calloc(1, sizeof(*ifs));
  107. if (!ifs) {
  108. char *host = inet_ntoa(address->sin_addr);
  109. logit(LOG_ERR, "Failed registering host %s socket: %s", host, strerror(errno));
  110. return -1;
  111. }
  112. ifs->in = in;
  113. ifs->out = out;
  114. ifs->mod = 1;
  115. ifs->cb = cb;
  116. ifs->addr = *address;
  117. if (mask)
  118. ifs->mask = *netmask;
  119. LIST_INSERT_HEAD(&il, ifs, link);
  120. return 0;
  121. }
  122. static int open_socket(char *ifname, struct sockaddr *addr, int port)
  123. {
  124. int sd, val, rc;
  125. char loop;
  126. struct ip_mreqn mreq;
  127. struct sockaddr_in sin, *address = (struct sockaddr_in *)addr;
  128. sd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  129. if (sd < 0)
  130. return -1;
  131. sin.sin_family = AF_INET;
  132. sin.sin_port = htons(port);
  133. sin.sin_addr = address->sin_addr;
  134. if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  135. close(sd);
  136. logit(LOG_ERR, "Failed binding to %s:%d: %s", inet_ntoa(address->sin_addr), port, strerror(errno));
  137. return -1;
  138. }
  139. #if 0
  140. ENABLE_SOCKOPT(sd, SOL_SOCKET, SO_REUSEADDR);
  141. #ifdef SO_REUSEPORT
  142. ENABLE_SOCKOPT(sd, SOL_SOCKET, SO_REUSEPORT);
  143. #endif
  144. #endif
  145. memset(&mreq, 0, sizeof(mreq));
  146. mreq.imr_address = address->sin_addr;
  147. mreq.imr_multiaddr.s_addr = inet_addr(MC_SSDP_GROUP);
  148. if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
  149. close(sd);
  150. logit(LOG_ERR, "Failed joining group %s: %s", MC_SSDP_GROUP, strerror(errno));
  151. return -1;
  152. }
  153. val = 2; /* Default 2, but should be configurable */
  154. rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, &val, sizeof(val));
  155. if (rc < 0) {
  156. close(sd);
  157. logit(LOG_ERR, "Failed setting multicast TTL: %s", strerror(errno));
  158. return -1;
  159. }
  160. loop = 0;
  161. rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
  162. if (rc < 0) {
  163. close(sd);
  164. logit(LOG_ERR, "Failed disabing multicast loop: %s", strerror(errno));
  165. return -1;
  166. }
  167. rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &address->sin_addr, sizeof(address->sin_addr));
  168. if (rc < 0) {
  169. close(sd);
  170. logit(LOG_ERR, "Failed setting multicast interface: %s", strerror(errno));
  171. return -1;
  172. }
  173. logit(LOG_DEBUG, "Adding new interface %s with address %s", ifname, inet_ntoa(address->sin_addr));
  174. return sd;
  175. }
  176. static int close_socket(void)
  177. {
  178. int ret = 0;
  179. struct ifsock *ifs, *tmp;
  180. LIST_FOREACH_SAFE(ifs, &il, link, tmp) {
  181. LIST_REMOVE(ifs, link);
  182. if (ifs->out != -1)
  183. ret |= close(ifs->out);
  184. else
  185. ret |= close(ifs->in);
  186. free(ifs);
  187. }
  188. return ret;
  189. }
  190. static int filter_addr(struct sockaddr *sa)
  191. {
  192. struct ifsock *ifs;
  193. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  194. if (!sa)
  195. return 1;
  196. if (sa->sa_family != AF_INET)
  197. return 1;
  198. if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
  199. return 1;
  200. if (sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
  201. return 1;
  202. ifs = find_outbound(sa);
  203. if (ifs) {
  204. if (ifs->addr.sin_addr.s_addr != htonl(INADDR_ANY))
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. static int filter_iface(char *ifname, char *iflist[], size_t num)
  210. {
  211. size_t i;
  212. if (!num) {
  213. logit(LOG_DEBUG, "No interfaces to filter, using all with an IP address.");
  214. return 0;
  215. }
  216. logit(LOG_DEBUG, "Filter %s? Comparing %zd entries ...", ifname, num);
  217. for (i = 0; i < num; i++) {
  218. logit(LOG_DEBUG, "Filter %s? Comparing with %s ...", ifname, iflist[i]);
  219. if (!strcmp(ifname, iflist[i]))
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. static void compose_addr(struct sockaddr_in *sin, char *group, int port)
  225. {
  226. memset(sin, 0, sizeof(*sin));
  227. sin->sin_family = AF_INET;
  228. sin->sin_port = htons(port);
  229. sin->sin_addr.s_addr = inet_addr(group);
  230. }
  231. static void compose_response(char *type, char *host, char *buf, size_t len)
  232. {
  233. char usn[256];
  234. char date[42];
  235. time_t now;
  236. /* RFC1123 date, as specified in RFC2616 */
  237. now = time(NULL);
  238. strftime(date, sizeof(date), "%a, %d %b %Y %T %Z", gmtime(&now));
  239. if (type) {
  240. if (!strcmp(type, uuid))
  241. type = NULL;
  242. else
  243. snprintf(usn, sizeof(usn), "%s::%s", uuid, type);
  244. }
  245. if (!type)
  246. strncpy(usn, uuid, sizeof(usn));
  247. snprintf(buf, len, "HTTP/1.1 200 OK\r\n"
  248. "Server: %s\r\n"
  249. "Date: %s\r\n"
  250. "Location: http://%s:%d%s\r\n"
  251. "ST: %s\r\n"
  252. "EXT: \r\n"
  253. "USN: %s\r\n"
  254. "Cache-Control: max-age=%d\r\n"
  255. "\r\n",
  256. server_string,
  257. date,
  258. host, LOCATION_PORT, LOCATION_DESC,
  259. type,
  260. usn,
  261. CACHE_TIMEOUT);
  262. }
  263. static void compose_search(char *type, char *buf, size_t len)
  264. {
  265. snprintf(buf, len, "M-SEARCH * HTTP/1.1\r\n"
  266. "Host: %s:%d\r\n"
  267. "MAN: \"ssdp:discover\"\r\n"
  268. "MX: 1\r\n"
  269. "ST: %s\r\n"
  270. "User-Agent: %s\r\n"
  271. "\r\n",
  272. MC_SSDP_GROUP, MC_SSDP_PORT,
  273. type,
  274. server_string);
  275. }
  276. static void compose_notify(char *type, char *host, char *buf, size_t len)
  277. {
  278. char usn[256];
  279. if (type) {
  280. if (!strcmp(type, SSDP_ST_ALL))
  281. type = NULL;
  282. else
  283. snprintf(usn, sizeof(usn), "%s::%s", uuid, type);
  284. }
  285. if (!type) {
  286. type = usn;
  287. strncpy(usn, uuid, sizeof(usn));
  288. }
  289. snprintf(buf, len, "NOTIFY * HTTP/1.1\r\n"
  290. "Host: %s:%d\r\n"
  291. "Server: %s\r\n"
  292. "Location: http://%s:%d%s\r\n"
  293. "NT: %s\r\n"
  294. "NTS: ssdp:alive\r\n"
  295. "USN: %s\r\n"
  296. "Cache-Control: max-age=%d\r\n"
  297. "\r\n",
  298. MC_SSDP_GROUP, MC_SSDP_PORT,
  299. server_string,
  300. host, LOCATION_PORT, LOCATION_DESC,
  301. type,
  302. usn,
  303. CACHE_TIMEOUT);
  304. }
  305. size_t pktlen(unsigned char *buf)
  306. {
  307. size_t hdr = sizeof(struct udphdr);
  308. return strlen((char *)buf + hdr) + hdr;
  309. }
  310. static void send_search(struct ifsock *ifs, char *type)
  311. {
  312. ssize_t num;
  313. char buf[MAX_PKT_SIZE];
  314. struct sockaddr dest;
  315. memset(buf, 0, sizeof(buf));
  316. compose_search(type, buf, sizeof(buf));
  317. compose_addr((struct sockaddr_in *)&dest, MC_SSDP_GROUP, MC_SSDP_PORT);
  318. logit(LOG_DEBUG, "Sending M-SEARCH ...");
  319. num = sendto(ifs->out, buf, strlen(buf), 0, &dest, sizeof(struct sockaddr_in));
  320. if (num < 0)
  321. logit(LOG_WARNING, "Failed sending SSDP M-SEARCH");
  322. }
  323. static void send_message(struct ifsock *ifs, char *type, struct sockaddr *sa)
  324. {
  325. int s;
  326. size_t i, len, note = 0;
  327. ssize_t num;
  328. char host[NI_MAXHOST];
  329. char buf[MAX_PKT_SIZE];
  330. struct sockaddr dest;
  331. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  332. gethostname(hostname, sizeof(hostname));
  333. s = getnameinfo((struct sockaddr *)&ifs->addr, sizeof(struct sockaddr_in), host, sizeof(host), NULL, 0, NI_NUMERICHOST);
  334. if (s) {
  335. logit(LOG_WARNING, "Failed getnameinfo(): %s", gai_strerror(s));
  336. return;
  337. }
  338. if (ifs->addr.sin_addr.s_addr == htonl(INADDR_ANY))
  339. return;
  340. if (ifs->out == -1)
  341. return;
  342. if (!strcmp(type, SSDP_ST_ALL))
  343. type = NULL;
  344. memset(buf, 0, sizeof(buf));
  345. if (sin)
  346. compose_response(type, host, buf, sizeof(buf));
  347. else
  348. compose_notify(type, host, buf, sizeof(buf));
  349. if (!sin) {
  350. note = 1;
  351. compose_addr((struct sockaddr_in *)&dest, MC_SSDP_GROUP, MC_SSDP_PORT);
  352. sin = (struct sockaddr_in *)&dest;
  353. }
  354. logit(LOG_DEBUG, "Sending %s from %s ...", !note ? "reply" : "notify", host);
  355. num = sendto(ifs->out, buf, strlen(buf), 0, sin, sizeof(struct sockaddr_in));
  356. if (num < 0)
  357. logit(LOG_WARNING, "Failed sending SSDP %s, type: %s: %s", !note ? "reply" : "notify", type, strerror(errno));
  358. }
  359. static void ssdp_recv(int sd)
  360. {
  361. ssize_t len;
  362. struct sockaddr sa;
  363. socklen_t salen;
  364. char buf[MAX_PKT_SIZE + 1];
  365. memset(buf, 0, sizeof(buf));
  366. len = recvfrom(sd, buf, sizeof(buf) - 1, MSG_DONTWAIT, &sa, &salen);
  367. if (len > 0) {
  368. if (sa.sa_family != AF_INET)
  369. return;
  370. if (strstr(buf, "M-SEARCH *")) {
  371. size_t i;
  372. char *ptr, *type;
  373. struct ifsock *ifs;
  374. struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
  375. ifs = find_outbound(&sa);
  376. if (!ifs) {
  377. logit(LOG_DEBUG, "No matching socket for client %s", inet_ntoa(sin->sin_addr));
  378. return;
  379. }
  380. logit(LOG_DEBUG, "Matching socket for client %s", inet_ntoa(sin->sin_addr));
  381. type = strcasestr(buf, "\r\nST:");
  382. if (!type) {
  383. logit(LOG_DEBUG, "No Search Type (ST:) found in M-SEARCH *, assuming " SSDP_ST_ALL);
  384. type = SSDP_ST_ALL;
  385. send_message(ifs, type, &sa);
  386. return;
  387. }
  388. type = strchr(type, ':');
  389. if (!type)
  390. return;
  391. type++;
  392. while (isspace(*type))
  393. type++;
  394. ptr = strstr(type, "\r\n");
  395. if (!ptr)
  396. return;
  397. *ptr = 0;
  398. for (i = 0; supported_types[i]; i++) {
  399. if (!strcmp(supported_types[i], type)) {
  400. logit(LOG_DEBUG, "M-SEARCH * ST: %s from %s port %d", type,
  401. inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
  402. send_message(ifs, type, &sa);
  403. return;
  404. }
  405. }
  406. logit(LOG_DEBUG, "M-SEARCH * for unsupported ST: %s from %s", type,
  407. inet_ntoa(sin->sin_addr));
  408. }
  409. }
  410. }
  411. static int multicast_init(void)
  412. {
  413. int sd;
  414. struct sockaddr sa;
  415. struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
  416. sd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  417. if (sd < 0) {
  418. logit(LOG_ERR, "Failed opening multicast socket: %s", strerror(errno));
  419. return -1;
  420. }
  421. memset(&sa, 0, sizeof(sa));
  422. sin->sin_family = AF_INET;
  423. sin->sin_addr.s_addr = inet_addr(MC_SSDP_GROUP);
  424. sin->sin_port = htons(MC_SSDP_PORT);
  425. if (bind(sd, &sa, sizeof(*sin)) < 0) {
  426. close(sd);
  427. logit(LOG_ERR, "Failed binding to %s:%d: %s", inet_ntoa(sin->sin_addr), MC_SSDP_PORT, strerror(errno));
  428. return -1;
  429. }
  430. register_socket(sd, -1, &sa, NULL, ssdp_recv);
  431. return sd;
  432. }
  433. static int multicast_join(int sd, struct sockaddr *sa)
  434. {
  435. struct ip_mreqn mreq;
  436. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  437. memset(&mreq, 0, sizeof(mreq));
  438. mreq.imr_address = sin->sin_addr;
  439. mreq.imr_multiaddr.s_addr = inet_addr(MC_SSDP_GROUP);
  440. if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
  441. if (EADDRINUSE == errno)
  442. return 0;
  443. logit(LOG_ERR, "Failed joining group %s: %s", MC_SSDP_GROUP, strerror(errno));
  444. return -1;
  445. }
  446. return 0;
  447. }
  448. static void mark(void)
  449. {
  450. struct ifsock *ifs;
  451. LIST_FOREACH(ifs, &il, link) {
  452. if (ifs->out != -1)
  453. ifs->stale = 1;
  454. else
  455. ifs->stale = 0;
  456. }
  457. }
  458. static int sweep(void)
  459. {
  460. int modified = 0;
  461. struct ifsock *ifs, *tmp;
  462. LIST_FOREACH_SAFE(ifs, &il, link, tmp) {
  463. if (!ifs->stale)
  464. continue;
  465. modified++;
  466. logit(LOG_DEBUG, "Removing stale ifs %s", inet_ntoa(ifs->addr.sin_addr));
  467. LIST_REMOVE(ifs, link);
  468. close(ifs->out);
  469. free(ifs);
  470. }
  471. return modified;
  472. }
  473. static int ssdp_init(int in, char *iflist[], size_t num)
  474. {
  475. int modified;
  476. size_t i;
  477. struct ifaddrs *ifaddrs, *ifa;
  478. logit(LOG_INFO, "Updating interfaces ...");
  479. if (getifaddrs(&ifaddrs) < 0) {
  480. logit(LOG_ERR, "Failed getifaddrs(): %s", strerror(errno));
  481. return -1;
  482. }
  483. /* Mark all outbound interfaces as stale */
  484. mark();
  485. /* First pass, clear stale marker from exact matches */
  486. for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
  487. struct ifsock *ifs;
  488. /* Do we already have it? */
  489. ifs = find_iface(ifa->ifa_addr);
  490. if (ifs) {
  491. ifs->stale = 0;
  492. continue;
  493. }
  494. }
  495. /* Clean out any stale interface addresses */
  496. modified = sweep();
  497. /* Second pass, add new ones */
  498. for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
  499. int sd;
  500. /* Interface filtering, optional command line argument */
  501. if (filter_iface(ifa->ifa_name, iflist, num)) {
  502. logit(LOG_DEBUG, "Skipping %s, not in iflist.", ifa->ifa_name);
  503. continue;
  504. }
  505. /* Do we have another in the same subnet? */
  506. if (filter_addr(ifa->ifa_addr))
  507. continue;
  508. sd = open_socket(ifa->ifa_name, ifa->ifa_addr, MC_SSDP_PORT);
  509. if (sd < 0)
  510. continue;
  511. multicast_join(in, ifa->ifa_addr);
  512. if (register_socket(in, sd, ifa->ifa_addr, ifa->ifa_netmask, ssdp_recv)) {
  513. close(sd);
  514. break;
  515. }
  516. modified++;
  517. }
  518. freeifaddrs(ifaddrs);
  519. return modified;
  520. }
  521. static void handle_message(int sd)
  522. {
  523. struct ifsock *ifs;
  524. LIST_FOREACH(ifs, &il, link) {
  525. if (ifs->in != sd)
  526. continue;
  527. if (ifs->cb)
  528. ifs->cb(sd);
  529. }
  530. }
  531. static void wait_message(time_t tmo)
  532. {
  533. int num = 1, timeout;
  534. size_t ifnum = 0;
  535. struct pollfd pfd[MAX_NUM_IFACES];
  536. struct ifsock *ifs;
  537. LIST_FOREACH(ifs, &il, link) {
  538. if (ifs->out != -1)
  539. continue;
  540. pfd[ifnum].fd = ifs->in;
  541. pfd[ifnum].events = POLLIN | POLLHUP;
  542. ifnum++;
  543. }
  544. while (1) {
  545. size_t i;
  546. timeout = tmo - time(NULL);
  547. if (timeout < 0)
  548. break;
  549. num = poll(pfd, ifnum, timeout * 1000);
  550. if (num < 0) {
  551. if (EINTR == errno)
  552. break;
  553. err(1, "Unrecoverable error");
  554. }
  555. if (num == 0)
  556. break;
  557. for (i = 0; num > 0 && i < ifnum; i++) {
  558. if (pfd[i].revents & POLLIN) {
  559. handle_message(pfd[i].fd);
  560. num--;
  561. }
  562. }
  563. }
  564. }
  565. static void announce(int mod)
  566. {
  567. struct ifsock *ifs;
  568. logit(LOG_INFO, "Sending SSDP NOTIFY new:%d ...", mod);
  569. LIST_FOREACH(ifs, &il, link) {
  570. size_t i;
  571. if (mod && !ifs->mod)
  572. continue;
  573. ifs->mod = 0;
  574. // send_search(ifs, "upnp:rootdevice");
  575. for (i = 0; supported_types[i]; i++) {
  576. /* UUID sent in SSDP_ST_ALL, first announce */
  577. if (!strcmp(supported_types[i], uuid))
  578. continue;
  579. send_message(ifs, supported_types[i], NULL);
  580. }
  581. }
  582. }
  583. static void lsb_init(void)
  584. {
  585. FILE *fp;
  586. char *ptr;
  587. char line[80];
  588. const char *file = "/etc/lsb-release";
  589. fp = fopen(file, "r");
  590. if (!fp) {
  591. fallback:
  592. logit(LOG_WARNING, "No %s found on system, using built-in server string.", file);
  593. return;
  594. }
  595. while (fgets(line, sizeof(line), fp)) {
  596. line[strlen(line) - 1] = 0;
  597. ptr = strstr(line, "DISTRIB_ID");
  598. if (ptr && (ptr = strchr(ptr, '=')))
  599. os = strdup(++ptr);
  600. ptr = strstr(line, "DISTRIB_RELEASE");
  601. if (ptr && (ptr = strchr(ptr, '=')))
  602. ver = strdup(++ptr);
  603. }
  604. fclose(fp);
  605. if (os && ver)
  606. snprintf(server_string, sizeof(server_string), "%s/%s UPnP/1.0 %s/%s",
  607. os, ver, PACKAGE_NAME, PACKAGE_VERSION);
  608. else
  609. goto fallback;
  610. logit(LOG_DEBUG, "Server: %s", server_string);
  611. }
  612. /* https://en.wikipedia.org/wiki/Universally_unique_identifier */
  613. static void uuidgen(void)
  614. {
  615. FILE *fp;
  616. char buf[42];
  617. const char *file = _PATH_VARDB PACKAGE_NAME ".cache";
  618. fp = fopen(file, "r");
  619. if (!fp) {
  620. fp = fopen(file, "w");
  621. if (!fp)
  622. logit(LOG_WARNING, "Cannot create UUID cache, %s: %s", file, strerror(errno));
  623. generate:
  624. srand(time(NULL));
  625. snprintf(buf, sizeof(buf), "uuid:%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x",
  626. rand() & 0xFFFFFFFF,
  627. rand() & 0xFFFF,
  628. (rand() & 0x0FFF) | 0x4000, /* M 4 MSB version => version 4 */
  629. (rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */
  630. rand() & 0xFFFFFF, rand() & 0xFFFFFF);
  631. if (fp) {
  632. logit(LOG_DEBUG, "Creating new UUID cache file, %s", file);
  633. fprintf(fp, "%s\n", buf);
  634. fclose(fp);
  635. }
  636. } else {
  637. if (!fgets(buf, sizeof(buf), fp)) {
  638. fclose(fp);
  639. goto generate;
  640. }
  641. buf[strlen(buf) - 1] = 0;
  642. fclose(fp);
  643. }
  644. strcpy(uuid, buf);
  645. logit(LOG_DEBUG, "URN: %s", uuid);
  646. }
  647. static void exit_handler(int signo)
  648. {
  649. (void)signo;
  650. running = 0;
  651. }
  652. static void signal_init(void)
  653. {
  654. signal(SIGTERM, exit_handler);
  655. signal(SIGINT, exit_handler);
  656. signal(SIGHUP, exit_handler);
  657. signal(SIGQUIT, exit_handler);
  658. }
  659. static int usage(int code)
  660. {
  661. printf("Usage: %s [-dhv] [-i SEC] [IFACE [IFACE ...]]\n"
  662. "\n"
  663. " -d Developer debug mode\n"
  664. " -h This help text\n"
  665. " -i SEC SSDP notify interval (30-900), default %d sec\n"
  666. " -r SEC Interface refresh interval (5-1800), default %d sec\n"
  667. " -v Show program version\n"
  668. "\n"
  669. "Bug report address: %-40s\n", PACKAGE_NAME, NOTIFY_INTERVAL, REFRESH_INTERVAL, PACKAGE_BUGREPORT);
  670. return code;
  671. }
  672. int main(int argc, char *argv[])
  673. {
  674. int i, c, sd;
  675. int log_level = LOG_NOTICE;
  676. int log_opts = LOG_CONS | LOG_PID;
  677. int interval = NOTIFY_INTERVAL;
  678. int refresh = REFRESH_INTERVAL;
  679. time_t now, rtmo = 0, itmo = 0;
  680. while ((c = getopt(argc, argv, "dhi:r:v")) != EOF) {
  681. switch (c) {
  682. case 'd':
  683. debug = 1;
  684. break;
  685. case 'h':
  686. return usage(0);
  687. case 'i':
  688. interval = atoi(optarg);
  689. if (interval < 30 || interval > 900)
  690. errx(1, "Invalid announcement interval (30-900).");
  691. break;
  692. case 'r':
  693. refresh = atoi(optarg);
  694. if (refresh < 5 || refresh > 1800)
  695. errx(1, "Invalid refresh interval (5-1800).");
  696. break;
  697. case 'v':
  698. puts(PACKAGE_VERSION);
  699. return 0;
  700. default:
  701. break;
  702. }
  703. }
  704. signal_init();
  705. if (debug) {
  706. log_level = LOG_DEBUG;
  707. log_opts |= LOG_PERROR;
  708. }
  709. openlog(PACKAGE_NAME, log_opts, LOG_DAEMON);
  710. setlogmask(LOG_UPTO(log_level));
  711. uuidgen();
  712. lsb_init();
  713. web_init();
  714. sd = multicast_init();
  715. if (sd < 0)
  716. err(1, "Failed creating multicast socket");
  717. while (running) {
  718. now = time(NULL);
  719. if (rtmo <= now) {
  720. if (ssdp_init(sd, &argv[optind], argc - optind) > 0)
  721. announce(1);
  722. rtmo = now + refresh;
  723. }
  724. if (itmo <= now) {
  725. announce(0);
  726. itmo = now + interval;
  727. }
  728. wait_message(MIN(rtmo, itmo));
  729. }
  730. closelog();
  731. return close_socket();
  732. }
  733. /**
  734. * Local Variables:
  735. * indent-tabs-mode: t
  736. * c-file-style: "linux"
  737. * End:
  738. */