/src/coturn/src/apps/common/stun_buffer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011, 2012, 2013 Citrix Systems |
3 | | * |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * 3. Neither the name of the project nor the names of its contributors |
15 | | * may be used to endorse or promote products derived from this software |
16 | | * without specific prior written permission. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
19 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
22 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 | | * SUCH DAMAGE. |
29 | | */ |
30 | | |
31 | | #include "stun_buffer.h" |
32 | | |
33 | | ////////////////////// BUFFERS /////////////////////////// |
34 | | |
35 | 0 | int stun_init_buffer(stun_buffer *buf) { |
36 | 0 | if (!buf) |
37 | 0 | return -1; |
38 | 0 | memset(buf->buf, 0, sizeof(buf->buf)); |
39 | 0 | buf->len = 0; |
40 | 0 | buf->offset = 0; |
41 | 0 | buf->coffset = 0; |
42 | 0 | return 0; |
43 | 0 | } |
44 | | |
45 | 0 | int stun_get_size(const stun_buffer *buf) { |
46 | 0 | if (!buf) |
47 | 0 | return 0; |
48 | 0 | return sizeof(buf->buf); |
49 | 0 | } |
50 | | |
51 | | //////////////////////////////////////////////////////////// |
52 | | |
53 | 0 | void stun_tid_from_message(const stun_buffer *buf, stun_tid *id) { |
54 | 0 | stun_tid_from_message_str(buf->buf, (size_t)(buf->len), id); |
55 | 0 | } |
56 | | |
57 | 0 | void stun_tid_generate_in_message(stun_buffer *buf, stun_tid *id) { |
58 | 0 | if (buf) { |
59 | 0 | stun_tid_generate_in_message_str(buf->buf, id); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | //////////////////////////////////////////////////////// |
64 | | |
65 | 0 | static inline int is_channel_msg(const stun_buffer *buf) { |
66 | 0 | if (buf && buf->len > 0) { |
67 | 0 | return is_channel_msg_str(buf->buf, (size_t)(buf->len)); |
68 | 0 | } |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | 206 | int stun_is_command_message(const stun_buffer *buf) { |
73 | 206 | if (!buf || buf->len <= 0) |
74 | 0 | return 0; |
75 | 206 | else |
76 | 206 | return stun_is_command_message_str(buf->buf, (size_t)(buf->len)); |
77 | 206 | } |
78 | | |
79 | 0 | int stun_is_request(const stun_buffer *buf) { return stun_is_request_str(buf->buf, (size_t)buf->len); } |
80 | | |
81 | 51 | int stun_is_success_response(const stun_buffer *buf) { |
82 | 51 | return stun_is_success_response_str(buf->buf, (size_t)(buf->len)); |
83 | 51 | } |
84 | | |
85 | 0 | int stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_msg, size_t err_msg_size) { |
86 | 0 | return stun_is_error_response_str(buf->buf, (size_t)(buf->len), err_code, err_msg, err_msg_size); |
87 | 0 | } |
88 | | |
89 | 57 | int stun_is_response(const stun_buffer *buf) { return stun_is_response_str(buf->buf, (size_t)(buf->len)); } |
90 | | |
91 | 0 | int stun_is_indication(const stun_buffer *buf) { |
92 | 0 | if (is_channel_msg(buf)) |
93 | 0 | return 0; |
94 | 0 | return IS_STUN_INDICATION(stun_get_msg_type(buf)); |
95 | 0 | } |
96 | | |
97 | 0 | uint16_t stun_get_method(const stun_buffer *buf) { return stun_get_method_str(buf->buf, (size_t)(buf->len)); } |
98 | | |
99 | 0 | uint16_t stun_get_msg_type(const stun_buffer *buf) { |
100 | 0 | if (!buf) |
101 | 0 | return (uint16_t)-1; |
102 | 0 | return stun_get_msg_type_str(buf->buf, (size_t)buf->len); |
103 | 0 | } |
104 | | |
105 | | //////////////////////////////////////////////////////////// |
106 | | |
107 | 0 | static void stun_init_command(uint16_t message_type, stun_buffer *buf) { |
108 | 0 | buf->len = stun_get_size(buf); |
109 | 0 | stun_init_command_str(message_type, buf->buf, (size_t *)(&(buf->len))); |
110 | 0 | } |
111 | | |
112 | 0 | void stun_init_request(uint16_t method, stun_buffer *buf) { stun_init_command(stun_make_request(method), buf); } |
113 | | |
114 | 0 | void stun_init_indication(uint16_t method, stun_buffer *buf) { stun_init_command(stun_make_indication(method), buf); } |
115 | | |
116 | 0 | void stun_init_success_response(uint16_t method, stun_buffer *buf, stun_tid *id) { |
117 | 0 | buf->len = stun_get_size(buf); |
118 | 0 | stun_init_success_response_str(method, buf->buf, (size_t *)(&(buf->len)), id); |
119 | 0 | } |
120 | | |
121 | | void stun_init_error_response(uint16_t method, stun_buffer *buf, uint16_t error_code, const uint8_t *reason, |
122 | 0 | stun_tid *id) { |
123 | 0 | buf->len = stun_get_size(buf); |
124 | 0 | stun_init_error_response_str(method, buf->buf, (size_t *)(&(buf->len)), error_code, reason, id); |
125 | 0 | } |
126 | | |
127 | | /////////////////////////////////////////////////////////////////////////////// |
128 | | |
129 | 0 | int stun_get_command_message_len(const stun_buffer *buf) { |
130 | 0 | return stun_get_command_message_len_str(buf->buf, (size_t)(buf->len)); |
131 | 0 | } |
132 | | |
133 | | /////////////////////////////////////////////////////////////////////////////// |
134 | | |
135 | 0 | int stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, int do_padding) { |
136 | 0 | return stun_init_channel_message_str(chnumber, buf->buf, (size_t *)(&(buf->len)), length, do_padding); |
137 | 0 | } |
138 | | |
139 | 0 | int stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, int is_padding_mandatory) { |
140 | 0 | if (!buf) |
141 | 0 | return 0; |
142 | 0 | size_t blen = (size_t)buf->len; |
143 | 0 | int ret = stun_is_channel_message_str(buf->buf, &blen, chnumber, is_padding_mandatory); |
144 | 0 | if (ret) { |
145 | 0 | buf->len = blen; |
146 | 0 | } |
147 | 0 | return ret; |
148 | 0 | } |
149 | | |
150 | | /////////////////////////////////////////////////////////////////////////////// |
151 | | |
152 | | int stun_set_allocate_request(stun_buffer *buf, uint32_t lifetime, int af4, int af6, uint8_t transport, int mobile, |
153 | 0 | const char *rt, int ep) { |
154 | 0 | return stun_set_allocate_request_str(buf->buf, (size_t *)(&(buf->len)), lifetime, af4, af6, transport, mobile, rt, |
155 | 0 | ep); |
156 | 0 | } |
157 | | |
158 | | int stun_set_allocate_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *relayed_addr1, |
159 | | const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime, |
160 | | uint32_t max_lifetime, int error_code, const uint8_t *reason, uint64_t reservation_token, |
161 | 0 | char *mobile_id) { |
162 | |
|
163 | 0 | return stun_set_allocate_response_str(buf->buf, (size_t *)(&(buf->len)), tid, relayed_addr1, relayed_addr2, |
164 | 0 | reflexive_addr, lifetime, max_lifetime, error_code, reason, reservation_token, |
165 | 0 | mobile_id); |
166 | 0 | } |
167 | | |
168 | | /////////////////////////////////////////////////////////////////////////////// |
169 | | |
170 | 0 | uint16_t stun_set_channel_bind_request(stun_buffer *buf, const ioa_addr *peer_addr, uint16_t channel_number) { |
171 | |
|
172 | 0 | return stun_set_channel_bind_request_str(buf->buf, (size_t *)(&(buf->len)), peer_addr, channel_number); |
173 | 0 | } |
174 | | |
175 | 0 | void stun_set_channel_bind_response(stun_buffer *buf, stun_tid *tid, int error_code, const uint8_t *reason) { |
176 | 0 | stun_set_channel_bind_response_str(buf->buf, (size_t *)(&(buf->len)), tid, error_code, reason); |
177 | 0 | } |
178 | | |
179 | | //////////////////////////////////////////////////////////////// |
180 | | |
181 | 0 | stun_attr_ref stun_attr_get_first(const stun_buffer *buf) { |
182 | 0 | return stun_attr_get_first_str(buf->buf, (size_t)(buf->len)); |
183 | 0 | } |
184 | | |
185 | 0 | stun_attr_ref stun_attr_get_next(const stun_buffer *buf, stun_attr_ref prev) { |
186 | 0 | return stun_attr_get_next_str(buf->buf, (size_t)(buf->len), prev); |
187 | 0 | } |
188 | | |
189 | 0 | int stun_attr_add(stun_buffer *buf, uint16_t attr, const char *avalue, int alen) { |
190 | 0 | return stun_attr_add_str(buf->buf, (size_t *)(&(buf->len)), attr, (const uint8_t *)avalue, alen); |
191 | 0 | } |
192 | | |
193 | 0 | int stun_attr_add_channel_number(stun_buffer *buf, uint16_t chnumber) { |
194 | 0 | return stun_attr_add_channel_number_str(buf->buf, (size_t *)(&(buf->len)), chnumber); |
195 | 0 | } |
196 | | |
197 | 0 | int stun_attr_add_addr(stun_buffer *buf, uint16_t attr_type, const ioa_addr *ca) { |
198 | 0 | return stun_attr_add_addr_str(buf->buf, (size_t *)(&(buf->len)), attr_type, ca); |
199 | 0 | } |
200 | | |
201 | 0 | int stun_attr_get_addr(const stun_buffer *buf, stun_attr_ref attr, ioa_addr *ca, const ioa_addr *default_addr) { |
202 | |
|
203 | 0 | return stun_attr_get_addr_str(buf->buf, (size_t)(buf->len), attr, ca, default_addr); |
204 | 0 | } |
205 | | |
206 | 0 | int stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_addr *ca, const ioa_addr *default_addr) { |
207 | |
|
208 | 0 | return stun_attr_get_first_addr_str(buf->buf, (size_t)(buf->len), attr_type, ca, default_addr); |
209 | 0 | } |
210 | | |
211 | 0 | int stun_attr_add_even_port(stun_buffer *buf, uint8_t value) { |
212 | 0 | if (value) |
213 | 0 | value = 0x80; |
214 | 0 | return stun_attr_add(buf, STUN_ATTRIBUTE_EVEN_PORT, (const char *)&value, 1); |
215 | 0 | } |
216 | | |
217 | 0 | uint16_t stun_attr_get_first_channel_number(const stun_buffer *buf) { |
218 | 0 | return stun_attr_get_first_channel_number_str(buf->buf, (size_t)(buf->len)); |
219 | 0 | } |
220 | | |
221 | 0 | stun_attr_ref stun_attr_get_first_by_type(const stun_buffer *buf, uint16_t attr_type) { |
222 | 0 | return stun_attr_get_first_by_type_str(buf->buf, (size_t)(buf->len), attr_type); |
223 | 0 | } |
224 | | |
225 | | /////////////////////////////////////////////////////////////////////////////// |
226 | | |
227 | 0 | void stun_set_binding_request(stun_buffer *buf) { stun_set_binding_request_str(buf->buf, (size_t *)(&(buf->len))); } |
228 | | |
229 | | int stun_set_binding_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *reflexive_addr, int error_code, |
230 | 0 | const uint8_t *reason) { |
231 | 0 | return stun_set_binding_response_str(buf->buf, (size_t *)(&(buf->len)), tid, reflexive_addr, error_code, reason, 0, 0, |
232 | 0 | 1); |
233 | 0 | } |
234 | | |
235 | 0 | void stun_prepare_binding_request(stun_buffer *buf) { stun_set_binding_request_str(buf->buf, (size_t *)(&(buf->len))); } |
236 | | |
237 | 37 | int stun_is_binding_response(const stun_buffer *buf) { |
238 | 37 | return stun_is_binding_response_str(buf->buf, (size_t)(buf->len)); |
239 | 37 | } |
240 | | |
241 | | /////////////////////////////////////////////////////// |