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.

208 lines
7.8 KiB

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