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.

216 lines
5.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* Micro web server for serving SSDP .xml file
  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 <err.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <ifaddrs.h>
  22. #include <netdb.h>
  23. #include <poll.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <arpa/inet.h>
  30. #include <net/if.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include "ssdp.h"
  35. const char *xml =
  36. "<?xml version=\"1.0\"?>\r\n"
  37. "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\r\n"
  38. " <specVersion>\r\n"
  39. " <major>1</major>\r\n"
  40. " <minor>0</minor>\r\n"
  41. " </specVersion>\r\n"
  42. " <device>\r\n"
  43. " <deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:1</deviceType>\r\n"
  44. " <friendlyName>%s</friendlyName>\r\n"
  45. " <manufacturer>%s</manufacturer>\r\n%s"
  46. " <modelName>%s</modelName>\r\n"
  47. " <UDN>uuid:%s</UDN>\r\n"
  48. " <presentationURL>http://%s</presentationURL>\r\n"
  49. " </device>\r\n"
  50. "</root>\r\n"
  51. "\r\n";
  52. /* Peek into SOCK_STREAM on accepted client socket to figure out inbound interface */
  53. static struct sockaddr_in *stream_peek(int sd, char *ifname)
  54. {
  55. struct ifaddrs *ifaddr, *ifa;
  56. static struct sockaddr_in sin;
  57. socklen_t len = sizeof(sin);
  58. if (-1 == getsockname(sd, (struct sockaddr *)&sin, &len))
  59. return NULL;
  60. if (-1 == getifaddrs(&ifaddr))
  61. return NULL;
  62. for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
  63. size_t len = sizeof(struct in_addr);
  64. struct sockaddr_in *iin;
  65. if (!ifa->ifa_addr)
  66. continue;
  67. if (ifa->ifa_addr->sa_family != AF_INET)
  68. continue;
  69. iin = (struct sockaddr_in *)ifa->ifa_addr;
  70. if (!memcmp(&sin.sin_addr, &iin->sin_addr, len)) {
  71. strncpy(ifname, ifa->ifa_name, IF_NAMESIZE);
  72. break;
  73. }
  74. }
  75. freeifaddrs(ifaddr);
  76. return &sin;
  77. }
  78. static void respond(int sd, struct sockaddr_in *sin)
  79. {
  80. struct pollfd pfd = {
  81. .fd = sd,
  82. .events = POLLIN,
  83. };
  84. char *head = "HTTP/1.1 200 OK\r\n"
  85. "Content-Type: text/xml\r\n"
  86. "Connection: close\r\n"
  87. "\r\n";
  88. char hostname[64], url[128] = "";
  89. char mesg[1024], *reqline[3];
  90. int rc, rcvd, fd, bytes_read;
  91. /* Check for early disconnect or client timeout */
  92. rc = poll(&pfd, 1, 1000);
  93. if (rc <= 0)
  94. goto error;
  95. memset(mesg, 0, sizeof(mesg));
  96. rcvd = recv(sd, mesg, sizeof(mesg), 0);
  97. if (rcvd <= 0) {
  98. logit(LOG_WARNING, "web recv() error: %s", strerror(errno));
  99. goto error;
  100. }
  101. logit(LOG_DEBUG, "%s", mesg);
  102. reqline[0] = strtok(mesg, " \t\n");
  103. if (strncmp(reqline[0], "GET", 4) == 0) {
  104. reqline[1] = strtok(NULL, " \t");
  105. reqline[2] = strtok(NULL, " \t\n");
  106. if (strncmp(reqline[2], "HTTP/1.0", 8) != 0 && strncmp(reqline[2], "HTTP/1.1", 8) != 0) {
  107. if (write(sd, "HTTP/1.1 400 Bad Request\r\n", 26) < 0)
  108. logit(LOG_WARNING, "Failed returning status 400 to client: %s", strerror(errno));
  109. goto error;
  110. }
  111. /* XXX: Add support for icon as well */
  112. if (!strstr(reqline[1], LOCATION_DESC)) {
  113. if (write(sd, "HTTP/1.1 404 Not Found\r\n", 24) < 0)
  114. logit(LOG_WARNING, "Failed returning status 404 to client: %s", strerror(errno));
  115. goto error;
  116. }
  117. gethostname(hostname, sizeof(hostname));
  118. #ifdef MANUFACTURER_URL
  119. snprintf(url, sizeof(url), " <manufacturerURL>%s</manufacturerURL>\r\n", MANUFACTURER_URL);
  120. #endif
  121. logit(LOG_DEBUG, "Sending XML reply ...");
  122. send(sd, head, strlen(head), 0);
  123. snprintf(mesg, sizeof(mesg), xml,
  124. hostname,
  125. MANUFACTURER,
  126. url,
  127. MODEL,
  128. uuid,
  129. inet_ntoa(sin->sin_addr));
  130. if (send(sd, mesg, strlen(mesg), 0) < 0)
  131. logit(LOG_WARNING, "Failed sending file to client: %s", strerror(errno));
  132. }
  133. error:
  134. shutdown(sd, SHUT_RDWR);
  135. close(sd);
  136. }
  137. void web_recv(int sd)
  138. {
  139. int client;
  140. char ifname[IF_NAMESIZE] = "UNKNOWN";
  141. struct sockaddr_in *sin;
  142. client = accept(sd, NULL, NULL);
  143. if (client < 0) {
  144. logit(LOG_ERR, "accept() error: %s", strerror(errno));
  145. return;
  146. }
  147. sin = stream_peek(client, ifname);
  148. if (!sin) {
  149. logit(LOG_ERR, "Failed resolving client interface: %s", strerror(errno));
  150. return;
  151. }
  152. respond(client, sin);
  153. shutdown(client, SHUT_RDWR);
  154. close(client);
  155. }
  156. void web_init(void)
  157. {
  158. int sd;
  159. struct sockaddr sa;
  160. struct sockaddr_in *sin;
  161. memset(&sa, 0, sizeof(sa));
  162. sa.sa_family = AF_INET;
  163. sin = (struct sockaddr_in *)&sa;
  164. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  165. sin->sin_port = htons(LOCATION_PORT);
  166. sd = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  167. if (sd == -1)
  168. err(1, "Failed creating web socket");
  169. ENABLE_SOCKOPT(sd, SOL_SOCKET, SO_REUSEADDR);
  170. #ifdef SO_REUSEPORT
  171. ENABLE_SOCKOPT(sd, SOL_SOCKET, SO_REUSEPORT);
  172. #endif
  173. if (bind(sd, &sa, sizeof(sa)) < 0)
  174. err(1, "Failed binding web socket");
  175. if (listen(sd, 10) != 0)
  176. err(1, "Failed setting web listen backlog");
  177. register_socket(sd, -1, &sa, NULL, web_recv);
  178. }
  179. /**
  180. * Local Variables:
  181. * indent-tabs-mode: t
  182. * c-file-style: "linux"
  183. * End:
  184. */