Commit 1117bc64 authored by Dafydd Harries's avatar Dafydd Harries

split address code into separate file

darcs-hash:20070123112239-c9803-4ee436dea71845eb5e857dab3d98a2180149e304.gz
parent 00d15dcb
......@@ -6,7 +6,11 @@ AM_CFLAGS = -Wall -Werror \
noinst_LTLIBRARIES = libagent.la
libagent_la_SOURCES = agent.h agent.c
libagent_la_SOURCES = \
address.h \
address.c \
agent.h \
agent.c
libagent_la_LIBADD = \
$(top_builddir)/udp/libudp.la \
......
#include <arpa/inet.h>
#include <glib.h>
#include "address.h"
Address *
address_new (void)
{
return g_slice_new0 (Address);
}
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4)
{
addr->type = ADDRESS_TYPE_IPV4;
addr->addr_ipv4 = addr_ipv4;
}
/**
* address_set_ipv4_from_string ()
*
* Returns FALSE on error.
*/
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str)
{
struct in_addr iaddr;
if (inet_aton (str, &iaddr) != 0)
{
address_set_ipv4 (addr, ntohl (iaddr.s_addr));
return TRUE;
}
else
{
/* invalid address */
return FALSE;
}
}
gchar *
address_to_string (Address *addr)
{
struct in_addr iaddr;
gchar ip_str[INET_ADDRSTRLEN];
const gchar *ret;
g_assert (addr->type == ADDRESS_TYPE_IPV4);
iaddr.s_addr = htonl (addr->addr_ipv4);
ret = inet_ntop (AF_INET, &iaddr, ip_str, INET_ADDRSTRLEN);
g_assert (ret);
return g_strdup (ip_str);
}
gboolean
address_equal (Address *a, Address *b)
{
if (a->type != b->type)
return FALSE;
if (a->type == ADDRESS_TYPE_IPV4)
return a->addr_ipv4 == b->addr_ipv4;
g_assert_not_reached ();
}
Address *
address_dup (Address *a)
{
Address *dup = g_slice_new0 (Address);
*dup = *a;
return dup;
}
void
address_free (Address *addr)
{
g_slice_free (Address, addr);
}
/* "private" in the sense of "not routable on the Internet" */
static gboolean
ipv4_address_is_private (guint32 addr)
{
/* http://tools.ietf.org/html/rfc3330 */
return (
/* 10.0.0.0/8 */
((addr & 0xff000000) == 0x0a000000) ||
/* 172.16.0.0/12 */
((addr & 0xfff00000) == 0xac100000) ||
/* 192.168.0.0/16 */
((addr & 0xffff0000) == 0xc0a80000) ||
/* 127.0.0.0/8 */
((addr & 0xff000000) == 0x7f000000));
}
gboolean
address_is_private (Address *a)
{
if (a->type == ADDRESS_TYPE_IPV4)
return ipv4_address_is_private (a->addr_ipv4);
g_assert_not_reached ();
}
#ifndef _ADDRESS_H
#define _ADDRESS_H
typedef enum address_type AddressType;
enum address_type
{
ADDRESS_TYPE_IPV4,
ADDRESS_TYPE_IPV6,
};
typedef struct _address Address;
/* XXX: need access to fields to convert to sockaddr_in */
struct _address
{
AddressType type;
union
{
guint32 addr_ipv4;
guchar addr_ipv6[16];
};
};
Address *
address_new (void);
void
address_free (Address *addr);
Address *
address_dup (Address *a);
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4);
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str);
gboolean
address_equal (Address *a, Address *b);
gchar *
address_to_string (Address *addr);
#endif /* _ADDRESS_H */
......@@ -12,118 +12,6 @@
#include <agent.h>
/*** address ***/
Address *
address_new (void)
{
return g_slice_new0 (Address);
}
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4)
{
addr->type = ADDRESS_TYPE_IPV4;
addr->addr_ipv4 = addr_ipv4;
}
/**
* address_set_ipv4_from_string ()
*
* Returns FALSE on error.
*/
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str)
{
struct in_addr iaddr;
if (inet_aton (str, &iaddr) != 0)
{
address_set_ipv4 (addr, ntohl (iaddr.s_addr));
return TRUE;
}
else
{
/* invalid address */
return FALSE;
}
}
gchar *
address_to_string (Address *addr)
{
struct in_addr iaddr;
gchar ip_str[INET_ADDRSTRLEN];
const gchar *ret;
g_assert (addr->type == ADDRESS_TYPE_IPV4);
iaddr.s_addr = htonl (addr->addr_ipv4);
ret = inet_ntop (AF_INET, &iaddr, ip_str, INET_ADDRSTRLEN);
g_assert (ret);
return g_strdup (ip_str);
}
gboolean
address_equal (Address *a, Address *b)
{
if (a->type != b->type)
return FALSE;
if (a->type == ADDRESS_TYPE_IPV4)
return a->addr_ipv4 == b->addr_ipv4;
g_assert_not_reached ();
}
Address *
address_dup (Address *a)
{
Address *dup = g_slice_new0 (Address);
*dup = *a;
return dup;
}
void
address_free (Address *addr)
{
g_slice_free (Address, addr);
}
/* "private" in the sense of "not routable on the Internet" */
static gboolean
ipv4_address_is_private (guint32 addr)
{
/* http://tools.ietf.org/html/rfc3330 */
return (
/* 10.0.0.0/8 */
((addr & 0xff000000) == 0x0a000000) ||
/* 172.16.0.0/12 */
((addr & 0xfff00000) == 0xac100000) ||
/* 192.168.0.0/16 */
((addr & 0xffff0000) == 0xc0a80000) ||
/* 127.0.0.0/8 */
((addr & 0xff000000) == 0x7f000000));
}
gboolean
address_is_private (Address *a)
{
if (a->type == ADDRESS_TYPE_IPV4)
return ipv4_address_is_private (a->addr_ipv4);
g_assert_not_reached ();
}
/*** candidate ***/
......
......@@ -2,6 +2,10 @@
#ifndef _AGENT_H
#define _AGENT_H
#include "address.h"
typedef enum media_type MediaType;
enum media_type
......@@ -11,44 +15,6 @@ enum media_type
};
/*** address ***/
typedef enum address_type AddressType;
enum address_type
{
ADDRESS_TYPE_IPV4,
ADDRESS_TYPE_IPV6,
};
typedef struct _address Address;
/* XXX: need access to fields to convert to sockaddr_in */
struct _address
{
AddressType type;
union
{
guint32 addr_ipv4;
guchar addr_ipv6[16];
};
};
Address *
address_new (void);
void
address_free (Address *addr);
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4);
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str);
gboolean
address_equal (Address *a, Address *b);
gchar *
address_to_string (Address *addr);
/*** candidate ***/
typedef enum candidate_type CandidateType;
......
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