Commit 4f456a46 authored by Olivier Crête's avatar Olivier Crête

Remove the "length" parameter from NiceOutputMessage

It was used correctly only half the time anyway
parent 5c235a86
...@@ -215,9 +215,13 @@ gsize ...@@ -215,9 +215,13 @@ gsize
memcpy_buffer_to_input_message (NiceInputMessage *message, memcpy_buffer_to_input_message (NiceInputMessage *message,
const guint8 *buffer, gsize buffer_length); const guint8 *buffer, gsize buffer_length);
guint8 * guint8 *
compact_input_message (NiceInputMessage *message, gsize *buffer_length); compact_input_message (const NiceInputMessage *message, gsize *buffer_length);
guint8 * guint8 *
compact_output_message (const NiceOutputMessage *message, gsize *buffer_length); compact_output_message (const NiceOutputMessage *message, gsize *buffer_length);
gsize
output_message_get_size (const NiceOutputMessage *message);
#endif /*_NICE_AGENT_PRIV_H */ #endif /*_NICE_AGENT_PRIV_H */
...@@ -84,7 +84,8 @@ ...@@ -84,7 +84,8 @@
#define MAX_TCP_MTU 1400 /* Use 1400 because of VPNs and we assume IEE 802.3 */ #define MAX_TCP_MTU 1400 /* Use 1400 because of VPNs and we assume IEE 802.3 */
static void static void
nice_debug_message_composition (NiceInputMessage *messages, guint n_messages); nice_debug_input_message_composition (const NiceInputMessage *messages,
guint n_messages);
G_DEFINE_TYPE (NiceAgent, nice_agent, G_TYPE_OBJECT); G_DEFINE_TYPE (NiceAgent, nice_agent, G_TYPE_OBJECT);
...@@ -1050,7 +1051,8 @@ pseudo_tcp_socket_send_messages (PseudoTcpSocket *self, ...@@ -1050,7 +1051,8 @@ pseudo_tcp_socket_send_messages (PseudoTcpSocket *self,
* used in reliable mode, and there is no concept of a ‘message’, but is * used in reliable mode, and there is no concept of a ‘message’, but is
* necessary because the calling API has no way of returning to the client * necessary because the calling API has no way of returning to the client
* and indicating that a message was partially sent. */ * and indicating that a message was partially sent. */
if (message->length > pseudo_tcp_socket_get_available_send_space (self)) { if (output_message_get_size (message) >
pseudo_tcp_socket_get_available_send_space (self)) {
return i; return i;
} }
...@@ -1232,7 +1234,7 @@ pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data) ...@@ -1232,7 +1234,7 @@ pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data)
nice_debug ("%s: Client buffers case: Received %d valid messages:", nice_debug ("%s: Client buffers case: Received %d valid messages:",
G_STRFUNC, n_valid_messages); G_STRFUNC, n_valid_messages);
nice_debug_message_composition (component->recv_messages, nice_debug_input_message_composition (component->recv_messages,
component->n_recv_messages); component->n_recv_messages);
if (n_valid_messages < 0) { if (n_valid_messages < 0) {
...@@ -1292,8 +1294,6 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket, ...@@ -1292,8 +1294,6 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket,
if (component->selected_pair.local != NULL) { if (component->selected_pair.local != NULL) {
NiceSocket *sock; NiceSocket *sock;
NiceAddress *addr; NiceAddress *addr;
GOutputVector local_buf;
NiceOutputMessage local_message;
sock = component->selected_pair.local->sockptr; sock = component->selected_pair.local->sockptr;
...@@ -1312,15 +1312,8 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket, ...@@ -1312,15 +1312,8 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket,
addr = &component->selected_pair.remote->addr; addr = &component->selected_pair.remote->addr;
local_buf.buffer = buffer; if (nice_socket_send (sock, addr, len, buffer))
local_buf.size = len;
local_message.buffers = &local_buf;
local_message.n_buffers = 1;
local_message.length = len;
if (nice_socket_send_messages (sock, addr, &local_message, 1)) {
return WR_SUCCESS; return WR_SUCCESS;
}
} else { } else {
nice_debug ("%s: WARNING: Failed to send pseudo-TCP packet from agent %p " nice_debug ("%s: WARNING: Failed to send pseudo-TCP packet from agent %p "
"as no pair has been selected yet.", G_STRFUNC, component->agent); "as no pair has been selected yet.", G_STRFUNC, component->agent);
...@@ -2749,13 +2742,14 @@ done: ...@@ -2749,13 +2742,14 @@ done:
/* Print the composition of an array of messages. No-op if debugging is /* Print the composition of an array of messages. No-op if debugging is
* disabled. */ * disabled. */
static void static void
nice_debug_message_composition (NiceInputMessage *messages, guint n_messages) nice_debug_input_message_composition (const NiceInputMessage *messages,
guint n_messages)
{ {
#ifndef NDEBUG #ifndef NDEBUG
guint i; guint i;
for (i = 0; i < n_messages; i++) { for (i = 0; i < n_messages; i++) {
NiceInputMessage *message = &messages[i]; const NiceInputMessage *message = &messages[i];
guint j; guint j;
nice_debug ("Message %p (from: %p, length: %" G_GSIZE_FORMAT ")", message, nice_debug ("Message %p (from: %p, length: %" G_GSIZE_FORMAT ")", message,
...@@ -2774,30 +2768,20 @@ nice_debug_message_composition (NiceInputMessage *messages, guint n_messages) ...@@ -2774,30 +2768,20 @@ nice_debug_message_composition (NiceInputMessage *messages, guint n_messages)
#endif #endif
} }
/* Concatenate all the buffers in the given @recv_message into a single, newly static guint8 *
* allocated, monolithic buffer which is returned. The length of the new buffer compact_message (const NiceOutputMessage *message, gsize buffer_length)
* is returned in @buffer_length, and should be equal to the length field of
* @recv_message.
*
* The return value must be freed with g_free(). */
guint8 *
compact_input_message (NiceInputMessage *message, gsize *buffer_length)
{ {
guint8 *buffer; guint8 *buffer;
gsize offset = 0; gsize offset = 0;
guint i; guint i;
nice_debug ("%s: **WARNING: SLOW PATH**", G_STRFUNC); buffer = g_malloc (buffer_length);
nice_debug_message_composition (message, 1);
*buffer_length = message->length;
buffer = g_malloc (*buffer_length);
for (i = 0; for (i = 0;
(message->n_buffers >= 0 && i < (guint) message->n_buffers) || (message->n_buffers >= 0 && i < (guint) message->n_buffers) ||
(message->n_buffers < 0 && message->buffers[i].buffer != NULL); (message->n_buffers < 0 && message->buffers[i].buffer != NULL);
i++) { i++) {
gsize len = MIN (*buffer_length - offset, message->buffers[i].size); gsize len = MIN (buffer_length - offset, message->buffers[i].size);
memcpy (buffer + offset, message->buffers[i].buffer, len); memcpy (buffer + offset, message->buffers[i].buffer, len);
offset += len; offset += len;
} }
...@@ -2805,6 +2789,25 @@ compact_input_message (NiceInputMessage *message, gsize *buffer_length) ...@@ -2805,6 +2789,25 @@ compact_input_message (NiceInputMessage *message, gsize *buffer_length)
return buffer; return buffer;
} }
/* Concatenate all the buffers in the given @recv_message into a single, newly
* allocated, monolithic buffer which is returned. The length of the new buffer
* is returned in @buffer_length, and should be equal to the length field of
* @recv_message.
*
* The return value must be freed with g_free(). */
guint8 *
compact_input_message (const NiceInputMessage *message, gsize *buffer_length)
{
nice_debug ("%s: **WARNING: SLOW PATH**", G_STRFUNC);
nice_debug_input_message_composition (message, 1);
/* This works as long as NiceInputMessage is a subset of eNiceOutputMessage */
*buffer_length = message->length;
return compact_message ((NiceOutputMessage *) message, *buffer_length);
}
/* Returns the number of bytes copied. Silently drops any data from @buffer /* Returns the number of bytes copied. Silently drops any data from @buffer
* which doesn’t fit in @message. */ * which doesn’t fit in @message. */
gsize gsize
...@@ -2834,7 +2837,7 @@ memcpy_buffer_to_input_message (NiceInputMessage *message, ...@@ -2834,7 +2837,7 @@ memcpy_buffer_to_input_message (NiceInputMessage *message,
message->length += len; message->length += len;
} }
nice_debug_message_composition (message, 1); nice_debug_input_message_composition (message, 1);
if (buffer_length > 0) { if (buffer_length > 0) {
g_warning ("Dropped %" G_GSIZE_FORMAT " bytes of data from the end of " g_warning ("Dropped %" G_GSIZE_FORMAT " bytes of data from the end of "
...@@ -2855,9 +2858,27 @@ memcpy_buffer_to_input_message (NiceInputMessage *message, ...@@ -2855,9 +2858,27 @@ memcpy_buffer_to_input_message (NiceInputMessage *message,
guint8 * guint8 *
compact_output_message (const NiceOutputMessage *message, gsize *buffer_length) compact_output_message (const NiceOutputMessage *message, gsize *buffer_length)
{ {
/* This works as long as NiceInputMessage and NiceOutputMessage are layed out nice_debug ("%s: **WARNING: SLOW PATH**", G_STRFUNC);
* identically. */
return compact_input_message ((NiceInputMessage *) message, buffer_length); *buffer_length = output_message_get_size (message);
return compact_message (message, *buffer_length);
}
gsize
output_message_get_size (const NiceOutputMessage *message)
{
guint i;
gsize message_len = 0;
/* Find the total size of the message */
for (i = 0;
(message->n_buffers >= 0 && i < (guint) message->n_buffers) ||
(message->n_buffers < 0 && message->buffers[i].buffer != NULL);
i++)
message_len += message->buffers[i].size;
return message_len;
} }
/** /**
...@@ -3034,7 +3055,7 @@ nice_agent_recv_messages_blocking_or_nonblocking (NiceAgent *agent, ...@@ -3034,7 +3055,7 @@ nice_agent_recv_messages_blocking_or_nonblocking (NiceAgent *agent,
} }
nice_debug ("%s: (%s):", G_STRFUNC, blocking ? "blocking" : "non-blocking"); nice_debug ("%s: (%s):", G_STRFUNC, blocking ? "blocking" : "non-blocking");
nice_debug_message_composition (messages, n_messages); nice_debug_input_message_composition (messages, n_messages);
/* Set the component’s receive buffer. */ /* Set the component’s receive buffer. */
context = component_dup_io_context (component); context = component_dup_io_context (component);
...@@ -3320,8 +3341,8 @@ nice_agent_send ( ...@@ -3320,8 +3341,8 @@ nice_agent_send (
const gchar *buf) const gchar *buf)
{ {
GOutputVector local_buf = { buf, len }; GOutputVector local_buf = { buf, len };
NiceOutputMessage local_message = { &local_buf, 1, len };
gint n_sent_messages; gint n_sent_messages;
NiceOutputMessage local_message = { &local_buf, 1 };
n_sent_messages = nice_agent_send_messages_nonblocking (agent, stream_id, n_sent_messages = nice_agent_send_messages_nonblocking (agent, stream_id,
component_id, &local_message, 1, NULL, NULL); component_id, &local_message, 1, NULL, NULL);
......
...@@ -176,7 +176,6 @@ typedef struct { ...@@ -176,7 +176,6 @@ typedef struct {
* which contain data to transmit for this message * which contain data to transmit for this message
* @n_buffers: number of #GOutputVectors in @buffers, or -1 to indicate @buffers * @n_buffers: number of #GOutputVectors in @buffers, or -1 to indicate @buffers
* is %NULL-terminated * is %NULL-terminated
* @length: total number of valid bytes contiguously stored in @buffers
* *
* Represents a single message to transmit on the network. For * Represents a single message to transmit on the network. For
* reliable connections, this is essentially just an array of * reliable connections, this is essentially just an array of
...@@ -197,7 +196,6 @@ typedef struct { ...@@ -197,7 +196,6 @@ typedef struct {
typedef struct { typedef struct {
GOutputVector *buffers; GOutputVector *buffers;
gint n_buffers; gint n_buffers;
gsize length;
} NiceOutputMessage; } NiceOutputMessage;
......
...@@ -403,7 +403,7 @@ nice_output_stream_write (GOutputStream *stream, const void *buffer, gsize count ...@@ -403,7 +403,7 @@ nice_output_stream_write (GOutputStream *stream, const void *buffer, gsize count
do { do {
GOutputVector local_buf = { (const guint8 *) buffer + len, count - len }; GOutputVector local_buf = { (const guint8 *) buffer + len, count - len };
NiceOutputMessage local_message = {&local_buf, 1, count - len}; NiceOutputMessage local_message = {&local_buf, 1};
/* Have to unlock while calling into the agent because /* Have to unlock while calling into the agent because
* it will take the agent lock which will cause a deadlock if one of * it will take the agent lock which will cause a deadlock if one of
...@@ -523,7 +523,7 @@ nice_output_stream_write_nonblocking (GPollableOutputStream *stream, ...@@ -523,7 +523,7 @@ nice_output_stream_write_nonblocking (GPollableOutputStream *stream,
NiceOutputStreamPrivate *priv = NICE_OUTPUT_STREAM (stream)->priv; NiceOutputStreamPrivate *priv = NICE_OUTPUT_STREAM (stream)->priv;
NiceAgent *agent; /* owned */ NiceAgent *agent; /* owned */
GOutputVector local_buf = { buffer, count }; GOutputVector local_buf = { buffer, count };
NiceOutputMessage local_message = { &local_buf, 1, count }; NiceOutputMessage local_message = { &local_buf, 1 };
gint n_sent_messages; gint n_sent_messages;
/* Closed streams are not writeable. */ /* Closed streams are not writeable. */
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#endif #endif
#include "http.h" #include "http.h"
#include "agent-priv.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -165,7 +166,6 @@ nice_http_socket_new (NiceSocket *base_socket, ...@@ -165,7 +166,6 @@ nice_http_socket_new (NiceSocket *base_socket,
local_bufs.size = strlen (msg); local_bufs.size = strlen (msg);
local_messages.buffers = &local_bufs; local_messages.buffers = &local_bufs;
local_messages.n_buffers = 1; local_messages.n_buffers = 1;
local_messages.length = local_bufs.size;
nice_socket_send_messages (priv->base_socket, NULL, &local_messages, 1); nice_socket_send_messages (priv->base_socket, NULL, &local_messages, 1);
priv->state = HTTP_STATE_INIT; priv->state = HTTP_STATE_INIT;
...@@ -266,7 +266,6 @@ memcpy_ring_buffer_to_input_messages (HttpPriv *priv, ...@@ -266,7 +266,6 @@ memcpy_ring_buffer_to_input_messages (HttpPriv *priv,
message->buffers[j].size = message->buffers[j].size =
memcpy_ring_buffer_to_buffer (priv, memcpy_ring_buffer_to_buffer (priv,
message->buffers[j].buffer, message->buffers[j].size); message->buffers[j].buffer, message->buffers[j].size);
message->length += message->buffers[j].size;
} }
} }
...@@ -618,15 +617,15 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to, ...@@ -618,15 +617,15 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const NiceOutputMessage *message = &messages[i]; const NiceOutputMessage *message = &messages[i];
struct to_be_sent *tbs = NULL; struct to_be_sent *tbs = NULL;
guint j; guint j;
gsize message_len_remaining = message->length; gsize message_len_remaining = output_message_get_size (message);
gsize offset = 0; gsize offset = 0;
if (message->length == 0) if (message_len_remaining == 0)
continue; continue;
tbs = g_slice_new0 (struct to_be_sent); tbs = g_slice_new0 (struct to_be_sent);
tbs->buf = g_malloc (message->length); tbs->buf = g_malloc (message_len_remaining);
tbs->length = message->length; tbs->length = message_len_remaining;
if (to) if (to)
tbs->to = *to; tbs->to = *to;
g_queue_push_tail (&priv->send_queue, tbs); g_queue_push_tail (&priv->send_queue, tbs);
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#endif #endif
#include "pseudossl.h" #include "pseudossl.h"
#include "agent-priv.h"
#include <string.h> #include <string.h>
...@@ -229,12 +230,15 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to, ...@@ -229,12 +230,15 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const NiceOutputMessage *message = &messages[i]; const NiceOutputMessage *message = &messages[i];
guint j; guint j;
gsize offset = 0; gsize offset = 0;
gsize message_len;
tbs = g_slice_new0 (struct to_be_sent); tbs = g_slice_new0 (struct to_be_sent);
/* Compact the buffer. */ message_len = output_message_get_size (message);
tbs->buf = g_malloc (message->length);
tbs->length = message->length; /* Compact the buffer. */
tbs->buf = g_malloc (message_len);
tbs->length = message_len;
if (to != NULL) if (to != NULL)
tbs->to = *to; tbs->to = *to;
g_queue_push_tail (&priv->send_queue, tbs); g_queue_push_tail (&priv->send_queue, tbs);
...@@ -246,12 +250,12 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to, ...@@ -246,12 +250,12 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const GOutputVector *buffer = &message->buffers[j]; const GOutputVector *buffer = &message->buffers[j];
gsize len; gsize len;
len = MIN (message->length - offset, buffer->size); len = MIN (message_len - offset, buffer->size);
memcpy (tbs->buf + offset, buffer->buffer, len); memcpy (tbs->buf + offset, buffer->buffer, len);
offset += len; offset += len;
} }
g_assert_cmpuint (offset, ==, message->length); g_assert (offset == message_len);
} }
} }
......
...@@ -146,7 +146,7 @@ nice_socket_send (NiceSocket *sock, const NiceAddress *to, gsize len, ...@@ -146,7 +146,7 @@ nice_socket_send (NiceSocket *sock, const NiceAddress *to, gsize len,
const gchar *buf) const gchar *buf)
{ {
GOutputVector local_buf = { buf, len }; GOutputVector local_buf = { buf, len };
NiceOutputMessage local_message = { &local_buf, 1, len }; NiceOutputMessage local_message = { &local_buf, 1};
gint ret; gint ret;
ret = sock->send_messages (sock, to, &local_message, 1); ret = sock->send_messages (sock, to, &local_message, 1);
...@@ -169,4 +169,3 @@ nice_socket_free (NiceSocket *sock) ...@@ -169,4 +169,3 @@ nice_socket_free (NiceSocket *sock)
g_slice_free (NiceSocket,sock); g_slice_free (NiceSocket,sock);
} }
} }
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#endif #endif
#include "socks5.h" #include "socks5.h"
#include "agent-priv.h"
#include <string.h> #include <string.h>
...@@ -463,8 +464,8 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to, ...@@ -463,8 +464,8 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
tbs = g_slice_new0 (struct to_be_sent); tbs = g_slice_new0 (struct to_be_sent);
/* Compact the buffer. */ /* Compact the buffer. */
tbs->buf = g_malloc (message->length); tbs->length = output_message_get_size (message);
tbs->length = message->length; tbs->buf = g_malloc (tbs->length);
if (to != NULL) if (to != NULL)
tbs->to = *to; tbs->to = *to;
g_queue_push_tail (&priv->send_queue, tbs); g_queue_push_tail (&priv->send_queue, tbs);
...@@ -476,12 +477,12 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to, ...@@ -476,12 +477,12 @@ add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const GOutputVector *buffer = &message->buffers[j]; const GOutputVector *buffer = &message->buffers[j];
gsize len; gsize len;
len = MIN (message->length - offset, buffer->size); len = MIN (tbs->length - offset, buffer->size);
memcpy (tbs->buf + offset, buffer->buffer, len); memcpy (tbs->buf + offset, buffer->buffer, len);
offset += len; offset += len;
} }
g_assert_cmpuint (offset, ==, message->length); g_assert (offset == tbs->length);
} }
} }
......
...@@ -78,7 +78,7 @@ static gboolean socket_is_reliable (NiceSocket *sock); ...@@ -78,7 +78,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static void add_to_be_sent (NiceSocket *sock, const NiceOutputMessage *message, static void add_to_be_sent (NiceSocket *sock, const NiceOutputMessage *message,
gsize message_offset, gboolean head); gsize message_offset, gsize message_len, gboolean head);
static void free_to_be_sent (struct to_be_sent *tbs); static void free_to_be_sent (struct to_be_sent *tbs);
static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition, static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition,
gpointer data); gpointer data);
...@@ -258,12 +258,15 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message) ...@@ -258,12 +258,15 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message)
TcpPriv *priv = sock->priv; TcpPriv *priv = sock->priv;
gssize ret; gssize ret;
GError *gerr = NULL; GError *gerr = NULL;
gsize message_len;
/* Don't try to access the socket if it had an error, otherwise we risk a /* Don't try to access the socket if it had an error, otherwise we risk a
* crash with SIGPIPE (Broken pipe) */ * crash with SIGPIPE (Broken pipe) */
if (priv->error) if (priv->error)
return -1; return -1;
message_len = output_message_get_size (message);
/* First try to send the data, don't send it later if it can be sent now /* First try to send the data, don't send it later if it can be sent now
* this way we avoid allocating memory on every send */ * this way we avoid allocating memory on every send */
if (g_queue_is_empty (&priv->send_queue)) { if (g_queue_is_empty (&priv->send_queue)) {
...@@ -274,15 +277,15 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message) ...@@ -274,15 +277,15 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message)
if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK) || if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK) ||
g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_FAILED)) { g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_FAILED)) {
/* Queue the message and send it later. */ /* Queue the message and send it later. */
add_to_be_sent (sock, message, 0, FALSE); add_to_be_sent (sock, message, 0, message_len, FALSE);
ret = message->length; ret = message_len;
} }
g_error_free (gerr); g_error_free (gerr);
} else if ((gsize) ret < message->length) { } else if ((gsize) ret < message_len) {
/* Partial send. */ /* Partial send. */
add_to_be_sent (sock, message, ret, TRUE); add_to_be_sent (sock, message, ret, message_len, TRUE);
ret = message->length; ret = message_len;
} }
} else { } else {
/* FIXME: This dropping will break http/socks5/etc /* FIXME: This dropping will break http/socks5/etc
...@@ -305,8 +308,8 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message) ...@@ -305,8 +308,8 @@ socket_send_message (NiceSocket *sock, const NiceOutputMessage *message)
} }
/* Queue the message and send it later. */ /* Queue the message and send it later. */
add_to_be_sent (sock, message, 0, FALSE); add_to_be_sent (sock, message, 0, message_len, FALSE);
ret = message->length; ret = message_len;
} }
return ret; return ret;
...@@ -389,9 +392,9 @@ socket_send_more ( ...@@ -389,9 +392,9 @@ socket_send_more (
if (gerr != NULL && if (gerr != NULL &&
g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
GOutputVector local_buf = { tbs->buf, tbs->length }; GOutputVector local_buf = { tbs->buf, tbs->length };
NiceOutputMessage local_message = {&local_buf, 1, local_buf.size}; NiceOutputMessage local_message = {&local_buf, 1};
add_to_be_sent (sock, &local_message, 0, TRUE); add_to_be_sent (sock, &local_message, 0, local_buf.size, TRUE);
free_to_be_sent (tbs); free_to_be_sent (tbs);
g_error_free (gerr); g_error_free (gerr);
break; break;
...@@ -399,9 +402,9 @@ socket_send_more ( ...@@ -399,9 +402,9 @@ socket_send_more (
g_error_free (gerr); g_error_free (gerr);
} else if (ret < (int) tbs->length) { } else if (ret < (int) tbs->length) {
GOutputVector local_buf = { tbs->buf + ret, tbs->length - ret }; GOutputVector local_buf = { tbs->buf + ret, tbs->length - ret };
NiceOutputMessage local_message = {&local_buf, 1, local_buf.size}; NiceOutputMessage local_message = {&local_buf, 1};
add_to_be_sent (sock, &local_message, 0, TRUE); add_to_be_sent (sock, &local_message, 0, local_buf.size, TRUE);
free_to_be_sent (tbs); free_to_be_sent (tbs);
break; break;
} }
...@@ -424,22 +427,24 @@ socket_send_more ( ...@@ -424,22 +427,24 @@ socket_send_more (
/* Queue data starting at byte offset @message_offset from @message’s /* Queue data starting at byte offset @message_offset from @message’s
* buffers. */ * buffers.
*
* Returns the message's length */
static void static void
add_to_be_sent (NiceSocket *sock, const NiceOutputMessage *message, add_to_be_sent (NiceSocket *sock, const NiceOutputMessage *message,
gsize message_offset, gboolean head) gsize message_offset, gsize message_len, gboolean head)
{ {
TcpPriv *priv = sock->priv; TcpPriv *priv = sock->priv;
struct to_be_sent *tbs; struct to_be_sent *tbs;
guint j; guint j;
gsize offset = 0; gsize offset = 0;
if (message_offset >= message->length) if (message_offset >= message_len)
return; return;
tbs = g_slice_new0 (struct to_be_sent); tbs = g_slice_new0 (struct to_be_sent);
tbs->buf = g_malloc (message->length - message_offset); tbs->length = message_len - message_offset;
tbs->length = message->length - message_offset; tbs->buf = g_malloc (tbs->length);
tbs->can_drop = !head; tbs->can_drop = !head;
if (head) if (head)
......
...@@ -250,7 +250,6 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -250,7 +250,6 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
local_bufs = g_malloc_n (n_bufs + 2, sizeof (GOutputVector)); local_bufs = g_malloc_n (n_bufs + 2, sizeof (GOutputVector));
local_message.buffers = local_bufs; local_message.buffers = local_bufs;
local_message.n_buffers = n_bufs + 2; local_message.n_buffers = n_bufs + 2;
local_message.length = message->length;
/* Copy the existing buffers across. */ /* Copy the existing buffers across. */
for (j = 0; j < n_bufs; j++) { for (j = 0; j < n_bufs; j++) {
...@@ -260,11 +259,10 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -260,11 +259,10 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
/* Header buffer. */ /* Header buffer. */
if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
header_buf = htons (message->length); header_buf = htons (output_message_get_size (message));
local_bufs[0].buffer = &header_buf; local_bufs[0].buffer = &header_buf;
local_bufs[0].size = sizeof (header_buf); local_bufs[0].size = sizeof (header_buf);
local_message.length += sizeof (header_buf);
} else { } else {
/* Skip over the allocated header buffer. */ /* Skip over the allocated header buffer. */
local_message.buffers++; local_message.buffers++;
...@@ -274,11 +272,11 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -274,11 +272,11 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
/* Tail buffer. */ /* Tail buffer. */
if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 || if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) { priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) {
gsize padlen = (message->length % 4) ? 4 - (message->length % 4) : 0; gsize message_len = output_message_get_size (message);
gsize padlen = (message_len % 4) ? 4 - (message_len % 4) : 0;
local_bufs[n_bufs].buffer = &padbuf; local_bufs[n_bufs].buffer = &padbuf;
local_bufs[n_bufs].size = padlen; local_bufs[n_bufs].size = padlen;
local_message.length += padlen;
} else { } else {
/* Skip over the allocated tail buffer. */ /* Skip over the allocated tail buffer. */
local_message.n_buffers--; local_message.n_buffers--;
...@@ -289,7 +287,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -289,7 +287,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
g_free (local_bufs); g_free (local_bufs);
if (ret == 1) if (ret == 1)
return local_message.length; return output_message_get_size (&local_message);
return ret; return ret;
} }
......
...@@ -576,16 +576,18 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -576,16 +576,18 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
if (binding) { if (binding) {
if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 || if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) { priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) {
if (message->length + sizeof(uint32_t) <= sizeof(buffer)) { gsize message_len = output_message_get_size (message);
if (message_len + sizeof(uint32_t) <= sizeof(buffer)) {
guint j; guint j;
uint16_t len16, channel16; uint16_t len16, channel16;
gsize message_offset = 0; gsize message_offset = 0;
len16 = htons ((uint16_t) message->length); len16 = htons ((uint16_t) message_len);
channel16 = htons (binding->channel); channel16 = htons (binding->channel);
memcpy (buffer, &channel16, sizeof(uint16_t)); memcpy (buffer, &channel16, sizeof(uint16_t));
memcpy (buffer + sizeof(uint16_t), &len16,sizeof(uint16_t)); memcpy (buffer + sizeof(uint16_t), &len16, sizeof(uint16_t));
/* FIXME: Slow path! This should be replaced by code which manipulates /* FIXME: Slow path! This should be replaced by code which manipulates
* the GOutputVector array, rather than the buffer contents * the GOutputVector array, rather than the buffer contents
...@@ -597,25 +599,21 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -597,25 +599,21 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
const GOutputVector *out_buf = &message->buffers[j]; const GOutputVector *out_buf = &message->buffers[j];
gsize out_len; gsize out_len;
out_len = MIN (message->length - message_offset, out_buf->size); out_len = MIN (message_len - message_offset, out_buf->size);
memcpy (buffer + sizeof (uint32_t) + message_offset, memcpy (buffer + sizeof (uint32_t) + message_offset,
out_buf->buffer, out_len); out_buf->buffer, out_len);
message_offset += out_len; message_offset += out_len;
} }
msg_len = message->length + sizeof(uint32_t); msg_len = message_len + sizeof(uint32_t);
} else { } else {
return 0; return 0;
} }
} else { } else {
NiceOutputMessage local_message = {
message->buffers, message->n_buffers, message->length
};
ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr, ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr,
&local_message, 1); message, 1);
if (ret == 1) if (ret == 1)
return message->length; return output_message_get_size (message);
return ret; return ret;
} }
} else { } else {
...@@ -708,7 +706,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, ...@@ -708,7 +706,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
return msg_len; return msg_len;
} else { } else {
GOutputVector local_buf = { buffer, msg_len }; GOutputVector local_buf = { buffer, msg_len };
NiceOutputMessage local_message = {&local_buf, 1, msg_len}; NiceOutputMessage local_message = {&local_buf, 1};
ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr, ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr,
&local_message, 1); &local_message, 1);
...@@ -722,7 +720,7 @@ send: ...@@ -722,7 +720,7 @@ send:
/* Error condition pass through to the base socket. */ /* Error condition pass through to the base socket. */
ret = nice_socket_send_messages (priv->base_socket, to, message, 1); ret = nice_socket_send_messages (priv->base_socket, to, message, 1);
if (ret == 1) if (ret == 1)
return message->length; return output_message_get_size (message);
return ret; return ret;
} }
...@@ -915,8 +913,6 @@ nice_turn_socket_parse_recv_message (NiceSocket *sock, NiceSocket **from_sock, ...@@ -915,8 +913,6 @@ nice_turn_socket_parse_recv_message (NiceSocket *sock, NiceSocket **from_sock,
message->buffers[0].buffer != NULL && message->buffers[0].buffer != NULL &&
message->buffers[1].buffer == NULL)) { message->buffers[1].buffer == NULL)) {
/* Fast path. Single massive buffer. */ /* Fast path. Single massive buffer. */
g_assert_cmpuint (message->length, <=, message->buffers[0].size);
len = nice_turn_socket_parse_recv (sock, from_sock, len = nice_turn_socket_parse_recv (sock, from_sock,
message->from, message->length, message->buffers[0].buffer, message->from, message->length, message->buffers[0].buffer,
message->from, message->buffers[0].buffer, message->length); message->from, message->buffers[0].buffer, message->length);
......
...@@ -273,7 +273,6 @@ test_multi_message_recv (guint n_sends, guint n_receives, ...@@ -273,7 +273,6 @@ test_multi_message_recv (guint n_sends, guint n_receives,
send_messages[i].buffers = send_bufs + i * n_bufs_per_message; send_messages[i].buffers = send_bufs + i * n_bufs_per_message;
send_messages[i].n_buffers = n_bufs_per_message; send_messages[i].n_buffers = n_bufs_per_message;
send_messages[i].length = 0;
} }
/* Set up the receive buffers. Yay for dynamic tests! */ /* Set up the receive buffers. Yay for dynamic tests! */
......
...@@ -549,12 +549,12 @@ generate_messages_to_transmit (TestIOStreamThreadData *data, ...@@ -549,12 +549,12 @@ generate_messages_to_transmit (TestIOStreamThreadData *data,
NiceOutputMessage *message = &((*messages)[i]); NiceOutputMessage *message = &((*messages)[i]);
guint j; guint j;
gsize max_message_size; gsize max_message_size;
gsize message_len = 0;
message->n_buffers = message->n_buffers =
generate_buffer_count (test_data->transmit.buffer_count_strategy, generate_buffer_count (test_data->transmit.buffer_count_strategy,
test_data->transmit_size_rand, buffer_offset); test_data->transmit_size_rand, buffer_offset);
message->buffers = g_malloc_n (message->n_buffers, sizeof (GOutputVector)); message->buffers = g_malloc_n (message->n_buffers, sizeof (GOutputVector));
message->length = 0;
/* Limit the overall message size to the smaller of (n_bytes / n_messages) /* Limit the overall message size to the smaller of (n_bytes / n_messages)
* and MAX_MESSAGE_SIZE, to ensure each message is non-empty. */ * and MAX_MESSAGE_SIZE, to ensure each message is non-empty. */
...@@ -572,12 +572,12 @@ generate_messages_to_transmit (TestIOStreamThreadData *data, ...@@ -572,12 +572,12 @@ generate_messages_to_transmit (TestIOStreamThreadData *data,
buf_len = buf_len =
MIN (buf_len, MIN (buf_len,
test_data->n_bytes - test_data->transmitted_bytes - total_buf_len); test_data->n_bytes - test_data->transmitted_bytes - total_buf_len);
buf_len = MIN (buf_len, max_message_size - message->length); buf_len = MIN (buf_len, max_message_size - message_len);
buffer->size = buf_len; buffer->size = buf_len;
buf = g_malloc (buffer->size); buf = g_malloc (buffer->size);
buffer->buffer = buf; buffer->buffer = buf;
message->length += buf_len; message_len += buf_len;
total_buf_len += buf_len; total_buf_len += buf_len;
/* Fill it with data. */ /* Fill it with data. */
...@@ -587,13 +587,13 @@ generate_messages_to_transmit (TestIOStreamThreadData *data, ...@@ -587,13 +587,13 @@ generate_messages_to_transmit (TestIOStreamThreadData *data,
buffer_offset += buf_len; buffer_offset += buf_len;
/* Reached the maximum UDP payload size? */ /* Reached the maximum UDP payload size? */
if (message->length >= max_message_size) { if (message_len >= max_message_size) {
message->n_buffers = j + 1; message->n_buffers = j + 1;
break; break;
} }
} }
g_assert_cmpuint (message->length, <=, max_message_size); g_assert_cmpuint (message_len, <=, max_message_size);
} }
} }
...@@ -613,6 +613,22 @@ notify_transmitted_buffer (TestIOStreamThreadData *data, gsize buffer_offset, ...@@ -613,6 +613,22 @@ notify_transmitted_buffer (TestIOStreamThreadData *data, gsize buffer_offset,
g_free (*buf); g_free (*buf);
} }
static gsize
output_message_get_size (const NiceOutputMessage *message)
{
guint i;
gsize message_len = 0;
/* Find the total size of the message */
for (i = 0;
(message->n_buffers >= 0 && i < (guint) message->n_buffers) ||
(message->n_buffers < 0 && message->buffers[i].buffer != NULL);
i++)
message_len += message->buffers[i].size;
return message_len;
}
/* Similar to notify_transmitted_buffer(), except it operates on an array of /* Similar to notify_transmitted_buffer(), except it operates on an array of
* messages from generate_messages_to_transmit(). */ * messages from generate_messages_to_transmit(). */
static void static void
...@@ -632,7 +648,7 @@ notify_transmitted_messages (TestIOStreamThreadData *data, gsize buffer_offset, ...@@ -632,7 +648,7 @@ notify_transmitted_messages (TestIOStreamThreadData *data, gsize buffer_offset,
guint j; guint j;
if (i < (guint) n_sent_messages) if (i < (guint) n_sent_messages)
test_data->transmitted_bytes += message->length; test_data->transmitted_bytes += output_message_get_size (message);
for (j = 0; j < (guint) message->n_buffers; j++) { for (j = 0; j < (guint) message->n_buffers; j++) {
GOutputVector *buffer = &message->buffers[j]; GOutputVector *buffer = &message->buffers[j];
......
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