summaryrefslogtreecommitdiffstats
path: root/src/discover.c
blob: ee365d6de380ac5c39e04e971190310ef0c60f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* AVRemote
   
 (c) 2011 Nederlands Instituut voor Mediakunst (NIMk)
     2011 Denis Roio <jaromil@nimk.nl>

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as
     published by the Free Software Foundation, either version 3 of the
     License, or (at your option) any later version.
     
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Affero General Public License for more details.
     
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>

*/

#include <config.h>

#include <stdio.h>
#include <string.h>

#include <miniwget.h>
#include <miniupnpc.h>
#include <upnpcommands.h>
#include <upnperrors.h>


int upnp_discover()
{
    const char * rootdescurl = 0;
    const char * multicastif = 0;
    const char * minissdpdpath = 0;
    char lanaddr[64];
    struct UPNPDev *devlist = 0;
    struct UPNPDev *dev;
    struct UPNPUrls urls;
    struct IGDdatas data;
    int r, err;

    // damn programmers who change API prototypes in headers
    // without versioning.

/* #ifdef UPNPDISCOVER_SUCCESS */
    devlist = upnpDiscover(1000, multicastif, minissdpdpath, 0, 0, &err);
/* #else */
/*     devlist = upnpDiscover(1000, multicastif, minissdpdpath, 0); */
/* #endif */

    r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
    if (!r) {
      fprintf(stderr,"no valid UPnP devices found\n");

    } else if (r == 3) { // 3 = an UPnP root device has been found (not an IGD)

      dev = devlist;
      while(dev) {

	// parse out ip and port from url
	char ip[256];
	char port[64];
	char tmp[512];
	char *p, *pp;

	memcpy(tmp,dev->descURL,511);
	p = tmp;

	// ip
	do p+=2; while(*p != '/'); p++;
	pp = p; do pp++; while(*pp != ':'); *pp = 0;
	snprintf(ip,255,"%s",p);

	// port
	p = pp+1; pp = p;
	do pp++; while(*pp != '/'); *pp = 0;
	snprintf(port,63,"%s",p);

	fprintf(stderr,"%s\t%s\t%s\t%s\n", dev->st, dev->descURL, ip, port);
	dev = dev->pNext;
      }

      /* fprintf(stderr,
	      " controlURL: %s\n"
	      " ipcondescURL: %s\n"
	      " controlURL_CIF: %s\n",
	      urls.controlURL, urls.ipcondescURL, urls.controlURL_CIF); */
      
      FreeUPNPUrls(&urls);
    }
    freeUPNPDevlist(devlist); devlist = 0;

    return(r);
}