1 /*
2 Copyright (c) 2007, Adobe Systems, Incorporated
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9 * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12 * Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15 
16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
17   contributors may be used to endorse or promote products derived from
18   this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 
34 
35 #ifndef _stun_msg_h
36 #define _stun_msg_h
37 
38 #include "csi_platform.h"
39 #include "nr_api.h"
40 #include "transport_addr.h"
41 
42 #define NR_STUN_MAX_USERNAME_BYTES                 513
43 #define NR_STUN_MAX_ERROR_CODE_REASON_BYTES        763
44 #define NR_STUN_MAX_ERROR_CODE_REASON_CHARS        128
45 #define NR_STUN_MAX_REALM_BYTES                    763
46 #define NR_STUN_MAX_REALM_CHARS                    128
47 #define NR_STUN_MAX_NONCE_BYTES                    763
48 #define NR_STUN_MAX_NONCE_CHARS                    128
49 #define NR_STUN_MAX_SERVER_BYTES                   763
50 #define NR_STUN_MAX_SERVER_CHARS                   128
51 #define NR_STUN_MAX_STRING_SIZE                    763  /* any possible string */
52 #define NR_STUN_MAX_UNKNOWN_ATTRIBUTES             16
53 #define NR_STUN_MAX_MESSAGE_SIZE                   2048
54 
55 #define NR_STUN_MAGIC_COOKIE            0x2112A442
56 #define NR_STUN_MAGIC_COOKIE2           0xc5cb4e1d  /* used recognize old stun messages */
57 
58 typedef struct { UCHAR octet[12]; }  UINT12;
59 
60 typedef struct nr_stun_attr_error_code_ {
61     UINT2     number;
62     char      reason[NR_STUN_MAX_ERROR_CODE_REASON_BYTES+1];  /* +1 for \0 */
63 } nr_stun_attr_error_code;
64 
65 typedef struct nr_stun_attr_fingerprint_ {
66     UINT4     checksum;
67     int       valid;
68 } nr_stun_attr_fingerprint;
69 
70 typedef struct nr_stun_attr_message_integrity_ {
71     UCHAR     hash[20];
72     int       unknown_user;
73     UCHAR     password[1024];
74     int       passwordlen;
75     int       valid;
76 } nr_stun_attr_message_integrity;
77 
78 typedef struct nr_stun_attr_unknown_attributes_ {
79     UINT2     attribute[NR_STUN_MAX_UNKNOWN_ATTRIBUTES];
80     int       num_attributes;
81 } nr_stun_attr_unknown_attributes;
82 
83 typedef struct nr_stun_attr_xor_mapped_address_ {
84         nr_transport_addr               masked;
85         nr_transport_addr               unmasked;
86 } nr_stun_attr_xor_mapped_address;
87 
88 typedef struct nr_stun_attr_data_ {
89     UCHAR  data[NR_STUN_MAX_MESSAGE_SIZE];
90     int    length;
91 } nr_stun_attr_data;
92 
93 
94 typedef struct nr_stun_encoded_attribute_ {
95     UINT2                               type;
96     UINT2                               length;
97     UCHAR                               value[NR_STUN_MAX_MESSAGE_SIZE];
98 } nr_stun_encoded_attribute;
99 
100 typedef struct nr_stun_message_attribute_ {
101     UINT2                               type;
102     UINT2                               length;
103     union {
104         nr_transport_addr               address;
105         nr_transport_addr               alternate_server;
106         nr_stun_attr_error_code         error_code;
107         nr_stun_attr_fingerprint        fingerprint;
108         nr_transport_addr               mapped_address;
109         nr_stun_attr_message_integrity  message_integrity;
110         char                            nonce[NR_STUN_MAX_NONCE_BYTES+1];  /* +1 for \0 */
111         char                            realm[NR_STUN_MAX_REALM_BYTES+1];  /* +1 for \0 */
112         nr_stun_attr_xor_mapped_address relay_address;
113         char                            server_name[NR_STUN_MAX_SERVER_BYTES+1];  /* +1 for \0 */
114         nr_stun_attr_unknown_attributes unknown_attributes;
115         char                            username[NR_STUN_MAX_USERNAME_BYTES+1];  /* +1 for \0 */
116         nr_stun_attr_xor_mapped_address xor_mapped_address;
117 
118 #ifdef USE_ICE
119         UINT4                           priority;
120         UINT8                           ice_controlled;
121         UINT8                           ice_controlling;
122 #endif /* USE_ICE */
123 
124 #ifdef USE_TURN
125         UINT4                           lifetime_secs;
126         nr_transport_addr               remote_address;
127         UCHAR                           requested_transport;
128         nr_stun_attr_data               data;
129 #endif /* USE_TURN */
130 
131 #ifdef USE_STUND_0_96
132         UINT4                           change_request;
133 #endif /* USE_STUND_0_96 */
134 
135         /* make sure there's enough room here to place any possible
136          * attribute */
137         UCHAR                           largest_possible_attribute[NR_STUN_MAX_MESSAGE_SIZE];
138     } u;
139     nr_stun_encoded_attribute          *encoding;
140     int                                 encoding_length;
141     char                               *name;
142     char                               *type_name;
143     int                                 invalid;
144     TAILQ_ENTRY(nr_stun_message_attribute_)  entry;
145 } nr_stun_message_attribute;
146 
147 typedef TAILQ_HEAD(nr_stun_message_attribute_head_,nr_stun_message_attribute_) nr_stun_message_attribute_head;
148 
149 typedef struct nr_stun_message_header_ {
150     UINT2                               type;
151     UINT2                               length;
152     UINT4                               magic_cookie;
153     UINT12                              id;
154 } nr_stun_message_header;
155 
156 typedef struct nr_stun_message_ {
157     char                               *name;
158     UCHAR                               buffer[NR_STUN_MAX_MESSAGE_SIZE];
159     int                                 length;
160     nr_stun_message_header              header;
161     int                                 comprehension_required_unknown_attributes;
162     int                                 comprehension_optional_unknown_attributes;
163     nr_stun_message_attribute_head      attributes;
164 } nr_stun_message;
165 
166 int nr_stun_message_create(nr_stun_message **msg);
167 int nr_stun_message_create2(nr_stun_message **msg, UCHAR *buffer, int length);
168 int nr_stun_message_destroy(nr_stun_message **msg);
169 
170 int nr_stun_message_attribute_create(nr_stun_message *msg, nr_stun_message_attribute **attr);
171 int nr_stun_message_attribute_destroy(nr_stun_message *msg, nr_stun_message_attribute **attr);
172 
173 int nr_stun_message_has_attribute(nr_stun_message *msg, UINT2 type, nr_stun_message_attribute **attribute);
174 
175 int nr_stun_message_add_alternate_server_attribute(nr_stun_message *msg, nr_transport_addr *alternate_server);
176 int nr_stun_message_add_error_code_attribute(nr_stun_message *msg, UINT2 number, char *reason);
177 int nr_stun_message_add_fingerprint_attribute(nr_stun_message *msg);
178 int nr_stun_message_add_message_integrity_attribute(nr_stun_message *msg, Data *password);
179 int nr_stun_message_add_nonce_attribute(nr_stun_message *msg, char *nonce);
180 int nr_stun_message_add_realm_attribute(nr_stun_message *msg, char *realm);
181 int nr_stun_message_add_server_attribute(nr_stun_message *msg, char *server_name);
182 int nr_stun_message_add_unknown_attributes_attribute(nr_stun_message *msg, nr_stun_attr_unknown_attributes *unknown_attributes);
183 int nr_stun_message_add_username_attribute(nr_stun_message *msg, char *username);
184 int nr_stun_message_add_xor_mapped_address_attribute(nr_stun_message *msg, nr_transport_addr *mapped_address);
185 
186 #ifdef USE_ICE
187 int nr_stun_message_add_ice_controlled_attribute(nr_stun_message *msg, UINT8 ice_controlled);
188 int nr_stun_message_add_ice_controlling_attribute(nr_stun_message *msg, UINT8 ice_controlling);
189 int nr_stun_message_add_priority_attribute(nr_stun_message *msg, UINT4 priority);
190 int nr_stun_message_add_use_candidate_attribute(nr_stun_message *msg);
191 #endif /* USE_ICE */
192 
193 #ifdef USE_TURN
194 int nr_stun_message_add_data_attribute(nr_stun_message *msg, UCHAR *data, int length);
195 int nr_stun_message_add_lifetime_attribute(nr_stun_message *msg, UINT4 lifetime_secs);
196 int nr_stun_message_add_requested_transport_attribute(nr_stun_message *msg, UCHAR transport);
197 int
198 nr_stun_message_add_xor_peer_address_attribute(nr_stun_message *msg, nr_transport_addr *peer_address);
199 #endif /* USE_TURN */
200 
201 #ifdef USE_STUND_0_96
202 int nr_stun_message_add_change_request_attribute(nr_stun_message *msg, UINT4 change_request);
203 #endif /* USE_STUND_0_96 */
204 
205 #endif
206 
207