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.

211 lines
8.3 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __KERNEL_MODULE_SSDP_ANNOUNCER_HEADER__
  2. #define __KERNEL_MODULE_SSDP_ANNOUNCER_HEADER__
  3. #include "kernelModule.h"
  4. #include "../components/base/basicThread.h"
  5. #include "../components/base/basicEventHeap.h"
  6. #include <ctype.h>
  7. #include <err.h>
  8. #include <errno.h>
  9. #include <getopt.h>
  10. #include <ifaddrs.h>
  11. #include <netdb.h>
  12. #include <paths.h>
  13. #include <poll.h>
  14. #include <stdio.h>
  15. #include <signal.h>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/ip.h>
  23. #include <netinet/udp.h>
  24. #include <sys/param.h> /* MIN() */
  25. #include <sys/socket.h>
  26. #include <queue.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/syscall.h>
  30. #define gettid() syscall(SYS_gettid)
  31. #define SSDP_TAG_DEVICE_SERVER_STRING "SERVER_STRING"
  32. #define SSDP_TAG_DEVICE_FRIENDLY_NAME "DEVICE_FRIENDLY_NAME"
  33. #define SSDP_TAG_DEVICE_TYPE "DEVICE_TYPE"
  34. #define SSDP_TAG_DEVICE_BASE_URL "BASE_URL"
  35. #define SSDP_TAG_AUX_WEB_SERVICE "AUX_WEB_SERVICE"
  36. #define SSDP_TAG_DEVICE_MANUFACTURER "MANUFACTURER"
  37. #define SSDP_TAG_DEVICE_MANUFACTURER_URL "MANUFACTURER_URL"
  38. #define SSDP_TAG_DEVICE_MODEL_NAME "MODEL_NAME"
  39. #define SSDP_TAG_DEVICE_MODEL_NUMBER "MODEL_NUMBER"
  40. #define SSDP_TAG_DEVICE_MODEL_DESCRIPTION "MODEL_DESCRIPTION"
  41. #define SSDP_TAG_DEVICE_MODEL_URL "MODEL_URL"
  42. #define SSDP_TAG_DEVICE_SERIAL "DEVICE_SERIAL_NUMBER"
  43. #define SSDP_TAG_DEVICE_UUID "DEVICE_UUID"
  44. #define SSDP_TAG_DEVICE_UPC "DEVICE_UPC"
  45. #define SSDP_TAG_DEVICE_ICONS "ICONS"
  46. #define SSDP_TAG_DEVICE_ICON_IMAGE_FORMAT "FORMAT"
  47. #define SSDP_TAG_DEVICE_ICON_WIDTH "WIDTH"
  48. #define SSDP_TAG_DEVICE_ICON_HEIGHT "HEIGHT"
  49. #define SSDP_TAG_DEVICE_ICON_DEPTH "DEPTH"
  50. #define SSDP_TAG_DEVICE_ICON_URL "URL"
  51. #define SSDP_TAG_DEVICE_SERVICES "SERVICES"
  52. #define SSDP_TAG_DEVICE_SERVICE_TYPE "TYPE"
  53. #define SSDP_TAG_DEVICE_SERVICE_ID "ID"
  54. #define SSDP_TAG_DEVICE_SERVICE_DESC_URL "DESCRIPTION_URL"
  55. #define SSDP_TAG_DEVICE_SERVICE_CTRL_URL "CONTROL_URL"
  56. #define SSDP_TAG_DEVICE_SERVICE_EVTS_URL "EVENTS_URL"
  57. #define SSDP_TAG_DEVICE_PRESENTATION_URL "PRESENTATION_URL"
  58. typedef struct S_SSDP_ICON_DATA{
  59. char format[6];
  60. int width;
  61. int height;
  62. int depth;
  63. char url[128];
  64. }SSDP_ICON_DATA;
  65. typedef struct S_SSDP_SERVICE_DATA{
  66. char type[128];
  67. char id[64];
  68. char desc[128];
  69. char ctrl[128];
  70. char evt[128];
  71. }SSDP_SERVICE_DATA;
  72. typedef struct S_SSDP_ICON_STORAGE{
  73. SSDP_ICON_DATA * array;
  74. int size;
  75. }SSDP_ICON_STORAGE;
  76. typedef struct S_SSDP_SERVICE_STORAGE{
  77. SSDP_SERVICE_DATA * array;
  78. int size;
  79. }SSDP_SERVICE_STORAGE;
  80. /* Notify should be less than half the cache timeout */
  81. #define NOTIFY_INTERVAL 300
  82. #define REFRESH_INTERVAL 600
  83. #define CACHE_TIMEOUT 1800
  84. #define MAX_NUM_IFACES 100
  85. #define MAX_PKT_SIZE 2000
  86. #define SSDP_MAX_SUPPORTED_TYPES 10
  87. #define MC_SSDP_GROUP "239.255.255.250"
  88. #define MC_SSDP_PORT 1900
  89. #define LOCATION_PORT (MC_SSDP_PORT + 1)
  90. #define LOCATION_DESC "/description.xml"
  91. #define PACKAGE_NAME "Argus"
  92. #define PACKAGE_VERSION "1.0.5"
  93. #define MODEL "Server"
  94. #define MANUFACTURER "Toogler(ltd)"
  95. #define SSDP_ST_ALL "ssdp:all"
  96. #define SERVER_STRING "POSIX UPnP/1.0 " PACKAGE_NAME "/" PACKAGE_VERSION
  97. typedef enum E_SOC_HANDLER_TYPE{
  98. SOC_HANDLER_SSDP = 0,
  99. SOC_HANDLER_WEB = 1
  100. }SOC_HANDLER_TYPE;
  101. struct ifsock {
  102. LIST_ENTRY(ifsock) link;
  103. int stale;
  104. int mod;
  105. /*
  106. * Sockets for inbound and outbound
  107. *
  108. * - The inbound is the multicast socket, shared between all ifaces
  109. * - The outbound is bound to the iface address and a random port
  110. */
  111. int in, out;
  112. /* Interface address and netmask */
  113. struct sockaddr_in addr;
  114. struct sockaddr_in mask;
  115. SOC_HANDLER_TYPE type;
  116. };
  117. class kernelModuleSSDPAnnouncer: public kernelModule, public basicThread {
  118. private:
  119. int m_iSocketFID;
  120. LIST_HEAD(, ifsock) m_lInterfaceLinks;
  121. struct sockaddr_in * address;
  122. struct sockaddr_in * netmask;
  123. SSDP_ICON_STORAGE m_sIcons;
  124. SSDP_SERVICE_STORAGE m_sServices;
  125. /***/
  126. char m_sDeviceType[64];
  127. char m_sServerName[128];
  128. char m_sDeviceName[64];
  129. char m_sDeviceSerial[64];
  130. char m_sDevicePresentationURL[128];
  131. char m_sUniversalProductCode[42];
  132. char m_sUUID[42];
  133. char m_sHostName[64];
  134. char m_sManufacturer[64];
  135. char m_sBaseURL[128];
  136. char m_sManufacturerURL[128];
  137. char m_sModelName[64];
  138. char m_sModelNumber[64];
  139. char m_sModelSite[128];
  140. char m_sModelDescription[128];
  141. bool m_bUseWebService;
  142. uint16_t m_nWebPort;
  143. time_t m_sTimeOut;
  144. time_t m_nRefreshTime;
  145. time_t m_nNotifyTime;
  146. char * supported_types[SSDP_MAX_SUPPORTED_TYPES];
  147. char m_pXMLTEMPLATE[4096];
  148. struct ifsock * find_outbound( struct sockaddr *sa );
  149. struct ifsock * find_iface( struct sockaddr *sa );
  150. int multicast_init(void);
  151. int multicast_join( int sd, struct sockaddr *sa );
  152. //int register_socket(int in, int out, struct sockaddr *addr, struct sockaddr *mask, void (*cb)(int sd) );
  153. bool register_socket(int in, int out, struct sockaddr *addr, struct sockaddr *mask, SOC_HANDLER_TYPE type );
  154. void announce( int mod );
  155. int open_socket(char *ifname, struct sockaddr *addr, int port);
  156. int close_socket(void);
  157. int filter_addr(struct sockaddr *sa);
  158. int filter_iface(char *ifname, char *iflist[], size_t num);
  159. void compose_addr(struct sockaddr_in *sin, char *group, int port);
  160. void compose_response(char *type, char *host, char *buf, size_t len);
  161. void compose_search(char *type, char *buf, size_t len);
  162. void compose_notify(char *type, char *host, char *buf, size_t len);
  163. void compose_xml( CSTR peerIP );
  164. void uuidgen(void);
  165. bool web_init( int PortID );
  166. bool ssdp_init(int inSo );
  167. void send_search( struct ifsock *ifs, char *type );
  168. void send_message( struct ifsock *ifs, char *type, struct sockaddr *sa );
  169. void ssdp_recv(int sd);
  170. void web_recv(int sd);
  171. bool stream_peek(int sd, char *ifname, int size );
  172. void respond(int sd, char *ifname);
  173. void handle_message(int sd);
  174. void mark(void);
  175. bool sweep(void);
  176. protected:
  177. virtual void m_fThreadProc( void );
  178. public:
  179. explicit kernelModuleSSDPAnnouncer( uint32_t moduleID );
  180. ~kernelModuleSSDPAnnouncer();
  181. virtual bool reloadSettings();
  182. virtual bool startService();
  183. virtual bool stopService( bool force = false );
  184. virtual void dispatchEvent(EVENT_CONTAINER *Evt);
  185. virtual bool validate();
  186. };
  187. #endif