miniupnpc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #define _CRT_SECURE_NO_WARNINGS
  2. /* $Id: miniupnpc.c,v 1.149 2016/02/09 09:50:46 nanard Exp $ */
  3. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  4. * Project : miniupnp
  5. * Web : http://miniupnp.free.fr/
  6. * Author : Thomas BERNARD
  7. * copyright (c) 2005-2016 Thomas Bernard
  8. * This software is subjet to the conditions detailed in the
  9. * provided LICENSE file. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #ifdef _WIN32
  14. /* Win32 Specific includes and defines */
  15. #include <winsock2.h>
  16. #include <ws2tcpip.h>
  17. #include <io.h>
  18. #include <iphlpapi.h>
  19. #define snprintf _snprintf
  20. #define strdup _strdup
  21. #ifndef strncasecmp
  22. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  23. #define strncasecmp _memicmp
  24. #else /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
  25. #define strncasecmp memicmp
  26. #endif /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
  27. #endif /* #ifndef strncasecmp */
  28. #define MAXHOSTNAMELEN 64
  29. #else /* #ifdef _WIN32 */
  30. /* Standard POSIX includes */
  31. #include <unistd.h>
  32. #if defined(__amigaos__) && !defined(__amigaos4__)
  33. /* Amiga OS 3 specific stuff */
  34. #define socklen_t int
  35. #else
  36. #include <sys/select.h>
  37. #endif
  38. #include <sys/socket.h>
  39. #include <sys/types.h>
  40. #include <sys/param.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <netdb.h>
  44. #include <net/if.h>
  45. #if !defined(__amigaos__) && !defined(__amigaos4__)
  46. #include <poll.h>
  47. #endif
  48. #include <strings.h>
  49. #include <errno.h>
  50. #define closesocket close
  51. #endif /* #else _WIN32 */
  52. #ifdef __GNU__
  53. #define MAXHOSTNAMELEN 64
  54. #endif
  55. #include "miniupnpc.h"
  56. #include "minissdpc.h"
  57. #include "miniwget.h"
  58. #include "minisoap.h"
  59. #include "minixml.h"
  60. #include "upnpcommands.h"
  61. #include "connecthostport.h"
  62. /* compare the begining of a string with a constant string */
  63. #define COMPARE(str, cstr) (0==memcmp(str, cstr, sizeof(cstr) - 1))
  64. #ifndef MAXHOSTNAMELEN
  65. #define MAXHOSTNAMELEN 64
  66. #endif
  67. #define SOAPPREFIX "s"
  68. #define SERVICEPREFIX "u"
  69. #define SERVICEPREFIX2 'u'
  70. /* check if an ip address is a private (LAN) address
  71. * see https://tools.ietf.org/html/rfc1918 */
  72. static int is_rfc1918addr(const char * addr)
  73. {
  74. /* 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) */
  75. if(COMPARE(addr, "192.168."))
  76. return 1;
  77. /* 10.0.0.0 - 10.255.255.255 (10/8 prefix) */
  78. if(COMPARE(addr, "10."))
  79. return 1;
  80. /* 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) */
  81. if(COMPARE(addr, "172.")) {
  82. int i = atoi(addr + 4);
  83. if((16 <= i) && (i <= 31))
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. /* root description parsing */
  89. MINIUPNP_LIBSPEC void parserootdesc(const char * buffer, int bufsize, struct IGDdatas * data)
  90. {
  91. struct xmlparser parser;
  92. /* xmlparser object */
  93. parser.xmlstart = buffer;
  94. parser.xmlsize = bufsize;
  95. parser.data = data;
  96. parser.starteltfunc = IGDstartelt;
  97. parser.endeltfunc = IGDendelt;
  98. parser.datafunc = IGDdata;
  99. parser.attfunc = 0;
  100. parsexml(&parser);
  101. #ifdef DEBUG
  102. printIGD(data);
  103. #endif
  104. }
  105. /* simpleUPnPcommand2 :
  106. * not so simple !
  107. * return values :
  108. * pointer - OK
  109. * NULL - error */
  110. char * simpleUPnPcommand2(int s, const char * url, const char * service,
  111. const char * action, struct UPNParg * args,
  112. int * bufsize, const char * httpversion)
  113. {
  114. char hostname[MAXHOSTNAMELEN+1];
  115. unsigned short port = 0;
  116. char * path;
  117. char soapact[128];
  118. char soapbody[2048];
  119. int soapbodylen;
  120. char * buf;
  121. int n;
  122. int status_code;
  123. *bufsize = 0;
  124. snprintf(soapact, sizeof(soapact), "%s#%s", service, action);
  125. if(args==NULL)
  126. {
  127. soapbodylen = snprintf(soapbody, sizeof(soapbody),
  128. "<?xml version=\"1.0\"?>\r\n"
  129. "<" SOAPPREFIX ":Envelope "
  130. "xmlns:" SOAPPREFIX "=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  131. SOAPPREFIX ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  132. "<" SOAPPREFIX ":Body>"
  133. "<" SERVICEPREFIX ":%s xmlns:" SERVICEPREFIX "=\"%s\">"
  134. "</" SERVICEPREFIX ":%s>"
  135. "</" SOAPPREFIX ":Body></" SOAPPREFIX ":Envelope>"
  136. "\r\n", action, service, action);
  137. if ((unsigned int)soapbodylen >= sizeof(soapbody))
  138. return NULL;
  139. }
  140. else
  141. {
  142. char * p;
  143. const char * pe, * pv;
  144. const char * const pend = soapbody + sizeof(soapbody);
  145. soapbodylen = snprintf(soapbody, sizeof(soapbody),
  146. "<?xml version=\"1.0\"?>\r\n"
  147. "<" SOAPPREFIX ":Envelope "
  148. "xmlns:" SOAPPREFIX "=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  149. SOAPPREFIX ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  150. "<" SOAPPREFIX ":Body>"
  151. "<" SERVICEPREFIX ":%s xmlns:" SERVICEPREFIX "=\"%s\">",
  152. action, service);
  153. if ((unsigned int)soapbodylen >= sizeof(soapbody))
  154. return NULL;
  155. p = soapbody + soapbodylen;
  156. while(args->elt)
  157. {
  158. if(p >= pend) /* check for space to write next byte */
  159. return NULL;
  160. *(p++) = '<';
  161. pe = args->elt;
  162. while(p < pend && *pe)
  163. *(p++) = *(pe++);
  164. if(p >= pend) /* check for space to write next byte */
  165. return NULL;
  166. *(p++) = '>';
  167. if((pv = args->val))
  168. {
  169. while(p < pend && *pv)
  170. *(p++) = *(pv++);
  171. }
  172. if((p+2) > pend) /* check for space to write next 2 bytes */
  173. return NULL;
  174. *(p++) = '<';
  175. *(p++) = '/';
  176. pe = args->elt;
  177. while(p < pend && *pe)
  178. *(p++) = *(pe++);
  179. if(p >= pend) /* check for space to write next byte */
  180. return NULL;
  181. *(p++) = '>';
  182. args++;
  183. }
  184. if((p+4) > pend) /* check for space to write next 4 bytes */
  185. return NULL;
  186. *(p++) = '<';
  187. *(p++) = '/';
  188. *(p++) = SERVICEPREFIX2;
  189. *(p++) = ':';
  190. pe = action;
  191. while(p < pend && *pe)
  192. *(p++) = *(pe++);
  193. strncpy(p, "></" SOAPPREFIX ":Body></" SOAPPREFIX ":Envelope>\r\n",
  194. pend - p);
  195. if(soapbody[sizeof(soapbody)-1]) /* strncpy pads buffer with 0s, so if it doesn't end in 0, could not fit full string */
  196. return NULL;
  197. }
  198. if(!parseURL(url, hostname, &port, &path, NULL)) return NULL;
  199. if(s < 0) {
  200. s = connecthostport(hostname, port, 0);
  201. if(s < 0) {
  202. /* failed to connect */
  203. return NULL;
  204. }
  205. }
  206. n = soapPostSubmit(s, path, hostname, port, soapact, soapbody, httpversion);
  207. if(n<=0) {
  208. #ifdef DEBUG
  209. printf("Error sending SOAP request\n");
  210. #endif
  211. closesocket(s);
  212. return NULL;
  213. }
  214. buf = getHTTPResponse(s, bufsize, &status_code);
  215. #ifdef DEBUG
  216. if(*bufsize > 0 && buf)
  217. {
  218. printf("HTTP %d SOAP Response :\n%.*s\n", status_code, *bufsize, buf);
  219. }
  220. else
  221. {
  222. printf("HTTP %d, empty SOAP response. size=%d\n", status_code, *bufsize);
  223. }
  224. #endif
  225. closesocket(s);
  226. return buf;
  227. }
  228. /* simpleUPnPcommand :
  229. * not so simple !
  230. * return values :
  231. * pointer - OK
  232. * NULL - error */
  233. char * simpleUPnPcommand(int s, const char * url, const char * service,
  234. const char * action, struct UPNParg * args,
  235. int * bufsize)
  236. {
  237. char * buf;
  238. #if 1
  239. buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.1");
  240. #else
  241. buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.0");
  242. if (!buf || *bufsize == 0)
  243. {
  244. #if DEBUG
  245. printf("Error or no result from SOAP request; retrying with HTTP/1.1\n");
  246. #endif
  247. buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.1");
  248. }
  249. #endif
  250. return buf;
  251. }
  252. /* upnpDiscoverDevices() :
  253. * return a chained list of all devices found or NULL if
  254. * no devices was found.
  255. * It is up to the caller to free the chained list
  256. * delay is in millisecond (poll).
  257. * UDA v1.1 says :
  258. * The TTL for the IP packet SHOULD default to 2 and
  259. * SHOULD be configurable. */
  260. MINIUPNP_LIBSPEC struct UPNPDev *
  261. upnpDiscoverDevices(const char * const deviceTypes[],
  262. int delay, const char * multicastif,
  263. const char * minissdpdsock, int localport,
  264. int ipv6, unsigned char ttl,
  265. int * error,
  266. int searchalltypes)
  267. {
  268. struct UPNPDev * tmp;
  269. struct UPNPDev * devlist = 0;
  270. #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
  271. int deviceIndex;
  272. #endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  273. if(error)
  274. *error = UPNPDISCOVER_UNKNOWN_ERROR;
  275. #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
  276. /* first try to get infos from minissdpd ! */
  277. if(!minissdpdsock)
  278. minissdpdsock = "/var/run/minissdpd.sock";
  279. for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) {
  280. struct UPNPDev * minissdpd_devlist;
  281. int only_rootdevice = 1;
  282. minissdpd_devlist = getDevicesFromMiniSSDPD(deviceTypes[deviceIndex],
  283. minissdpdsock, 0);
  284. if(minissdpd_devlist) {
  285. #ifdef DEBUG
  286. printf("returned by MiniSSDPD: %s\t%s\n",
  287. minissdpd_devlist->st, minissdpd_devlist->descURL);
  288. #endif /* DEBUG */
  289. if(!strstr(minissdpd_devlist->st, "rootdevice"))
  290. only_rootdevice = 0;
  291. for(tmp = minissdpd_devlist; tmp->pNext != NULL; tmp = tmp->pNext) {
  292. #ifdef DEBUG
  293. printf("returned by MiniSSDPD: %s\t%s\n",
  294. tmp->pNext->st, tmp->pNext->descURL);
  295. #endif /* DEBUG */
  296. if(!strstr(tmp->st, "rootdevice"))
  297. only_rootdevice = 0;
  298. }
  299. tmp->pNext = devlist;
  300. devlist = minissdpd_devlist;
  301. if(!searchalltypes && !only_rootdevice)
  302. break;
  303. }
  304. }
  305. for(tmp = devlist; tmp != NULL; tmp = tmp->pNext) {
  306. /* We return what we have found if it was not only a rootdevice */
  307. if(!strstr(tmp->st, "rootdevice")) {
  308. if(error)
  309. *error = UPNPDISCOVER_SUCCESS;
  310. return devlist;
  311. }
  312. }
  313. #endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
  314. /* direct discovery if minissdpd responses are not sufficient */
  315. {
  316. struct UPNPDev * discovered_devlist;
  317. discovered_devlist = ssdpDiscoverDevices(deviceTypes, delay, multicastif, localport,
  318. ipv6, ttl, error, searchalltypes);
  319. if(devlist == NULL)
  320. devlist = discovered_devlist;
  321. else {
  322. for(tmp = devlist; tmp->pNext != NULL; tmp = tmp->pNext);
  323. tmp->pNext = discovered_devlist;
  324. }
  325. }
  326. return devlist;
  327. }
  328. /* upnpDiscover() Discover IGD device */
  329. MINIUPNP_LIBSPEC struct UPNPDev *
  330. upnpDiscover(int delay, const char * multicastif,
  331. const char * minissdpdsock, int localport,
  332. int ipv6, unsigned char ttl,
  333. int * error)
  334. {
  335. static const char * const deviceList[] = {
  336. #if 0
  337. "urn:schemas-upnp-org:device:InternetGatewayDevice:2",
  338. "urn:schemas-upnp-org:service:WANIPConnection:2",
  339. #endif
  340. "urn:schemas-upnp-org:device:InternetGatewayDevice:1",
  341. "urn:schemas-upnp-org:service:WANIPConnection:1",
  342. "urn:schemas-upnp-org:service:WANPPPConnection:1",
  343. "upnp:rootdevice",
  344. /*"ssdp:all",*/
  345. 0
  346. };
  347. return upnpDiscoverDevices(deviceList,
  348. delay, multicastif, minissdpdsock, localport,
  349. ipv6, ttl, error, 0);
  350. }
  351. /* upnpDiscoverAll() Discover all UPnP devices */
  352. MINIUPNP_LIBSPEC struct UPNPDev *
  353. upnpDiscoverAll(int delay, const char * multicastif,
  354. const char * minissdpdsock, int localport,
  355. int ipv6, unsigned char ttl,
  356. int * error)
  357. {
  358. static const char * const deviceList[] = {
  359. /*"upnp:rootdevice",*/
  360. "ssdp:all",
  361. 0
  362. };
  363. return upnpDiscoverDevices(deviceList,
  364. delay, multicastif, minissdpdsock, localport,
  365. ipv6, ttl, error, 0);
  366. }
  367. /* upnpDiscoverDevice() Discover a specific device */
  368. MINIUPNP_LIBSPEC struct UPNPDev *
  369. upnpDiscoverDevice(const char * device, int delay, const char * multicastif,
  370. const char * minissdpdsock, int localport,
  371. int ipv6, unsigned char ttl,
  372. int * error)
  373. {
  374. const char * const deviceList[] = {
  375. device,
  376. 0
  377. };
  378. return upnpDiscoverDevices(deviceList,
  379. delay, multicastif, minissdpdsock, localport,
  380. ipv6, ttl, error, 0);
  381. }
  382. static char *
  383. build_absolute_url(const char * baseurl, const char * descURL,
  384. const char * url, unsigned int scope_id)
  385. {
  386. int l, n;
  387. char * s;
  388. const char * base;
  389. char * p;
  390. #if defined(IF_NAMESIZE) && !defined(_WIN32)
  391. char ifname[IF_NAMESIZE];
  392. #else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  393. char scope_str[8];
  394. #endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  395. if( (url[0] == 'h')
  396. &&(url[1] == 't')
  397. &&(url[2] == 't')
  398. &&(url[3] == 'p')
  399. &&(url[4] == ':')
  400. &&(url[5] == '/')
  401. &&(url[6] == '/'))
  402. return strdup(url);
  403. base = (baseurl[0] == '\0') ? descURL : baseurl;
  404. n = strlen(base);
  405. if(n > 7) {
  406. p = strchr(base + 7, '/');
  407. if(p)
  408. n = p - base;
  409. }
  410. l = n + strlen(url) + 1;
  411. if(url[0] != '/')
  412. l++;
  413. if(scope_id != 0) {
  414. #if defined(IF_NAMESIZE) && !defined(_WIN32)
  415. if(if_indextoname(scope_id, ifname)) {
  416. l += 3 + strlen(ifname); /* 3 == strlen(%25) */
  417. }
  418. #else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  419. /* under windows, scope is numerical */
  420. l += 3 + snprintf(scope_str, sizeof(scope_str), "%u", scope_id);
  421. #endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  422. }
  423. s = malloc(l);
  424. if(s == NULL) return NULL;
  425. memcpy(s, base, n);
  426. if(scope_id != 0) {
  427. s[n] = '\0';
  428. if(0 == memcmp(s, "http://[fe80:", 13)) {
  429. /* this is a linklocal IPv6 address */
  430. p = strchr(s, ']');
  431. if(p) {
  432. /* insert %25<scope> into URL */
  433. #if defined(IF_NAMESIZE) && !defined(_WIN32)
  434. memmove(p + 3 + strlen(ifname), p, strlen(p) + 1);
  435. memcpy(p, "%25", 3);
  436. memcpy(p + 3, ifname, strlen(ifname));
  437. n += 3 + strlen(ifname);
  438. #else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  439. memmove(p + 3 + strlen(scope_str), p, strlen(p) + 1);
  440. memcpy(p, "%25", 3);
  441. memcpy(p + 3, scope_str, strlen(scope_str));
  442. n += 3 + strlen(scope_str);
  443. #endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
  444. }
  445. }
  446. }
  447. if(url[0] != '/')
  448. s[n++] = '/';
  449. memcpy(s + n, url, l - n);
  450. return s;
  451. }
  452. /* Prepare the Urls for usage...
  453. */
  454. MINIUPNP_LIBSPEC void
  455. GetUPNPUrls(struct UPNPUrls * urls, struct IGDdatas * data,
  456. const char * descURL, unsigned int scope_id)
  457. {
  458. /* strdup descURL */
  459. urls->rootdescURL = strdup(descURL);
  460. /* get description of WANIPConnection */
  461. urls->ipcondescURL = build_absolute_url(data->urlbase, descURL,
  462. data->first.scpdurl, scope_id);
  463. urls->controlURL = build_absolute_url(data->urlbase, descURL,
  464. data->first.controlurl, scope_id);
  465. urls->controlURL_CIF = build_absolute_url(data->urlbase, descURL,
  466. data->CIF.controlurl, scope_id);
  467. urls->controlURL_6FC = build_absolute_url(data->urlbase, descURL,
  468. data->IPv6FC.controlurl, scope_id);
  469. #ifdef DEBUG
  470. printf("urls->ipcondescURL='%s'\n", urls->ipcondescURL);
  471. printf("urls->controlURL='%s'\n", urls->controlURL);
  472. printf("urls->controlURL_CIF='%s'\n", urls->controlURL_CIF);
  473. printf("urls->controlURL_6FC='%s'\n", urls->controlURL_6FC);
  474. #endif
  475. }
  476. MINIUPNP_LIBSPEC void
  477. FreeUPNPUrls(struct UPNPUrls * urls)
  478. {
  479. if(!urls)
  480. return;
  481. free(urls->controlURL);
  482. urls->controlURL = 0;
  483. free(urls->ipcondescURL);
  484. urls->ipcondescURL = 0;
  485. free(urls->controlURL_CIF);
  486. urls->controlURL_CIF = 0;
  487. free(urls->controlURL_6FC);
  488. urls->controlURL_6FC = 0;
  489. free(urls->rootdescURL);
  490. urls->rootdescURL = 0;
  491. }
  492. int
  493. UPNPIGD_IsConnected(struct UPNPUrls * urls, struct IGDdatas * data)
  494. {
  495. char status[64];
  496. unsigned int uptime;
  497. status[0] = '\0';
  498. UPNP_GetStatusInfo(urls->controlURL, data->first.servicetype,
  499. status, &uptime, NULL);
  500. if(0 == strcmp("Connected", status))
  501. return 1;
  502. else if(0 == strcmp("Up", status)) /* Also accept "Up" */
  503. return 1;
  504. else
  505. return 0;
  506. }
  507. /* UPNP_GetValidIGD() :
  508. * return values :
  509. * -1 = Internal error
  510. * 0 = NO IGD found
  511. * 1 = A valid connected IGD has been found
  512. * 2 = A valid IGD has been found but it reported as
  513. * not connected
  514. * 3 = an UPnP device has been found but was not recognized as an IGD
  515. *
  516. * In any positive non zero return case, the urls and data structures
  517. * passed as parameters are set. Dont forget to call FreeUPNPUrls(urls) to
  518. * free allocated memory.
  519. */
  520. MINIUPNP_LIBSPEC int
  521. UPNP_GetValidIGD(struct UPNPDev * devlist,
  522. struct UPNPUrls * urls,
  523. struct IGDdatas * data,
  524. char * lanaddr, int lanaddrlen)
  525. {
  526. struct xml_desc {
  527. char * xml;
  528. int size;
  529. int is_igd;
  530. } * desc = NULL;
  531. struct UPNPDev * dev;
  532. int ndev = 0;
  533. int i;
  534. int state = -1; /* state 1 : IGD connected. State 2 : IGD. State 3 : anything */
  535. int n_igd = 0;
  536. char extIpAddr[16];
  537. char myLanAddr[40];
  538. int status_code = -1;
  539. if(!devlist)
  540. {
  541. #ifdef DEBUG
  542. printf("Empty devlist\n");
  543. #endif
  544. return 0;
  545. }
  546. /* counting total number of devices in the list */
  547. for(dev = devlist; dev; dev = dev->pNext)
  548. ndev++;
  549. if(ndev > 0)
  550. {
  551. desc = calloc(ndev, sizeof(struct xml_desc));
  552. if(!desc)
  553. return -1; /* memory allocation error */
  554. }
  555. /* Step 1 : downloading descriptions and testing type */
  556. for(dev = devlist, i = 0; dev; dev = dev->pNext, i++)
  557. {
  558. /* we should choose an internet gateway device.
  559. * with st == urn:schemas-upnp-org:device:InternetGatewayDevice:1 */
  560. desc[i].xml = miniwget_getaddr(dev->descURL, &(desc[i].size),
  561. myLanAddr, sizeof(myLanAddr),
  562. dev->scope_id, &status_code);
  563. #ifdef DEBUG
  564. if(!desc[i].xml)
  565. {
  566. printf("error getting XML description %s\n", dev->descURL);
  567. }
  568. #endif
  569. if(desc[i].xml)
  570. {
  571. memset(data, 0, sizeof(struct IGDdatas));
  572. memset(urls, 0, sizeof(struct UPNPUrls));
  573. parserootdesc(desc[i].xml, desc[i].size, data);
  574. if(COMPARE(data->CIF.servicetype,
  575. "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:"))
  576. {
  577. desc[i].is_igd = 1;
  578. n_igd++;
  579. if(lanaddr)
  580. strncpy(lanaddr, myLanAddr, lanaddrlen);
  581. }
  582. }
  583. }
  584. /* iterate the list to find a device depending on state */
  585. for(state = 1; state <= 3; state++)
  586. {
  587. for(dev = devlist, i = 0; dev; dev = dev->pNext, i++)
  588. {
  589. if(desc[i].xml)
  590. {
  591. memset(data, 0, sizeof(struct IGDdatas));
  592. memset(urls, 0, sizeof(struct UPNPUrls));
  593. parserootdesc(desc[i].xml, desc[i].size, data);
  594. if(desc[i].is_igd || state >= 3 )
  595. {
  596. int is_connected;
  597. GetUPNPUrls(urls, data, dev->descURL, dev->scope_id);
  598. /* in state 2 and 3 we dont test if device is connected ! */
  599. if(state >= 2)
  600. goto free_and_return;
  601. is_connected = UPNPIGD_IsConnected(urls, data);
  602. #ifdef DEBUG
  603. printf("UPNPIGD_IsConnected(%s) = %d\n",
  604. urls->controlURL, is_connected);
  605. #endif
  606. /* checks that status is connected AND there is a external IP address assigned */
  607. if(is_connected &&
  608. (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0)) {
  609. if(!is_rfc1918addr(extIpAddr) && (extIpAddr[0] != '\0')
  610. && (0 != strcmp(extIpAddr, "0.0.0.0")))
  611. goto free_and_return;
  612. }
  613. FreeUPNPUrls(urls);
  614. if(data->second.servicetype[0] != '\0') {
  615. #ifdef DEBUG
  616. printf("We tried %s, now we try %s !\n",
  617. data->first.servicetype, data->second.servicetype);
  618. #endif
  619. /* swaping WANPPPConnection and WANIPConnection ! */
  620. memcpy(&data->tmp, &data->first, sizeof(struct IGDdatas_service));
  621. memcpy(&data->first, &data->second, sizeof(struct IGDdatas_service));
  622. memcpy(&data->second, &data->tmp, sizeof(struct IGDdatas_service));
  623. GetUPNPUrls(urls, data, dev->descURL, dev->scope_id);
  624. is_connected = UPNPIGD_IsConnected(urls, data);
  625. #ifdef DEBUG
  626. printf("UPNPIGD_IsConnected(%s) = %d\n",
  627. urls->controlURL, is_connected);
  628. #endif
  629. if(is_connected &&
  630. (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0)) {
  631. if(!is_rfc1918addr(extIpAddr) && (extIpAddr[0] != '\0')
  632. && (0 != strcmp(extIpAddr, "0.0.0.0")))
  633. goto free_and_return;
  634. }
  635. FreeUPNPUrls(urls);
  636. }
  637. }
  638. memset(data, 0, sizeof(struct IGDdatas));
  639. }
  640. }
  641. }
  642. state = 0;
  643. free_and_return:
  644. if(desc) {
  645. for(i = 0; i < ndev; i++) {
  646. if(desc[i].xml) {
  647. free(desc[i].xml);
  648. }
  649. }
  650. free(desc);
  651. }
  652. return state;
  653. }
  654. /* UPNP_GetIGDFromUrl()
  655. * Used when skipping the discovery process.
  656. * return value :
  657. * 0 - Not ok
  658. * 1 - OK */
  659. int
  660. UPNP_GetIGDFromUrl(const char * rootdescurl,
  661. struct UPNPUrls * urls,
  662. struct IGDdatas * data,
  663. char * lanaddr, int lanaddrlen)
  664. {
  665. char * descXML;
  666. int descXMLsize = 0;
  667. descXML = miniwget_getaddr(rootdescurl, &descXMLsize,
  668. lanaddr, lanaddrlen, 0, NULL);
  669. if(descXML) {
  670. memset(data, 0, sizeof(struct IGDdatas));
  671. memset(urls, 0, sizeof(struct UPNPUrls));
  672. parserootdesc(descXML, descXMLsize, data);
  673. free(descXML);
  674. descXML = NULL;
  675. GetUPNPUrls(urls, data, rootdescurl, 0);
  676. return 1;
  677. } else {
  678. return 0;
  679. }
  680. }