/src/opensips/parser/parse_methods.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2004 Juha Heinanen |
3 | | * |
4 | | * |
5 | | * This file is part of opensips, a free SIP server. |
6 | | * |
7 | | * opensips is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version |
11 | | * |
12 | | * opensips is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | * |
21 | | * History: |
22 | | * -------- |
23 | | * 2005-07-05 - moved and merged method types in msg_parser.h (ramona) |
24 | | * - changed and exported parse_method() to use it from other |
25 | | * files (ramona) |
26 | | */ |
27 | | |
28 | | #include <strings.h> |
29 | | #include "../dprint.h" |
30 | | #include "../trim.h" |
31 | | #include "../core_stats.h" |
32 | | #include "parse_methods.h" |
33 | | #include "msg_parser.h" |
34 | | |
35 | | |
36 | | /* |
37 | | * Check if argument is valid RFC3261 token character. |
38 | | */ |
39 | | static inline int method_char(char _c) |
40 | 0 | { |
41 | 0 | return (_c >= 65 && _c <= 90) /* upper alpha */ |
42 | 0 | || (_c >= 97 && _c <= 122) /* lower aplha */ |
43 | 0 | || (_c >= 48 && _c <= 57) /* digits */ |
44 | 0 | || (_c == '-') || (_c == '.') || (_c == '!') || (_c == '%') |
45 | 0 | || (_c == '*') || (_c == '_') || (_c == '+') || (_c == '`') |
46 | 0 | || (_c == '\'') || (_c == '~'); |
47 | 0 | } |
48 | | |
49 | | |
50 | | /* |
51 | | * Parse a method pointed by start, end is the last character to check (if NULL |
52 | | * assume that start is a zero terminated string) |
53 | | * => assign enum bit to method. |
54 | | * Returns pointer to next char if parse succeeded |
55 | | * and NULL otherwise. |
56 | | */ |
57 | | char* parse_method(char* start, char* end, unsigned int* method) |
58 | 0 | { |
59 | 0 | int len=0; |
60 | 0 | int max=0; |
61 | |
|
62 | 0 | if (!start || !method) { |
63 | 0 | LM_ERR("invalid parameter value\n"); |
64 | 0 | return NULL; |
65 | 0 | } |
66 | | |
67 | 0 | if(end) |
68 | 0 | max = end - start; |
69 | 0 | *method = METHOD_UNDEF; |
70 | |
|
71 | 0 | switch (start[0]) { |
72 | 0 | case 'A': |
73 | 0 | case 'a': |
74 | 0 | if(end && max<3) |
75 | 0 | goto unknown; |
76 | | |
77 | 0 | if ((start[1]=='c' || start[1]=='C') |
78 | 0 | && (start[2]=='k' || start[2]=='K')) |
79 | 0 | { |
80 | 0 | *method = METHOD_ACK; |
81 | 0 | len = 3; |
82 | 0 | goto done; |
83 | 0 | } |
84 | 0 | goto unknown; |
85 | | |
86 | 0 | case 'B': |
87 | 0 | case 'b': |
88 | 0 | if(end && max<3) |
89 | 0 | goto unknown; |
90 | | |
91 | 0 | if ((start[1]=='y' || start[1]=='Y') |
92 | 0 | && (start[2]=='e' || start[2]=='E')) |
93 | 0 | { |
94 | 0 | *method = METHOD_BYE; |
95 | 0 | len = 3; |
96 | 0 | goto done; |
97 | 0 | } |
98 | 0 | goto unknown; |
99 | | |
100 | 0 | case 'C': |
101 | 0 | case 'c': |
102 | 0 | if(end && max<6) |
103 | 0 | goto unknown; |
104 | 0 | if ((start[1]=='a' || start[1]=='A') |
105 | 0 | && (start[2]=='n' || start[2]=='N') |
106 | 0 | && (start[3]=='c' || start[3]=='C') |
107 | 0 | && (start[4]=='e' || start[4]=='E') |
108 | 0 | && (start[5]=='l' || start[5]=='L')) |
109 | 0 | { |
110 | 0 | *method = METHOD_CANCEL; |
111 | 0 | len = 6; |
112 | 0 | goto done; |
113 | 0 | } |
114 | 0 | goto unknown; |
115 | | |
116 | 0 | case 'I': |
117 | 0 | case 'i': |
118 | 0 | if(end && max<4) |
119 | 0 | goto unknown; |
120 | 0 | if(start[1]!='n' && start[1]!='N') |
121 | 0 | goto unknown; |
122 | | |
123 | 0 | if ((start[2]=='f' || start[2]=='F') |
124 | 0 | && (start[3]=='o' || start[3]=='O')) |
125 | 0 | { |
126 | 0 | *method = METHOD_INFO; |
127 | 0 | len = 4; |
128 | 0 | goto done; |
129 | 0 | } |
130 | | |
131 | 0 | if(end && max<6) |
132 | 0 | goto unknown; |
133 | 0 | if ((start[2]=='v' || start[2]=='V') |
134 | 0 | && (start[3]=='i' || start[3]=='I') |
135 | 0 | && (start[4]=='t' || start[4]=='T') |
136 | 0 | && (start[5]=='e' || start[5]=='E')) |
137 | 0 | { |
138 | 0 | *method = METHOD_INVITE; |
139 | 0 | len = 6; |
140 | 0 | goto done; |
141 | 0 | } |
142 | 0 | goto unknown; |
143 | | |
144 | 0 | case 'M': |
145 | 0 | case 'm': |
146 | 0 | if(end && max<7) |
147 | 0 | goto unknown; |
148 | 0 | if ((start[1]=='e' || start[1]=='E') |
149 | 0 | && (start[2]=='s' || start[2]=='S') |
150 | 0 | && (start[3]=='s' || start[3]=='S') |
151 | 0 | && (start[4]=='a' || start[4]=='A') |
152 | 0 | && (start[5]=='g' || start[5]=='G') |
153 | 0 | && (start[6]=='e' || start[6]=='E')) { |
154 | 0 | *method = METHOD_MESSAGE; |
155 | 0 | len = 7; |
156 | 0 | goto done; |
157 | 0 | } |
158 | 0 | goto unknown; |
159 | | |
160 | 0 | case 'N': |
161 | 0 | case 'n': |
162 | 0 | if(end && max<6) |
163 | 0 | goto unknown; |
164 | 0 | if ((start[1]=='o' || start[1]=='O') |
165 | 0 | && (start[2]=='t' || start[2]=='T') |
166 | 0 | && (start[3]=='i' || start[3]=='I') |
167 | 0 | && (start[4]=='f' || start[4]=='F') |
168 | 0 | && (start[5]=='y' || start[5]=='Y')) |
169 | 0 | { |
170 | 0 | *method = METHOD_NOTIFY; |
171 | 0 | len = 6; |
172 | 0 | goto done; |
173 | 0 | } |
174 | 0 | goto unknown; |
175 | | |
176 | 0 | case 'O': |
177 | 0 | case 'o': |
178 | 0 | if(end && max<7) |
179 | 0 | goto unknown; |
180 | 0 | if((start[1]=='p' || start[1]=='P') |
181 | 0 | && (start[2]=='t' || start[2]=='T') |
182 | 0 | && (start[3]=='i' || start[3]=='I') |
183 | 0 | && (start[4]=='o' || start[4]=='O') |
184 | 0 | && (start[5]=='n' || start[5]=='N') |
185 | 0 | && (start[6]=='s' || start[6]=='S')) |
186 | 0 | { |
187 | 0 | *method = METHOD_OPTIONS; |
188 | 0 | len = 7; |
189 | 0 | goto done; |
190 | 0 | } |
191 | 0 | goto unknown; |
192 | | |
193 | 0 | case 'P': |
194 | 0 | case 'p': |
195 | 0 | if(end && max<5) |
196 | 0 | goto unknown; |
197 | 0 | if((start[1]=='r' || start[1]=='R') |
198 | 0 | && (start[2]=='a' || start[2]=='A') |
199 | 0 | && (start[3]=='c' || start[3]=='C') |
200 | 0 | && (start[4]=='k' || start[4]=='K')) |
201 | 0 | { |
202 | 0 | *method = METHOD_PRACK; |
203 | 0 | len = 5; |
204 | 0 | goto done; |
205 | 0 | } |
206 | | |
207 | 0 | if(end && max<7) |
208 | 0 | goto unknown; |
209 | | |
210 | 0 | if ((start[1]=='u' || start[1]=='U') |
211 | 0 | && (start[2]=='b' || start[2]=='B') |
212 | 0 | && (start[3]=='l' || start[3]=='L') |
213 | 0 | && (start[4]=='i' || start[4]=='I') |
214 | 0 | && (start[5]=='s' || start[5]=='S') |
215 | 0 | && (start[6]=='h' || start[6]=='H')) |
216 | 0 | { |
217 | 0 | *method = METHOD_PUBLISH; |
218 | 0 | len = 7; |
219 | 0 | goto done; |
220 | 0 | } |
221 | 0 | goto unknown; |
222 | | |
223 | 0 | case 'R': |
224 | 0 | case 'r': |
225 | 0 | if(end && max<5) |
226 | 0 | goto unknown; |
227 | 0 | if(start[1]!='e' && start[1]!='E') |
228 | 0 | goto unknown; |
229 | | |
230 | 0 | if((start[2]=='f' || start[2]=='F') |
231 | 0 | && (start[3]=='e' || start[3]=='E') |
232 | 0 | && (start[4]=='r' || start[4]=='R')) |
233 | 0 | { |
234 | 0 | *method = METHOD_REFER; |
235 | 0 | len = 5; |
236 | 0 | goto done; |
237 | 0 | } |
238 | | |
239 | 0 | if(end && max<8) |
240 | 0 | goto unknown; |
241 | | |
242 | 0 | if ((start[2]=='g' || start[2]=='G') |
243 | 0 | && (start[3]=='i' || start[3]=='I') |
244 | 0 | && (start[4]=='s' || start[4]=='S') |
245 | 0 | && (start[5]=='t' || start[5]=='T') |
246 | 0 | && (start[6]=='e' || start[6]=='E') |
247 | 0 | && (start[7]=='r' || start[7]=='R')) |
248 | 0 | { |
249 | 0 | *method = METHOD_REGISTER; |
250 | 0 | len = 8; |
251 | 0 | goto done; |
252 | 0 | } |
253 | 0 | goto unknown; |
254 | | |
255 | 0 | case 'S': |
256 | 0 | case 's': |
257 | 0 | if(end && max<9) |
258 | 0 | goto unknown; |
259 | 0 | if ((start[1]=='u' || start[1]=='U') |
260 | 0 | && (start[2]=='b' || start[2]=='B') |
261 | 0 | && (start[3]=='s' || start[3]=='S') |
262 | 0 | && (start[4]=='c' || start[4]=='C') |
263 | 0 | && (start[5]=='r' || start[5]=='R') |
264 | 0 | && (start[6]=='i' || start[6]=='I') |
265 | 0 | && (start[7]=='b' || start[7]=='B') |
266 | 0 | && (start[8]=='e' || start[8]=='E')) |
267 | 0 | { |
268 | 0 | *method = METHOD_SUBSCRIBE; |
269 | 0 | len = 9; |
270 | 0 | goto done; |
271 | 0 | } |
272 | 0 | goto unknown; |
273 | | |
274 | 0 | case 'U': |
275 | 0 | case 'u': |
276 | 0 | if(end && max<6) |
277 | 0 | goto unknown; |
278 | 0 | if ((start[1]=='p' || start[1]=='P') |
279 | 0 | && (start[2]=='d' || start[2]=='D') |
280 | 0 | && (start[3]=='a' || start[3]=='A') |
281 | 0 | && (start[4]=='t' || start[4]=='T') |
282 | 0 | && (start[5]=='e' || start[5]=='E')) { |
283 | 0 | *method = METHOD_UPDATE; |
284 | 0 | len = 6; |
285 | 0 | goto done; |
286 | 0 | } |
287 | 0 | goto unknown; |
288 | | |
289 | 0 | default: |
290 | 0 | goto unknown; |
291 | 0 | } |
292 | | |
293 | 0 | done: |
294 | 0 | if(!end || (end && len < max)) |
295 | 0 | { |
296 | 0 | if(start[len]!='\0' && start[len]!=',' && start[len]!=' ' |
297 | 0 | && start[len]!='\t' && start[len]!='\r' && start[len]!='\n') |
298 | 0 | goto unknown; |
299 | 0 | } |
300 | | |
301 | 0 | return (start+len); |
302 | | |
303 | 0 | unknown: |
304 | 0 | *method = METHOD_OTHER; |
305 | 0 | if(end) |
306 | 0 | { |
307 | 0 | while(len < max) |
308 | 0 | { |
309 | 0 | if((start[len]=='\0' || start[len]==',' || start[len]==' ' |
310 | 0 | || start[len]=='\t' || start[len]=='\r' |
311 | 0 | || start[len]=='\n')) |
312 | 0 | return (start+len); |
313 | | |
314 | 0 | if(!method_char(start[len])) |
315 | 0 | { |
316 | 0 | LM_ERR("invalid character %c\n", start[len]); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | | |
320 | 0 | len++; |
321 | 0 | } |
322 | 0 | return end; |
323 | 0 | } |
324 | | |
325 | 0 | while(start[len]!='\0' && start[len]!=',' && start[len]!=' ' |
326 | 0 | && start[len]!='\t' && start[len]!='\r' && start[len]!='\n') |
327 | 0 | { |
328 | 0 | if(!method_char(start[len])) |
329 | 0 | { |
330 | 0 | LM_ERR("invalid character %c!\n", start[len]); |
331 | 0 | return NULL; |
332 | 0 | } |
333 | 0 | len++; |
334 | 0 | } |
335 | | |
336 | 0 | return (start+len); |
337 | 0 | } |
338 | | |
339 | | |
340 | | /* |
341 | | * Parse comma separated list of methods pointed by _body and assign their |
342 | | * enum bits to _methods. Returns 0 on success and -1 on failure. |
343 | | */ |
344 | | int parse_methods(str* _body, unsigned int* _methods) |
345 | 0 | { |
346 | 0 | str next; |
347 | 0 | char *p; |
348 | 0 | char *p0; |
349 | 0 | unsigned int method; |
350 | |
|
351 | 0 | if (!_body || !_methods) { |
352 | 0 | LM_ERR("invalid parameter value\n"); |
353 | 0 | return -1; |
354 | 0 | } |
355 | | |
356 | 0 | next.len = _body->len; |
357 | 0 | next.s = _body->s; |
358 | |
|
359 | 0 | trim_leading(&next); |
360 | |
|
361 | 0 | *_methods = 0; |
362 | 0 | if (next.len == 0) { |
363 | 0 | goto done; |
364 | 0 | } |
365 | | |
366 | 0 | method = 0; |
367 | 0 | p = next.s; |
368 | |
|
369 | 0 | while (p<next.s+next.len) { |
370 | 0 | if((p0=parse_method(p, next.s+next.len, &method))!=NULL) { |
371 | 0 | *_methods |= method; |
372 | 0 | p = p0; |
373 | 0 | } else { |
374 | 0 | LM_ERR("invalid method [%.*s]\n", next.len, next.s); |
375 | 0 | return -1; |
376 | 0 | } |
377 | | |
378 | 0 | while(p<next.s+next.len && (*p==' ' || *p=='\t' |
379 | 0 | || *p=='\r' || *p=='\n')) |
380 | 0 | p++; |
381 | 0 | if(p>=next.s+next.len || *p == '\0') |
382 | 0 | goto done; |
383 | | |
384 | | |
385 | 0 | if (*p == ',') |
386 | 0 | { |
387 | 0 | p++; |
388 | 0 | while(p<next.s+next.len && (*p==' ' || *p=='\t' |
389 | 0 | || *p=='\r' || *p=='\n')) |
390 | 0 | p++; |
391 | 0 | if(p>=next.s+next.len) |
392 | 0 | goto done; |
393 | 0 | } else { |
394 | 0 | LM_ERR("comma expected\n"); |
395 | 0 | return -1; |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | 0 | done: |
400 | 0 | LM_DBG("methods 0x%X\n", *_methods); |
401 | 0 | return 0; |
402 | 0 | } |