summaryrefslogtreecommitdiffstats
path: root/src/parser/parse_ctrl.cpp
blob: 55ff9af14a9a5531becf4421680b35c12288d28c (plain)
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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program 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.

    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
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "parse_ctrl.h"
#include "protocol.h"
#include "util.h"
#include "audits/memman.h"

// Interface to Bison
extern int yyparse(void);

// Interface to Flex
struct yy_buffer_state;
extern struct yy_buffer_state *yy_scan_string(const char *);
extern void yy_delete_buffer(struct yy_buffer_state *);

t_mutex t_parser::mtx_parser;
bool t_parser::check_max_forwards = true;
bool t_parser::compact_headers = false;
bool t_parser::multi_values_as_list = true;
int t_parser::comment_level = 0;
list<string> t_parser::parse_errors;

string t_parser::unfold(const string &h) {
	string::size_type i;
	string s = h;

	while ((i = s.find("\r\n ")) != string::npos) {
		s.replace(i, 3, " ");
	}

	while ((i = s.find("\r\n\t")) != string::npos) {
		s.replace(i, 3, " ");
	}

	// This is only for easy testing of hand edited messages
	// in Linux where the end of line character is \n only.
	while ((i = s.find("\n ")) != string::npos) {
		s.replace(i, 2, " ");
	}

	while ((i = s.find("\n\t")) != string::npos) {
		s.replace(i, 2, " ");
	}

	return s;
}

t_parser::t_context t_parser::context = t_parser::X_INITIAL;
t_sip_message *t_parser::msg = NULL;

t_sip_message *t_parser::parse(const string &s, list<string> &parse_errors_) {
	t_mutex_guard guard(mtx_parser);
	
	int ret;
	struct yy_buffer_state *b;
	msg = NULL;

	parse_errors.clear();

	string x = unfold(s);

	b = yy_scan_string(x.c_str());
	ret = yyparse();
	yy_delete_buffer(b);

	if (ret != 0) {
		if (msg) {
			MEMMAN_DELETE(msg);
			delete msg;
			msg = NULL;
		}
		throw ret;
	}

	parse_errors_ = parse_errors;
	return msg;
}

t_sip_message *t_parser::parse_headers(const string &s, list<string> &parse_errors_) {
	string msg("INVITE sip:fake@fake.invalid SIP/2.0");
	msg += CRLF;
	
	list<t_parameter> hdr_list = str2param_list(s);
	for (list<t_parameter>::iterator i = hdr_list.begin();
	     i != hdr_list.end(); i++)
	{
		msg += unescape_hex(i->name);
		msg += ": ";
		msg += unescape_hex(i->value);
		msg += CRLF;
	}
	
	msg += CRLF;
	
	return parse(msg, parse_errors_);
}

void t_parser::enter_ctx_comment(void) {
	comment_level = 0;
	context = t_parser::X_COMMENT;
}

void t_parser::inc_comment_level(void) {
	comment_level++;
}

bool t_parser::dec_comment_level(void) {
	if (comment_level == 0) return false;
	comment_level--;
	return true;
}

void t_parser::add_header_error(const string &header_name) {
	string s = "Parse error in header: " + header_name;
	parse_errors.push_back(s);
}

t_syntax_error::t_syntax_error(const string &e) {
	error = e;
}