#include "ssdp.h" static pthread_mutex_t _SDDP_mutex = PTHREAD_MUTEX_INITIALIZER; _net_SSDP::_net_SSDP() {} _net_SSDP::~_net_SSDP() { if (m_nStatus == SSDP_BUSY) { m_nStatus = SSDP_FAIL; /* wait till query thread will exit */ pthread_join(m_pthread, NULL); }; } _SSDP_STATUS _net_SSDP::getStatus() { return m_nStatus; } /* http://tools.ietf.org/html/draft-cai-ssdp-v1-03 http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0.pdf (pp18-22) Query: M-SEARCH * HTTP/1.1 HOST: 239.255.255.250:1900 MAN: "ssdp:discover" MX: seconds to delay response ST: search target (see below) ssdp:all Search for all devices and services. upnp:rootdevice Search for root devices only. uuid:device-UUID Search for a particular device. Device UUID specified by UPnP vendor. urn:schemas-upnp-org:device:deviceType:v Search for any device of this type. Device type and version defined by UPnP Forum working committee. urn:schemas-upnp-org:service:serviceType:v Search for any service of this type. Service type and version defined by UPnP Forum working committee. 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. 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. Reacts: NOTIFY * HTTP/1.1 HOST:239.255.255.250:1900 NT:urn:schemas-microsoft-com:nhed:presence:1 NTS:ssdp:byebye LOCATION:* USN:uuid:00000000-0000-0000-0200-00125A8A0960::urn:schemas-microsoft- com:nhed:presence:1 NOTIFY * HTTP/1.1 HOST:239.255.255.250:1900 NT:urn:schemas-microsoft-com:nhed:presence:1 NTS:ssdp:alive LOCATION:* CACHE-CONTROL:max-age=4 AL: USN:uuid:00000000-0000-0000-0200-00125A8A0960::urn:schemas-microsoft- com:nhed:presence:1 SERVER:dashboard/1.0 UpnP/1.0 xbox/2.0 HTTP/1.1 200 OK CACHE-CONTROL: max-age = seconds until advertisement expires DATE: when response was generated EXT: LOCATION: URL for UPnP description for root device SERVER: OS/version UPnP/1.0 product/version ST: search target USN: advertisement UUID */ void _net_SSDP::t_procedure(void * handle) { struct sockaddr_in laddr; /* our address */ struct sockaddr_in raddr; /* remote address */ socklen_t addrlen = sizeof(raddr); /* length of addresses */ int count; /* # bytes received */ int fd; /* our socket */ int TTL; unsigned char buf[BUFSIZE]; /* receive buffer */ int fd; _net_SSDP * me; pthread_mutex_lock(&_SDDP_mutex); me = (_net_SSDP *) handle; if ((fd = socket(AF_INET, SOCK_DGRAM, SOCK_NONBLOCK)) < 0) { m_nStatus = SSDP_FAIL; pthread_mutex_unlock(&_SDDP_mutex); return; }; TTL = SSDP_QUERY_TTL; while (m_nStatus == SSDP_BUSY && TTL) { TTL--; count = receive(buf); if (count > 0) { m_nStatus = SSDP_READY; } usleep(1000); }; pthread_mutex_unlock(&_SDDP_mutex); } bool _net_SSDP::seachServce(const char * URL) { if (m_nStatus != SSDP_BUSY) { m_nStatus = SSDP_BUSY; pthread_create(&m_pthread, NULL, t_procedure, this); } else { return false; } return true; } bool _net_SSDP::seachServce(char * URL) { if (m_nStatus != SSDP_BUSY) { m_nStatus = SSDP_BUSY; pthread_create(&m_pthread, NULL, t_procedure, this); } else { return false; } return true; }