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.

120 lines
3.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "ssdp.h"
  2. static pthread_mutex_t _SDDP_mutex = PTHREAD_MUTEX_INITIALIZER;
  3. _net_SSDP::_net_SSDP(){
  4. }
  5. _net_SSDP::~_net_SSDP(){
  6. if( m_nStatus == SSDP_BUSY ){
  7. m_nStatus = SSDP_FAIL;
  8. /* wait till query thread will exit */
  9. pthread_join( m_pthread, NULL );
  10. };
  11. }
  12. _SSDP_STATUS _net_SSDP::getStatus(){
  13. return m_nStatus;
  14. }
  15. /*
  16. http://tools.ietf.org/html/draft-cai-ssdp-v1-03
  17. http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0.pdf (pp18-22)
  18. Query:
  19. M-SEARCH * HTTP/1.1
  20. HOST: 239.255.255.250:1900
  21. MAN: "ssdp:discover"
  22. MX: seconds to delay response
  23. ST: search target (see below)
  24. ssdp:all Search for all devices and services.
  25. upnp:rootdevice Search for root devices only.
  26. uuid:device-UUID Search for a particular device. Device UUID specified by UPnP vendor.
  27. urn:schemas-upnp-org:device:deviceType:v Search for any device of this type. Device type and version defined by UPnP Forum working committee.
  28. urn:schemas-upnp-org:service:serviceType:v Search for any service of this type. Service type and version defined by UPnP Forum working committee.
  29. urn:domain-name:device:deviceType:v Search for any device of this type. Domain name, device type and version defined by UPnP vendor. Period characters in the domain name must be replaced with hyphens in accordance with RFC 2141.
  30. urn:domain-name:service:serviceType:v Search for any service of this type. Domain name, service type and version defined by UPnP vendor. Period characters in the domain name must be replaced with hyphens in accordance with RFC 2141.
  31. Reacts:
  32. NOTIFY * HTTP/1.1
  33. HOST:239.255.255.250:1900
  34. NT:urn:schemas-microsoft-com:nhed:presence:1
  35. NTS:ssdp:byebye
  36. LOCATION:*
  37. USN:uuid:00000000-0000-0000-0200-00125A8A0960::urn:schemas-microsoft-
  38. com:nhed:presence:1
  39. NOTIFY * HTTP/1.1
  40. HOST:239.255.255.250:1900
  41. NT:urn:schemas-microsoft-com:nhed:presence:1
  42. NTS:ssdp:alive
  43. LOCATION:*
  44. CACHE-CONTROL:max-age=4
  45. AL:<urn:schemas-microsoft-com:nhed:attributes?type=X02&firmwarever=
  46. 5766.0&udn=uuid:10000000-0000-0000-0200-00125A8A0960>
  47. USN:uuid:00000000-0000-0000-0200-00125A8A0960::urn:schemas-microsoft-
  48. com:nhed:presence:1
  49. SERVER:dashboard/1.0 UpnP/1.0 xbox/2.0
  50. HTTP/1.1 200 OK
  51. CACHE-CONTROL: max-age = seconds until advertisement expires
  52. DATE: when response was generated
  53. EXT:
  54. LOCATION: URL for UPnP description for root device
  55. SERVER: OS/version UPnP/1.0 product/version
  56. ST: search target
  57. USN: advertisement UUID
  58. */
  59. void _net_SSDP::t_procedure( void *handle ){
  60. struct sockaddr_in laddr; /* our address */
  61. struct sockaddr_in raddr; /* remote address */
  62. socklen_t addrlen = sizeof(raddr); /* length of addresses */
  63. int count; /* # bytes received */
  64. int fd; /* our socket */
  65. int TTL;
  66. unsigned char buf[BUFSIZE]; /* receive buffer */
  67. int fd;
  68. _net_SSDP *me;
  69. pthread_mutex_lock( &_SDDP_mutex );
  70. me = (_net_SSDP *) handle;
  71. if (( fd = socket( AF_INET, SOCK_DGRAM, SOCK_NONBLOCK )) < 0) {
  72. m_nStatus = SSDP_FAIL;
  73. pthread_mutex_unlock( &_SDDP_mutex );
  74. return;
  75. };
  76. TTL = SSDP_QUERY_TTL;
  77. while( m_nStatus == SSDP_BUSY && TTL ){
  78. TTL--;
  79. count = receive( buf );
  80. if( count > 0 ){
  81. m_nStatus = SSDP_READY;
  82. }
  83. usleep(1000);
  84. };
  85. pthread_mutex_unlock( &_SDDP_mutex );
  86. }
  87. bool _net_SSDP::seachServce(const char *URL){
  88. if( m_nStatus != SSDP_BUSY ){
  89. m_nStatus = SSDP_BUSY;
  90. pthread_create( &m_pthread, NULL, t_procedure, this );
  91. } else return false;
  92. return true;
  93. }
  94. bool _net_SSDP::seachServce(char *URL){
  95. if( m_nStatus != SSDP_BUSY ){
  96. m_nStatus = SSDP_BUSY;
  97. pthread_create( &m_pthread, NULL, t_procedure, this );
  98. } else return false;
  99. return true;
  100. }