Repository navigation
Expand file tree
/
Copy pathstunuri.c
More file actions
222 lines (179 loc) · 4.74 KB
/
Copy pathstunuri.c
File metadata and controls
222 lines (179 loc) · 4.74 KB
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
/**
* @file stunuri.c URI Scheme for STUN/TURN Protocol
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <re.h>
#include <baresip.h>
#include "core.h"
/*
https://tools.ietf.org/html/rfc7064
https://tools.ietf.org/html/rfc7065
+-----------------------+
| URI |
+-----------------------+
| stun:example.org |
| stuns:example.org |
| stun:example.org:8000 |
+-----------------------+
+---------------------------------+----------+--------+-------------+
| URI | <secure> | <port> | <transport> |
+---------------------------------+----------+--------+-------------+
| turn:example.org | false | | |
| turns:example.org | true | | |
| turn:example.org:8000 | false | 8000 | |
| turn:example.org?transport=udp | false | | UDP |
| turn:example.org?transport=tcp | false | | TCP |
| turns:example.org?transport=tcp | true | | TLS |
+---------------------------------+----------+--------+-------------+
*/
static void destructor(void *arg)
{
struct stun_uri *su = arg;
mem_deref(su->host);
}
/**
* Decode a STUN uri from a string
*
* @param sup Pointer to allocated STUN uri
* @param pl Pointer-length string
*
* @return 0 if success, otherwise errorcode
*/
int stunuri_decode(struct stun_uri **sup, const struct pl *pl)
{
struct uri uri;
int err;
if (!sup || !pl)
return EINVAL;
err = uri_decode(&uri, pl);
if (err) {
warning("stunuri: decode '%r' failed (%m)\n", pl, err);
return err;
}
err = stunuri_decode_uri(sup, &uri);
return err;
}
/**
* Decode a STUN uri from URI object
*
* @param sup Pointer to allocated STUN uri
* @param uri URI object
*
* @return 0 if success, otherwise errorcode
*/
int stunuri_decode_uri(struct stun_uri **sup, const struct uri *uri)
{
struct stun_uri *su;
enum stun_scheme scheme;
struct pl tp;
int proto = IPPROTO_UDP;
int err;
if (!sup || !uri)
return EINVAL;
if (0 == pl_strcasecmp(&uri->scheme, "stun"))
scheme = STUN_SCHEME_STUN;
else if (0 == pl_strcasecmp(&uri->scheme, "stuns"))
scheme = STUN_SCHEME_STUNS;
else if (0 == pl_strcasecmp(&uri->scheme, "turn"))
scheme = STUN_SCHEME_TURN;
else if (0 == pl_strcasecmp(&uri->scheme, "turns"))
scheme = STUN_SCHEME_TURNS;
else {
warning("stunuri: scheme not supported (%r)\n", &uri->scheme);
return ENOTSUP;
}
if (0 == re_regex(uri->headers.p, uri->headers.l,
"transport=[a-z]+", &tp)) {
if (0 == pl_strcasecmp(&tp, "udp"))
proto = IPPROTO_UDP;
else if (0 == pl_strcasecmp(&tp, "tcp"))
proto = IPPROTO_TCP;
else {
warning("stunuri: unsupported transport '%r'\n", &tp);
return EPROTONOSUPPORT;
}
}
if (pl_isset(&uri->password)) {
warning("The \"user:password\" format in the stunserver"
" userinfo field is deprecated.\n");
}
su = mem_zalloc(sizeof(*su), destructor);
if (!su)
return ENOMEM;
su->scheme = scheme;
err = pl_strdup(&su->host, &uri->host);
su->port = uri->port;
su->proto = proto;
if (err)
mem_deref(su);
else
*sup = su;
return err;
}
/**
* Set the hostname on a STUN uri
*
* @param su STUN uri
* @param host Hostname to set
*
* @return 0 if success, otherwise errorcode
*/
int stunuri_set_host(struct stun_uri *su, const char *host)
{
if (!su || !host)
return EINVAL;
su->host = mem_deref(su->host);
return str_dup(&su->host, host);
}
/**
* Set the port number on a STUN uri
*
* @param su STUN uri
* @param port Port number to set
*
* @return 0 if success, otherwise errorcode
*/
int stunuri_set_port(struct stun_uri *su, uint16_t port)
{
if (!su)
return EINVAL;
su->port = port;
return 0;
}
/**
* Print a STUN uri
*
* @param pf Print function
* @param su STUN uri
*
* @return 0 if success, otherwise errorcode
*/
int stunuri_print(struct re_printf *pf, const struct stun_uri *su)
{
int err = 0;
if (!su)
return 0;
err |= re_hprintf(pf, "scheme=%s", stunuri_scheme_name(su->scheme));
err |= re_hprintf(pf, " host='%s'", su->host);
err |= re_hprintf(pf, " port=%u", su->port);
err |= re_hprintf(pf, " proto=%s", net_proto2name(su->proto));
return err;
}
/**
* Get the name of the STUN scheme
*
* @param scheme STUN scheme
*
* @return String with STUN scheme
*/
const char *stunuri_scheme_name(enum stun_scheme scheme)
{
switch (scheme) {
case STUN_SCHEME_STUN: return "stun";
case STUN_SCHEME_STUNS: return "stuns";
case STUN_SCHEME_TURN: return "turn";
case STUN_SCHEME_TURNS: return "turns";
default: return "?";
}
}