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.

358 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. Copyright 2006 - 2011 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef MICROSTACK_NO_STDAFX
  14. #include "stdafx.h"
  15. #endif
  16. #include <windows.h>
  17. #include <math.h>
  18. #include <winioctl.h>
  19. #include <winbase.h>
  20. #include <winerror.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <winsock2.h>
  26. #include <ws2tcpip.h>
  27. #include <malloc.h>
  28. #include "ILibSSDPClient.h"
  29. #include "ILibParsers.h"
  30. #define UPNP_PORT 1900
  31. #define UPNP_GROUP "239.255.255.250"
  32. #define DEBUGSTATEMENT(x)
  33. #define strncasecmp(x,y,z) _strnicmp(x,y,z)
  34. struct SSDPClientModule
  35. {
  36. void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);
  37. void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);
  38. void (*Destroy)(void* object);
  39. void (*FunctionCallback)(void *sender, char* UDN, int Alive, char* LocationURL, int Timeout, void *user);
  40. char* DeviceURN;
  41. int DeviceURNLength;
  42. int *IPAddress;
  43. int NumIPAddress;
  44. SOCKET SSDPListenSocket;
  45. SOCKET MSEARCH_Response_Socket;
  46. int Terminate;
  47. void *Reserved;
  48. };
  49. void ILibReadSSDP(SOCKET ReadSocket, struct SSDPClientModule *module)
  50. {
  51. int bytesRead = 0;
  52. char* buffer = (char*)malloc(4096);
  53. struct sockaddr_in addr;
  54. int addrlen = sizeof(struct sockaddr_in);
  55. struct packetheader *packet;
  56. struct packetheader_field_node *node;
  57. struct parser_result* pnode;
  58. char* Location = NULL;
  59. char* UDN = NULL;
  60. int Timeout = 0;
  61. int Alive = 0;
  62. int OK;
  63. int rt;
  64. bytesRead = recvfrom(ReadSocket, buffer, 4096, 0, (struct sockaddr *) &addr, &addrlen);
  65. if(bytesRead<=0)
  66. {
  67. FREE(buffer);
  68. return;
  69. }
  70. packet = ILibParsePacketHeader(buffer,0,bytesRead);
  71. if(packet->Directive==NULL)
  72. {
  73. /* M-SEARCH Response */
  74. if(packet->StatusCode==200)
  75. {
  76. node = packet->FirstField;
  77. while(node!=NULL)
  78. {
  79. if(strncasecmp(node->Field,"LOCATION",8)==0)
  80. {
  81. Location = (char*)MALLOC(node->FieldDataLength+1);
  82. memcpy(Location,node->FieldData,node->FieldDataLength);
  83. Location[node->FieldDataLength] = '\0';
  84. }
  85. if(strncasecmp(node->Field,"CACHE-CONTROL",13)==0)
  86. {
  87. pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, "=", 1);
  88. pnode->LastResult->data[pnode->LastResult->datalength] = '\0';
  89. Timeout = atoi(pnode->LastResult->data);
  90. ILibDestructParserResults(pnode);
  91. }
  92. if(strncasecmp(node->Field,"USN",3)==0)
  93. {
  94. pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, "::", 2);
  95. pnode->FirstResult->data[pnode->FirstResult->datalength] = '\0';
  96. UDN = pnode->FirstResult->data+5;
  97. ILibDestructParserResults(pnode);
  98. }
  99. node = node->NextField;
  100. }
  101. if(module->FunctionCallback!=NULL)
  102. {
  103. module->FunctionCallback(module,UDN,-1,Location,Timeout,module->Reserved);
  104. }
  105. }
  106. }
  107. else
  108. {
  109. /* Notify Packet */
  110. if(strncasecmp(packet->Directive,"NOTIFY",6)==0)
  111. {
  112. OK = 0;
  113. rt = 0;
  114. node = packet->FirstField;
  115. while(node!=NULL)
  116. {
  117. node->Field[node->FieldLength] = '\0';
  118. if(strncasecmp(node->Field,"NT",2)==0 && node->FieldLength==2)
  119. {
  120. node->FieldData[node->FieldDataLength] = '\0';
  121. if(strncasecmp(node->FieldData,module->DeviceURN,module->DeviceURNLength)==0)
  122. {
  123. OK = -1;
  124. }
  125. else if(strncasecmp(node->FieldData,"upnp:rootdevice",15)==0)
  126. {
  127. rt = -1;
  128. }
  129. else
  130. {
  131. break;
  132. }
  133. }
  134. if(strncasecmp(node->Field,"NTS",3)==0)
  135. {
  136. if(strncasecmp(node->FieldData,"ssdp:alive",10)==0)
  137. {
  138. Alive = -1;
  139. rt = 0;
  140. }
  141. else
  142. {
  143. Alive = 0;
  144. OK = 0;
  145. }
  146. }
  147. if(strncasecmp(node->Field,"USN",3)==0)
  148. {
  149. pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, "::", 2);
  150. pnode->FirstResult->data[pnode->FirstResult->datalength] = '\0';
  151. UDN = pnode->FirstResult->data+5;
  152. ILibDestructParserResults(pnode);
  153. }
  154. if(strncasecmp(node->Field,"LOCATION",8)==0)
  155. {
  156. Location = (char*)MALLOC(node->FieldDataLength+1);
  157. memcpy(Location,node->FieldData,node->FieldDataLength);
  158. Location[node->FieldDataLength] = '\0';
  159. }
  160. if(strncasecmp(node->Field,"CACHE-CONTROL",13)==0)
  161. {
  162. pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, "=", 1);
  163. pnode->LastResult->data[pnode->LastResult->datalength] = '\0';
  164. Timeout = atoi(pnode->LastResult->data);
  165. ILibDestructParserResults(pnode);
  166. }
  167. node = node->NextField;
  168. }
  169. if((OK!=0 && Alive!=0) || (rt!=0 && Alive==0))
  170. {
  171. if(module->FunctionCallback!=NULL)
  172. {
  173. module->FunctionCallback(module,UDN,Alive,Location,Timeout,module->Reserved);
  174. }
  175. }
  176. }
  177. }
  178. if(Location!=NULL) {FREE(Location);}
  179. ILibDestructPacket(packet);
  180. FREE(buffer);
  181. }
  182. void ILibSSDPClientModule_PreSelect(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
  183. {
  184. struct SSDPClientModule *module = (struct SSDPClientModule*)object;
  185. FD_SET(module->SSDPListenSocket,readset);
  186. FD_SET(module->MSEARCH_Response_Socket, readset);
  187. }
  188. void ILibSSDPClientModule_PostSelect(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset)
  189. {
  190. struct SSDPClientModule *module = (struct SSDPClientModule*)object;
  191. if(slct>0)
  192. {
  193. if(FD_ISSET(module->SSDPListenSocket,readset)!=0)
  194. {
  195. ILibReadSSDP(module->SSDPListenSocket,module);
  196. }
  197. if(FD_ISSET(module->MSEARCH_Response_Socket,readset)!=0)
  198. {
  199. ILibReadSSDP(module->MSEARCH_Response_Socket,module);
  200. }
  201. }
  202. }
  203. void ILibSSDPClientModule_Destroy(void *object)
  204. {
  205. struct SSDPClientModule *s = (struct SSDPClientModule*)object;
  206. FREE(s->DeviceURN);
  207. if(s->IPAddress!=NULL)
  208. {
  209. FREE(s->IPAddress);
  210. }
  211. }
  212. void ILibSSDP_IPAddressListChanged(void *SSDPToken)
  213. {
  214. struct SSDPClientModule *RetVal = (struct SSDPClientModule*)SSDPToken;
  215. int i;
  216. struct sockaddr_in dest_addr;
  217. struct ip_mreq mreq;
  218. char* buffer;
  219. int bufferlength;
  220. struct in_addr interface_addr;
  221. dest_addr.sin_family = AF_INET;
  222. dest_addr.sin_addr.s_addr = inet_addr(UPNP_GROUP);
  223. dest_addr.sin_port = htons(UPNP_PORT);
  224. for(i=0;i<RetVal->NumIPAddress;++i)
  225. {
  226. mreq.imr_multiaddr.s_addr = inet_addr(UPNP_GROUP);
  227. mreq.imr_interface.s_addr = RetVal->IPAddress[i];
  228. if (setsockopt(RetVal->SSDPListenSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq)) < 0)
  229. {
  230. }
  231. }
  232. buffer = (char*)MALLOC(105+RetVal->DeviceURNLength);
  233. bufferlength = sprintf(buffer,"M-SEARCH * HTTP/1.1\r\nMX: 3\r\nST: %s\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\n\r\n",RetVal->DeviceURN);
  234. FREE(RetVal->IPAddress);
  235. RetVal->NumIPAddress = ILibGetLocalIPAddressList(&(RetVal->IPAddress));
  236. for(i=0;i<RetVal->NumIPAddress;++i)
  237. {
  238. interface_addr.s_addr = RetVal->IPAddress[i];
  239. if (setsockopt(RetVal->MSEARCH_Response_Socket, IPPROTO_IP, IP_MULTICAST_IF,(char*)&interface_addr, sizeof(interface_addr)) == 0)
  240. {
  241. sendto(RetVal->MSEARCH_Response_Socket, buffer, bufferlength, 0, (struct sockaddr *) &dest_addr, sizeof(dest_addr));
  242. }
  243. }
  244. FREE(buffer);
  245. }
  246. void* ILibCreateSSDPClientModule(void *chain, char* DeviceURN, int DeviceURNLength, void (*CallbackPtr)(void *sender, char* UDN, int Alive, char* LocationURL, int Timeout, void *user),void *user)
  247. {
  248. int i;
  249. struct sockaddr_in addr;
  250. struct sockaddr_in dest_addr;
  251. struct SSDPClientModule *RetVal = (struct SSDPClientModule*)MALLOC(sizeof(struct SSDPClientModule));
  252. int ra = 1;
  253. struct ip_mreq mreq;
  254. char* buffer;
  255. int bufferlength;
  256. char* _DeviceURN;
  257. struct in_addr interface_addr;
  258. unsigned char TTL = 4;
  259. memset((char *)&addr, 0, sizeof(addr));
  260. memset((char *)&interface_addr, 0, sizeof(interface_addr));
  261. memset((char *)&(addr), 0, sizeof(dest_addr));
  262. dest_addr.sin_family = AF_INET;
  263. dest_addr.sin_addr.s_addr = inet_addr(UPNP_GROUP);
  264. dest_addr.sin_port = htons(UPNP_PORT);
  265. addr.sin_family = AF_INET;
  266. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  267. addr.sin_port = htons(UPNP_PORT);
  268. RetVal->Destroy = &ILibSSDPClientModule_Destroy;
  269. RetVal->PreSelect = &ILibSSDPClientModule_PreSelect;
  270. RetVal->PostSelect = &ILibSSDPClientModule_PostSelect;
  271. RetVal->Reserved = user;
  272. RetVal->Terminate = 0;
  273. RetVal->FunctionCallback = CallbackPtr;
  274. RetVal->DeviceURN = (char*)MALLOC(DeviceURNLength+1);
  275. memcpy(RetVal->DeviceURN,DeviceURN,DeviceURNLength);
  276. RetVal->DeviceURN[DeviceURNLength] = '\0';
  277. RetVal->DeviceURNLength = DeviceURNLength;
  278. RetVal->NumIPAddress = ILibGetLocalIPAddressList(&(RetVal->IPAddress));
  279. RetVal->SSDPListenSocket = socket(AF_INET, SOCK_DGRAM, 0);
  280. ILibGetDGramSocket(htonl(INADDR_ANY), &(RetVal->MSEARCH_Response_Socket));
  281. if (setsockopt(RetVal->MSEARCH_Response_Socket, IPPROTO_IP, IP_MULTICAST_TTL,(char*)&TTL, sizeof(TTL)) < 0)
  282. {
  283. /* Ignore this case */
  284. }
  285. if (setsockopt(RetVal->SSDPListenSocket, SOL_SOCKET, SO_REUSEADDR,(char*)&ra, sizeof(ra)) < 0)
  286. {
  287. DEBUGSTATEMENT(printf("Setting SockOpt SO_REUSEADDR failed\r\n"));
  288. exit(1);
  289. }
  290. if (bind(RetVal->SSDPListenSocket, (struct sockaddr *) &(addr), sizeof(addr)) < 0)
  291. {
  292. printf("SSDPListenSocket bind");
  293. exit(1);
  294. }
  295. for(i=0;i<RetVal->NumIPAddress;++i)
  296. {
  297. mreq.imr_multiaddr.s_addr = inet_addr(UPNP_GROUP);
  298. mreq.imr_interface.s_addr = RetVal->IPAddress[i];
  299. if (setsockopt(RetVal->SSDPListenSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq)) < 0)
  300. {
  301. printf("SSDPListenSocket setsockopt mreq");
  302. exit(1);
  303. }
  304. }
  305. ILibAddToChain(chain,RetVal);
  306. _DeviceURN = (char*)MALLOC(DeviceURNLength+1);
  307. memcpy(_DeviceURN,DeviceURN,DeviceURNLength);
  308. _DeviceURN[DeviceURNLength] = '\0';
  309. buffer = (char*)MALLOC(105+DeviceURNLength);
  310. bufferlength = sprintf(buffer,"M-SEARCH * HTTP/1.1\r\nMX: 3\r\nST: %s\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\n\r\n",_DeviceURN);
  311. for(i=0;i<RetVal->NumIPAddress;++i)
  312. {
  313. interface_addr.s_addr = RetVal->IPAddress[i];
  314. if (setsockopt(RetVal->MSEARCH_Response_Socket, IPPROTO_IP, IP_MULTICAST_IF,(char*)&interface_addr, sizeof(interface_addr)) == 0)
  315. {
  316. sendto(RetVal->MSEARCH_Response_Socket, buffer, bufferlength, 0, (struct sockaddr *) &dest_addr, sizeof(dest_addr));
  317. }
  318. }
  319. FREE(_DeviceURN);
  320. FREE(buffer);
  321. return(RetVal);
  322. }