1/* $Id: igd_desc_parse.c,v 1.16 2014/11/17 17:19:13 nanard Exp $ */
2/* Project : miniupnp
3 * http://miniupnp.free.fr/
4 * Author : Thomas Bernard
5 * Copyright (c) 2005-2014 Thomas Bernard
6 * This software is subject to the conditions detailed in the
7 * LICENCE file provided in this distribution. */
8
9#include "igd_desc_parse.h"
10#include <stdio.h>
11#include <string.h>
12
13/* Start element handler :
14 * update nesting level counter and copy element name */
15void IGDstartelt(void * d, const char * name, int l)
16{
17	struct IGDdatas * datas = (struct IGDdatas *)d;
18	memcpy( datas->cureltname, name, l);
19	datas->cureltname[l] = '\0';
20	datas->level++;
21	if( (l==7) && !memcmp(name, "service", l) ) {
22		datas->tmp.controlurl[0] = '\0';
23		datas->tmp.eventsuburl[0] = '\0';
24		datas->tmp.scpdurl[0] = '\0';
25		datas->tmp.servicetype[0] = '\0';
26	}
27}
28
29#define COMPARE(str, cstr) (0==memcmp(str, cstr, sizeof(cstr) - 1))
30
31/* End element handler :
32 * update nesting level counter and update parser state if
33 * service element is parsed */
34void IGDendelt(void * d, const char * name, int l)
35{
36	struct IGDdatas * datas = (struct IGDdatas *)d;
37	datas->level--;
38	/*printf("endelt %2d %.*s\n", datas->level, l, name);*/
39	if( (l==7) && !memcmp(name, "service", l) )
40	{
41		if(COMPARE(datas->tmp.servicetype,
42		           "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:")) {
43			memcpy(&datas->CIF, &datas->tmp, sizeof(struct IGDdatas_service));
44		} else if(COMPARE(datas->tmp.servicetype,
45			                "urn:schemas-upnp-org:service:WANIPv6FirewallControl:")) {
46			memcpy(&datas->IPv6FC, &datas->tmp, sizeof(struct IGDdatas_service));
47		} else if(COMPARE(datas->tmp.servicetype,
48		                  "urn:schemas-upnp-org:service:WANIPConnection:")
49		         || COMPARE(datas->tmp.servicetype,
50		                    "urn:schemas-upnp-org:service:WANPPPConnection:") ) {
51			if(datas->first.servicetype[0] == '\0') {
52				memcpy(&datas->first, &datas->tmp, sizeof(struct IGDdatas_service));
53			} else {
54				memcpy(&datas->second, &datas->tmp, sizeof(struct IGDdatas_service));
55			}
56		}
57	}
58}
59
60/* Data handler :
61 * copy data depending on the current element name and state */
62void IGDdata(void * d, const char * data, int l)
63{
64	struct IGDdatas * datas = (struct IGDdatas *)d;
65	char * dstmember = 0;
66	/*printf("%2d %s : %.*s\n",
67           datas->level, datas->cureltname, l, data);	*/
68	if( !strcmp(datas->cureltname, "URLBase") )
69		dstmember = datas->urlbase;
70	else if( !strcmp(datas->cureltname, "presentationURL") )
71		dstmember = datas->presentationurl;
72	else if( !strcmp(datas->cureltname, "serviceType") )
73		dstmember = datas->tmp.servicetype;
74	else if( !strcmp(datas->cureltname, "controlURL") )
75		dstmember = datas->tmp.controlurl;
76	else if( !strcmp(datas->cureltname, "eventSubURL") )
77		dstmember = datas->tmp.eventsuburl;
78	else if( !strcmp(datas->cureltname, "SCPDURL") )
79		dstmember = datas->tmp.scpdurl;
80/*	else if( !strcmp(datas->cureltname, "deviceType") )
81		dstmember = datas->devicetype_tmp;*/
82	if(dstmember)
83	{
84		if(l>=MINIUPNPC_URL_MAXSIZE)
85			l = MINIUPNPC_URL_MAXSIZE-1;
86		memcpy(dstmember, data, l);
87		dstmember[l] = '\0';
88	}
89}
90
91#ifdef DEBUG
92void printIGD(struct IGDdatas * d)
93{
94	printf("urlbase = '%s'\n", d->urlbase);
95	printf("WAN Device (Common interface config) :\n");
96	/*printf(" deviceType = '%s'\n", d->CIF.devicetype);*/
97	printf(" serviceType = '%s'\n", d->CIF.servicetype);
98	printf(" controlURL = '%s'\n", d->CIF.controlurl);
99	printf(" eventSubURL = '%s'\n", d->CIF.eventsuburl);
100	printf(" SCPDURL = '%s'\n", d->CIF.scpdurl);
101	printf("primary WAN Connection Device (IP or PPP Connection):\n");
102	/*printf(" deviceType = '%s'\n", d->first.devicetype);*/
103	printf(" servicetype = '%s'\n", d->first.servicetype);
104	printf(" controlURL = '%s'\n", d->first.controlurl);
105	printf(" eventSubURL = '%s'\n", d->first.eventsuburl);
106	printf(" SCPDURL = '%s'\n", d->first.scpdurl);
107	printf("secondary WAN Connection Device (IP or PPP Connection):\n");
108	/*printf(" deviceType = '%s'\n", d->second.devicetype);*/
109	printf(" servicetype = '%s'\n", d->second.servicetype);
110	printf(" controlURL = '%s'\n", d->second.controlurl);
111	printf(" eventSubURL = '%s'\n", d->second.eventsuburl);
112	printf(" SCPDURL = '%s'\n", d->second.scpdurl);
113	printf("WAN IPv6 Firewall Control :\n");
114	/*printf(" deviceType = '%s'\n", d->IPv6FC.devicetype);*/
115	printf(" servicetype = '%s'\n", d->IPv6FC.servicetype);
116	printf(" controlURL = '%s'\n", d->IPv6FC.controlurl);
117	printf(" eventSubURL = '%s'\n", d->IPv6FC.eventsuburl);
118	printf(" SCPDURL = '%s'\n", d->IPv6FC.scpdurl);
119}
120#endif /* DEBUG */
121
122