Commit 9b2d4301 authored by Dafydd Harries's avatar Dafydd Harries

add candidate utility code

darcs-hash:20070117060418-c9803-9d9334ee8b542b072a63f67a86eb97daadbc9f9a.gz
parent 65eee8d9
......@@ -5,7 +5,7 @@ libagent_a_SOURCES = agent.h agent.c
noinst_LIBRARIES = libagent.a
noinst_PROGRAMS = test test-readline
noinst_PROGRAMS = test test-readline test-util
test_LDADD = libagent.a $(GLIB_LIBS)
......@@ -13,5 +13,9 @@ test_readline_SOURCES = test-readline.c readline.h readline.c
test_readline_LDADD = $(GLIB_LIBS)
TESTS = test test-readline
test_util_SOURCES = test-util.c util.h util.c
test_util_LDADD = libagent.a $(GLIB_LIBS)
TESTS = test test-readline test-util
#include <string.h>
#include <arpa/inet.h>
#include <glib.h>
#include <agent.h>
#include <util.h>
int
main (void)
{
Address *addr;
Candidate *candidate;
gchar *str;
candidate = candidate_from_string ("x");
g_assert (candidate == NULL);
addr = address_new_ipv4_from_string ("192.168.0.1");
candidate = candidate_from_string ("H/192.168.0.1/1234");
g_assert (candidate != NULL);
g_assert (address_equal (addr, candidate->addr));
g_assert (candidate->port == 1234);
address_free (addr);
str = candidate_to_string (candidate);
g_assert (0 == strcmp (str, "H/192.168.0.1/1234"));
g_free (str);
candidate_free (candidate);
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <glib.h>
#include <agent.h>
/* format is:
* type/ip/port
*/
Candidate *
candidate_from_string (const gchar *s)
{
CandidateType type;
Candidate *candidate;
Address *addr;
gchar *first_slash;
gchar *last_slash;
gchar tmp[128];
guint len;
guint32 ip;
guint16 port;
if (s == NULL || s[0] == '\0')
return NULL;
switch (s[0])
{
case 'H':
type = CANDIDATE_TYPE_HOST;
break;
case 'S':
type = CANDIDATE_TYPE_SERVER_REFLEXIVE;
break;
case 'P':
type = CANDIDATE_TYPE_PEER_REFLEXIVE;
break;
case 'R':
type = CANDIDATE_TYPE_RELAYED;
break;
default:
return NULL;
}
/* extract IP address */
first_slash = index (s, '/');
last_slash = rindex (s, '/');
if (first_slash == NULL ||
last_slash == NULL ||
first_slash == last_slash)
return NULL;
len = last_slash - first_slash - 1;
if (len > sizeof (tmp) - 1)
return NULL;
strncpy (tmp, first_slash + 1, len);
tmp[len] = '\0';
if (inet_pton (AF_INET, tmp, &ip) < 1)
return NULL;
/* extract port */
port = strtol (last_slash + 1, NULL, 10);
candidate = candidate_new (type);
addr = address_new_ipv4 (ip);
candidate->addr = addr;
candidate->port = port;
return candidate;
}
gchar *
candidate_to_string (Candidate *candidate)
{
gchar *addr_tmp;
gchar *ret;
gchar type;
if (!candidate->addr)
return NULL;
switch (candidate->type)
{
case CANDIDATE_TYPE_HOST:
type = 'H';
break;
case CANDIDATE_TYPE_SERVER_REFLEXIVE:
type = 'S';
break;
case CANDIDATE_TYPE_PEER_REFLEXIVE:
type = 'P';
break;
case CANDIDATE_TYPE_RELAYED:
type = 'R';
break;
default:
return NULL;
}
addr_tmp = address_to_string (candidate->addr);
ret = g_strdup_printf ("%c/%s/%d", type, addr_tmp, candidate->port);
g_free (addr_tmp);
return ret;
}
Candidate *
candidate_from_string (const gchar *s);
gchar *
candidate_to_string (Candidate *candidate);
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