Commit f8a1f922 authored by Dafydd Harries's avatar Dafydd Harries
Browse files

add support for STUN usernames/passwords

darcs-hash:20070129203101-c9803-ea9942fbd30ad6d5c47441d7e6d784179e434e15.gz
parent dbd0a358
......@@ -57,6 +57,19 @@ _stun_attribute_unpack (StunAttribute *attr, guint length, const gchar *s)
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;
......
......@@ -34,12 +34,16 @@ typedef struct _StunAttribute StunAttribute;
struct _StunAttribute {
guint16 type;
guint16 length;
struct {
guint8 padding;
guint8 af;
guint16 port;
guint32 ip;
} address;
union {
struct {
guint8 padding;
guint8 af;
guint16 port;
guint32 ip;
} address;
gchar username[128];
gchar password[128];
};
};
typedef struct _StunMessage StunMessage;
......
......@@ -76,7 +76,9 @@ END_TEST
START_TEST (test_attribute_unpack)
{
StunAttribute *attr = stun_attribute_unpack (12,
StunAttribute *attr;
attr = stun_attribute_unpack (12,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
......@@ -86,10 +88,36 @@ START_TEST (test_attribute_unpack)
fail_unless (NULL != attr);
fail_unless (attr->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
// length is not used
fail_unless (attr->length == 0);
fail_unless (attr->address.af == 1);
fail_unless (attr->address.port == 2345);
fail_unless (attr->address.ip == 0x02030405);
stun_attribute_free (attr);
attr = stun_attribute_unpack (9,
"\x00\x06" // type
"\x00\x05" // length
"abcde" // value
);
fail_unless (NULL != attr);
fail_unless (attr->length == 5);
fail_unless (attr->type == STUN_ATTRIBUTE_USERNAME);
fail_unless (0 == memcmp (attr->username, "abcde", 5));
stun_attribute_free (attr);
attr = stun_attribute_unpack (10,
"\x00\x07" // type
"\x00\x06" // length
"fghijk" // value
);
fail_unless (NULL != attr);
fail_unless (attr->length == 6);
fail_unless (attr->type == STUN_ATTRIBUTE_PASSWORD);
fail_unless (0 == memcmp (attr->password, "fghijk", 6));
stun_attribute_free (attr);
}
END_TEST
......
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