/
msrp_parser.c
249 lines (222 loc) · 6.02 KB
/
msrp_parser.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (C) 2022 - OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/* here we have MSRP "parsing"-specific functions
*/
#include <ctype.h>
#include "../../ut.h"
#include "msrp_parser.h"
#define _READ4(_p) \
((*((unsigned char *)(_p) + 0) + \
(*((unsigned char *)(_p) + 1) << 8) + \
(*((unsigned char *)(_p) + 2) << 16) + \
(*((unsigned char *)(_p) + 3) << 24)) | 0x20202020)
#define MSRP_DEBUG
static int _parse_msrp_method( str *method_s, enum msrp_method *method_id)
{
if (method_s==NULL || method_id==NULL || method_s->s==NULL)
return -1;
if (method_s->len<4) {
/* too short to be a know method */
goto other;
}
switch ( _READ4(method_s->s) ) {
case 0x646e6573 : /*SEND*/
if (method_s->len==4) {
*method_id = MSRP_METHOD_SEND;
return 0;
}
break;
case 0x68747561 : /*AUTH*/
if (method_s->len==4) {
*method_id = MSRP_METHOD_AUTH;
return 0;
}
break;
case 0x6f706572 : /*REPO*/
if (method_s->len==6 && method_s->s[4]=='R' && method_s->s[5]=='T') {
*method_id = MSRP_METHOD_REPORT;
return 0;
}
break;
}
other:
*method_id = MSRP_METHOD_OTHER;
return 0;
}
int parse_msrp_msg( char* buf, int len, struct msrp_msg *msg)
{
struct hdr_field *hf = NULL;
char *p, *end;
str mth;
#define link_hdr(_hook, _hdr) \
do{ \
if (msg->_hook) { \
LM_ERR("duplicated hdr _hdr found\n"); \
goto error; \
} \
msg->_hook=_hdr;\
}while(0)
/* complete the parsing of the first line */
if (msg->fl.u.request.method.len >= 3 &&
isdigit(msg->fl.u.request.method.s[0]) &&
isdigit(msg->fl.u.request.method.s[1]) &&
isdigit(msg->fl.u.request.method.s[2])) {
/* it looks like a reply */
msg->fl.type = MSRP_REPLY;
mth = msg->fl.u.request.method;
msg->fl.u.reply.status.s = mth.s;
msg->fl.u.reply.status.len = 3;
if (msg->fl.u.request.method.len >= 3 + 1 + 1 &&
msg->fl.u.request.method.s[3]==' ') {
msg->fl.u.reply.reason.s = mth.s + 4;
msg->fl.u.reply.reason.len = mth.len - 4;
}
/* this converstion is risk free as the we already tested
* for a valid number above */
str2int( &msg->fl.u.reply.status,
(unsigned int*)&msg->fl.u.reply.status_no );
} else {
/* let's hope it is a request, check for spaces at least */
if (q_memchr( msg->fl.u.request.method.s, ' ',
msg->fl.u.request.method.len)!=NULL) {
/* ups, spaces in the method name */
goto error;
}
msg->fl.type = MSRP_REQUEST;
_parse_msrp_method( & msg->fl.u.request.method,
& msg->fl.u.request.method_id);
}
/* parse the headers */
p = msg->fl.eol+1;
end = msg->body.s ?
msg->body.s /* the headers end where the body start (we do not chare
* of extra CRLF between the hdr and body */
:
(buf+len-7-msg->fl.ident.len-1-CRLF_LEN); /* the headers end right
* before the EOM, so we go go backward
* EOM = "-------" transact-id continuation-flag CRLF*/
while( p<end ){
hf = pkg_malloc(sizeof(struct hdr_field));
if ( hf==NULL ) {
LM_ERR("pkg memory allocation failed\n");
goto error;
}
memset( hf, 0, sizeof(struct hdr_field));
hf->type=HDR_ERROR_T;
p = get_hdr_field( p, end, hf);
switch (hf->type){
case HDR_EOH_T:
pkg_free(hf);
hf = NULL;
/* done, EOH */
goto done;
case HDR_TO_PATH_T:
link_hdr( to_path, hf);
break;
case HDR_FROM_PATH_T:
link_hdr( from_path, hf);
break;
case HDR_MESSAGE_ID_T:
link_hdr( message_id, hf);
break;
case HDR_BYTE_RANGE_T:
link_hdr( byte_range, hf);
break;
case HDR_FAILURE_REPORT_T:
link_hdr( failure_report, hf);
break;
case HDR_SUCCESS_REPORT_T:
link_hdr( success_report, hf);
break;
case HDR_STATUS_T:
link_hdr( status, hf);
break;
case HDR_USE_PATH_T:
link_hdr( use_path, hf);
break;
case HDR_CONTENTTYPE_T:
link_hdr( content_type, hf);
break;
case HDR_AUTHORIZATION_T:
link_hdr( authorization, hf);
break;
case HDR_EXPIRES_T:
link_hdr( expires, hf);
break;
case HDR_OTHER_T:
break;
case HDR_ERROR_T:
default:
LM_INFO("bad header field\n");
goto err_free_hf;
}
/* add the header to the list*/
if (msg->last_header==0){
msg->headers=hf;
msg->last_header=hf;
}else{
msg->last_header->next=hf;
msg->last_header=hf;
}
}
done:
/* some validation on the hdr presence */
if (msg->from_path==NULL) {
LM_ERR("MSRP msg has no from-path hdr\n");
goto error;
}
if (msg->to_path==NULL) {
LM_ERR("MSRP msg has no to-path hdr\n");
goto error;
}
/* TO and FROM (in this order) must be the first ones */
if (msg->headers->type!=HDR_TO_PATH_T &&
msg->headers->next->type!=HDR_FROM_PATH_T) {
LM_ERR("TO and FROM are not the first headers\n");
goto error;
}
if (msg->fl.type == MSRP_REQUEST) {
if (msg->body.s && msg->content_type==NULL) {
LM_ERR("MSRP req has no content-type hdr\n");
goto error;
}
if (msg->fl.u.request.method_id != MSRP_METHOD_AUTH &&
msg->message_id==NULL) {
LM_ERR("MSRP req has no message-id hdr\n");
goto error;
}
} else {
}
return 0;
err_free_hf:
if (hf) pkg_free(hf);
error:
return -1;
}
void free_msrp_msg( struct msrp_msg *msg)
{
if (msg->headers)
free_hdr_field_lst(msg->headers);
if (msg->to_path && msg->to_path->parsed)
free_msrp_path( (struct msrp_url *)msg->to_path->parsed );
if (msg->from_path && msg->from_path->parsed)
free_msrp_path( (struct msrp_url *)msg->from_path->parsed );
}