Commit 3f64a8a6 authored by Dafydd Harries's avatar Dafydd Harries

reindent stun.c for consistency

darcs-hash:20070110164644-c9803-71026228f7b135922e39f9d6a7b2d75f77125e0f.gz
parent 90abca99
...@@ -43,16 +43,17 @@ stun_attribute_unpack (guint length, const gchar *s) ...@@ -43,16 +43,17 @@ stun_attribute_unpack (guint length, const gchar *s)
g_assert (length); g_assert (length);
attr = stun_attribute_new (ntohs (*(guint16 *)s)); attr = stun_attribute_new (ntohs (*(guint16 *)s));
switch (attr->type) { switch (attr->type)
case STUN_ATTRIBUTE_MAPPED_ADDRESS: {
attr->address.af = (guint8) s[5]; case STUN_ATTRIBUTE_MAPPED_ADDRESS:
g_assert (attr->address.af == 1); attr->address.af = (guint8) s[5];
attr->address.port = ntohs (*(guint16 *)(s + 6)); g_assert (attr->address.af == 1);
attr->address.ip = ntohl (*(guint32 *)(s + 8)); attr->address.port = ntohs (*(guint16 *)(s + 6));
break; attr->address.ip = ntohl (*(guint32 *)(s + 8));
default: break;
break; default:
} break;
}
return attr; return attr;
} }
...@@ -60,39 +61,41 @@ stun_attribute_unpack (guint length, const gchar *s) ...@@ -60,39 +61,41 @@ stun_attribute_unpack (guint length, const gchar *s)
guint guint
stun_attribute_pack (StunAttribute *attr, gchar **packed) stun_attribute_pack (StunAttribute *attr, gchar **packed)
{ {
switch (attr->type) { switch (attr->type)
case STUN_ATTRIBUTE_MAPPED_ADDRESS: {
{ case STUN_ATTRIBUTE_MAPPED_ADDRESS:
StunAttribute *ret = g_malloc0 (sizeof (StunAttribute)); {
StunAttribute *ret = g_malloc0 (sizeof (StunAttribute));
ret->type = htons (attr->type);
ret->length = htons (8); ret->type = htons (attr->type);
ret->address.af = attr->address.af; ret->length = htons (8);
ret->address.port = htons (attr->address.port); ret->address.af = attr->address.af;
ret->address.ip = htonl (attr->address.ip); ret->address.port = htons (attr->address.port);
*packed = (gchar *) ret; ret->address.ip = htonl (attr->address.ip);
return 12; *packed = (gchar *) ret;
} return 12;
default: }
return 0; default:
return 0;
} }
} }
gchar * gchar *
stun_attribute_dump (StunAttribute *attr) stun_attribute_dump (StunAttribute *attr)
{ {
switch (attr->type) { switch (attr->type)
case STUN_ATTRIBUTE_MAPPED_ADDRESS: {
return g_strdup_printf ( case STUN_ATTRIBUTE_MAPPED_ADDRESS:
"MAPPED-ADDRESS %d.%d.%d.%d:%d", return g_strdup_printf (
(attr->address.ip & 0xff000000) >> 24, "MAPPED-ADDRESS %d.%d.%d.%d:%d",
(attr->address.ip & 0x00ff0000) >> 16, (attr->address.ip & 0xff000000) >> 24,
(attr->address.ip & 0x0000ff00) >> 8, (attr->address.ip & 0x00ff0000) >> 16,
(attr->address.ip & 0x000000ff) >> 0, (attr->address.ip & 0x0000ff00) >> 8,
attr->address.port); (attr->address.ip & 0x000000ff) >> 0,
default: attr->address.port);
return g_strdup_printf ("UNKNOWN (%d)", attr->type); default:
} return g_strdup_printf ("UNKNOWN (%d)", attr->type);
}
} }
void void
...@@ -121,12 +124,13 @@ stun_message_free (StunMessage *msg) ...@@ -121,12 +124,13 @@ stun_message_free (StunMessage *msg)
{ {
StunAttribute **attr; StunAttribute **attr;
if (msg->attributes) { if (msg->attributes)
for (attr = msg->attributes; *attr; attr++) {
stun_attribute_free (*attr); for (attr = msg->attributes; *attr; attr++)
stun_attribute_free (*attr);
g_free (msg->attributes); g_free (msg->attributes);
} }
g_slice_free (StunMessage, msg); g_slice_free (StunMessage, msg);
} }
...@@ -152,10 +156,11 @@ stun_message_unpack (guint length, gchar *s) ...@@ -152,10 +156,11 @@ stun_message_unpack (guint length, gchar *s)
/* count the number of attributes */ /* count the number of attributes */
for (offset = 20; offset < length; offset += attr_length + 4) { for (offset = 20; offset < length; offset += attr_length + 4)
attr_length = ntohs (*(guint16 *)(s + offset + 2)); {
n_attributes++; attr_length = ntohs (*(guint16 *)(s + offset + 2));
} n_attributes++;
}
/* allocate memory for the attribute list and terminate it */ /* allocate memory for the attribute list and terminate it */
...@@ -164,10 +169,12 @@ stun_message_unpack (guint length, gchar *s) ...@@ -164,10 +169,12 @@ stun_message_unpack (guint length, gchar *s)
/* unpack attributes */ /* unpack attributes */
for (i = 0, offset = 20; i < n_attributes; i++, offset += attr_length + 4) { for (i = 0, offset = 20; i < n_attributes; i++, offset += attr_length + 4)
attr_length = ntohs (*(guint16 *)(s + offset + 2)); {
attr = msg->attributes[i] = stun_attribute_unpack (attr_length, s + offset); attr_length = ntohs (*(guint16 *)(s + offset + 2));
} attr = msg->attributes[i] = stun_attribute_unpack (attr_length,
s + offset);
}
return msg; return msg;
} }
...@@ -180,12 +187,13 @@ stun_message_pack (StunMessage *msg, gchar **packed) ...@@ -180,12 +187,13 @@ stun_message_pack (StunMessage *msg, gchar **packed)
guint16 packed_length; guint16 packed_length;
guint length = 0; guint length = 0;
if (msg->attributes) { if (msg->attributes)
StunAttribute **attr; {
StunAttribute **attr;
for (attr = msg->attributes; *attr; attr++) for (attr = msg->attributes; *attr; attr++)
length += 4 + (*attr)->length; length += 4 + (*attr)->length;
} }
packed_type = htons (msg->type); packed_type = htons (msg->type);
packed_length = htons (length); packed_length = htons (length);
...@@ -197,16 +205,18 @@ stun_message_pack (StunMessage *msg, gchar **packed) ...@@ -197,16 +205,18 @@ stun_message_pack (StunMessage *msg, gchar **packed)
((gchar *) &packed_length)[1]); ((gchar *) &packed_length)[1]);
g_string_append_len (tmp, msg->transaction_id, 16); g_string_append_len (tmp, msg->transaction_id, 16);
if (msg->attributes) { if (msg->attributes)
StunAttribute **attr; {
StunAttribute **attr;
for (attr = msg->attributes; *attr; attr++) {
gchar *attr_packed; for (attr = msg->attributes; *attr; attr++)
guint attr_length = stun_attribute_pack (*attr, &attr_packed); {
g_string_append_len (tmp, attr_packed, attr_length); gchar *attr_packed;
g_free (attr_packed); guint attr_length = stun_attribute_pack (*attr, &attr_packed);
g_string_append_len (tmp, attr_packed, attr_length);
g_free (attr_packed);
}
} }
}
*packed = g_string_free (tmp, FALSE); *packed = g_string_free (tmp, FALSE);
return length + 20; return length + 20;
...@@ -238,13 +248,13 @@ stun_message_dump (StunMessage *msg) ...@@ -238,13 +248,13 @@ stun_message_dump (StunMessage *msg)
*(guint32 *)(msg->transaction_id + 8), *(guint32 *)(msg->transaction_id + 8),
*(guint32 *)(msg->transaction_id + 12)); *(guint32 *)(msg->transaction_id + 12));
if (msg->attributes) { if (msg->attributes)
for (attr = msg->attributes; *attr; attr++) { for (attr = msg->attributes; *attr; attr++)
gchar *dump = stun_attribute_dump (*attr); {
g_string_append_printf (tmp, "\n %s", dump); gchar *dump = stun_attribute_dump (*attr);
g_free (dump); g_string_append_printf (tmp, "\n %s", dump);
} g_free (dump);
} }
return g_string_free (tmp, FALSE); return g_string_free (tmp, FALSE);
} }
......
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