Coverage Report

Created: 2025-12-13 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_methods.c
Line
Count
Source
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
415k
{
41
415k
  return (_c >= 65 && _c <= 90)    /* upper alpha */
42
268k
    || (_c >= 97 && _c <= 122)   /* lower aplha */
43
33.7k
    || (_c >= 48 && _c <= 57)    /* digits */
44
27.1k
    || (_c == '-') || (_c == '.') || (_c == '!') || (_c == '%')
45
18.7k
    || (_c == '*') || (_c == '_') || (_c == '+') || (_c == '`')
46
6.39k
    || (_c == '\'') || (_c == '~');
47
415k
}
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
120k
{
59
120k
  int len=0;
60
120k
  int max=0;
61
62
120k
   if (!start || !method) {
63
0
     LM_ERR("invalid parameter value\n");
64
0
     return NULL;
65
0
   }
66
67
120k
   if(end)
68
120k
     max = end - start;
69
120k
   *method = METHOD_UNDEF;
70
71
120k
   switch (start[0]) {
72
3.57k
     case 'A':
73
5.05k
     case 'a':
74
5.05k
      if(end && max<3)
75
2.65k
        goto unknown;
76
77
2.39k
      if ((start[1]=='c' || start[1]=='C')
78
1.94k
          && (start[2]=='k' || start[2]=='K'))
79
1.48k
      {
80
1.48k
        *method = METHOD_ACK;
81
1.48k
        len = 3;
82
1.48k
        goto done;
83
1.48k
      }
84
913
      goto unknown;
85
86
9.91k
    case 'B':
87
14.1k
    case 'b':
88
14.1k
      if(end && max<3)
89
7.96k
        goto unknown;
90
91
6.16k
      if ((start[1]=='y' || start[1]=='Y')
92
4.14k
          && (start[2]=='e' || start[2]=='E'))
93
1.28k
      {
94
1.28k
        *method = METHOD_BYE;
95
1.28k
        len = 3;
96
1.28k
        goto done;
97
1.28k
      }
98
4.87k
      goto unknown;
99
100
4.87k
    case 'C':
101
6.14k
    case 'c':
102
6.14k
      if(end && max<6)
103
1.92k
        goto unknown;
104
4.22k
      if ((start[1]=='a' || start[1]=='A')
105
3.60k
          && (start[2]=='n' || start[2]=='N')
106
3.07k
          && (start[3]=='c' || start[3]=='C')
107
2.43k
          && (start[4]=='e' || start[4]=='E')
108
1.89k
          && (start[5]=='l' || start[5]=='L'))
109
1.12k
      {
110
1.12k
        *method = METHOD_CANCEL;
111
1.12k
        len = 6;
112
1.12k
        goto done;
113
1.12k
      }
114
3.09k
      goto unknown;
115
116
4.98k
    case 'I':
117
7.34k
    case 'i':
118
7.34k
      if(end && max<4)
119
1.80k
        goto unknown;
120
5.53k
      if(start[1]!='n' && start[1]!='N')
121
468
        goto unknown;
122
123
5.06k
      if ((start[2]=='f' || start[2]=='F')
124
1.80k
          && (start[3]=='o' || start[3]=='O'))
125
1.07k
      {
126
1.07k
        *method = METHOD_INFO;
127
1.07k
        len = 4;
128
1.07k
        goto done;
129
1.07k
      }
130
131
3.98k
      if(end && max<6)
132
696
        goto unknown;
133
3.29k
      if ((start[2]=='v' || start[2]=='V')
134
2.67k
          && (start[3]=='i' || start[3]=='I')
135
1.68k
          && (start[4]=='t' || start[4]=='T')
136
1.26k
          && (start[5]=='e' || start[5]=='E'))
137
840
      {
138
840
        *method = METHOD_INVITE;
139
840
        len = 6;
140
840
        goto done;
141
840
      }
142
2.45k
      goto unknown;
143
144
5.57k
    case 'M':
145
8.62k
    case 'm':
146
8.62k
      if(end && max<7)
147
3.53k
        goto unknown;
148
5.09k
      if ((start[1]=='e' || start[1]=='E')
149
4.55k
          && (start[2]=='s' || start[2]=='S')
150
4.06k
          && (start[3]=='s' || start[3]=='S')
151
3.09k
          && (start[4]=='a' || start[4]=='A')
152
2.39k
          && (start[5]=='g' || start[5]=='G')
153
1.85k
          && (start[6]=='e' || start[6]=='E')) {
154
1.16k
        *method = METHOD_MESSAGE;
155
1.16k
        len = 7;
156
1.16k
        goto done;
157
1.16k
      }
158
3.92k
      goto unknown;
159
160
3.92k
    case 'N':
161
8.39k
    case 'n':
162
8.39k
      if(end && max<6)
163
2.40k
        goto unknown;
164
5.99k
      if ((start[1]=='o' || start[1]=='O')
165
5.55k
          && (start[2]=='t' || start[2]=='T')
166
5.12k
          && (start[3]=='i' || start[3]=='I')
167
4.62k
          && (start[4]=='f' || start[4]=='F')
168
3.92k
          && (start[5]=='y' || start[5]=='Y'))
169
3.50k
      {
170
3.50k
        *method = METHOD_NOTIFY;
171
3.50k
        len = 6;
172
3.50k
        goto done;
173
3.50k
      }
174
2.49k
      goto unknown;
175
176
3.62k
    case 'O':
177
7.37k
    case 'o':
178
7.37k
      if(end && max<7)
179
3.17k
        goto unknown;
180
4.20k
      if((start[1]=='p' || start[1]=='P')
181
3.49k
          && (start[2]=='t' || start[2]=='T')
182
3.00k
          && (start[3]=='i' || start[3]=='I')
183
2.52k
          && (start[4]=='o' || start[4]=='O')
184
1.95k
          && (start[5]=='n' || start[5]=='N')
185
1.53k
          && (start[6]=='s' || start[6]=='S'))
186
945
      {
187
945
        *method = METHOD_OPTIONS;
188
945
        len = 7;
189
945
        goto done;
190
945
      }
191
3.25k
      goto unknown;
192
193
3.25k
    case 'P':
194
9.19k
    case 'p':
195
9.19k
      if(end && max<5)
196
1.53k
        goto unknown;
197
7.65k
      if((start[1]=='r' || start[1]=='R')
198
3.40k
          && (start[2]=='a' || start[2]=='A')
199
2.69k
          && (start[3]=='c' || start[3]=='C')
200
2.00k
          && (start[4]=='k' || start[4]=='K'))
201
1.41k
      {
202
1.41k
        *method = METHOD_PRACK;
203
1.41k
        len = 5;
204
1.41k
        goto done;
205
1.41k
      }
206
207
6.24k
      if(end && max<7)
208
1.23k
        goto unknown;
209
210
5.00k
      if ((start[1]=='u' || start[1]=='U')
211
4.08k
           && (start[2]=='b' || start[2]=='B')
212
3.43k
           && (start[3]=='l' || start[3]=='L')
213
2.93k
           && (start[4]=='i' || start[4]=='I')
214
2.48k
           && (start[5]=='s' || start[5]=='S')
215
1.64k
           && (start[6]=='h' || start[6]=='H'))
216
1.04k
      {
217
1.04k
        *method = METHOD_PUBLISH;
218
1.04k
        len = 7;
219
1.04k
        goto done;
220
1.04k
      }
221
3.95k
      goto unknown;
222
223
7.53k
    case 'R':
224
11.7k
    case 'r':
225
11.7k
      if(end && max<5)
226
3.40k
        goto unknown;
227
8.33k
      if(start[1]!='e' && start[1]!='E')
228
530
        goto unknown;
229
230
7.80k
      if((start[2]=='f' || start[2]=='F')
231
1.79k
           && (start[3]=='e' || start[3]=='E')
232
1.19k
           && (start[4]=='r' || start[4]=='R'))
233
778
      {
234
778
        *method = METHOD_REFER;
235
778
        len = 5;
236
778
        goto done;
237
778
      }
238
239
7.03k
      if(end && max<8)
240
992
        goto unknown;
241
242
6.03k
      if ((start[2]=='g' || start[2]=='G')
243
5.39k
           && (start[3]=='i' || start[3]=='I')
244
4.82k
           && (start[4]=='s' || start[4]=='S')
245
4.38k
           && (start[5]=='t' || start[5]=='T')
246
3.89k
           && (start[6]=='e' || start[6]=='E')
247
3.47k
           && (start[7]=='r' || start[7]=='R'))
248
2.62k
      {
249
2.62k
        *method = METHOD_REGISTER;
250
2.62k
        len = 8;
251
2.62k
        goto done;
252
2.62k
      }
253
3.41k
      goto unknown;
254
255
3.41k
    case 'S':
256
9.67k
    case 's':
257
9.67k
      if(end && max<9)
258
2.66k
        goto unknown;
259
7.00k
      if ((start[1]=='u' || start[1]=='U')
260
6.30k
           && (start[2]=='b' || start[2]=='B')
261
5.82k
           && (start[3]=='s' || start[3]=='S')
262
5.04k
           && (start[4]=='c' || start[4]=='C')
263
4.49k
           && (start[5]=='r' || start[5]=='R')
264
4.05k
           && (start[6]=='i' || start[6]=='I')
265
3.63k
           && (start[7]=='b' || start[7]=='B')
266
1.83k
           && (start[8]=='e' || start[8]=='E'))
267
1.08k
      {
268
1.08k
        *method = METHOD_SUBSCRIBE;
269
1.08k
        len = 9;
270
1.08k
        goto done;
271
1.08k
      }
272
5.91k
      goto unknown;
273
274
5.91k
    case 'U':
275
6.89k
    case 'u':
276
6.89k
      if(end && max<6)
277
1.15k
        goto unknown;
278
5.74k
      if ((start[1]=='p' || start[1]=='P')
279
5.19k
          && (start[2]=='d' || start[2]=='D')
280
4.74k
          && (start[3]=='a' || start[3]=='A')
281
4.31k
          && (start[4]=='t' || start[4]=='T')
282
3.48k
          && (start[5]=='e' || start[5]=='E')) {
283
2.70k
        *method = METHOD_UPDATE;
284
2.70k
        len = 6;
285
2.70k
        goto done;
286
2.70k
      }
287
3.03k
      goto unknown;
288
289
25.6k
    default:
290
25.6k
      goto unknown;
291
120k
    }
292
293
21.0k
done:
294
21.0k
  if(!end || (end && len < max))
295
5.70k
  {
296
5.70k
    if(start[len]!='\0' && start[len]!=',' && start[len]!=' '
297
4.19k
        && start[len]!='\t' && start[len]!='\r' && start[len]!='\n')
298
4.19k
      goto unknown;
299
5.70k
  }
300
301
16.8k
  return (start+len);
302
303
103k
unknown:
304
103k
  *method = METHOD_OTHER;
305
103k
  if(end)
306
103k
  {
307
516k
    while(len < max)
308
442k
    {
309
442k
      if((start[len]=='\0' || start[len]==',' || start[len]==' '
310
415k
            || start[len]=='\t' || start[len]=='\r'
311
415k
            || start[len]=='\n'))
312
27.3k
        return (start+len);
313
314
415k
      if(!method_char(start[len]))
315
1.56k
      {
316
1.56k
        LM_ERR("invalid character %c\n", start[len]);
317
1.56k
        return NULL;
318
1.56k
      }
319
320
413k
      len++;
321
413k
    }
322
74.4k
    return end;
323
103k
  }
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
}