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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/* Copyright (C) 2019-2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program 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
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
// written by Giuseppe Longo <giuseppe@glono.it>
use nom7::bytes::streaming::{take, take_while, take_while1};
use nom7::character::streaming::{char, crlf};
use nom7::character::{is_alphabetic, is_alphanumeric, is_space};
use nom7::combinator::map_res;
use nom7::sequence::delimited;
use nom7::{Err, IResult, Needed};
use std;
use std::collections::HashMap;
#[derive(Debug)]
pub struct Header {
pub name: String,
pub value: String,
}
#[derive(Debug)]
pub struct Request {
pub method: String,
pub path: String,
pub version: String,
pub headers: HashMap<String, String>,
pub request_line_len: u16,
pub headers_len: u16,
pub body_offset: u16,
pub body_len: u16,
}
#[derive(Debug)]
pub struct Response {
pub version: String,
pub code: String,
pub reason: String,
pub response_line_len: u16,
pub headers_len: u16,
pub body_offset: u16,
pub body_len: u16,
}
#[inline]
fn is_token_char(b: u8) -> bool {
is_alphanumeric(b) || b"!%'*+-._`".contains(&b)
}
#[inline]
fn is_method_char(b: u8) -> bool {
is_alphabetic(b)
}
#[inline]
fn is_request_uri_char(b: u8) -> bool {
is_alphanumeric(b) || is_token_char(b) || b"~#@:".contains(&b)
}
#[inline]
fn is_version_char(b: u8) -> bool {
is_alphanumeric(b) || b"./".contains(&b)
}
#[inline]
fn is_reason_phrase(b: u8) -> bool {
is_alphanumeric(b) || is_token_char(b) || b"$&(),/:;=?@[\\]^ ".contains(&b)
}
fn is_header_name(b: u8) -> bool {
is_alphanumeric(b) || is_token_char(b)
}
fn is_header_value(b: u8) -> bool {
is_alphanumeric(b) || is_token_char(b) || b"\"#$&(),/;:<=>?@[]{}()^|~\\\t\n\r ".contains(&b)
}
pub fn sip_parse_request(oi: &[u8]) -> IResult<&[u8], Request> {
let (i, method) = parse_method(oi)?;
let (i, _) = char(' ')(i)?;
let (i, path) = parse_request_uri(i)?;
let (i, _) = char(' ')(i)?;
let (i, version) = parse_version(i)?;
let (hi, _) = crlf(i)?;
let request_line_len = oi.len() - hi.len();
let (phi, headers) = parse_headers(hi)?;
let headers_len = hi.len() - phi.len();
let (bi, _) = crlf(phi)?;
let body_offset = oi.len() - bi.len();
Ok((
bi,
Request {
method: method.into(),
path: path.into(),
version: version.into(),
headers,
request_line_len: request_line_len as u16,
headers_len: headers_len as u16,
body_offset: body_offset as u16,
body_len: bi.len() as u16,
},
))
}
pub fn sip_parse_response(oi: &[u8]) -> IResult<&[u8], Response> {
let (i, version) = parse_version(oi)?;
let (i, _) = char(' ')(i)?;
let (i, code) = parse_code(i)?;
let (i, _) = char(' ')(i)?;
let (i, reason) = parse_reason(i)?;
let (hi, _) = crlf(i)?;
let response_line_len = oi.len() - hi.len();
let (phi, _headers) = parse_headers(hi)?;
let headers_len = hi.len() - phi.len();
let (bi, _) = crlf(phi)?;
let body_offset = oi.len() - bi.len();
Ok((
bi,
Response {
version: version.into(),
code: code.into(),
reason: reason.into(),
response_line_len: response_line_len as u16,
headers_len: headers_len as u16,
body_offset: body_offset as u16,
body_len: bi.len() as u16,
},
))
}
#[inline]
fn parse_method(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take_while(is_method_char), std::str::from_utf8)(i)
}
#[inline]
fn parse_request_uri(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take_while1(is_request_uri_char), std::str::from_utf8)(i)
}
#[inline]
fn parse_version(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take_while1(is_version_char), std::str::from_utf8)(i)
}
#[inline]
fn parse_code(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take(3_usize), std::str::from_utf8)(i)
}
#[inline]
fn parse_reason(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take_while(is_reason_phrase), std::str::from_utf8)(i)
}
#[inline]
fn header_name(i: &[u8]) -> IResult<&[u8], &str> {
map_res(take_while(is_header_name), std::str::from_utf8)(i)
}
#[inline]
fn header_value(i: &[u8]) -> IResult<&[u8], &str> {
map_res(parse_header_value, std::str::from_utf8)(i)
}
#[inline]
fn hcolon(i: &[u8]) -> IResult<&[u8], char> {
delimited(take_while(is_space), char(':'), take_while(is_space))(i)
}
fn message_header(i: &[u8]) -> IResult<&[u8], Header> {
let (i, n) = header_name(i)?;
let (i, _) = hcolon(i)?;
let (i, v) = header_value(i)?;
let (i, _) = crlf(i)?;
Ok((
i,
Header {
name: String::from(n),
value: String::from(v),
},
))
}
pub fn sip_take_line(i: &[u8]) -> IResult<&[u8], Option<String>> {
let (i, line) = map_res(take_while1(is_reason_phrase), std::str::from_utf8)(i)?;
Ok((i, Some(line.into())))
}
pub fn parse_headers(mut input: &[u8]) -> IResult<&[u8], HashMap<String, String>> {
let mut headers_map: HashMap<String, String> = HashMap::new();
loop {
match crlf(input) as IResult<&[u8], _> {
Ok((_, _)) => {
break;
}
Err(Err::Error(_)) => {}
Err(Err::Failure(_)) => {}
Err(Err::Incomplete(e)) => return Err(Err::Incomplete(e)),
};
let (rest, header) = message_header(input)?;
headers_map.insert(header.name, header.value);
input = rest;
}
Ok((input, headers_map))
}
fn parse_header_value(buf: &[u8]) -> IResult<&[u8], &[u8]> {
let mut end_pos = 0;
let mut trail_spaces = 0;
let mut idx = 0;
while idx < buf.len() {
match buf[idx] {
b'\n' => {
idx += 1;
if idx >= buf.len() {
return Err(Err::Incomplete(Needed::new(1)));
}
match buf[idx] {
b' ' | b'\t' => {
idx += 1;
continue;
}
_ => {
return Ok((&buf[(end_pos + trail_spaces)..], &buf[..end_pos]));
}
}
}
b' ' | b'\t' => {
trail_spaces += 1;
}
b'\r' => {}
b => {
trail_spaces = 0;
if !is_header_value(b) {
return Err(Err::Incomplete(Needed::new(1)));
}
end_pos = idx + 1;
}
}
idx += 1;
}
Ok((&b""[..], buf))
}
#[cfg(test)]
mod tests {
use crate::sip::parser::*;
#[test]
fn test_parse_request() {
let buf: &[u8] = "REGISTER sip:sip.cybercity.dk SIP/2.0\r\n\
From: <sip:voi18063@sip.cybercity.dk>;tag=903df0a\r\n\
To: <sip:voi18063@sip.cybercity.dk>\r\n\
Content-Length: 0\r\n\
\r\n"
.as_bytes();
match sip_parse_request(buf) {
Ok((_, req)) => {
assert_eq!(req.method, "REGISTER");
assert_eq!(req.path, "sip:sip.cybercity.dk");
assert_eq!(req.version, "SIP/2.0");
assert_eq!(req.headers["Content-Length"], "0");
}
_ => {
assert!(false);
}
}
}
#[test]
fn test_parse_request_trail_space_header() {
let buf: &[u8] = "REGISTER sip:sip.cybercity.dk SIP/2.0\r\n\
From: <sip:voi18063@sip.cybercity.dk>;tag=903df0a\r\n\
To: <sip:voi18063@sip.cybercity.dk>\r\n\
Content-Length: 4 \r\n\
\r\nABCD"
.as_bytes();
let (body, req) = sip_parse_request(buf).expect("parsing failed");
assert_eq!(req.method, "REGISTER");
assert_eq!(req.path, "sip:sip.cybercity.dk");
assert_eq!(req.version, "SIP/2.0");
assert_eq!(req.headers["Content-Length"], "4");
assert_eq!(body, "ABCD".as_bytes());
}
#[test]
fn test_parse_response() {
let buf: &[u8] = "SIP/2.0 401 Unauthorized\r\n\
\r\n"
.as_bytes();
match sip_parse_response(buf) {
Ok((_, resp)) => {
assert_eq!(resp.version, "SIP/2.0");
assert_eq!(resp.code, "401");
assert_eq!(resp.reason, "Unauthorized");
}
_ => {
assert!(false);
}
}
}
}
|