2
* This file is part of the Nice GLib ICE library.
4
* (C) 2007 Nokia Corporation. All rights reserved.
5
* Contact: Rémi Denis-Courmont
7
* The contents of this file are subject to the Mozilla Public License Version
8
* 1.1 (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
10
* http://www.mozilla.org/MPL/
12
* Software distributed under the License is distributed on an "AS IS" basis,
13
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14
* for the specific language governing rights and limitations under the
17
* The Original Code is the Nice GLib ICE library.
19
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
20
* Corporation. All Rights Reserved.
23
* Rémi Denis-Courmont, Nokia
25
* Alternatively, the contents of this file may be used under the terms of the
26
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27
* case the provisions of LGPL are applicable instead of those above. If you
28
* wish to allow use of your version of this file only under the terms of the
29
* LGPL and not to allow others to use your version of this file under the
30
* MPL, indicate your decision by deleting the provisions above and replace
31
* them with the notice and other provisions required by the LGPL. If you do
32
* not delete the provisions above, a recipient may use your version of this
33
* file under either the MPL or the LGPL.
40
#include <sys/types.h>
42
#include "stun/stunagent.h"
54
#define EAFNOSUPPORT -4
57
#define EINPROGRESS -7
61
#include <sys/types.h>
62
#include <sys/socket.h>
63
#include <arpa/inet.h>
68
static void fatal (const char *msg, ...)
72
vfprintf (stderr, msg, ap);
78
static const char usr[] = "admin";
79
static const char pwd[] = "s3kr3t";
81
bool dynamic_check_validater (StunAgent *agent,
82
StunMessage *message, uint8_t *username, uint16_t username_len,
83
uint8_t **password, size_t *password_len, void *user_data)
86
if (username_len != strlen (usr) ||
87
memcmp (username, usr, strlen (usr)) != 0)
88
fatal ("vector test : Validater received wrong username!");
90
*password = (uint8_t *) pwd;
91
*password_len = strlen (pwd);
97
dynamic_check (StunAgent *agent, StunMessage *msg, size_t len)
101
if (stun_agent_validate (agent, &msg2, msg->buffer, len, dynamic_check_validater, NULL) != STUN_VALIDATION_SUCCESS)
102
fatal ("Could not validate message");
104
printf ("Built message of %u bytes\n", (unsigned)len);
109
finish_check (StunAgent *agent, StunMessage *msg)
111
uint8_t buf[STUN_MAX_MESSAGE_SIZE + 8];
114
StunMessage msg2 = {0};
115
msg2.agent = msg->agent;
117
msg2.buffer_len = sizeof(buf);
118
memcpy (msg2.buffer, msg->buffer, sizeof(buf) > msg->buffer_len ? msg->buffer_len : sizeof(buf));
120
len = stun_agent_finish_message (agent, msg, NULL, 0);
123
fatal ("Cannot finish message");
124
dynamic_check (agent, msg, len);
126
if (stun_message_find (&msg2, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, &plen) != NULL)
127
fatal ("Missing HMAC test failed");
129
stun_message_append_string (&msg2, STUN_ATTRIBUTE_USERNAME, usr);
131
len = stun_agent_finish_message (agent, &msg2, pwd, strlen (pwd));
134
fatal ("Cannot finish message with short-term creds");
135
dynamic_check (agent, &msg2, len);
141
check_af (const char *name, int family, socklen_t addrlen)
143
struct sockaddr_storage addr;
147
uint16_t known_attributes[] = {STUN_ATTRIBUTE_USERNAME, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, STUN_ATTRIBUTE_ERROR_CODE, 0};
149
stun_agent_init (&agent, known_attributes,
150
STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_USE_FINGERPRINT);
152
assert (addrlen <= sizeof (addr));
154
memset (&addr, 0, sizeof (addr));
155
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
157
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
158
(struct sockaddr *)&addr, addrlen) != EAFNOSUPPORT)
159
fatal ("Unknown address family test failed");
160
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
161
(struct sockaddr *)&addr, addrlen) != EAFNOSUPPORT)
162
fatal ("Unknown address family xor test failed");
164
addr.ss_family = family;
165
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
166
(struct sockaddr *)&addr, addrlen - 1) != EINVAL)
167
fatal ("Too small %s sockaddr test failed", name);
169
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
170
(struct sockaddr *)&addr, addrlen - 1) != EINVAL)
171
fatal ("Too small %s sockaddr xor test failed", name);
173
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
174
(struct sockaddr *)&addr, addrlen))
175
fatal ("%s sockaddr test failed", name);
177
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
178
(struct sockaddr *)&addr, addrlen))
179
fatal ("%s sockaddr xor test failed", name);
186
struct sockaddr addr;
190
uint16_t known_attributes[] = {STUN_ATTRIBUTE_USERNAME, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, STUN_ATTRIBUTE_ERROR_CODE, 0};
192
stun_agent_init (&agent, known_attributes,
193
STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_USE_FINGERPRINT);
195
/* Request formatting test */
196
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
197
finish_check (&agent, &msg);
198
if (memcmp (buf, "\x00\x01", 2))
199
fatal ("Request formatting test failed");
201
/* Response formatting test */
202
stun_agent_init_response (&agent, &msg, buf, sizeof (buf), &msg);
203
finish_check (&agent, &msg);
204
if (memcmp (buf, "\x01\x01", 2))
205
fatal ("Response formatting test failed");
207
/* Error formatting test */
208
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
209
finish_check (&agent, &msg);
210
if (!stun_agent_init_error (&agent, &msg, buf, sizeof (buf), &msg, 400))
211
fatal ("Error initialization test failed");
212
finish_check (&agent, &msg);
213
if (memcmp (buf, "\x01\x11", 2))
214
fatal ("Error formatting test failed");
215
/* Unknown error formatting test */
216
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
217
finish_check (&agent, &msg);
218
if (!stun_agent_init_error (&agent, &msg, buf, sizeof (buf), &msg, 666))
219
fatal ("Unknown error initialization test failed");
220
finish_check (&agent, &msg);
221
if (memcmp (buf, "\x01\x11", 2))
222
fatal ("Unknown error formatting test failed");
225
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
228
stun_message_append_flag (&msg, 0xffff) != ENOBUFS;
232
fatal ("Overflow protection test failed");
235
if (stun_message_append32 (&msg, 0xffff, 0x12345678) != ENOBUFS)
236
fatal ("Double-word overflow test failed");
237
if (stun_message_append64 (&msg, 0xffff,
238
0x123456789abcdef0) != ENOBUFS)
239
fatal ("Quad-word overflow test failed");
240
if (stun_message_append_string (&msg, 0xffff, "foobar") != ENOBUFS)
241
fatal ("String overflow test failed");
243
memset (&addr, 0, sizeof (addr));
244
addr.sa_family = AF_INET;
246
addr.sa_len = sizeof (addr);
248
if (stun_message_append_xor_addr (&msg, 0xffff, &addr,
249
sizeof (addr)) != ENOBUFS)
250
fatal ("Address overflow test failed");
252
if (stun_agent_finish_message (&agent, &msg, NULL, 0) != 0)
253
fatal ("Fingerprint overflow test failed");
254
if (stun_agent_finish_message (&agent, &msg, pwd, strlen (pwd)) != 0)
255
fatal ("Message integrity overflow test failed");
257
/* Address attributes tests */
258
check_af ("IPv4", AF_INET, sizeof (struct sockaddr_in));
260
check_af ("IPv6", AF_INET6, sizeof (struct sockaddr_in6));