Commit 4a22f24f authored by Martti Mela's avatar Martti Mela

STUN: first working lifetime determination.

darcs-hash:20060109121858-1b897-de60ac293655650cdf19e06c677492fb2da418a0.gz
parent c3ca3940
This diff is collapsed.
......@@ -44,9 +44,10 @@
SOFIA_BEGIN_DECLS
typedef struct stun_handle_s stun_handle_t;
typedef struct stun_socket_s stun_socket_t;
typedef struct stun_request_s stun_request_t;
typedef struct stun_handle_s stun_handle_t;
typedef struct stun_socket_s stun_socket_t;
typedef struct stun_request_s stun_request_t;
typedef struct stun_discovery_s stun_discovery_t;
#ifndef STUN_MAGIC_T
#define STUN_MAGIC_T struct stun_magic_t
......@@ -57,6 +58,18 @@ typedef STUN_MAGIC_T stun_magic_t;
extern char const stun_version[]; /**< Name and version of STUN software */
/**
* STUN Action types. These define the current discovery process.
*/
typedef enum stun_action_s {
stun_action_no_action,
stun_action_binding_request,
stun_action_keepalive,
stun_action_get_nattype,
stun_action_get_lifetime,
} stun_action_t;
/**
* States of the STUN client->server query process.
*/
......@@ -109,6 +122,8 @@ typedef enum stun_state_e {
typedef void (*stun_event_f)(stun_magic_t *magic,
stun_handle_t *se,
stun_request_t *req,
stun_discovery_t *sd,
stun_action_t action,
stun_state_t event);
/* Return the socket associated with the stun_socket_t structure */
......@@ -151,10 +166,9 @@ int stun_handle_get_nattype(stun_handle_t *se,
tag_type_t tag, tag_value_t value,
...);
int stun_handle_get_lifetime(stun_handle_t *se,
su_localinfo_t *my_addr,
int *addrlen,
int *lifetime);
int stun_handle_get_lifetime(stun_handle_t *sh,
tag_type_t tag, tag_value_t value,
...);
/** other functions */
int stun_handle_set_uname_pwd(stun_handle_t *se,
......@@ -163,7 +177,8 @@ int stun_handle_set_uname_pwd(stun_handle_t *se,
const char *pwd,
int len_pwd);
char const *stun_nattype(stun_request_t *req);
char const *stun_nattype(stun_discovery_t *sd);
int stun_lifetime(stun_discovery_t *sd);
SOFIA_END_DECLS
......
......@@ -552,14 +552,15 @@ int stun_free_message(stun_msg_t *msg) {
}
int stun_send_message(int sockfd, su_sockaddr_t *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
int stun_send_message(su_socket_t s, su_sockaddr_t *to_addr,
stun_msg_t *msg, stun_buffer_t *pwd)
{
int err;
char ipaddr[SU_ADDRSIZE + 2];
stun_encode_message(msg, pwd);
err = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size,
err = sendto(s, msg->enc_buf.data, msg->enc_buf.size,
0, (struct sockaddr *)to_addr, sizeof(struct sockaddr_in));
if (err > 0) {
......@@ -567,7 +568,7 @@ int stun_send_message(int sockfd, su_sockaddr_t *to_addr, stun_msg_t *msg, stun_
SU_DEBUG_3(("%s: message sent to %s:%u\n", __func__,
ipaddr, ntohs(to_addr->su_port)));
debug_print(&msg->enc_buf);
debug_print(&msg->enc_buf);
}
else
STUN_ERROR(errno, sendto);
......
......@@ -53,10 +53,16 @@ SOFIA_BEGIN_DECLS
extern char const STUN_DEBUG[]; /* dummy declaration for Doxygen */
/* XXX -- mela: note that this are 100 times too small */
#if 1
#define STUN_LIFETIME_EST 3500 /**< 6 min? */
#define STUN_LIFETIME_MAX 18000 /**< 30 min? */
#define STUN_LIFETIME_CI 50 /**< 5 sec confidence interval */
#else
#define STUN_LIFETIME_EST 350 /**< 6 min? */
#define STUN_LIFETIME_MAX 1800 /**< 30 min? */
#define STUN_LIFETIME_CI 5 /**< 5 sec confidence interval */
#endif
#define STUN_ERROR(errno, what) \
{ int err = errno; \
......@@ -67,9 +73,6 @@ extern char const STUN_DEBUG[]; /* dummy declaration for Doxygen */
int stun_is_requested(tag_type_t tag, tag_value_t value, ...);
int stun_poll(stun_socket_t *ss);
int stun_get_lifetime(stun_handle_t *se,
su_localinfo_t *my_addr, int *addrlen,
int *lifetime);
/** other functions */
int stun_set_uname_pwd(stun_handle_t *se, const char *uname, int len_uname,
......@@ -78,7 +81,7 @@ int stun_set_uname_pwd(stun_handle_t *se, const char *uname, int len_uname,
/* internal functions declaration */
int stun_make_sharedsecret_req(stun_msg_t *msg);
int stun_send_message(su_socket_t ss, su_sockaddr_t *srvr,
int stun_send_message(su_socket_t s, su_sockaddr_t *srvr,
stun_msg_t *msg, stun_buffer_t *pwd);
int stun_make_binding_req(stun_handle_t *se, stun_request_t *req,
......
......@@ -63,25 +63,35 @@ struct stunc_s {
};
void stunc_callback(stunc_t *stunc, stun_handle_t *en,
void stunc_callback(stunc_t *stunc, stun_handle_t *sh,
stun_request_t *req,
stun_discovery_t *sd,
stun_action_t action,
stun_state_t event)
{
su_localinfo_t *li = NULL;
char ipaddr[48];
char const *nattype;
int lifetime;
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
switch (event) {
case stun_tls_done:
su_root_break(stun_handle_root(en));
su_root_break(stun_handle_root(sh));
break;
case stun_discovery_done:
nattype = stun_nattype(req);
SU_DEBUG_3(("%s: NAT type: %s\n", __func__, nattype));
su_root_break(stun_handle_root(en));
if (action == stun_action_get_nattype) {
nattype = stun_nattype(sd);
SU_DEBUG_3(("%s: NAT type: %s\n", __func__, nattype));
}
else if (action == stun_action_get_lifetime) {
lifetime = stun_lifetime(sd);
SU_DEBUG_3(("%s: Life time is %d s.\n", __func__, lifetime));
}
su_root_break(stun_handle_root(sh));
break;
case stun_bind_done:
......@@ -89,18 +99,18 @@ void stunc_callback(stunc_t *stunc, stun_handle_t *en,
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));
su_root_break(stun_handle_root(sh));
break;
case stun_bind_error:
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_handle_root(en));
su_root_break(stun_handle_root(sh));
break;
case stun_bind_timeout:
case stun_tls_connection_failed:
case stun_error:
su_root_break(stun_handle_root(en));
su_root_break(stun_handle_root(sh));
default:
break;
......@@ -114,7 +124,6 @@ int main(int argc, char *argv[])
{
int s, lifetime;
int msg_integrity;
//socklen_t addrlen;
stunc_t stunc[1];
su_root_t *root = su_root_create(stunc);
stun_handle_t *se;
......@@ -175,6 +184,13 @@ int main(int argc, char *argv[])
su_root_run(root);
if (stun_handle_get_lifetime(se, /* STUNTAG_SOCKET(s), */ TAG_NULL()) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_handle_get_lifetime()"));
return -1;
}
su_root_run(root);
stun_handle_destroy(se);
return 0;
......
......@@ -63,13 +63,14 @@ struct torture_s {
char const *name = "torture_stun";
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_state_t event);
int torture_test_init(su_root_t *root, char *addr);
int torture_test_stun_bind(char *addr);
int torture_test_get_nattype(char *addr);
int torture_test_get_lifetime(char *addr);
void torture_callback(torture_t *t, stun_handle_t *en,
stun_request_t *req,
stun_discovery_t *sd,
stun_state_t event);
static int test_deinit(void);
......@@ -169,26 +170,30 @@ int main(int argc, char *argv[])
}
void torture_callback(torture_t *torturer, stun_handle_t *en, stun_state_t ev)
void torture_callback(torture_t *t,
stun_handle_t *sh,
stun_request_t *req,
stun_discovery_t *sd,
stun_state_t event)
{
char ipaddr[48];
int s = -1;
su_localinfo_t *li;
SU_DEBUG_3(("%s: called by event \"%s\"\n", __func__, stun_str_state(ev)));
SU_DEBUG_3(("%s: called by event \"%s\"\n", __func__, stun_str_state(event)));
if (ev == stun_bind_done) {
li = stun_request_get_localinfo(en);
s = stun_handle_get_bind_socket(en);
if (event == stun_bind_done) {
li = stun_request_get_localinfo(req);
s = stun_handle_get_bind_socket(sh);
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));
su_root_break(stun_handle_root(sh));
}
else if (ev >= stun_error) {
else if (event >= stun_error) {
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_handle_root(en));
su_root_break(stun_handle_root(sh));
}
#if 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