miniupnpc.c 20 KB

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