Commit 8f68163b authored by Dafydd Harries's avatar Dafydd Harries

add simple STUN client

darcs-hash:20061220202640-c9803-88f897518a8adab7a22fd294ff9512a803e5e403.gz
parent 70990e42
...@@ -5,3 +5,7 @@ libstun_a_SOURCES = stun.c ...@@ -5,3 +5,7 @@ libstun_a_SOURCES = stun.c
noinst_LIBRARIES = libstun.a noinst_LIBRARIES = libstun.a
noinst_PROGRAMS = stun-client
stun_client_LDADD = libstun.a $(GLIB_LIBS)
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <glib.h>
#include <stun.h>
const gchar *server = "stun.fwdnet.net";
const guint port = 3478;
static gboolean
resolve (const gchar *name, struct hostent *ret)
{
int res;
int h_errno;
struct hostent *he;
gchar buf[1024];
res = gethostbyname_r (server, ret, buf, sizeof (buf) / sizeof (gchar), &he,
&h_errno);
return (res == 0);
}
int
main (void)
{
struct hostent he1, *he;
struct sockaddr_in sin;
guint sock;
gchar *packed;
guint length;
gchar buffer[256];
StunMessage *msg;
StunAttribute **attr;
he = &he1;
g_assert (resolve(server, he));
g_assert (he->h_addr_list != NULL);
sin.sin_family = AF_INET;
sin.sin_port = htons (port);
memcpy (&sin.sin_addr, he->h_addr_list[0], sizeof (struct in_addr));
sock = socket (AF_INET, SOCK_DGRAM, 0);
connect (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr));
msg = stun_message_binding_request_new ();
length = stun_message_pack (msg, &packed);
#ifdef DEBUG
{
gchar *dump = stun_message_dump (msg);
g_debug (dump);
g_free (dump);
}
#endif
send (sock, packed, length, 0);
g_free (packed);
stun_message_free (msg);
length = recv (sock, buffer, 256, 0);
msg = stun_message_unpack (length, buffer);
#ifdef DEBUG
{
gchar *dump = stun_message_dump (msg);
g_debug (dump);
g_free (dump);
}
#endif
for (attr = msg->attributes; *attr; attr++)
{
if ((*attr)->type == STUN_ATTRIBUTE_MAPPED_ADDRESS)
{
guint32 ip = (*attr)->address.ip;
g_print ("%d.%d.%d.%d\n",
(ip & 0xff000000) >> 24,
(ip & 0x00ff0000) >> 16,
(ip & 0x0000ff00) >> 8,
(ip & 0x000000ff));
break;
}
}
stun_message_free (msg);
return 0;
}
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