Commit 842d8848 authored by Dafydd Harries's avatar Dafydd Harries

add n_attributes paramenter to stun_message_new

darcs-hash:20070201125530-c9803-b048be80885d9d82f7e1a2fcdd24f1675b85b345.gz
parent 0f798e75
...@@ -494,8 +494,7 @@ RESPOND: ...@@ -494,8 +494,7 @@ RESPOND:
/* XXX: add username to response */ /* XXX: add username to response */
response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
msg->transaction_id); msg->transaction_id, 1);
response->attributes = g_malloc0 (2 * sizeof (StunAttribute));
response->attributes[0] = stun_attribute_mapped_address_new ( response->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), ntohs (from.sin_port)); ntohl (from.sin_addr.s_addr), ntohs (from.sin_port));
len = stun_message_pack (response, &packed); len = stun_message_pack (response, &packed);
......
...@@ -40,7 +40,7 @@ test_stun_no_password ( ...@@ -40,7 +40,7 @@ test_stun_no_password (
/* send binding request without username */ /* send binding request without username */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
"0123456789abcdef"); "0123456789abcdef", 0);
packed_len = stun_message_pack (breq, &packed); packed_len = stun_message_pack (breq, &packed);
udp_fake_socket_push_recv (sock, &from, packed_len, packed); udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed); g_free (packed);
...@@ -79,8 +79,7 @@ test_stun_invalid_password ( ...@@ -79,8 +79,7 @@ test_stun_invalid_password (
/* send binding request with incorrect username */ /* send binding request with incorrect username */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
"0123456789abcdef"); "0123456789abcdef", 1);
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
breq->attributes[0] = stun_attribute_username_new ("lala"); breq->attributes[0] = stun_attribute_username_new ("lala");
packed_len = stun_message_pack (breq, &packed); packed_len = stun_message_pack (breq, &packed);
g_assert (packed_len != 0); g_assert (packed_len != 0);
...@@ -124,8 +123,7 @@ test_stun_valid_password ( ...@@ -124,8 +123,7 @@ test_stun_valid_password (
/* send binding request with correct username */ /* send binding request with correct username */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
"0123456789abcdef"); "0123456789abcdef", 1);
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
username = g_strconcat ( username = g_strconcat (
"username", "username",
((NiceCandidate *) agent->local_candidates->data)->username, ((NiceCandidate *) agent->local_candidates->data)->username,
...@@ -144,8 +142,7 @@ test_stun_valid_password ( ...@@ -144,8 +142,7 @@ test_stun_valid_password (
/* construct expected response packet */ /* construct expected response packet */
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
"0123456789abcdef"); "0123456789abcdef", 1);
bres->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
bres->attributes[0] = stun_attribute_mapped_address_new ( bres->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), 5678); ntohl (from.sin_addr.s_addr), 5678);
packed_len = stun_message_pack (bres, &packed); packed_len = stun_message_pack (bres, &packed);
......
...@@ -16,8 +16,7 @@ send_stun (UDPSocket *udpsock, struct sockaddr_in sin) ...@@ -16,8 +16,7 @@ send_stun (UDPSocket *udpsock, struct sockaddr_in sin)
gchar buf[1024]; gchar buf[1024];
StunMessage *msg; StunMessage *msg;
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL); msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute));
msg->attributes[0] = stun_attribute_username_new ("lala"); msg->attributes[0] = stun_attribute_username_new ("lala");
{ {
......
...@@ -37,8 +37,8 @@ handle_packet ( ...@@ -37,8 +37,8 @@ handle_packet (
} }
stun_message_free (msg); stun_message_free (msg);
msg = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, msg->transaction_id); msg = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, msg->transaction_id,
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute)); 1);
msg->attributes[0] = stun_attribute_mapped_address_new ( msg->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from->sin_addr.s_addr), ntohs (from->sin_port)); ntohl (from->sin_addr.s_addr), ntohs (from->sin_port));
length = stun_message_pack (msg, &packed); length = stun_message_pack (msg, &packed);
......
...@@ -189,18 +189,23 @@ stun_message_init (StunMessage *msg, guint type, gchar *id) ...@@ -189,18 +189,23 @@ stun_message_init (StunMessage *msg, guint type, gchar *id)
} }
StunMessage * StunMessage *
stun_message_new (guint type, gchar *id) stun_message_new (guint type, gchar *id, guint n_attributes)
{ {
StunMessage *msg = g_slice_new0 (StunMessage); StunMessage *msg = g_slice_new0 (StunMessage);
stun_message_init (msg, type, id); stun_message_init (msg, type, id);
if (n_attributes != 0)
msg->attributes = g_malloc0 (
(n_attributes + 1) * sizeof (StunAttribute *));
return msg; return msg;
} }
StunMessage * StunMessage *
stun_message_binding_request_new () stun_message_binding_request_new ()
{ {
return stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL); return stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 0);
} }
void void
...@@ -233,10 +238,6 @@ stun_message_unpack (guint length, gchar *s) ...@@ -233,10 +238,6 @@ stun_message_unpack (guint length, gchar *s)
g_assert (length >= 20); g_assert (length >= 20);
/* unpack the header */
msg = stun_message_new (ntohs (*(guint16 *) s), s + 4);
/* count the number of attributes */ /* count the number of attributes */
for (offset = 20; offset < length; offset += attr_length) for (offset = 20; offset < length; offset += attr_length)
...@@ -245,10 +246,9 @@ stun_message_unpack (guint length, gchar *s) ...@@ -245,10 +246,9 @@ stun_message_unpack (guint length, gchar *s)
n_attributes++; n_attributes++;
} }
/* allocate memory for the attribute list and terminate it */ /* create message structure */
msg->attributes = g_malloc0 ((n_attributes + 1) * sizeof (StunAttribute *)); msg = stun_message_new (ntohs (*(guint16 *) s), s + 4, n_attributes);
msg->attributes[n_attributes] = NULL;
/* unpack attributes */ /* unpack attributes */
......
...@@ -104,7 +104,7 @@ stun_message_init (StunMessage *msg, guint type, gchar *id); ...@@ -104,7 +104,7 @@ stun_message_init (StunMessage *msg, guint type, gchar *id);
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
StunMessage * StunMessage *
stun_message_new (guint type, gchar *id); stun_message_new (guint type, gchar *id, guint n_attributes);
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
StunMessage * StunMessage *
......
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