security_events: Fix error caused by DTD validation error
[asterisk/asterisk.git] / res / res_pjsip_transport_websocket.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jason Parker <jparker@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \brief WebSocket transport module
21  */
22
23 /*** MODULEINFO
24         <depend>pjproject</depend>
25         <depend>res_pjsip</depend>
26         <depend>res_http_websocket</depend>
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include <pjsip.h>
33 #include <pjsip_ua.h>
34
35 #include "asterisk/module.h"
36 #include "asterisk/http_websocket.h"
37 #include "asterisk/res_pjsip.h"
38 #include "asterisk/res_pjsip_session.h"
39 #include "asterisk/taskprocessor.h"
40
41 static int transport_type_ws;
42 static int transport_type_wss;
43
44 /*!
45  * \brief Wrapper for pjsip_transport, for storing the WebSocket session
46  */
47 struct ws_transport {
48         pjsip_transport transport;
49         pjsip_rx_data rdata;
50         struct ast_websocket *ws_session;
51 };
52
53 /*!
54  * \brief Send a message over the WebSocket connection.
55  *
56  * Called by pjsip transport manager.
57  */
58 static pj_status_t ws_send_msg(pjsip_transport *transport,
59                             pjsip_tx_data *tdata,
60                             const pj_sockaddr_t *rem_addr,
61                             int addr_len,
62                             void *token,
63                             pjsip_transport_callback callback)
64 {
65         struct ws_transport *wstransport = (struct ws_transport *)transport;
66
67         if (ast_websocket_write(wstransport->ws_session, AST_WEBSOCKET_OPCODE_TEXT, tdata->buf.start, (int)(tdata->buf.cur - tdata->buf.start))) {
68                 return PJ_EUNKNOWN;
69         }
70
71         return PJ_SUCCESS;
72 }
73
74 /*!
75  * \brief Destroy the pjsip transport.
76  *
77  * Called by pjsip transport manager.
78  */
79 static pj_status_t ws_destroy(pjsip_transport *transport)
80 {
81         struct ws_transport *wstransport = (struct ws_transport *)transport;
82
83         if (wstransport->transport.ref_cnt) {
84                 pj_atomic_destroy(wstransport->transport.ref_cnt);
85         }
86
87         if (wstransport->transport.lock) {
88                 pj_lock_destroy(wstransport->transport.lock);
89         }
90
91         pjsip_endpt_release_pool(wstransport->transport.endpt, wstransport->transport.pool);
92
93         return PJ_SUCCESS;
94 }
95
96 static int transport_shutdown(void *data)
97 {
98         pjsip_transport *transport = data;
99
100         pjsip_transport_shutdown(transport);
101         return 0;
102 }
103
104 struct transport_create_data {
105         struct ws_transport *transport;
106         struct ast_websocket *ws_session;
107 };
108
109 /*!
110  * \brief Create a pjsip transport.
111  */
112 static int transport_create(void *data)
113 {
114         struct transport_create_data *create_data = data;
115         struct ws_transport *newtransport;
116
117         pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
118         struct pjsip_tpmgr *tpmgr = pjsip_endpt_get_tpmgr(endpt);
119
120         pj_pool_t *pool;
121
122         pj_str_t buf;
123
124         if (!(pool = pjsip_endpt_create_pool(endpt, "ws", 512, 512))) {
125                 ast_log(LOG_ERROR, "Failed to allocate WebSocket endpoint pool.\n");
126                 return -1;
127         }
128
129         if (!(newtransport = PJ_POOL_ZALLOC_T(pool, struct ws_transport))) {
130                 ast_log(LOG_ERROR, "Failed to allocate WebSocket transport.\n");
131                 pjsip_endpt_release_pool(endpt, pool);
132                 return -1;
133         }
134
135         newtransport->ws_session = create_data->ws_session;
136
137         pj_atomic_create(pool, 0, &newtransport->transport.ref_cnt);
138         pj_lock_create_recursive_mutex(pool, pool->obj_name, &newtransport->transport.lock);
139
140         newtransport->transport.pool = pool;
141         pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, ast_sockaddr_stringify(ast_websocket_remote_address(newtransport->ws_session))), &newtransport->transport.key.rem_addr);
142         newtransport->transport.key.rem_addr.addr.sa_family = pj_AF_INET();
143         newtransport->transport.key.type = ast_websocket_is_secure(newtransport->ws_session) ? transport_type_wss : transport_type_ws;
144
145         newtransport->transport.addr_len = pj_sockaddr_get_len(&newtransport->transport.key.rem_addr);
146
147         pj_sockaddr_cp(&newtransport->transport.local_addr, &newtransport->transport.key.rem_addr);
148
149         newtransport->transport.local_name.host.ptr = (char *)pj_pool_alloc(pool, newtransport->transport.addr_len+4);
150         pj_sockaddr_print(&newtransport->transport.key.rem_addr, newtransport->transport.local_name.host.ptr, newtransport->transport.addr_len+4, 0);
151         newtransport->transport.local_name.host.slen = pj_ansi_strlen(newtransport->transport.local_name.host.ptr);
152         newtransport->transport.local_name.port = pj_sockaddr_get_port(&newtransport->transport.key.rem_addr);
153
154         newtransport->transport.type_name = (char *)pjsip_transport_get_type_name(newtransport->transport.key.type);
155         newtransport->transport.flag = pjsip_transport_get_flag_from_type((pjsip_transport_type_e)newtransport->transport.key.type);
156         newtransport->transport.info = (char *)pj_pool_alloc(newtransport->transport.pool, 64);
157
158         newtransport->transport.endpt = endpt;
159         newtransport->transport.tpmgr = tpmgr;
160         newtransport->transport.send_msg = &ws_send_msg;
161         newtransport->transport.destroy = &ws_destroy;
162
163         pjsip_transport_register(newtransport->transport.tpmgr, (pjsip_transport *)newtransport);
164
165         create_data->transport = newtransport;
166         return 0;
167 }
168
169 struct transport_read_data {
170         struct ws_transport *transport;
171         char *payload;
172         uint64_t payload_len;
173 };
174
175 /*!
176  * \brief Pass WebSocket data into pjsip transport manager.
177  */
178 static int transport_read(void *data)
179 {
180         struct transport_read_data *read_data = data;
181         struct ws_transport *newtransport = read_data->transport;
182         struct ast_websocket *session = newtransport->ws_session;
183
184         pjsip_rx_data *rdata = &newtransport->rdata;
185         int recvd;
186         pj_str_t buf;
187
188         rdata->tp_info.pool = newtransport->transport.pool;
189         rdata->tp_info.transport = &newtransport->transport;
190
191         pj_gettimeofday(&rdata->pkt_info.timestamp);
192
193         pj_memcpy(rdata->pkt_info.packet, read_data->payload, sizeof(rdata->pkt_info.packet));
194         rdata->pkt_info.len = read_data->payload_len;
195         rdata->pkt_info.zero = 0;
196
197         pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, ast_sockaddr_stringify(ast_websocket_remote_address(session))), &rdata->pkt_info.src_addr);
198         rdata->pkt_info.src_addr.addr.sa_family = pj_AF_INET();
199
200         rdata->pkt_info.src_addr_len = sizeof(rdata->pkt_info.src_addr);
201
202         pj_ansi_strcpy(rdata->pkt_info.src_name, ast_sockaddr_stringify_host(ast_websocket_remote_address(session)));
203         rdata->pkt_info.src_port = ast_sockaddr_port(ast_websocket_remote_address(session));
204
205         recvd = pjsip_tpmgr_receive_packet(rdata->tp_info.transport->tpmgr, rdata);
206
207         return (read_data->payload_len == recvd) ? 0 : -1;
208 }
209
210 /*!
211  \brief WebSocket connection handler.
212  */
213 static void websocket_cb(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
214 {
215         struct ast_taskprocessor *serializer = NULL;
216         struct transport_create_data create_data;
217         struct ws_transport *transport = NULL;
218         struct transport_read_data read_data;
219
220         if (ast_websocket_set_nonblock(session)) {
221                 ast_websocket_unref(session);
222                 return;
223         }
224
225         if (!(serializer = ast_sip_create_serializer())) {
226                 ast_websocket_unref(session);
227                 return;
228         }
229
230         create_data.ws_session = session;
231
232         if (ast_sip_push_task_synchronous(serializer, transport_create, &create_data)) {
233                 ast_log(LOG_ERROR, "Could not create WebSocket transport.\n");
234                 ast_websocket_unref(session);
235                 return;
236         }
237
238         transport = create_data.transport;
239         read_data.transport = transport;
240
241         while (ast_wait_for_input(ast_websocket_fd(session), -1) > 0) {
242                 enum ast_websocket_opcode opcode;
243                 int fragmented;
244
245                 if (ast_websocket_read(session, &read_data.payload, &read_data.payload_len, &opcode, &fragmented)) {
246                         break;
247                 }
248
249                 if (opcode == AST_WEBSOCKET_OPCODE_TEXT || opcode == AST_WEBSOCKET_OPCODE_BINARY) {
250                         ast_sip_push_task_synchronous(serializer, transport_read, &read_data);
251                 } else if (opcode == AST_WEBSOCKET_OPCODE_CLOSE) {
252                         break;
253                 }
254         }
255
256         ast_sip_push_task_synchronous(serializer, transport_shutdown, transport);
257
258         ast_taskprocessor_unreference(serializer);
259         ast_websocket_unref(session);
260 }
261
262 /*!
263  * \brief Store the transport a message came in on, so it can be used for outbound messages to that contact.
264  */
265 static pj_bool_t websocket_on_rx_msg(pjsip_rx_data *rdata)
266 {
267         pjsip_contact_hdr *contact;
268
269         long type = rdata->tp_info.transport->key.type;
270
271         if (type != (long)transport_type_ws && type != (long)transport_type_wss) {
272                 return PJ_FALSE;
273         }
274
275         if ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL)) &&
276                 (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))) {
277                 pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
278
279                 pj_cstr(&uri->host, rdata->pkt_info.src_name);
280                 uri->port = rdata->pkt_info.src_port;
281         }
282
283         rdata->msg_info.via->rport_param = 0;
284
285         return PJ_FALSE;
286 }
287
288 static pjsip_module websocket_module = {
289         .name = { "WebSocket Transport Module", 26 },
290         .id = -1,
291         .priority = PJSIP_MOD_PRIORITY_TRANSPORT_LAYER,
292         .on_rx_request = websocket_on_rx_msg,
293 };
294
295 static int load_module(void)
296 {
297         pjsip_transport_register_type(PJSIP_TRANSPORT_RELIABLE, "WS", 5060, &transport_type_ws);
298         pjsip_transport_register_type(PJSIP_TRANSPORT_RELIABLE, "WSS", 5060, &transport_type_wss);
299
300         if (ast_sip_register_service(&websocket_module) != PJ_SUCCESS) {
301                 return AST_MODULE_LOAD_DECLINE;
302         }
303
304         if (ast_websocket_add_protocol("sip", websocket_cb)) {
305                 ast_sip_unregister_service(&websocket_module);
306                 return AST_MODULE_LOAD_DECLINE;
307         }
308
309         return AST_MODULE_LOAD_SUCCESS;
310 }
311
312 static int unload_module(void)
313 {
314         ast_sip_unregister_service(&websocket_module);
315         ast_websocket_remove_protocol("sip", websocket_cb);
316
317         return 0;
318 }
319
320 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP WebSocket Transport Support",
321                 .load = load_module,
322                 .unload = unload_module,
323                 .load_pri = AST_MODPRI_APP_DEPEND,
324            );