summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nICEr/src/stun/stun_msg.c
blob: 7e01686109cd3e620af454dee5987538e2a01b7f (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <errno.h>
#include <csi_platform.h>

#ifdef WIN32
#include <winsock2.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#else   /* UNIX */
#include <string.h>
#endif  /* end UNIX */
#include <assert.h>

#include "stun.h"


int
nr_stun_message_create(nr_stun_message **msg)
{
    int _status;
    nr_stun_message *m = 0;

    m = RCALLOC(sizeof(*m));
    if (!m)
        ABORT(R_NO_MEMORY);

    TAILQ_INIT(&m->attributes);

    *msg = m;

    _status=0;
  abort:
    return(_status);
}

int
nr_stun_message_create2(nr_stun_message **msg, UCHAR *buffer, size_t length)
{
    int r,_status;
    nr_stun_message *m = 0;

    if (length > sizeof(m->buffer)) {
        ABORT(R_BAD_DATA);
    }

    if ((r=nr_stun_message_create(&m)))
        ABORT(r);

    memcpy(m->buffer, buffer, length);
    m->length = length;

    *msg = m;

    _status=0;
  abort:
    return(_status);
}

int
nr_stun_message_destroy(nr_stun_message **msg)
{
    int _status;
    nr_stun_message_attribute_head *attrs;
    nr_stun_message_attribute *attr;

    if (msg && *msg) {
        attrs = &(*msg)->attributes;
        while (!TAILQ_EMPTY(attrs)) {
            attr = TAILQ_FIRST(attrs);
            nr_stun_message_attribute_destroy(*msg, &attr);
        }

        RFREE(*msg);

        *msg = 0;
    }

    _status=0;
/*  abort: */
    return(_status);
}

int
nr_stun_message_attribute_create(nr_stun_message *msg, nr_stun_message_attribute **attr)
{
    int _status;
    nr_stun_message_attribute *a = 0;

    a = RCALLOC(sizeof(*a));
    if (!a)
        ABORT(R_NO_MEMORY);

    TAILQ_INSERT_TAIL(&msg->attributes, a, entry);

    *attr = a;

    _status=0;
  abort:
    return(_status);
}

int
nr_stun_message_attribute_destroy(nr_stun_message *msg, nr_stun_message_attribute **attr)
{
    int _status;
    nr_stun_message_attribute *a = 0;

    if (attr && *attr) {
        a = *attr;
        TAILQ_REMOVE(&msg->attributes, a, entry);

        RFREE(a);

        *attr = 0;
    }

    _status=0;
/*  abort: */
    return(_status);
}

int
nr_stun_message_has_attribute(nr_stun_message *msg, UINT2 type, nr_stun_message_attribute **attribute)
{
  nr_stun_message_attribute *attr = 0;
  nr_stun_message_get_attribute(msg, type, 0, &attr);

  if (attribute)
    *attribute = attr;

  return attr ? 1 : 0;
}

int
nr_stun_message_get_attribute(nr_stun_message *msg, UINT2 type, UINT2 index, nr_stun_message_attribute **attribute)
{
  nr_stun_message_attribute *attr;
  TAILQ_FOREACH(attr, &msg->attributes, entry) {
    if (attr->type == type && !attr->invalid) {
      if (!index) {
        *attribute = attr;
        return 0;
      }
      --index;
    }
  }
  *attribute = 0;
  return R_NOT_FOUND;
}

#define NR_STUN_MESSAGE_ADD_ATTRIBUTE(__type, __code) \
  { \
    int r,_status; \
    nr_stun_message_attribute *attr = 0; \
    if ((r=nr_stun_message_attribute_create(msg, &attr))) \
        ABORT(r); \
    attr->type = (__type); \
    { __code } \
    _status=0; \
  abort: \
    if (_status){ \
      nr_stun_message_attribute_destroy(msg, &attr); \
    } \
    return(_status); \
  }


int
nr_stun_message_add_alternate_server_attribute(nr_stun_message *msg, nr_transport_addr *alternate_server)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_ALTERNATE_SERVER,
    {
        if ((r=nr_transport_addr_copy(&attr->u.alternate_server, alternate_server)))
            ABORT(r);
    }
)

int
nr_stun_message_add_error_code_attribute(nr_stun_message *msg, UINT2 number, char *reason)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_ERROR_CODE,
    {
        attr->u.error_code.number = number;
        (void)strlcpy(attr->u.error_code.reason, reason, sizeof(attr->u.error_code.reason));
    }
)

int
nr_stun_message_add_fingerprint_attribute(nr_stun_message *msg)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_FINGERPRINT,
    {}
)

int
nr_stun_message_add_message_integrity_attribute(nr_stun_message *msg, Data *password)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_MESSAGE_INTEGRITY,
    {
        if (sizeof(attr->u.message_integrity.password) < password->len)
            ABORT(R_BAD_DATA);

        memcpy(attr->u.message_integrity.password, password->data, password->len);
        attr->u.message_integrity.passwordlen = password->len;
    }
)

int
nr_stun_message_add_nonce_attribute(nr_stun_message *msg, char *nonce)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_NONCE,
    { (void)strlcpy(attr->u.nonce, nonce, sizeof(attr->u.nonce)); }
)

int
nr_stun_message_add_realm_attribute(nr_stun_message *msg, char *realm)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_REALM,
    { (void)strlcpy(attr->u.realm, realm, sizeof(attr->u.realm)); }
)

int
nr_stun_message_add_server_attribute(nr_stun_message *msg, char *server_name)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_SERVER,
    { (void)strlcpy(attr->u.server_name, server_name, sizeof(attr->u.server_name)); }
)

int
nr_stun_message_add_unknown_attributes_attribute(nr_stun_message *msg, nr_stun_attr_unknown_attributes *unknown_attributes)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_UNKNOWN_ATTRIBUTES,
    { memcpy(&attr->u.unknown_attributes, unknown_attributes, sizeof(attr->u.unknown_attributes)); }
)

int
nr_stun_message_add_username_attribute(nr_stun_message *msg, char *username)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_USERNAME,
    { (void)strlcpy(attr->u.username, username, sizeof(attr->u.username)); }
)

int
nr_stun_message_add_requested_transport_attribute(nr_stun_message *msg, UCHAR protocol)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_REQUESTED_TRANSPORT,
    { attr->u.requested_transport = protocol; }
)

int
nr_stun_message_add_xor_mapped_address_attribute(nr_stun_message *msg, nr_transport_addr *mapped_address)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_XOR_MAPPED_ADDRESS,
    {
        if ((r=nr_transport_addr_copy(&attr->u.xor_mapped_address.unmasked, mapped_address)))
            ABORT(r);
    }
)

int
nr_stun_message_add_xor_peer_address_attribute(nr_stun_message *msg, nr_transport_addr *peer_address)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_XOR_PEER_ADDRESS,
    {
        if ((r=nr_transport_addr_copy(&attr->u.xor_mapped_address.unmasked, peer_address)))
            ABORT(r);
    }
)

#ifdef USE_ICE
int
nr_stun_message_add_ice_controlled_attribute(nr_stun_message *msg, UINT8 ice_controlled)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_ICE_CONTROLLED,
    { attr->u.ice_controlled = ice_controlled; }
)

int
nr_stun_message_add_ice_controlling_attribute(nr_stun_message *msg, UINT8 ice_controlling)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_ICE_CONTROLLING,
    { attr->u.ice_controlling = ice_controlling; }
)

int
nr_stun_message_add_priority_attribute(nr_stun_message *msg, UINT4 priority)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_PRIORITY,
    { attr->u.priority = priority; }
)

int
nr_stun_message_add_use_candidate_attribute(nr_stun_message *msg)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_USE_CANDIDATE,
    {}
)
#endif /* USE_ICE */

#ifdef USE_TURN
int
nr_stun_message_add_data_attribute(nr_stun_message *msg, UCHAR *data, int length)

NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_DATA,
    {
      if (length > NR_STUN_MAX_MESSAGE_SIZE)
        ABORT(R_BAD_ARGS);

      memcpy(attr->u.data.data, data, length);
      attr->u.data.length=length;
    }
)

int
nr_stun_message_add_lifetime_attribute(nr_stun_message *msg, UINT4 lifetime_secs)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_LIFETIME,
    { attr->u.lifetime_secs = lifetime_secs; }
)

#endif /* USE_TURN */

#ifdef USE_STUND_0_96
int
nr_stun_message_add_change_request_attribute(nr_stun_message *msg, UINT4 change_request)
NR_STUN_MESSAGE_ADD_ATTRIBUTE(
    NR_STUN_ATTR_OLD_CHANGE_REQUEST,
    { attr->u.change_request = change_request; }
)
#endif /* USE_STUND_0_96 */