ssdpd.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2012-2013 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <sys/confnet.h>
  35. #include <sys/timer.h>
  36. #include <arpa/inet.h>
  37. #include <pro/ssdp.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. static SSDP_DEVICE *regdev_root;
  41. static void AddResponseHeaders(HTTPU_SESSION *s, const char *location, const char *type)
  42. {
  43. HttpuAddHeader(s, NULL, "HTTP/1.1 200 OK", NULL);
  44. HttpuAddHeader(s, "CACHE-CONTROL", "max-age=1800", NULL);
  45. HttpuAddHeader(s, "EXT", NULL);
  46. HttpuAddHeader(s, "LOCATION", "http://", inet_ntoa(confnet.cdn_ip_addr), "/", location, "?", type, NULL);
  47. HttpuAddHeader(s, "SERVER", "NutOS/5.0, UPnP/1.0, Ethernut/5.0", NULL);
  48. }
  49. static void AddNotifyHeaders(HTTPU_SESSION *s, const char *location, const char *type)
  50. {
  51. HttpuAddHeader(s, NULL, "NOTIFY * HTTP/1.1", NULL);
  52. HttpuAddHeader(s, "HOST", ct_239_255_255_250, ":1900", NULL);
  53. HttpuAddHeader(s, "CACHE-CONTROL", "max-age=1800", NULL);
  54. HttpuAddHeader(s, "LOCATION", "http://", inet_ntoa(confnet.cdn_ip_addr), "/", location, "?", type, NULL);
  55. HttpuAddHeader(s, "SERVER", "NutOS/5.0, UPnP/1.0, Ethernut/5.0", NULL);
  56. }
  57. static void SendDeviceResponse(HTTPU_SESSION *s, SSDP_DEVICE *sdev, int mode)
  58. {
  59. NutSleep(100);
  60. AddResponseHeaders(s, sdev->sdev_url_desc, sdev->sdev_type);
  61. if (mode == 1) {
  62. HttpuAddHeader(s, "ST", ct_upnp_rootdevice, NULL);
  63. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, "::", ct_upnp_rootdevice, NULL);
  64. }
  65. else if (mode == 2) {
  66. HttpuAddHeader(s, "ST", "urn:", sdev->sdev_domain, ":device:", sdev->sdev_type, ":1", NULL);
  67. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, "::urn:", sdev->sdev_domain, ":device:", sdev->sdev_type, ":1", NULL);
  68. }
  69. else {
  70. HttpuAddHeader(s, "ST", ct_uuid_, sdev->sdev_uuid, NULL);
  71. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, NULL);
  72. }
  73. HttpuRespond(s);
  74. }
  75. static void SendDeviceNotify(HTTPU_SESSION *s, SSDP_DEVICE *sdev, int mode)
  76. {
  77. NutSleep(100);
  78. AddNotifyHeaders(s, sdev->sdev_url_desc, sdev->sdev_type);
  79. if (mode == 1) {
  80. HttpuAddHeader(s, "ST", ct_upnp_rootdevice, NULL);
  81. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, "::", ct_upnp_rootdevice, NULL);
  82. }
  83. else if (mode == 2) {
  84. HttpuAddHeader(s, "ST", "urn:", sdev->sdev_domain, ":device:", sdev->sdev_type, ":1", NULL);
  85. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, "::urn:", sdev->sdev_domain, ":device:", sdev->sdev_type, ":1", NULL);
  86. }
  87. else {
  88. HttpuAddHeader(s, "ST", ct_uuid_, sdev->sdev_uuid, NULL);
  89. HttpuAddHeader(s, "USN", ct_uuid_, sdev->sdev_uuid, NULL);
  90. }
  91. HttpuSend(s, inet_addr(ct_239_255_255_250), 1900);
  92. }
  93. static void SendServiceResponse(HTTPU_SESSION *s, SSDP_SERVICE *svc)
  94. {
  95. AddResponseHeaders(s, svc->ssvc_dev->sdev_url_desc, svc->ssvc_dev->sdev_type);
  96. HttpuAddHeader(s, "ST", "urn:", svc->ssvc_domain, ":service:", svc->ssvc_type, ":1", NULL);
  97. HttpuAddHeader(s, "USN", ct_uuid_, svc->ssvc_dev->sdev_uuid, "::", "urn:", svc->ssvc_domain, ":service:", svc->ssvc_type, ":1", NULL);
  98. HttpuRespond(s);
  99. }
  100. static void SendServiceNotify(HTTPU_SESSION *s, SSDP_SERVICE *svc)
  101. {
  102. AddNotifyHeaders(s, svc->ssvc_dev->sdev_url_desc, svc->ssvc_dev->sdev_type);
  103. HttpuAddHeader(s, "ST", "urn:", svc->ssvc_domain, ":service:", svc->ssvc_type, ":1", NULL);
  104. HttpuAddHeader(s, "USN", ct_uuid_, svc->ssvc_dev->sdev_uuid, "::", "urn:", svc->ssvc_domain, ":service:", svc->ssvc_type, ":1", NULL);
  105. HttpuSend(s, inet_addr(ct_239_255_255_250), 1900);
  106. }
  107. static void SendUuidResponse(HTTPU_SESSION *s, SSDP_DEVICE *tree, const char *uuid)
  108. {
  109. SSDP_DEVICE *dev;
  110. for (dev = tree; dev; dev = dev->sdev_next) {
  111. if (strcmp(dev->sdev_uuid, uuid) == 0) {
  112. SendDeviceResponse(s, dev, 0);
  113. }
  114. SendUuidResponse(s, dev->sdev_embedded, uuid);
  115. }
  116. }
  117. static void SendDeviceTypeResponse(HTTPU_SESSION *s, SSDP_DEVICE *tree, const char *domain, const char *type)
  118. {
  119. SSDP_DEVICE *dev;
  120. for (dev = tree; dev; dev = dev->sdev_next) {
  121. if (strcmp(dev->sdev_domain, domain) == 0 && strcmp(dev->sdev_type, type) == 0) {
  122. SendDeviceResponse(s, dev, 2);
  123. }
  124. SendDeviceTypeResponse(s, dev->sdev_embedded, domain, type);
  125. }
  126. }
  127. static void SendServiceTypeResponse(HTTPU_SESSION *s, SSDP_DEVICE *tree, const char *domain, const char *st)
  128. {
  129. SSDP_DEVICE *dev;
  130. for (dev = tree; dev; dev = dev->sdev_next) {
  131. SSDP_SERVICE *svc;
  132. for (svc = dev->sdev_svc; svc; svc = svc->ssvc_next) {
  133. if (strcmp(svc->ssvc_domain, domain) == 0 && strcmp(svc->ssvc_type, st) == 0) {
  134. SendServiceResponse(s, svc);
  135. }
  136. }
  137. SendServiceTypeResponse(s, dev->sdev_embedded, domain, st);
  138. }
  139. }
  140. static void SendRootResponse(HTTPU_SESSION *s, SSDP_DEVICE *tree)
  141. {
  142. SSDP_DEVICE *dev;
  143. for (dev = tree; dev; dev = dev->sdev_next) {
  144. SendDeviceResponse(s, dev, 1);
  145. }
  146. }
  147. static void SendAllResponse(HTTPU_SESSION *s, SSDP_DEVICE *tree)
  148. {
  149. SSDP_DEVICE *dev;
  150. SSDP_SERVICE *svc;
  151. for (dev = tree; dev; dev = dev->sdev_next) {
  152. SendDeviceResponse(s, dev, 1);
  153. SendDeviceResponse(s, dev, 0);
  154. SendDeviceResponse(s, dev, 2);
  155. SendAllResponse(s, dev->sdev_embedded);
  156. for (svc = dev->sdev_svc; svc; svc = svc->ssvc_next) {
  157. SendServiceResponse(s, svc);
  158. }
  159. }
  160. }
  161. /*
  162. * This function implements an SSPD responder. It is called by the SSDP
  163. * background thread for each incoming M-SEARCH request.
  164. */
  165. static void SendSearchResponse(HTTPU_SESSION *s)
  166. {
  167. const char *st = HttpuGetHeader(&s->s_rcvhdr, "ST");
  168. if (strcmp(st, "ssdp:all") == 0) {
  169. SendAllResponse(s, regdev_root);
  170. }
  171. else if (strcmp(st, ct_upnp_rootdevice) == 0) {
  172. SendRootResponse(s, regdev_root);
  173. }
  174. else {
  175. char *stcomp[5];
  176. SsdpSplitWords(strdup(st), ':', stcomp, 5);
  177. if (strcmp(stcomp[0], "uuid") == 0) {
  178. SendUuidResponse(s, regdev_root, stcomp[1]);
  179. }
  180. else if (strcmp(stcomp[0], "urn") == 0) {
  181. if (strcmp(stcomp[2], "device") == 0) {
  182. SendDeviceTypeResponse(s, regdev_root, stcomp[1], stcomp[3]);
  183. }
  184. else if (strcmp(stcomp[2], "service") == 0) {
  185. SendServiceTypeResponse(s, regdev_root, stcomp[1], stcomp[3]);
  186. }
  187. }
  188. free(stcomp[0]);
  189. }
  190. }
  191. int SsdpSendNotifications(HTTPU_SESSION *s, char *note)
  192. {
  193. static SSDP_DEVICE *sdev;
  194. static SSDP_SERVICE *ssvc;
  195. int rc = 0;
  196. if (ssvc == NULL) {
  197. if (sdev == NULL) {
  198. sdev = regdev_root;
  199. if (sdev) {
  200. SendDeviceNotify(s, sdev, 0);
  201. SendDeviceNotify(s, sdev, 1);
  202. SendDeviceNotify(s, sdev, 2);
  203. ssvc = sdev->sdev_svc;
  204. }
  205. } else {
  206. sdev = sdev->sdev_next;
  207. if (sdev) {
  208. SendDeviceNotify(s, sdev, 0);
  209. SendDeviceNotify(s, sdev, 1);
  210. SendDeviceNotify(s, sdev, 2);
  211. ssvc = sdev->sdev_svc;
  212. } else {
  213. rc = 900;
  214. }
  215. }
  216. } else {
  217. SendServiceNotify(s, ssvc);
  218. ssvc = ssvc->ssvc_next;
  219. }
  220. return rc;
  221. }
  222. int SsdpRegisterDeviceTree(SSDP_DEVICE *tree)
  223. {
  224. regdev_root = tree;
  225. return SsdpRegisterResponder(SendSearchResponse);
  226. }