Commit 2b71a9ff authored by Kai Vehmanen's avatar Kai Vehmanen

Remove older, duplicated STUN implementation from the tree.

darcs-hash:20070705093443-77cd4-fd11bc05143a73e102beafab6c09f33099766fc4.gz
parent e16ff6a9
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
#include <glib.h> #include <glib.h>
#include "stun/bind.h" #include "stun/bind.h"
#include "stun.h"
#include "udp.h" #include "udp.h"
#include "candidate.h" #include "candidate.h"
......
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
#include "agent-priv.h" #include "agent-priv.h"
#include "conncheck.h" #include "conncheck.h"
#include "discovery.h" #include "discovery.h"
#include "stun.h"
#include "stun-msg.h" #include "stun-msg.h"
static inline int priv_timer_expired (GTimeVal *restrict timer, GTimeVal *restrict now) static inline int priv_timer_expired (GTimeVal *restrict timer, GTimeVal *restrict now)
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
#include "agent.h" #include "agent.h"
#include "stream.h" #include "stream.h"
#include "stun.h" /* XXX: using the old STUN API, to be removed */
#include "stun-ice.h" /* note: the new STUN API */ #include "stun-ice.h" /* note: the new STUN API */
#define NICE_CANDIDATE_PAIR_MAX_FOUNDATION NICE_CANDIDATE_MAX_FOUNDATION*2 #define NICE_CANDIDATE_PAIR_MAX_FOUNDATION NICE_CANDIDATE_MAX_FOUNDATION*2
......
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <glib.h>
#include <stun.h>
static const gchar *server = "stun.fwdnet.net";
static guint port = 3478;
static gboolean
resolve (const gchar *name, struct hostent *ret)
{
int res;
int err;
struct hostent *he;
gchar buf[1024];
res = gethostbyname_r (name, ret, buf, sizeof (buf) / sizeof (gchar), &he,
&err);
return (res == 0);
}
int
main (int argc, char **argv)
{
struct hostent he;
struct sockaddr_in sin;
struct timeval tv;
fd_set fds;
guint sock;
gchar *packed;
guint length;
gchar buffer[256];
gint ret;
StunMessage *msg;
StunAttribute **attr;
if (argc > 1)
server = argv[1];
if (!resolve(server, &he))
{
g_debug ("failed to resolve %s\n", server);
return 1;
}
g_assert (he.h_addr_list != NULL);
sin.sin_family = AF_INET;
sin.sin_port = htons (port);
memcpy (&sin.sin_addr, he.h_addr_list[0], sizeof (struct in_addr));
sock = socket (AF_INET, SOCK_DGRAM, 0);
connect (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr));
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 0);
length = stun_message_pack (msg, &packed);
#ifdef DEBUG
{
gchar *dump = stun_message_dump (msg);
g_debug (dump);
g_free (dump);
}
#endif
send (sock, packed, length, 0);
g_free (packed);
stun_message_free (msg);
FD_ZERO (&fds);
FD_SET (sock, &fds);
tv.tv_sec = 5;
tv.tv_usec = 0;
ret = select (sock + 1, &fds, NULL, NULL, &tv);
if (ret < 0)
{
g_print ("error: %s", g_strerror (errno));
return 1;
}
else if (ret == 0)
{
g_print ("timeout\n");
return 1;
}
length = recv (sock, buffer, 256, 0);
msg = stun_message_unpack (length, buffer);
#ifdef DEBUG
{
gchar *dump = stun_message_dump (msg);
g_debug (dump);
g_free (dump);
}
#endif
for (attr = msg->attributes; *attr; attr++)
{
if ((*attr)->type == STUN_ATTRIBUTE_MAPPED_ADDRESS)
{
guint32 ip = (*attr)->address.ip;
g_print ("%d.%d.%d.%d\n",
(ip & 0xff000000) >> 24,
(ip & 0x00ff0000) >> 16,
(ip & 0x0000ff00) >> 8,
(ip & 0x000000ff));
break;
}
}
stun_message_free (msg);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <arpa/inet.h>
#include "stun.h"
/* round up to multiple of 4 */
G_GNUC_CONST
static guint
ceil4 (guint n)
{
if (n % 4 == 0)
return n;
else
return n + 4 - (n % 4);
}
G_GNUC_WARN_UNUSED_RESULT
static StunAttribute *
stun_attribute_new (guint type)
{
StunAttribute *attr = g_slice_new0 (StunAttribute);
attr->type = type;
return attr;
}
StunAttribute *
stun_attribute_mapped_address_new (guint32 ip, guint16 port)
{
StunAttribute *attr = stun_attribute_new (STUN_ATTRIBUTE_MAPPED_ADDRESS);
attr->length = 8;
attr->address.padding = 0;
attr->address.af = 1;
attr->address.ip = ip;
attr->address.port = port;
return attr;
}
StunAttribute *
stun_attribute_username_new (const gchar *username)
{
StunAttribute *attr;
attr = stun_attribute_new (STUN_ATTRIBUTE_USERNAME);
g_assert (strlen (username) < sizeof (attr->username));
attr->length = strlen (username);
strcpy (attr->username, username);
return attr;
}
void
stun_attribute_free (StunAttribute *attr)
{
g_slice_free (StunAttribute, attr);
}
G_GNUC_WARN_UNUSED_RESULT
static gboolean
_stun_attribute_unpack (StunAttribute *attr, guint length, const gchar *s)
{
guint type;
if (length < 4)
/* must start with 16 bit type, 16 bit length */
return FALSE;
type = ntohs (*(guint16 *) s);
switch (type)
{
case STUN_ATTRIBUTE_MAPPED_ADDRESS:
if (length != 12)
return FALSE;
attr->address.af = (guint8) s[5];
g_assert (attr->address.af == 1);
attr->address.port = ntohs (*(guint16 *)(s + 6));
attr->address.ip = ntohl (*(guint32 *)(s + 8));
break;
case STUN_ATTRIBUTE_USERNAME:
case STUN_ATTRIBUTE_PASSWORD:
if (length - 4 > sizeof (attr->username) / sizeof (gchar))
return FALSE;
attr->length = length - 4;
if (type == STUN_ATTRIBUTE_USERNAME)
memcpy (attr->username, s + 4, attr->length);
else
memcpy (attr->password, s + 4, attr->length);
break;
default:
/* unknown attribute; we can only unpack the type */
break;
}
attr->type = type;
return TRUE;
}
StunAttribute *
stun_attribute_unpack (guint length, const gchar *s)
{
StunAttribute *attr;
attr = stun_attribute_new (0);
if (_stun_attribute_unpack (attr, length, s))
return attr;
stun_attribute_free (attr);
return NULL;
}
guint
stun_attribute_pack (StunAttribute *attr, gchar **packed)
{
switch (attr->type)
{
case STUN_ATTRIBUTE_MAPPED_ADDRESS:
{
if (packed != NULL)
{
StunAttribute *ret = g_malloc0 (sizeof (StunAttribute));
ret->type = htons (attr->type);
ret->length = htons (8);
ret->address.af = attr->address.af;
ret->address.port = htons (attr->address.port);
ret->address.ip = htonl (attr->address.ip);
*packed = (gchar *) ret;
}
return 12;
}
case STUN_ATTRIBUTE_USERNAME:
{
if (packed != NULL)
{
StunAttribute *ret = g_malloc0 (sizeof (StunAttribute));
ret->type = htons (attr->type);
ret->length = htons (attr->length);
memcpy (ret->username, attr->username, attr->length);
*packed = (gchar *) ret;
}
return ceil4 (4 + attr->length);
}
default:
return 0;
}
}
gchar *
stun_attribute_dump (StunAttribute *attr)
{
switch (attr->type)
{
case STUN_ATTRIBUTE_MAPPED_ADDRESS:
return g_strdup_printf (
"MAPPED-ADDRESS %d.%d.%d.%d:%d",
(attr->address.ip & 0xff000000) >> 24,
(attr->address.ip & 0x00ff0000) >> 16,
(attr->address.ip & 0x0000ff00) >> 8,
(attr->address.ip & 0x000000ff) >> 0,
attr->address.port);
case STUN_ATTRIBUTE_USERNAME:
return g_strdup_printf (
"USERNAME \"%*s\"", attr->length, attr->username);
default:
return g_strdup_printf ("UNKNOWN (%d)", attr->type);
}
}
void
stun_message_init (StunMessage *msg, guint type, const gchar *id)
{
msg->type = type;
if (id != NULL)
memcpy (msg->transaction_id, id, 16);
}
StunMessage *
stun_message_new (guint type, const gchar *id, guint n_attributes)
{
StunMessage *msg = g_slice_new0 (StunMessage);
stun_message_init (msg, type, id);
if (n_attributes != 0)
msg->attributes = g_malloc0 (
(n_attributes + 1) * sizeof (StunAttribute *));
return msg;
}
void
stun_message_free (StunMessage *msg)
{
StunAttribute **attr;
if (msg->attributes)
{
for (attr = msg->attributes; *attr; attr++)
stun_attribute_free (*attr);
g_free (msg->attributes);
}
g_slice_free (StunMessage, msg);
}
StunMessage *
stun_message_unpack (guint length, const gchar *s)
{
guint attr_length;
guint n_attributes = 0;
guint i;
guint offset;
StunAttribute *attr;
StunMessage *msg;
/* message header is 20 bytes */
if (length < 20)
return NULL;
/* count the number of attributes */
for (offset = 20; offset < length; offset += attr_length)
{
attr_length = ceil4 (4 + ntohs (*(guint16 *)(s + offset + 2)));
n_attributes++;
}
/* create message structure */
msg = stun_message_new (ntohs (*(guint16 *) s), s + 4, n_attributes);
/* unpack attributes */
for (i = 0, offset = 20; i < n_attributes; i++, offset += attr_length)
{
attr_length = 4 + ntohs (*(guint16 *)(s + offset + 2));
attr = msg->attributes[i] = stun_attribute_unpack (attr_length,
s + offset);
attr_length = ceil4 (attr_length);
}
return msg;
}
guint
stun_message_pack (StunMessage *msg, gchar **packed)
{
gchar *tmp;
guint length = 0;
if (msg->attributes)
{
StunAttribute **attr;
for (attr = msg->attributes; *attr; attr++)
length += stun_attribute_pack (*attr, NULL);
}
g_assert (length % 4 == 0);
tmp = g_malloc0 (length + 20);
*(guint16 *) (tmp + 0) = htons (msg->type);
*(guint16 *) (tmp + 2) = htons (length);
memcpy (tmp + 4, msg->transaction_id, 16);
if (msg->attributes)
{
StunAttribute **attr;
gchar *pos = tmp + 20;
for (attr = msg->attributes; *attr; attr++)
{
gchar *attr_packed;
guint attr_length = stun_attribute_pack (*attr, &attr_packed);
memcpy (pos, attr_packed, attr_length);
g_free (attr_packed);
pos += attr_length;
}
}
*packed = tmp;
return length + 20;
}
gchar *
stun_message_dump (StunMessage *msg)
{
StunAttribute **attr;
GString *tmp = g_string_new ("");
const gchar *name;
switch (msg->type) {
case STUN_MESSAGE_BINDING_REQUEST:
name = "BINDING-REQUEST";
break;
case STUN_MESSAGE_BINDING_RESPONSE:
name = "BINDING-RESPONSE";
break;
case STUN_MESSAGE_BINDING_ERROR_RESPONSE:
name = "BINDING-ERROR-RESPONSE";
break;
default:
name = "(UNKNOWN)";
}
g_string_printf (tmp,
"%s %08x:%08x:%08x:%08x\n",
name,
ntohl (*(guint32 *)(msg->transaction_id)),
ntohl (*(guint32 *)(msg->transaction_id + 4)),
ntohl (*(guint32 *)(msg->transaction_id + 8)),
ntohl (*(guint32 *)(msg->transaction_id + 12)));
if (msg->attributes)
for (attr = msg->attributes; *attr; attr++)
{
gchar *dump = stun_attribute_dump (*attr);
g_string_append_printf (tmp, " %s\n", dump);
g_free (dump);
}
return g_string_free (tmp, FALSE);
}
StunAttribute *
stun_message_find_attribute (StunMessage *msg, StunAttributeType type)
{
StunAttribute **attr;
if (!msg->attributes)
return NULL;
for (attr = msg->attributes; *attr; attr++)
if ((*attr)->type == type)
return *attr;
return NULL;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef __STUN_H__
#define __STUN_H__
#include <glib.h>
G_BEGIN_DECLS
typedef enum
{
STUN_MESSAGE_BINDING_REQUEST = 0x001,
STUN_MESSAGE_BINDING_RESPONSE = 0x101,
STUN_MESSAGE_BINDING_ERROR_RESPONSE = 0x111,
STUN_MESSAGE_SHARED_SECRET_REQUEST = 0x002,
STUN_MESSAGE_SHARED_SECRET_RESPONSE = 0x102,
STUN_MESSAGE_SHARED_SECRET_ERROR_RESPONSE = 0x112
} StunMessageType;
/* a = defined by RFC 3489
* b = defined by RFC 3489bis
* c = defined by draft-ietf-behave-turn-02
*/
typedef enum
{
// mandatory parameters (<= 0x7fff)
STUN_ATTRIBUTE_MAPPED_ADDRESS = 0x0001, // ab
STUN_ATTRIBUTE_RESPONSE_ADDRESS = 0x0002, // a
STUN_ATTRIBUTE_CHANGE_REQUEST = 0x0003, // a
STUN_ATTRIBUTE_CHANGED_ADDRESS = 0x0004, // a
STUN_ATTRIBUTE_SOURCE_ADDRESS = 0x0005, // a
STUN_ATTRIBUTE_USERNAME = 0x0006, // ab
STUN_ATTRIBUTE_PASSWORD = 0x0007, // ab
STUN_ATTRIBUTE_MESSAGE_INTEGRITY = 0x0008, // ab
STUN_ATTRIBUTE_ERROR_CODE = 0x0009, // ab
STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES = 0x000a, // ab
STUN_ATTRIBUTE_REFLECTED_FROM = 0x000b, // a
STUN_ATTRIBUTE_REALM = 0x0014, // b
STUN_ATTRIBUTE_NONCE = 0x0015, // b
STUN_ATTRIBUTE_LIFETIME = 0x000D, // c
STUN_ATTRIBUTE_BANDWIDTH = 0x0010, // c
STUN_ATTRIBUTE_REMOTE_ADDRESS = 0x0012, // c
STUN_ATTRIBUTE_DATA = 0x0013, // c
STUN_ATTRIBUTE_RELAY_ADDRESS = 0x0016, // c
STUN_ATTRIBUTE_REQUESTED_PORT_PROPS = 0x0018, // c
STUN_ATTRIBUTE_REQUESTED_TRANSPORT = 0x0019, // c
STUN_ATTRIBUTE_REQUESTED_IP = 0x0022, // c
STUN_ATTRIBUTE_TIMER_VAL = 0x0021, // c
// optional parameters (> 0x7fff)
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS = 0x8020, // b
STUN_ATTRIBUTE_FINGERPRINT = 0x8023, // b
STUN_ATTRIBUTE_SERVER = 0x8022, // b
SUTN_ATTRIBUTE_ALTERNATE_SERVER = 0x8023, // b
STUN_ATTRIBUTE_REFRESH_INTERVAL = 0x8024, // b
} StunAttributeType;
typedef struct _StunAttribute StunAttribute;
struct _StunAttribute {
guint16 type;
guint16 length;
union {
struct {
guint8 padding;
guint8 af;
guint16 port;
guint32 ip;
} address;
gchar username[128];
gchar password[128];
};
};
typedef struct _StunMessage StunMessage;
struct _StunMessage {
guint16 type;
gchar transaction_id[16];
StunAttribute **attributes;
};
G_GNUC_WARN_UNUSED_RESULT
StunAttribute *
stun_attribute_mapped_address_new (guint32 ip_address, guint16 port);
StunAttribute *
stun_attribute_username_new (const gchar *username);
void
stun_attribute_free (StunAttribute *attr);
G_GNUC_WARN_UNUSED_RESULT
guint
stun_attribute_pack (StunAttribute *attr, gchar **ret);
G_GNUC_WARN_UNUSED_RESULT
gchar *
stun_attribute_dump (StunAttribute *attr);
G_GNUC_WARN_UNUSED_RESULT
StunAttribute *
stun_attribute_unpack (guint length, const gchar *s);
void
stun_message_init (StunMessage *msg, guint type, const gchar *id);
G_GNUC_WARN_UNUSED_RESULT
StunMessage *
stun_message_new (guint type, const gchar *id, guint n_attributes);
void
stun_message_free (StunMessage *msg);
G_GNUC_WARN_UNUSED_RESULT
guint
stun_message_pack (StunMessage *msg, gchar **packed);
G_GNUC_WARN_UNUSED_RESULT
gchar *
stun_message_dump (StunMessage *msg);
G_GNUC_WARN_UNUSED_RESULT
StunMessage *
stun_message_unpack (guint length, const gchar *s);
StunAttribute *
stun_message_find_attribute (StunMessage *msg, StunAttributeType type);
G_END_DECLS
#endif /* __STUN_H__ */
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
int
main (void)
{
gchar *dump;
StunAttribute *attr = stun_attribute_unpack (4,
"\x00\xff" // type
"\x00\x00" // length
);
dump = stun_attribute_dump (attr);
g_assert (0 == strcmp (dump, "UNKNOWN (255)"));
g_free (dump);
stun_attribute_free (attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#include <string.h>
#include "stun.h"
int
main (void)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *dump = stun_attribute_dump (attr);
g_assert (NULL != dump);
g_assert (0 == strcmp (dump, "MAPPED-ADDRESS 2.3.4.5:2345"));
g_free (dump);
stun_attribute_free (attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "stun.h"
int
main (void)
{
/* can't create an unknown attribute directly, so create a MAPPED-ADDRESS
* and change its type
*/
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed = NULL;
guint length;
attr->type = 0xff;
length = stun_attribute_pack (attr, &packed);
g_assert (0 == length);
g_assert (NULL == packed);
stun_attribute_free (attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
static void
test_pack_mapped_address (void)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed;
guint length;
length = stun_attribute_pack (attr, &packed);
g_assert (12 == length);
g_assert (NULL != packed);
g_assert (0 == memcmp (packed,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
length));
g_free (packed);
stun_attribute_free (attr);
}
static void
test_pack_username (void)
{
StunAttribute *attr;
gchar *packed;
guint length;
attr = stun_attribute_username_new ("abcdefghi");
length = stun_attribute_pack (attr, &packed);
// 4 bytes header + 9 bytes padded to 32 bits = 16
g_assert (16 == length);
// type
g_assert (0 == memcmp (packed + 0, "\x00\x06", 2));
// length
g_assert (0 == memcmp (packed + 2, "\x00\x09", 2));
// value
g_assert (0 == memcmp (packed + 4, "abcdefghi\0\0\0", length - 4));
g_free (packed);
stun_attribute_free (attr);
}
int
main (void)
{
test_pack_mapped_address ();
test_pack_username ();
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "stun.h"
int
main (void)
{
StunAttribute *attr = stun_attribute_unpack (8,
"\x00\xff" // type
"\x00\x04" // length
"\xff\xff" // some data
"\xff\xff"
);
g_assert (NULL != attr);
g_assert (attr->type == 0xff);
stun_attribute_free (attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "stun.h"
int
main (void)
{
StunAttribute *attr;
// attributes must be at least 4 bytes long
attr = stun_attribute_unpack (0, NULL);
g_assert (NULL == attr);
attr = stun_attribute_unpack (8,
"\x00\x01" // type = MAPPED-ADDRESS
"\x00\x04" // length = 4 (invalid!)
"\x00\x01" // padding, address family
"\x09\x29" // port
);
g_assert (NULL == attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
int
main (void)
{
StunAttribute *attr;
attr = stun_attribute_unpack (12,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
g_assert (NULL != attr);
g_assert (attr->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
// length is not used
g_assert (attr->length == 0);
g_assert (attr->address.af == 1);
g_assert (attr->address.port == 2345);
g_assert (attr->address.ip == 0x02030405);
stun_attribute_free (attr);
attr = stun_attribute_unpack (9,
"\x00\x06" // type
"\x00\x05" // length
"abcde" // value
);
g_assert (NULL != attr);
g_assert (attr->length == 5);
g_assert (attr->type == STUN_ATTRIBUTE_USERNAME);
g_assert (0 == memcmp (attr->username, "abcde", 5));
stun_attribute_free (attr);
attr = stun_attribute_unpack (10,
"\x00\x07" // type
"\x00\x06" // length
"fghijk" // value
);
g_assert (NULL != attr);
g_assert (attr->length == 6);
g_assert (attr->type == STUN_ATTRIBUTE_PASSWORD);
g_assert (0 == memcmp (attr->password, "fghijk", 6));
stun_attribute_free (attr);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
int
main (void)
{
StunMessage *msg;
gchar *dump;
msg = stun_message_new (0xffff, NULL, 0);
dump = stun_message_dump (msg);
g_assert (0 == strcmp (dump,
"(UNKNOWN) 00000000:00000000:00000000:00000000\n"));
g_free (dump);
stun_message_free (msg);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
int
main (void)
{
StunMessage *msg;
gchar *dump;
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 1);
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
dump = stun_message_dump (msg);
g_assert (NULL != dump);
g_assert (0 == strcmp (dump,
"BINDING-REQUEST 00010203:04050607:08090a0b:0c0d0e0f\n"
" MAPPED-ADDRESS 2.3.4.5:2345\n"));
g_free (dump);
stun_message_free (msg);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "stun.h"
int
main (void)
{
StunMessage *msg;
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
g_assert (NULL ==
stun_message_find_attribute (msg, STUN_ATTRIBUTE_MAPPED_ADDRESS));
msg->attributes[0] = stun_attribute_mapped_address_new (0x01020304, 1234);
g_assert (msg->attributes[0] ==
stun_message_find_attribute (msg, STUN_ATTRIBUTE_MAPPED_ADDRESS));
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "stun.h"
int
main (void)
{
StunMessage *msg;
gchar *packed;
guint length;
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
memcpy (msg->transaction_id,
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
length = stun_message_pack (msg, &packed);
g_assert (packed != NULL);
g_assert (length == 32);
g_assert (0 == memcmp (packed + 0, "\x00\x01", 2));
g_assert (0 == memcmp (packed + 2, "\x00\x0c", 2));
g_assert (0 == memcmp (packed + 4,
"\x00\x01\x02\x03"
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f", 16));
g_assert (0 == memcmp (packed + 20,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
12));
g_free (packed);
stun_message_free (msg);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "stun.h"
int
main (void)
{
StunMessage *msg;
/* invalid message */
msg = stun_message_unpack (5, "hello");
g_assert (msg == NULL);
/* valid message */
msg = stun_message_unpack (32,
"\x00\x01" // type
"\x00\x0c" // length
"\x00\x01\x02\x03" // transaction ID
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f"
"\x00\x01" // attr1 type
"\x00\x08" // attr1 length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
g_assert (msg->type == STUN_MESSAGE_BINDING_REQUEST);
g_assert (msg->attributes[0] != NULL);
g_assert (msg->attributes[0]->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
g_assert (msg->attributes[0]->address.port == 2345);
g_assert (msg->attributes[0]->address.ip == 0x02030405);
g_assert (msg->attributes[1] == NULL);
stun_message_free (msg);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment