Commit ebc37d94 authored by Dafydd Harries's avatar Dafydd Harries

make UDP code use NiceAddress instead of sockaddr_in

darcs-hash:20070205161016-c9803-f3eefe210e39ebae3dd075409dd83211aeee7148.gz
parent ce578135
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <glib.h> #include <glib.h>
...@@ -202,7 +200,6 @@ nice_agent_add_local_host_candidate ( ...@@ -202,7 +200,6 @@ nice_agent_add_local_host_candidate (
{ {
NiceRNG *rng; NiceRNG *rng;
NiceCandidate *candidate; NiceCandidate *candidate;
struct sockaddr_in sin;
candidate = nice_candidate_new (NICE_CANDIDATE_TYPE_HOST); candidate = nice_candidate_new (NICE_CANDIDATE_TYPE_HOST);
candidate->id = agent->next_candidate_id++; candidate->id = agent->next_candidate_id++;
...@@ -219,17 +216,13 @@ nice_agent_add_local_host_candidate ( ...@@ -219,17 +216,13 @@ nice_agent_add_local_host_candidate (
nice_rng_generate_bytes_print (rng, 8, candidate->password); nice_rng_generate_bytes_print (rng, 8, candidate->password);
nice_rng_free (rng); nice_rng_free (rng);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (address->addr_ipv4);
sin.sin_port = 0;
/* XXX: handle error */ /* XXX: handle error */
if (!nice_udp_socket_factory_make (agent->socket_factory, if (!nice_udp_socket_factory_make (agent->socket_factory,
&(candidate->sock), &sin)) &(candidate->sock), address))
g_assert_not_reached (); g_assert_not_reached ();
candidate->addr.port = ntohs (candidate->sock.addr.sin_port); candidate->addr = candidate->sock.addr;
candidate->base_addr.port = ntohs (candidate->sock.addr.sin_port); candidate->base_addr = candidate->sock.addr;
} }
...@@ -377,7 +370,7 @@ _handle_stun ( ...@@ -377,7 +370,7 @@ _handle_stun (
NiceAgent *agent, NiceAgent *agent,
Stream *stream, Stream *stream,
NiceCandidate *local, NiceCandidate *local,
struct sockaddr_in from, NiceAddress from,
StunMessage *msg) StunMessage *msg)
{ {
GSList *i; GSList *i;
...@@ -490,7 +483,7 @@ RESPOND: ...@@ -490,7 +483,7 @@ RESPOND:
response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
msg->transaction_id, 2); msg->transaction_id, 2);
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)); from.addr_ipv4, from.port);
response->attributes[1] = stun_attribute_username_new (username); response->attributes[1] = stun_attribute_username_new (username);
len = stun_message_pack (response, &packed); len = stun_message_pack (response, &packed);
nice_udp_socket_send (&local->sock, &from, len, packed); nice_udp_socket_send (&local->sock, &from, len, packed);
...@@ -537,7 +530,7 @@ _nice_agent_recv ( ...@@ -537,7 +530,7 @@ _nice_agent_recv (
{ {
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
struct sockaddr_in from; NiceAddress from;
len = nice_udp_socket_recv (&(candidate->sock), &from, len = nice_udp_socket_recv (&(candidate->sock), &from,
sizeof (buf) / sizeof (gchar), buf); sizeof (buf) / sizeof (gchar), buf);
...@@ -722,14 +715,10 @@ nice_agent_send ( ...@@ -722,14 +715,10 @@ nice_agent_send (
{ {
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceAddress *addr; NiceAddress *addr;
struct sockaddr_in sockaddr;
sock = &component->active_candidate->sock; sock = &component->active_candidate->sock;
addr = component->peer_addr; addr = component->peer_addr;
sockaddr.sin_family = AF_INET; nice_udp_socket_send (sock, addr, len, buf);
sockaddr.sin_addr.s_addr = htonl (addr->addr_ipv4);
sockaddr.sin_port = htons (addr->port);
nice_udp_socket_send (sock, &sockaddr, len, buf);
} }
} }
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
#ifndef _AGENT_H #ifndef _AGENT_H
#define _AGENT_H #define _AGENT_H
#include <arpa/inet.h>
#include <glib.h> #include <glib.h>
#include "udp.h" #include "udp.h"
......
...@@ -23,7 +23,7 @@ nice_candidate_free (NiceCandidate *candidate) ...@@ -23,7 +23,7 @@ nice_candidate_free (NiceCandidate *candidate)
{ {
/* better way of checking if socket is allocated? */ /* better way of checking if socket is allocated? */
if (candidate->sock.addr.sin_addr.s_addr != 0) if (candidate->sock.addr.addr_ipv4 != 0)
nice_udp_socket_close (&(candidate->sock)); nice_udp_socket_close (&(candidate->sock));
g_slice_free (NiceCandidate, candidate); g_slice_free (NiceCandidate, candidate);
......
...@@ -31,7 +31,6 @@ main (void) ...@@ -31,7 +31,6 @@ main (void)
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
NiceUDPSocket *sock; NiceUDPSocket *sock;
struct sockaddr_in from = {0,};
nice_udp_fake_socket_factory_init (&factory); nice_udp_fake_socket_factory_init (&factory);
...@@ -45,7 +44,7 @@ main (void) ...@@ -45,7 +44,7 @@ main (void)
/* recieve an RTP packet */ /* recieve an RTP packet */
candidate = agent->local_candidates->data; candidate = agent->local_candidates->data;
sock = &(candidate->sock); sock = &(candidate->sock);
nice_udp_fake_socket_push_recv (sock, &from, 7, "\x80lalala"); nice_udp_fake_socket_push_recv (sock, &addr, 7, "\x80lalala");
nice_agent_recv (agent, candidate->id); nice_agent_recv (agent, candidate->id);
g_assert (cb_called == TRUE); g_assert (cb_called == TRUE);
......
...@@ -15,7 +15,6 @@ send_connectivity_check ( ...@@ -15,7 +15,6 @@ send_connectivity_check (
NiceCandidate *local; NiceCandidate *local;
NiceCandidate *remote; NiceCandidate *remote;
gchar *username; gchar *username;
struct sockaddr_in remote_sockaddr = {0,};
g_assert (agent->local_candidates); g_assert (agent->local_candidates);
g_assert (agent->local_candidates->data); g_assert (agent->local_candidates->data);
...@@ -30,10 +29,6 @@ send_connectivity_check ( ...@@ -30,10 +29,6 @@ send_connectivity_check (
username = g_strconcat (local->username, remote->username, NULL); username = g_strconcat (local->username, remote->username, NULL);
remote_sockaddr.sin_family = AF_INET;
remote_sockaddr.sin_addr.s_addr = htonl (remote_addr->addr_ipv4);
remote_sockaddr.sin_port = htons (remote_addr->port);
{ {
StunMessage *msg; StunMessage *msg;
gchar *packed; gchar *packed;
...@@ -42,7 +37,7 @@ send_connectivity_check ( ...@@ -42,7 +37,7 @@ send_connectivity_check (
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1); msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
msg->attributes[0] = stun_attribute_username_new (username); msg->attributes[0] = stun_attribute_username_new (username);
len = stun_message_pack (msg, &packed); len = stun_message_pack (msg, &packed);
nice_udp_fake_socket_push_recv (sock, &remote_sockaddr, len, packed); nice_udp_fake_socket_push_recv (sock, remote_addr, len, packed);
g_free (packed); g_free (packed);
stun_message_free (msg); stun_message_free (msg);
} }
...@@ -51,13 +46,13 @@ send_connectivity_check ( ...@@ -51,13 +46,13 @@ send_connectivity_check (
{ {
StunMessage *msg; StunMessage *msg;
struct sockaddr_in addr = {0,}; NiceAddress addr = {0,};
gchar packed[1024]; gchar packed[1024];
gchar *dump; gchar *dump;
guint len; guint len;
len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed); len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
g_assert (0 == memcmp (&addr, &remote_sockaddr, sizeof (addr))); g_assert (nice_address_equal (&addr, remote_addr));
msg = stun_message_unpack (len, packed); msg = stun_message_unpack (len, packed);
dump = stun_message_dump (msg); dump = stun_message_dump (msg);
g_assert (0 == strcmp (dump, g_assert (0 == strcmp (dump,
...@@ -102,7 +97,7 @@ main (void) ...@@ -102,7 +97,7 @@ main (void)
{ {
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceCandidate *candidate; NiceCandidate *candidate;
struct sockaddr_in addr; NiceAddress addr;
gchar buf[1024]; gchar buf[1024];
guint len; guint len;
......
...@@ -20,12 +20,12 @@ handle_recv ( ...@@ -20,12 +20,12 @@ handle_recv (
static void static void
test_stun_no_password ( test_stun_no_password (
NiceAgent *agent, NiceAgent *agent,
struct sockaddr_in from) NiceAddress from)
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len; guint len;
struct sockaddr_in to = {0,};
gchar buf[1024]; gchar buf[1024];
guint packed_len; guint packed_len;
gchar *packed; gchar *packed;
...@@ -74,12 +74,12 @@ test_stun_no_password ( ...@@ -74,12 +74,12 @@ test_stun_no_password (
static void static void
test_stun_invalid_password ( test_stun_invalid_password (
NiceAgent *agent, NiceAgent *agent,
struct sockaddr_in from) NiceAddress from)
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len; guint len;
struct sockaddr_in to = {0,};
gchar buf[1024]; gchar buf[1024];
guint packed_len; guint packed_len;
gchar *packed; gchar *packed;
...@@ -129,13 +129,13 @@ test_stun_invalid_password ( ...@@ -129,13 +129,13 @@ test_stun_invalid_password (
static void static void
test_stun_valid_password ( test_stun_valid_password (
NiceAgent *agent, NiceAgent *agent,
struct sockaddr_in from) NiceAddress from)
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len; guint len;
guint packed_len; guint packed_len;
struct sockaddr_in to = {0,};
gchar buf[1024]; gchar buf[1024];
gchar *packed; gchar *packed;
gchar *username; gchar *username;
...@@ -173,7 +173,7 @@ test_stun_valid_password ( ...@@ -173,7 +173,7 @@ test_stun_valid_password (
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE, bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
"0123456789abcdef", 2); "0123456789abcdef", 2);
bres->attributes[0] = stun_attribute_mapped_address_new ( bres->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), 5678); from.addr_ipv4, 5678);
bres->attributes[1] = stun_attribute_username_new (username); bres->attributes[1] = stun_attribute_username_new (username);
packed_len = stun_message_pack (bres, &packed); packed_len = stun_message_pack (bres, &packed);
stun_message_free (bres); stun_message_free (bres);
...@@ -189,9 +189,7 @@ test_stun_valid_password ( ...@@ -189,9 +189,7 @@ test_stun_valid_password (
sizeof (buf) / sizeof (gchar), buf); sizeof (buf) / sizeof (gchar), buf);
g_assert (len == packed_len); g_assert (len == packed_len);
g_assert (0 == memcmp (buf, packed, len)); g_assert (0 == memcmp (buf, packed, len));
g_assert (to.sin_family == from.sin_family); g_assert (nice_address_equal (&to, &from));
g_assert (to.sin_addr.s_addr == from.sin_addr.s_addr);
g_assert (to.sin_port == from.sin_port);
g_free (packed); g_free (packed);
} }
...@@ -203,7 +201,6 @@ main (void) ...@@ -203,7 +201,6 @@ main (void)
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
NiceUDPSocket *sock; NiceUDPSocket *sock;
struct sockaddr_in from = {0,};
nice_udp_fake_socket_factory_init (&factory); nice_udp_fake_socket_factory_init (&factory);
...@@ -211,10 +208,6 @@ main (void) ...@@ -211,10 +208,6 @@ main (void)
g_assert (nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5")); g_assert (nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5"));
remote_addr.port = 5678; remote_addr.port = 5678;
from.sin_family = AF_INET;
from.sin_addr.s_addr = htonl (remote_addr.addr_ipv4);
from.sin_port = htons (remote_addr.port);
/* set up agent */ /* set up agent */
agent = nice_agent_new (&factory); agent = nice_agent_new (&factory);
nice_agent_add_local_address (agent, &local_addr); nice_agent_add_local_address (agent, &local_addr);
...@@ -226,9 +219,9 @@ main (void) ...@@ -226,9 +219,9 @@ main (void)
sock = &(candidate->sock); sock = &(candidate->sock);
/* run tests */ /* run tests */
test_stun_no_password (agent, from); test_stun_no_password (agent, remote_addr);
test_stun_invalid_password (agent, from); test_stun_invalid_password (agent, remote_addr);
test_stun_valid_password (agent, from); test_stun_valid_password (agent, remote_addr);
/* clean up */ /* clean up */
nice_agent_free (agent); nice_agent_free (agent);
......
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h>
#include <glib/gprintf.h> #include <glib/gprintf.h>
...@@ -10,7 +12,7 @@ ...@@ -10,7 +12,7 @@
#include "stun.h" #include "stun.h"
static void static void
send_stun (NiceUDPSocket *udpsock, struct sockaddr_in sin, gchar *username) send_stun (NiceUDPSocket *udpsock, NiceAddress addr, gchar *username)
{ {
gchar *packed; gchar *packed;
guint packed_len; guint packed_len;
...@@ -28,11 +30,11 @@ send_stun (NiceUDPSocket *udpsock, struct sockaddr_in sin, gchar *username) ...@@ -28,11 +30,11 @@ send_stun (NiceUDPSocket *udpsock, struct sockaddr_in sin, gchar *username)
} }
packed_len = stun_message_pack (msg, &packed); packed_len = stun_message_pack (msg, &packed);
nice_udp_socket_send (udpsock, &sin, packed_len, packed); nice_udp_socket_send (udpsock, &addr, packed_len, packed);
g_free (packed); g_free (packed);
stun_message_free (msg); stun_message_free (msg);
packed_len = nice_udp_socket_recv (udpsock, &sin, 1024, buf); packed_len = nice_udp_socket_recv (udpsock, &addr, 1024, buf);
g_assert (packed_len > 0); g_assert (packed_len > 0);
msg = stun_message_unpack (packed_len, buf); msg = stun_message_unpack (packed_len, buf);
g_assert (msg); g_assert (msg);
...@@ -51,7 +53,6 @@ static void ...@@ -51,7 +53,6 @@ static void
handle_connection (guint sock) handle_connection (guint sock)
{ {
gchar *line; gchar *line;
struct sockaddr_in sin;
NiceUDPSocketFactory man; NiceUDPSocketFactory man;
NiceUDPSocket udpsock; NiceUDPSocket udpsock;
NiceCandidate *candidate; NiceCandidate *candidate;
...@@ -75,17 +76,13 @@ handle_connection (guint sock) ...@@ -75,17 +76,13 @@ handle_connection (guint sock)
nice_udp_bsd_socket_factory_init (&man); nice_udp_bsd_socket_factory_init (&man);
sin.sin_family = AF_INET; if (!nice_udp_socket_factory_make (&man, &udpsock, NULL))
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = 0;
if (!nice_udp_socket_factory_make (&man, &udpsock, &sin))
goto OUT; goto OUT;
// send local candidate // send local candidate
line = g_strdup_printf ("H/127.0.0.1/%d/lala/titi\n", line = g_strdup_printf ("H/127.0.0.1/%d/lala/titi\n",
ntohs (udpsock.addr.sin_port)); ntohs (udpsock.addr.port));
if (write (sock, line, strlen (line)) != strlen (line)) if (write (sock, line, strlen (line)) != strlen (line))
g_assert_not_reached (); g_assert_not_reached ();
...@@ -94,18 +91,15 @@ handle_connection (guint sock) ...@@ -94,18 +91,15 @@ handle_connection (guint sock)
// agent doesn't initiate connectivity checks, so make our own for now // agent doesn't initiate connectivity checks, so make our own for now
sin.sin_addr.s_addr = htonl (candidate->addr.addr_ipv4);
sin.sin_port = htons (candidate->addr.port);
{ {
gchar *username; gchar *username;
username = g_strdup_printf ("lala%s", candidate->username); username = g_strdup_printf ("lala%s", candidate->username);
send_stun (&udpsock, sin, username); send_stun (&udpsock, candidate->addr, username);
g_free (username); g_free (username);
} }
nice_udp_socket_send (&udpsock, &sin, 6, "\x80hello"); nice_udp_socket_send (&udpsock, &candidate->addr, 6, "\x80hello");
nice_udp_socket_close (&udpsock); nice_udp_socket_close (&udpsock);
OUT: OUT:
......
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include "nice.h" #include "nice.h"
#include "readline.h" #include "readline.h"
#include "util.h" #include "util.h"
...@@ -44,8 +44,7 @@ make_agent ( ...@@ -44,8 +44,7 @@ make_agent (
g_assert (agent->local_candidates != NULL); g_assert (agent->local_candidates != NULL);
candidate = agent->local_candidates->data; candidate = agent->local_candidates->data;
g_debug ("allocated socket %d port %d for candidate %d", g_debug ("allocated socket %d port %d for candidate %d",
candidate->sock.fileno, ntohs (candidate->sock.addr.sin_port), candidate->sock.fileno, candidate->sock.addr.port, candidate->id);
candidate->id);
*ret_agent = agent; *ret_agent = agent;
*ret_sock = &(candidate->sock); *ret_sock = &(candidate->sock);
......
...@@ -43,7 +43,8 @@ accept_connection ( ...@@ -43,7 +43,8 @@ accept_connection (
NiceUDPSocket *sock) NiceUDPSocket *sock)
{ {
NiceAgent *agent; NiceAgent *agent;
struct sockaddr_in sin_recv, sin_send; NiceAddress recv_addr;
NiceAddress send_addr;
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
guint ret = 0; guint ret = 0;
...@@ -53,7 +54,7 @@ accept_connection ( ...@@ -53,7 +54,7 @@ accept_connection (
// accept incoming handshake // accept incoming handshake
len = nice_udp_socket_recv (sock, &sin_recv, 1, buf); len = nice_udp_socket_recv (sock, &recv_addr, 1, buf);
if (len != 1) if (len != 1)
{ {
...@@ -71,16 +72,16 @@ accept_connection ( ...@@ -71,16 +72,16 @@ accept_connection (
// send handshake reply // send handshake reply
sin_send = sin_recv; send_addr = recv_addr;
sin_send.sin_port = htons (1235); send_addr.port = 1235;
nice_udp_socket_send (sock, &sin_send, 1, buf); nice_udp_socket_send (sock, &send_addr, 1, buf);
// send codec // send codec
strcpy (buf, "1 0 PCMU 0 8000 0"); strcpy (buf, "1 0 PCMU 0 8000 0");
nice_udp_socket_send (sock, &sin_send, strlen (buf), buf); nice_udp_socket_send (sock, &send_addr, strlen (buf), buf);
strcpy (buf, "1 0 LAST 0 0 0"); strcpy (buf, "1 0 LAST 0 0 0");
nice_udp_socket_send (sock, &sin_send, strlen (buf), buf); nice_udp_socket_send (sock, &send_addr, strlen (buf), buf);
// send candidate // send candidate
...@@ -90,7 +91,7 @@ accept_connection ( ...@@ -90,7 +91,7 @@ accept_connection (
candidate = nice_agent_get_local_candidates (agent)->data; candidate = nice_agent_get_local_candidates (agent)->data;
len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s", len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s",
candidate->addr.port, candidate->username, candidate->password); candidate->addr.port, candidate->username, candidate->password);
nice_udp_socket_send (sock, &sin_send, len, buf); nice_udp_socket_send (sock, &send_addr, len, buf);
} }
// IO loop // IO loop
...@@ -105,7 +106,7 @@ accept_connection ( ...@@ -105,7 +106,7 @@ accept_connection (
if (nice_agent_poll_read (agent, fds) == NULL) if (nice_agent_poll_read (agent, fds) == NULL)
continue; continue;
len = nice_udp_socket_recv (sock, &sin_recv, 1024, buf); len = nice_udp_socket_recv (sock, &recv_addr, 1024, buf);
buf[len] = '\0'; buf[len] = '\0';
g_debug ("%s", buf); g_debug ("%s", buf);
...@@ -141,16 +142,14 @@ main (gint argc, gchar *argv[]) ...@@ -141,16 +142,14 @@ main (gint argc, gchar *argv[])
{ {
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
NiceUDPSocket sock; NiceUDPSocket sock;
struct sockaddr_in sin; NiceAddress addr = {0,};
guint ret; guint ret;
sin.sin_family = AF_INET; addr.port = 1234;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (1234);
nice_udp_bsd_socket_factory_init (&factory); nice_udp_bsd_socket_factory_init (&factory);
if (!nice_udp_socket_factory_make (&factory, &sock, &sin)) if (!nice_udp_socket_factory_make (&factory, &sock, &addr))
g_assert_not_reached (); g_assert_not_reached ();
ret = accept_connection (&factory, &sock); ret = accept_connection (&factory, &sock);
......
#include <arpa/inet.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
......
include $(top_srcdir)/common.mk include $(top_srcdir)/common.mk
AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS) AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS) -I $(top_srcdir)/address
COMMON_LDADD = libudp.la $(GLIB_LIBS) COMMON_LDADD = libudp.la $(GLIB_LIBS) $(top_builddir)/address/libaddress.la
noinst_LTLIBRARIES = libudp.la noinst_LTLIBRARIES = libudp.la
......
...@@ -9,7 +9,7 @@ main (void) ...@@ -9,7 +9,7 @@ main (void)
{ {
NiceUDPSocketFactory man; NiceUDPSocketFactory man;
NiceUDPSocket sock; NiceUDPSocket sock;
struct sockaddr_in sin = {0,}; NiceAddress addr = {0,};
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
...@@ -19,43 +19,40 @@ main (void) ...@@ -19,43 +19,40 @@ main (void)
/* create fake socket */ /* create fake socket */
sin.sin_addr.s_addr = INADDR_ANY; if (!nice_udp_socket_factory_make (&man, &sock, &addr))
sin.sin_port = 0;
if (!nice_udp_socket_factory_make (&man, &sock, &sin))
g_assert_not_reached (); g_assert_not_reached ();
/* test recv */ /* test recv */
memcpy (buf, "he\0lo", 5); memcpy (buf, "he\0lo", 5);
len = 5; len = 5;
sin.sin_addr.s_addr = htonl (0x01020304); addr.addr_ipv4 = 0x01020304;
sin.sin_port = htons (2345); addr.port = 2345;
nice_udp_fake_socket_push_recv (&sock, &sin, len, buf); nice_udp_fake_socket_push_recv (&sock, &addr, len, buf);
memset (buf, '\0', 5); memset (buf, '\0', 5);
memset (&sin, '\0', sizeof (sin)); memset (&addr, '\0', sizeof (addr));
len = nice_udp_socket_recv (&sock, &sin, sizeof (buf), buf); len = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_assert (len == 5); g_assert (len == 5);
g_assert (memcmp (buf, "he\0lo", 5) == 0); g_assert (memcmp (buf, "he\0lo", 5) == 0);
g_assert (ntohl (sin.sin_addr.s_addr) == 0x01020304); g_assert (addr.addr_ipv4 == 0x01020304);
g_assert (ntohs (sin.sin_port) == 2345); g_assert (addr.port == 2345);
/* test send */ /* test send */
memcpy (buf, "la\0la", 5); memcpy (buf, "la\0la", 5);
len = 5; len = 5;
nice_udp_socket_send (&sock, &sin, len, buf); nice_udp_socket_send (&sock, &addr, len, buf);
memset (buf, '\0', len); memset (buf, '\0', len);
memset (&sin, '\0', sizeof (sin)); memset (&addr, '\0', sizeof (addr));
len = nice_udp_fake_socket_pop_send (&sock, &sin, sizeof (buf), buf); len = nice_udp_fake_socket_pop_send (&sock, &addr, sizeof (buf), buf);
g_assert (len == 5); g_assert (len == 5);
g_assert (0 == memcmp (buf, "la\0la", 5)); g_assert (0 == memcmp (buf, "la\0la", 5));
g_assert (ntohl (sin.sin_addr.s_addr) == 0x01020304); g_assert (addr.addr_ipv4 == 0x01020304);
g_assert (ntohs (sin.sin_port) == 2345); g_assert (addr.port == 2345);
nice_udp_socket_close (&sock); nice_udp_socket_close (&sock);
nice_udp_socket_factory_close (&man); nice_udp_socket_factory_close (&man);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* http://en.wikipedia.org/wiki/Berkeley_sockets.) * http://en.wikipedia.org/wiki/Berkeley_sockets.)
*/ */
#include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include "udp-bsd.h" #include "udp-bsd.h"
...@@ -13,15 +15,20 @@ ...@@ -13,15 +15,20 @@
static gint static gint
socket_recv ( socket_recv (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *from, NiceAddress *from,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
gint recvd; gint recvd;
guint from_len = sizeof (struct sockaddr_in); struct sockaddr_in sin = {0,};
guint from_len = sizeof (sin);
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sin,
&from_len);
recvd = recvfrom (sock->fileno, buf, len, 0, from->type = NICE_ADDRESS_TYPE_IPV4;
(struct sockaddr *) from, &from_len); from->addr_ipv4 = ntohl (sin.sin_addr.s_addr);
from->port = ntohs (sin.sin_port);
return recvd; return recvd;
} }
...@@ -29,12 +36,17 @@ socket_recv ( ...@@ -29,12 +36,17 @@ socket_recv (
static gboolean static gboolean
socket_send ( socket_send (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *to, NiceAddress *to,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
sendto (sock->fileno, buf, len, 0, (struct sockaddr *) to, struct sockaddr_in sin;
sizeof (struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (to->addr_ipv4);
sin.sin_port = htons (to->port);
sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sin, sizeof (sin));
return TRUE; return TRUE;
} }
...@@ -50,29 +62,49 @@ static gboolean ...@@ -50,29 +62,49 @@ static gboolean
socket_factory_init_socket ( socket_factory_init_socket (
NiceUDPSocketFactory *man, NiceUDPSocketFactory *man,
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin) NiceAddress *addr)
{ {
gint sockfd; gint sockfd;
guint name_len = sizeof (struct sockaddr_in); struct sockaddr_in name = {0,};
guint name_len = sizeof (name);
sockfd = socket (PF_INET, SOCK_DGRAM, 0); sockfd = socket (PF_INET, SOCK_DGRAM, 0);
if (sock < 0) if (sock < 0)
return FALSE; return FALSE;
if (sin != NULL) name.sin_family = AF_INET;
if (bind (sockfd, (struct sockaddr *) sin, sizeof (*sin)) != 0)
if (addr != NULL)
{
if (addr->addr_ipv4 != 0)
name.sin_addr.s_addr = htonl (addr->addr_ipv4);
else
name.sin_addr.s_addr = INADDR_ANY;
if (addr->port != 0)
name.sin_port = htons (addr->port);
}
if (bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0)
{ {
close (sockfd); close (sockfd);
return FALSE; return FALSE;
} }
if (getsockname (sockfd, (struct sockaddr *) &(sock->addr), &name_len) != 0) if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0)
{ {
close (sockfd); close (sockfd);
return FALSE; return FALSE;
} }
if (name.sin_addr.s_addr == INADDR_ANY)
sock->addr.addr_ipv4 = 0;
else
sock->addr.addr_ipv4 = ntohl (name.sin_addr.s_addr);
sock->addr.port = ntohs (name.sin_port);
sock->fileno = sockfd; sock->fileno = sockfd;
sock->send = socket_send; sock->send = socket_send;
sock->recv = socket_recv; sock->recv = socket_recv;
......
...@@ -9,18 +9,17 @@ main (void) ...@@ -9,18 +9,17 @@ main (void)
{ {
NiceUDPSocketFactory man; NiceUDPSocketFactory man;
NiceUDPSocket sock; NiceUDPSocket sock;
struct sockaddr_in sin; NiceAddress addr;
nice_udp_bsd_socket_factory_init (&man); nice_udp_bsd_socket_factory_init (&man);
if (!nice_udp_socket_factory_make (&man, &sock, NULL)) if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached (); g_assert_not_reached ();
if (inet_pton (AF_INET, "127.0.0.1", &(sin.sin_addr)) < 0) if (!nice_address_set_ipv4_from_string (&addr, "127.0.0.1"))
g_assert_not_reached (); g_assert_not_reached ();
sin.sin_family = AF_INET; addr.port = 9999;
sin.sin_port = htons (9999);
for (;;) for (;;)
{ {
...@@ -30,8 +29,8 @@ main (void) ...@@ -30,8 +29,8 @@ main (void)
if (fgets (buf, sizeof (buf), stdin) == NULL) if (fgets (buf, sizeof (buf), stdin) == NULL)
break; break;
nice_udp_socket_send (&sock, &sin, strlen (buf), buf); nice_udp_socket_send (&sock, &addr, strlen (buf), buf);
length = nice_udp_socket_recv (&sock, NULL, sizeof (buf), buf); length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_print (buf); g_print (buf);
} }
......
...@@ -6,14 +6,12 @@ main (void) ...@@ -6,14 +6,12 @@ main (void)
{ {
NiceUDPSocketFactory man; NiceUDPSocketFactory man;
NiceUDPSocket sock; NiceUDPSocket sock;
struct sockaddr_in sin; NiceAddress addr = {0,};
nice_udp_bsd_socket_factory_init (&man); nice_udp_bsd_socket_factory_init (&man);
sin.sin_family = AF_INET; addr.port = 9999;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (9999);
if (!man.init (&man, &sock, &sin)) if (!man.init (&man, &sock, &addr))
{ {
g_debug ("failed to find to port 9999: server already running?"); g_debug ("failed to find to port 9999: server already running?");
return 1; return 1;
...@@ -24,11 +22,11 @@ main (void) ...@@ -24,11 +22,11 @@ main (void)
gchar buf[1024]; gchar buf[1024];
guint length; guint length;
length = sock.recv (&sock, &sin, sizeof (buf), buf); length = sock.recv (&sock, &addr, sizeof (buf), buf);
#ifdef DEBUG #ifdef DEBUG
g_debug ("%s:%d", inet_ntoa (sin.sin_addr), ntohs (sin.sin_port)); g_debug ("%s:%d", inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));
#endif #endif
sock.send (&sock, &sin, length, buf); sock.send (&sock, &addr, length, buf);
} }
return 0; return 0;
......
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <glib.h> #include <glib.h>
...@@ -12,7 +11,7 @@ typedef struct _Packet Packet; ...@@ -12,7 +11,7 @@ typedef struct _Packet Packet;
struct _Packet struct _Packet
{ {
struct sockaddr_in sin; NiceAddress addr;
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
}; };
...@@ -45,7 +44,7 @@ _g_slist_pop (GSList **list) ...@@ -45,7 +44,7 @@ _g_slist_pop (GSList **list)
static gboolean static gboolean
fake_send ( fake_send (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *to, NiceAddress *to,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
...@@ -54,7 +53,7 @@ fake_send ( ...@@ -54,7 +53,7 @@ fake_send (
packet = g_slice_new0 (Packet); packet = g_slice_new0 (Packet);
packet->len = len; packet->len = len;
packet->sin = *to; packet->addr = *to;
memcpy (packet->buf, buf, len); memcpy (packet->buf, buf, len);
priv = (UDPFakeSocketPriv *) sock->priv; priv = (UDPFakeSocketPriv *) sock->priv;
...@@ -66,7 +65,7 @@ fake_send ( ...@@ -66,7 +65,7 @@ fake_send (
static gint static gint
fake_recv ( fake_recv (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *from, NiceAddress *from,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
...@@ -74,7 +73,7 @@ fake_recv ( ...@@ -74,7 +73,7 @@ fake_recv (
priv = (UDPFakeSocketPriv *) sock->priv; priv = (UDPFakeSocketPriv *) sock->priv;
read (priv->recv_pipe_out, from, sizeof (struct sockaddr_in)); read (priv->recv_pipe_out, from, sizeof (NiceAddress));
read (priv->recv_pipe_out, &len, sizeof (guint)); read (priv->recv_pipe_out, &len, sizeof (guint));
read (priv->recv_pipe_out, buf, len); read (priv->recv_pipe_out, buf, len);
...@@ -97,7 +96,7 @@ static gboolean ...@@ -97,7 +96,7 @@ static gboolean
fake_socket_init ( fake_socket_init (
NiceUDPSocketFactory *man, NiceUDPSocketFactory *man,
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin) NiceAddress *addr)
{ {
int fds[2]; int fds[2];
static int port = 1; static int port = 1;
...@@ -111,13 +110,13 @@ fake_socket_init ( ...@@ -111,13 +110,13 @@ fake_socket_init (
priv->recv_pipe_in = fds[1]; priv->recv_pipe_in = fds[1];
sock->fileno = priv->recv_pipe_out; sock->fileno = priv->recv_pipe_out;
sock->addr.sin_family = sin->sin_family; sock->addr.type = addr->type;
sock->addr.sin_addr = sin->sin_addr; sock->addr.addr_ipv4 = addr->addr_ipv4;
if (sin->sin_port == 0) if (addr->port == 0)
sock->addr.sin_port = htons (port++); sock->addr.port = port++;
else else
sock->addr.sin_port = sin->sin_port; sock->addr.port = addr->port;
sock->send = fake_send; sock->send = fake_send;
sock->recv = fake_recv; sock->recv = fake_recv;
...@@ -129,14 +128,14 @@ fake_socket_init ( ...@@ -129,14 +128,14 @@ fake_socket_init (
void void
nice_udp_fake_socket_push_recv ( nice_udp_fake_socket_push_recv (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *from, NiceAddress *from,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
UDPFakeSocketPriv *priv; UDPFakeSocketPriv *priv;
priv = (UDPFakeSocketPriv *) sock->priv; priv = (UDPFakeSocketPriv *) sock->priv;
write (priv->recv_pipe_in, from, sizeof (struct sockaddr_in)); write (priv->recv_pipe_in, from, sizeof (NiceAddress));
write (priv->recv_pipe_in, &len, sizeof (guint)); write (priv->recv_pipe_in, &len, sizeof (guint));
write (priv->recv_pipe_in, buf, len); write (priv->recv_pipe_in, buf, len);
} }
...@@ -144,7 +143,7 @@ nice_udp_fake_socket_push_recv ( ...@@ -144,7 +143,7 @@ nice_udp_fake_socket_push_recv (
guint guint
nice_udp_fake_socket_pop_send ( nice_udp_fake_socket_pop_send (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *to, NiceAddress *to,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
...@@ -159,7 +158,7 @@ nice_udp_fake_socket_pop_send ( ...@@ -159,7 +158,7 @@ nice_udp_fake_socket_pop_send (
memcpy (buf, packet->buf, MIN (len, packet->len)); memcpy (buf, packet->buf, MIN (len, packet->len));
len = packet->len; len = packet->len;
*to = packet->sin; *to = packet->addr;
g_slice_free (Packet, packet); g_slice_free (Packet, packet);
return len; return len;
} }
......
...@@ -12,14 +12,14 @@ nice_udp_fake_socket_factory_init (NiceUDPSocketFactory *man); ...@@ -12,14 +12,14 @@ nice_udp_fake_socket_factory_init (NiceUDPSocketFactory *man);
void void
nice_udp_fake_socket_push_recv ( nice_udp_fake_socket_push_recv (
NiceUDPSocket *man, NiceUDPSocket *man,
struct sockaddr_in *from, NiceAddress *from,
guint len, guint len,
gchar *buf); gchar *buf);
guint guint
nice_udp_fake_socket_pop_send ( nice_udp_fake_socket_pop_send (
NiceUDPSocket *man, NiceUDPSocket *man,
struct sockaddr_in *to, NiceAddress *to,
guint len, guint len,
gchar *buf); gchar *buf);
......
...@@ -9,9 +9,9 @@ gboolean ...@@ -9,9 +9,9 @@ gboolean
nice_udp_socket_factory_make ( nice_udp_socket_factory_make (
NiceUDPSocketFactory *man, NiceUDPSocketFactory *man,
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin) NiceAddress *addr)
{ {
return man->init (man, sock, sin); return man->init (man, sock, addr);
} }
void void
...@@ -23,21 +23,21 @@ nice_udp_socket_factory_close (NiceUDPSocketFactory *man) ...@@ -23,21 +23,21 @@ nice_udp_socket_factory_close (NiceUDPSocketFactory *man)
guint guint
nice_udp_socket_recv ( nice_udp_socket_recv (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin, NiceAddress *from,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
return sock->recv (sock, sin, len, buf); return sock->recv (sock, from, len, buf);
} }
void void
nice_udp_socket_send ( nice_udp_socket_send (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin, NiceAddress *to,
guint len, guint len,
gchar *buf) gchar *buf)
{ {
sock->send (sock, sin, len, buf); sock->send (sock, to, len, buf);
} }
void void
......
...@@ -2,35 +2,33 @@ ...@@ -2,35 +2,33 @@
#ifndef _UDP_H #ifndef _UDP_H
#define _UDP_H #define _UDP_H
#include <arpa/inet.h> #include "address.h"
#include <glib.h>
G_BEGIN_DECLS G_BEGIN_DECLS
typedef struct _UDPSocket NiceUDPSocket; typedef struct _NiceUDPSocket NiceUDPSocket;
struct _UDPSocket struct _NiceUDPSocket
{ {
struct sockaddr_in addr; NiceAddress addr;
guint fileno; guint fileno;
gint (*recv) (NiceUDPSocket *sock, struct sockaddr_in *from, guint len, gint (*recv) (NiceUDPSocket *sock, NiceAddress *from, guint len,
gchar *buf); gchar *buf);
gboolean (*send) (NiceUDPSocket *sock, struct sockaddr_in *to, guint len, gboolean (*send) (NiceUDPSocket *sock, NiceAddress *to, guint len,
gchar *buf); gchar *buf);
void (*close) (NiceUDPSocket *sock); void (*close) (NiceUDPSocket *sock);
void *priv; void *priv;
}; };
typedef gboolean (*NiceUDPRecvFunc) (struct sockaddr_in *from, guint len, typedef gboolean (*NiceUDPRecvFunc) (NiceAddress *from, guint len,
gchar *buf); gchar *buf);
typedef struct _UDPSocketManager NiceUDPSocketFactory; typedef struct _NiceUDPSocketManager NiceUDPSocketFactory;
struct _UDPSocketManager struct _NiceUDPSocketManager
{ {
gboolean (*init) (NiceUDPSocketFactory *man, NiceUDPSocket *sock, gboolean (*init) (NiceUDPSocketFactory *man, NiceUDPSocket *sock,
struct sockaddr_in *sin); NiceAddress *sin);
void (*close) (NiceUDPSocketFactory *man); void (*close) (NiceUDPSocketFactory *man);
void *priv; void *priv;
}; };
...@@ -45,7 +43,7 @@ gboolean ...@@ -45,7 +43,7 @@ gboolean
nice_udp_socket_factory_make ( nice_udp_socket_factory_make (
NiceUDPSocketFactory *man, NiceUDPSocketFactory *man,
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin); NiceAddress *addr);
void void
nice_udp_socket_factory_close (NiceUDPSocketFactory *man); nice_udp_socket_factory_close (NiceUDPSocketFactory *man);
...@@ -54,14 +52,14 @@ G_GNUC_WARN_UNUSED_RESULT ...@@ -54,14 +52,14 @@ G_GNUC_WARN_UNUSED_RESULT
guint guint
nice_udp_socket_recv ( nice_udp_socket_recv (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin, NiceAddress *from,
guint len, guint len,
gchar *buf); gchar *buf);
void void
nice_udp_socket_send ( nice_udp_socket_send (
NiceUDPSocket *sock, NiceUDPSocket *sock,
struct sockaddr_in *sin, NiceAddress *to,
guint len, guint len,
gchar *buf); gchar *buf);
......
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