Commit bd179add authored by Dafydd Harries's avatar Dafydd Harries

add local/ directory

darcs-hash:20061221015014-c9803-0f6d2d17bdf4525ccdf6a2e8c622f587b32e352e.gz
parent 2dbc179b
SUBDIRS = stun tests
SUBDIRS = local stun tests
......@@ -55,3 +55,7 @@
# stun/ stuff
^stun/libstun.a$
^stun/stun-client$
# local/ stuff
^local/liblocal.a$
^local/list-local-interfaces$
#include <glib.h>
#include "local.h"
int
main (void)
{
GSList *i;
GSList *interfaces;
interfaces = list_local_interfaces ();
for (i = interfaces; i; i = i->next)
{
interface *iface = (interface *) i->data;
g_print ("%s: %d.%d.%d.%d\n",
iface->name,
(iface->addr & 0xff000000) >> 24,
(iface->addr & 0x00ff0000) >> 16,
(iface->addr & 0x0000ff00) >> 8,
(iface->addr & 0x000000ff));
interface_free (iface);
}
g_slist_free (interfaces);
return 0;
}
#include <glib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <unistd.h>
#include "local.h"
interface *
interface_new ()
{
return g_slice_new0 (interface);
}
void
interface_free (interface *iface)
{
g_free (iface->name);
g_slice_free (interface, iface);
}
GSList *
list_local_interfaces ()
{
char buf[1024];
guint sock;
guint i;
GSList *ret = NULL;
struct ifconf ifc;
sock = socket (PF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return NULL;
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl (sock, SIOCGIFCONF, &ifc) < 0)
return NULL;
/* FIXME: test case where ifc.ifc_len == sizeof (buf) (overflow) */
/* FIXME: support IPv6 */
for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++)
{
struct ifreq *ifr = ifc.ifc_req + i;
struct sockaddr_in *sin;
interface *iface;
if (ifr->ifr_addr.sa_family != AF_INET)
/* this probably shouldn't happen */
continue;
iface = g_slice_new0 (interface);
iface->name = g_strdup (ifr->ifr_name);
sin = (struct sockaddr_in *) &(ifr->ifr_addr);
iface->addr = (addr_ipv4) ntohl (sin->sin_addr.s_addr);
ret = g_slist_append (ret, iface);
}
close (sock);
return ret;
}
#ifndef _LOCAL_H
#define _LOCAL_H
typedef guint32 addr_ipv4;
typedef struct _interface interface;
struct _interface
{
gchar *name;
addr_ipv4 addr;
};
interface *
interface_new ();
void
interface_free (interface *iface);
GSList *
list_local_interfaces ();
#endif /* _LOCAL_H */
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