Commit 46e92b39 authored by Martti Mela's avatar Martti Mela

stun async api cleanup

darcs-hash:20051216095410-1b897-cc1e119d89aff04f39165c81fea62f5bd659b836.gz
parent 9bae6304
This diff is collapsed.
......@@ -41,7 +41,7 @@
#include <su_localinfo.h>
typedef struct stun_engine_s stun_engine_t;
typedef struct stun_handle_s stun_handle_t;
typedef struct stun_socket_s stun_socket_t;
#ifndef STUN_MAGIC_T
......@@ -58,7 +58,7 @@ extern char const stun_version[]; /**< Name and version of STUN software */
*/
typedef enum stun_states_e {
/* stun engine errors */
/* stun handle errors */
stun_client_connection_timeout = -10,
stun_client_connection_failed = -9,
stun_no_shared_secret_obtained = -8,
......@@ -67,7 +67,7 @@ typedef enum stun_states_e {
stun_client_error = -5,
stun_client_timeout = -1,
/* stun_engine related */
/* stun_handle related */
stun_shared_secret_obtained = 0,
stun_client_connecting,
stun_client_connected,
......@@ -85,28 +85,39 @@ typedef enum stun_states_e {
} stun_states_t;
/* Return the socket associated with the stun_socket_t structure */
int stun_socket_get_socket(stun_socket_t *ss);
char const *stun_str_state(stun_states_t state);
int stun_is_requested(tag_type_t tag, tag_value_t value, ...);
typedef void (*stun_event_f)(stun_magic_t *magic,
stun_engine_t *se,
stun_handle_t *se,
stun_socket_t *ss,
stun_states_t event);
su_root_t *stun_engine_root(stun_engine_t *self);
su_root_t *stun_handle_root(stun_handle_t *self);
stun_engine_t *stun_engine_tcreate(stun_magic_t *context,
stun_handle_t *stun_handle_tcreate(stun_magic_t *context,
su_root_t *root,
stun_event_f cb,
tag_type_t tag, tag_value_t value, ...);
stun_engine_t *stun_engine_create(stun_magic_t *context,
stun_handle_t *stun_handle_create(stun_magic_t *context,
su_root_t *root,
stun_event_f cb,
char const *server,
int use_msgint);
void stun_engine_destroy(stun_engine_t *);
int stun_connect_start(stun_handle_t *se);
void stun_handle_destroy(stun_handle_t *);
stun_socket_t *stun_socket_create(stun_engine_t *se, int sockfd);
stun_socket_t *stun_socket_create(stun_handle_t *se, int sockfd);
void stun_socket_destroy(stun_socket_t *ss);
/** Bind a socket using STUN. */
......@@ -114,7 +125,7 @@ int stun_bind(stun_socket_t *ss,
/* su_localinfo_t *my_addr, */
int *return_lifetime);
su_localinfo_t *stun_get_local_addr(stun_engine_t *en);
su_localinfo_t *stun_get_local_addr(stun_handle_t *en);
int stun_get_nattype(stun_socket_t *ss,
su_localinfo_t *my_addr,
......@@ -125,9 +136,9 @@ int stun_get_lifetime(stun_socket_t *ss,
int *lifetime);
/** other functions */
int stun_set_uname_pwd(stun_engine_t *se, const char *uname, int len_uname,
int stun_set_uname_pwd(stun_handle_t *se, const char *uname, int len_uname,
const char *pwd, int len_pwd);
char const *stun_nattype(stun_engine_t *se);
char const *stun_nattype(stun_handle_t *se);
#endif /* !defined(STUN_H) */
......@@ -70,11 +70,10 @@ int stun_get_lifetime(stun_socket_t *ss,
int *lifetime);
/** other functions */
int stun_set_uname_pwd(stun_engine_t *se, const char *uname, int len_uname,
int stun_set_uname_pwd(stun_handle_t *se, const char *uname, int len_uname,
const char *pwd, int len_pwd);
/* internal functions declaration */
int stun_connect_start(stun_engine_t *se);
int stun_make_sharedsecret_req(stun_msg_t *msg);
int stun_bind_test(stun_socket_t *ss,
......
......@@ -42,6 +42,7 @@ typedef struct stunc_s stunc_t;
#define STUN_MAGIC_T stunc_t
#include "stun.h"
#include "stun_tag.h"
#include <su.h>
#ifndef SU_DEBUG
......@@ -63,15 +64,11 @@ struct stunc_s {
};
void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_states_t event);
void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_states_t event)
void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_socket_t *ss, stun_states_t event)
{
su_localinfo_t *li = NULL;
if (event < 0)
su_root_break(stun_engine_root(en));
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
if (event == stun_client_done) {
char ipaddr[48];
......@@ -79,58 +76,86 @@ void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_states_t event)
inet_ntop(li->li_family, SU_ADDR(li->li_addr), ipaddr, sizeof(ipaddr)),
SU_DEBUG_3(("%s: local address NATed as %s:%u\n", __func__,
ipaddr, (unsigned) ntohs(li->li_addr->su_port)));
su_root_break(stun_engine_root(en));
su_root_break(stun_handle_root(en));
}
else if (event == stun_client_error) {
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_engine_root(en));
su_root_break(stun_handle_root(en));
}
if (event == stun_client_done || event == stun_client_error ||
event == stun_client_connection_timeout || event ||
stun_client_connection_failed) {
su_root_break(stun_handle_root(en));
}
return;
}
int main(int argc, char *argv[])
{
int result;
int s, lifetime;
int msg_integrity;
//socklen_t addrlen;
stunc_t stunc[1];
su_root_t *root = su_root_create(stunc);
stun_engine_t *se;
stun_handle_t *se;
stun_socket_t *ss;
if (argc != 3)
usage(1);
msg_integrity = atoi(argv[2]);
/* Running this test requires a local STUN server on default port */
se = stun_engine_create(stunc, root, stunc_callback, argv[1], atoi(argv[2]));
se = stun_handle_tcreate(stunc,
root,
stunc_callback,
STUNTAG_SERVER(argv[1]),
STUNTAG_INTEGRITY(msg_integrity),
TAG_NULL());
if (!se) {
SU_DEBUG_3(("%s: %s", __func__, "stun_engine_create"));
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_handle_tcreate()"));
return -1;
}
su_root_run(root);
if (se == NULL) { perror("stun_engine_create"); exit(1); }
if (msg_integrity == 1 && stun_connect_start(se) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_connect_start()"));
return -1;
}
else if (msg_integrity == 1)
su_root_run(root);
s = socket(AF_INET, SOCK_DGRAM, 0);
s = su_socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) { perror("socket"); exit(1); }
if (s == -1) {
SU_DEBUG_3(("%s: %s failed: %s\n", __func__, "stun_socket_create()", su_gli_strerror(errno)));
return -1;
}
ss = stun_socket_create(se, s);
if (ss == NULL) { perror("stun_socket_create"); exit(1); }
if (ss == NULL) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_socket_create()"));
return -1;
}
lifetime = 0;
result = stun_bind(ss, &lifetime);
if (stun_bind(ss, &lifetime) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_bind()"));
return -1;
}
su_root_run(root);
stun_socket_destroy(ss);
stun_engine_destroy(se);
stun_handle_destroy(se);
return 0;
}
......@@ -28,6 +28,7 @@
* @author Tat Chan <Tat.Chan@nokia.com>
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
* @author Ismo H. Puustinen
* @author eMartti Mela <Martti.Mela@nokia.com>
*
* @date Created: Thu Jul 24 17:21:00 2003 ppessi
* @date Last modified: Wed Jul 20 20:35:55 2005 kaiv
......@@ -48,6 +49,7 @@ typedef struct torture_s torture_t;
#define STUN_MAGIC_T torture_t
#include "stun.h"
#include "stun_tag.h"
#include <su.h>
#include <su_debug.h>
......@@ -62,10 +64,15 @@ struct torture_s {
char const *name = "torture_stun";
static int test_init(su_root_t *root, char *addr);
static int test_sync_stun(char *addr);
static int test_get_nattype(char *addr);
static int test_get_lifetime(char *addr);
static int torture_test_init(su_root_t *root, char *addr);
static int torture_test_stun_bind(char *addr);
static int torture_test_get_nattype(char *addr);
static int torture_test_get_lifetime(char *addr);
static void torture_callback(torture_t *torturer,
stun_handle_t *en,
stun_socket_t *ss,
stun_states_t event);
static int test_deinit(void);
int atonetaddr(struct sockaddr_in *addr, char *in);
......@@ -84,15 +91,19 @@ int tstflags = 0;
int test_bind = 0;
int test_nat = 0;
int test_lifetime = 0;
int fake_msg_int=0; /* use fake username password */
int bypass_msg_int=0;
int fake_msg_int = 0; /* use fake username password */
int bypass_msg_int = 0;
int retval = 0;
stun_handle_t *se;
int main(int argc, char *argv[])
{
torture_t torture[1];
su_root_t *root = su_root_create(torture);
int retval = 0;
int i;
char *clntaddr = "127.0.0.1", *srvraddr = "127.0.0.1";
......@@ -119,63 +130,110 @@ int main(int argc, char *argv[])
}
}
if(!test_bind && !test_nat && !test_lifetime) {usage(); return -1;}
if (!test_bind && !test_nat && !test_lifetime) {
usage();
return -1;
}
if ((retval |= torture_test_init(root, srvraddr))) {
test_deinit();
return retval;
}
if (!(retval |= test_init(root, srvraddr))) {
if (se && !bypass_msg_int) {
stun_connect_start(se);
su_root_run(root);
}
/* event loop is started always before a test. After the test
* su_root_break() is called in the callback. */
if(test_bind) {
retval |= torture_test_stun_bind(clntaddr);
su_root_run(root);
}
#if 0
if(test_nat) {
retval |= torture_test_get_nattype(clntaddr);
su_root_run(root);
}
if(test_lifetime) {
retval |= torture_test_get_lifetime(clntaddr);
su_root_run(root);
if(test_bind) {
retval |= test_sync_stun(clntaddr);
}
if(test_nat) {
retval |= test_get_nattype(clntaddr);
}
if(test_lifetime) {
retval |= test_get_lifetime(clntaddr);
}
}
#endif
test_deinit();
return retval;
}
stun_engine_t *se;
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_states_t ev);
void torture_callback(torture_t *torturer, stun_handle_t *en, stun_socket_t *ss, stun_states_t ev)
{
char ipaddr[48];
int s = -1;
su_localinfo_t *li;
SU_DEBUG_3(("%s: called by event \"%s\"\n", __func__, stun_str_state(ev)));
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_states_t ev)
{
SU_DEBUG_3(("%s: called\n", __func__));
if (ev == stun_client_done) {
li = stun_get_local_addr(en);
s = stun_socket_get_socket(ss);
inet_ntop(li->li_family, SU_ADDR(li->li_addr), ipaddr, sizeof(ipaddr)),
SU_DEBUG_3(("%s: local address NATed as %s:%u\n", __func__,
ipaddr, (unsigned) ntohs(li->li_addr->su_port)));
su_root_break(stun_handle_root(en));
}
else if (ev == stun_client_error || ev == stun_client_connection_timeout || ev == stun_client_connection_failed) {
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_handle_root(en));
}
#if 0
TEST(getsockname(s, (struct sockaddr *)&local, &locallen), 0);
TEST(locallen, addrlen);
my_addr = (struct sockaddr_in *) &addr.li_addr;
printf("*** stun_bind returns %s:%u\n", inet_ntoa(my_addr->sin_addr), (unsigned)ntohs(my_addr->sin_port));
printf("*** getsockname returns %s:%u\n", inet_ntoa(local.sin_addr), (unsigned)ntohs(local.sin_port));
TEST(memcmp(&local, (struct sockaddr_in *)&addr.li_addr, 8), 0);
su_close(s);
#endif
su_root_break(stun_engine_root(en));
return;
}
int test_init(su_root_t *root, char *server)
int torture_test_init(su_root_t *root, char *server)
{
torture_t torturer[1];
BEGIN();
/* Running this test requires a local STUN server on default port */
se = stun_engine_create(torturer,
root,
torture_callback,
server, !bypass_msg_int); TEST_1(se);
/* Running this test requires a local STUN server on default port
* XXX -- mela: does not */
se = stun_handle_tcreate(torturer,
root,
torture_callback,
STUNTAG_SERVER(server),
STUNTAG_INTEGRITY(!bypass_msg_int),
TAG_NULL());
TEST_1(se);
END();
}
int test_sync_stun(char *localaddr)
int torture_test_stun_bind(char *localaddr)
{
int result;
int s, lifetime;
socklen_t addrlen, locallen;
su_localinfo_t addr;
stun_socket_t *ss;
struct sockaddr_in *my_addr, local;
BEGIN();
......@@ -198,40 +256,14 @@ int test_sync_stun(char *localaddr)
}
}
memset(&addr, 0, sizeof(addr));
/* addrlen = sizeof(addr); */
lifetime = 0;
my_addr = (struct sockaddr_in *) &addr.li_addr;
atonetaddr(my_addr, localaddr);
/*
my_addr->sin_addr.s_addr = inet_addr(localaddr);
my_addr->sin_family = AF_INET;
my_addr->sin_port = 0;
*/
addrlen = sizeof(*my_addr);
result = stun_bind(ss, &lifetime); TEST(result, 0);
/* Just a check that getsockname() returns same address as stun_bind */
memset(&local, 0, sizeof(local)); locallen = sizeof(local);
TEST(getsockname(s, (struct sockaddr *)&local, &locallen), 0);
TEST(locallen, addrlen);
my_addr = (struct sockaddr_in *) &addr.li_addr;
printf("*** stun_bind returns %s:%u\n", inet_ntoa(my_addr->sin_addr), (unsigned)ntohs(my_addr->sin_port));
printf("*** getsockname returns %s:%u\n", inet_ntoa(local.sin_addr), (unsigned)ntohs(local.sin_port));
TEST(memcmp(&local, (struct sockaddr_in *)&addr.li_addr, 8), 0);
su_close(s);
END();
}
int test_get_lifetime(char *localaddr)
int torture_test_get_lifetime(char *localaddr)
{
int result, lifetime;
int s, addrlen;
......@@ -276,7 +308,7 @@ int test_get_lifetime(char *localaddr)
int test_get_nattype(char *localaddr)
int torture_test_get_nattype(char *localaddr)
{
int result;
int s, addrlen;
......@@ -385,7 +417,7 @@ static int test_deinit(void)
{
BEGIN();
stun_engine_destroy(se);
stun_handle_destroy(se);
END();
}
......@@ -394,7 +426,7 @@ static int test_deinit(void)
/* this function is just for testing convenience within the scope of
* torture_stun. It is only used by converting client address
* input. Server address of the form "a.b.c.d:port" is passed directly to
* stun_engine_create(). */
* stun_handle_create(). */
int atonetaddr(struct sockaddr_in *addr, char *in)
{
char *p, tmp[64];
......
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