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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/* $Id: testigddescparse.c,v 1.8 2015/02/08 08:46:06 nanard Exp $ */
/* Project : miniupnp
* http://miniupnp.free.fr/
* Author : Thomas Bernard
* Copyright (c) 2008-2015 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution.
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "igd_desc_parse.h"
#include "minixml.h"
#include "miniupnpc.h"
/* count number of differences */
int compare_service(struct IGDdatas_service * s, FILE * f)
{
int n = 0;
char line[1024];
while(fgets(line, sizeof(line), f)) {
char * value;
char * equal;
char * name;
char * parsedvalue;
int l;
l = strlen(line);
while((l > 0) && ((line[l-1] == '\r') || (line[l-1] == '\n') || (line[l-1] == ' ')))
line[--l] = '\0';
if(l == 0)
break; /* end on blank line */
if(line[0] == '#')
continue; /* skip comments */
equal = strchr(line, '=');
if(equal == NULL) {
fprintf(stderr, "Warning, line does not contain '=' : %s\n", line);
continue;
}
*equal = '\0';
name = line;
while(*name == ' ' || *name == '\t')
name++;
l = strlen(name);
while((l > 0) && (name[l-1] == ' ' || name[l-1] == '\t'))
name[--l] = '\0';
value = equal + 1;
while(*value == ' ' || *value == '\t')
value++;
if(strcmp(name, "controlurl") == 0)
parsedvalue = s->controlurl;
else if(strcmp(name, "eventsuburl") == 0)
parsedvalue = s->eventsuburl;
else if(strcmp(name, "scpdurl") == 0)
parsedvalue = s->scpdurl;
else if(strcmp(name, "servicetype") == 0)
parsedvalue = s->servicetype;
else {
fprintf(stderr, "unknown field '%s'\n", name);
continue;
}
if(0 != strcmp(parsedvalue, value)) {
fprintf(stderr, "difference : '%s' != '%s'\n", parsedvalue, value);
n++;
}
}
return n;
}
int compare_igd(struct IGDdatas * p, FILE * f)
{
int n = 0;
char line[1024];
struct IGDdatas_service * s;
while(fgets(line, sizeof(line), f)) {
char * colon;
int l = (int)strlen(line);
while((l > 0) && (line[l-1] == '\r' || (line[l-1] == '\n')))
line[--l] = '\0';
if(l == 0 || line[0] == '#')
continue; /* skip blank lines and comments */
colon = strchr(line, ':');
if(colon == NULL) {
fprintf(stderr, "Warning, no ':' : %s\n", line);
continue;
}
s = NULL;
*colon = '\0';
if(strcmp(line, "CIF") == 0)
s = &p->CIF;
else if(strcmp(line, "first") == 0)
s = &p->first;
else if(strcmp(line, "second") == 0)
s = &p->second;
else if(strcmp(line, "IPv6FC") == 0)
s = &p->IPv6FC;
else {
s = NULL;
fprintf(stderr, "*** unknown service '%s' ***\n", line);
n++;
continue;
}
n += compare_service(s, f);
}
if(n > 0)
fprintf(stderr, "*** %d difference%s ***\n", n, (n > 1) ? "s" : "");
return n;
}
int test_igd_desc_parse(char * buffer, int len, FILE * f)
{
int n;
struct IGDdatas igd;
struct xmlparser parser;
struct UPNPUrls urls;
memset(&igd, 0, sizeof(struct IGDdatas));
memset(&parser, 0, sizeof(struct xmlparser));
parser.xmlstart = buffer;
parser.xmlsize = len;
parser.data = &igd;
parser.starteltfunc = IGDstartelt;
parser.endeltfunc = IGDendelt;
parser.datafunc = IGDdata;
parsexml(&parser);
#ifdef DEBUG
printIGD(&igd);
#endif /* DEBUG */
GetUPNPUrls(&urls, &igd, "http://fake/desc/url/file.xml", 0);
printf("ipcondescURL='%s'\n", urls.ipcondescURL);
printf("controlURL='%s'\n", urls.controlURL);
printf("controlURL_CIF='%s'\n", urls.controlURL_CIF);
n = f ? compare_igd(&igd, f) : 0;
FreeUPNPUrls(&urls);
return n;
}
int main(int argc, char * * argv)
{
FILE * f;
char * buffer;
int len;
int r;
if(argc<2) {
fprintf(stderr, "Usage: %s file.xml [file.values]\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
if(!f) {
fprintf(stderr, "Cannot open %s for reading.\n", argv[1]);
return 1;
}
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(len);
if(!buffer) {
fprintf(stderr, "Memory allocation error.\n");
fclose(f);
return 1;
}
r = (int)fread(buffer, 1, len, f);
if(r != len) {
fprintf(stderr, "Failed to read file %s. %d out of %d bytes.\n",
argv[1], r, len);
fclose(f);
free(buffer);
return 1;
}
fclose(f);
f = NULL;
if(argc > 2) {
f = fopen(argv[2], "r");
if(!f) {
fprintf(stderr, "Cannot open %s for reading.\n", argv[2]);
free(buffer);
return 1;
}
}
r = test_igd_desc_parse(buffer, len, f);
free(buffer);
if(f)
fclose(f);
return r;
}
|