summaryrefslogtreecommitdiffstats
path: root/src/parser/hdr_via.cpp
blob: 12da66dba3c8cff1bcc4f24b05c4c049e40e3ea9 (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
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
/*
    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 <iostream>
#include "definitions.h"
#include "hdr_via.h"
#include "util.h"
#include "parse_ctrl.h"
#include "protocol.h"
#include "sockets/url.h"

t_via::t_via() {
	port = 0;
	ttl = 0;
	rport_present = false;
	rport = 0;
}

t_via::t_via(const string &_host, const int _port, bool add_rport) {
	protocol_name = "SIP";
	protocol_version = SIP_VERSION;
	transport = "UDP";
	host = _host;
	branch = RFC3261_COOKIE + random_token(8);

	if (_port != get_default_port("sip")) {
		port = _port;
	} else {
		port = 0;
	}

	ttl = 0;
	rport_present = add_rport;
	rport = 0;
}

void t_via::add_extension(const t_parameter &p) {
	extensions.push_back(p);
}

string t_via::encode(void) const {
	string s;

	s = protocol_name + '/' + protocol_version + '/' + transport;
	s += ' ';
	s += host;

	if (port > 0) {
		s += ':';
		s += int2str(port);
	}

	if (ttl > 0) s += int2str(ttl, ";ttl=%d");

	if (maddr.size() > 0) {
		s += ";maddr=";
		s += maddr;
	}

	if (received.size() > 0) {
		s += ";received=";
		s += received;
	}

	if (rport_present) {
		s += ";rport";
		if (rport > 0) {
			s += "=";
			s += int2str(rport);
		}
	}

	if (branch.size() > 0) {
		s += ";branch=";
		s += branch;
	}

	s += param_list2str(extensions);
	return s;
}

void t_via::get_response_dst(t_ip_port &ip_port) const {
	string url_str("sip:");

	// RFC 3261 18.2.2
	// Determine the address to send a repsonse to
	// NOTE: the received-parameter will be added by the listener if needed.
	
	if (tolower(transport) == "tcp") {
		// NOTE: The response must be sent over the connection on which
		// the request was received. The address returned here is an
		// alternative if that connection is closed already.
		if (received.size() > 0) {
			url_str += received;
		} else {
			url_str += host;
		}
			
		url_str += ":";
		
		// NOTE: The rport parameter is not processed here as it only
		// applies to unreliable transports (RFC 3581 4)
		url_str += int2str(port);
		
		t_url u(url_str);
		list<t_ip_port> ip_list = u.get_h_ip_srv("tcp");
		ip_port = ip_list.front();
	} else {
		if (maddr.size() > 0) {
			url_str += maddr;
		} else if (received.size() > 0) {
			url_str += received;
		} else {
			url_str += host;
		}
	
		// RFC 3581 4
		if (rport_present && rport > 0) {
			// NOTE: the rport value will be added by the UDP listener
			// if the rport parameter without value was present.
			url_str += ':';
			url_str += int2str(rport);
		} else if (port != 0) {
			url_str += ':';
			url_str += int2str(port);
		}
		
		// If there was no maddr parameter, then the URL will always point to
		// an IP address; either the host was an IP address or a received parameter
		// containing an IP address was added (see RFC 3261 18.2.1)
		// If there was an maddr, then the URL can be a domain that could have
		// multiple SRV records. RFC 3263 section 5 does not specify what to do in
		// this case. So just send the response to the first destination.
		t_url u(url_str);
		list<t_ip_port> ip_list = u.get_h_ip_srv("udp");
		ip_port = ip_list.front();
	}
}

bool t_via::rfc3261_compliant(void) const {
	return (branch.find(RFC3261_COOKIE) == 0);
}


t_hdr_via::t_hdr_via() : t_header("Via", "v") {}

void t_hdr_via::add_via(const t_via &v) {
	populated = true;
	via_list.push_back(v);
}

string t_hdr_via::encode(void) const {
	return (t_parser::multi_values_as_list ? 
			t_header::encode() : encode_multi_header());
}

string t_hdr_via::encode_multi_header(void) const {
	string s;

	if (!populated) return s;

	for (list<t_via>::const_iterator i = via_list.begin();
	     i != via_list.end(); i++)
	{
		s += (t_parser::compact_headers ? compact_name : header_name);
		s += ": ";
		s += i->encode();
		s += CRLF;
	}

	return s;
}

string t_hdr_via::encode_value(void) const {
	string s;

	if (!populated) return s;

	for (list<t_via>::const_iterator i = via_list.begin();
	     i != via_list.end(); i++)
	{
		if (i != via_list.begin()) s += ",";
		s += i->encode();
	}

	return s;
}

void t_hdr_via::get_response_dst(t_ip_port &ip_port) const {
	if (!populated) {
		ip_port.clear();
		return;
	}

	via_list.front().get_response_dst(ip_port);
}