Commit 0491bbe8 authored by Martti Mela's avatar Martti Mela
Browse files

fixed STUN timers

darcs-hash:20051219132612-1b897-2fa07934451fe4f165ef4703417a3f41ebeca029.gz
parent 1cda15ca
2005-12-19 Martti Mela <mela@dev-113.nrln.net> 2005-12-19 Martti Mela <martti.mela@nokia.com>
* stun.c: fixed TLS and bind timers.
* stun_common.c: changed every SU_DEBUG to contain __func__.
2005-12-19 Martti Mela <martti.mela@nokia.com>
* stun.c: TLS connection works now. * stun.c: TLS connection works now.
...@@ -7,7 +13,7 @@ ...@@ -7,7 +13,7 @@
* stunc.c, torture_stun.c: modified in accordance to stun.h. * stunc.c, torture_stun.c: modified in accordance to stun.h.
2005-12-16 Martti Mela <mela@dev-113.nrln.net> 2005-12-16 Martti Mela <martti.mela@nokia.com>
* stun.h: changed stun_engine_t to stun_handle_t. * stun.h: changed stun_engine_t to stun_handle_t.
......
...@@ -77,18 +77,19 @@ char const stun_nat_unknown[] = "NAT type undetermined", ...@@ -77,18 +77,19 @@ char const stun_nat_unknown[] = "NAT type undetermined",
struct stun_handle_s struct stun_handle_s
{ {
su_home_t st_home[1]; su_home_t st_home[1];
su_root_t *st_root; /**< event loop */ su_root_t *st_root; /**< event loop */
int st_root_index; /**< object index of su_root_register() */ int st_root_index; /**< object index of su_root_register() */
su_timer_t *st_connect_timer; /**< timer for TLS connection */
int st_max_retries; /**< max resend for sendto() */ int st_max_retries; /**< max resend for sendto() */
int st_retry_count; /**< current retry number */ int st_retry_count; /**< current retry number */
long st_timeout; /**< timeout for next sendto() */ long st_timeout; /**< timeout for next sendto() */
su_addrinfo_t st_pri_info; /**< server primary info */ su_addrinfo_t st_pri_info; /**< server primary info */
su_sockaddr_t st_pri_addr[1]; /**< server primary address */ su_sockaddr_t st_pri_addr[1]; /**< server primary address */
su_addrinfo_t st_sec_info; /**< server secondary info */ su_addrinfo_t st_sec_info; /**< server secondary info */
su_sockaddr_t st_sec_addr[1]; /**< server secondary address */ su_sockaddr_t st_sec_addr[1]; /**< server secondary address */
su_localinfo_t st_client_addr[1]; /**< local address returned by server */ su_localinfo_t st_client_addr[1]; /**< local address returned by server */
...@@ -615,14 +616,15 @@ int stun_tls_callback(su_root_magic_t *m, su_wait_t *w, stun_handle_t *self) ...@@ -615,14 +616,15 @@ int stun_tls_callback(su_root_magic_t *m, su_wait_t *w, stun_handle_t *self)
events & SU_WAIT_IN ? " IN" : "", events & SU_WAIT_IN ? " IN" : "",
events & SU_WAIT_OUT ? " OUT" : "")); events & SU_WAIT_OUT ? " OUT" : ""));
if (!(events & SU_WAIT_CONNECT || if (events & SU_WAIT_ERR) {
events & SU_WAIT_IN ||
events & SU_WAIT_OUT)) {
su_wait_destroy(w); su_wait_destroy(w);
su_root_deregister(self->st_root, self->st_root_index); su_root_deregister(self->st_root, self->st_root_index);
SU_DEBUG_3(("Shared secret NOT obtained from server. " \ /* Destroy the timeout timer */
"Proceed without username/password.\n")); su_timer_destroy(self->st_connect_timer);
SU_DEBUG_3(("%s: shared secret not obtained from server. " \
"Proceed without username/password.\n", __func__));
self->st_state = stun_tls_connection_failed; self->st_state = stun_tls_connection_failed;
self->st_callback(self->st_context, self, self->st_state); self->st_callback(self->st_context, self, self->st_state);
return 0; return 0;
...@@ -889,7 +891,7 @@ int stun_handle_request_shared_secret(stun_handle_t *se) ...@@ -889,7 +891,7 @@ int stun_handle_request_shared_secret(stun_handle_t *se)
STUN_ERROR(errno, setsockopt); STUN_ERROR(errno, setsockopt);
return -1; return -1;
} }
SU_DEBUG_3(("%s: %s: %s\n", __func__, "setsockopt", SU_DEBUG_7(("%s: %s: %s\n", __func__, "setsockopt",
su_strerror(errno))); su_strerror(errno)));
/* Do an asynchronous connect(). Three error codes are ok, /* Do an asynchronous connect(). Three error codes are ok,
...@@ -927,6 +929,7 @@ int stun_handle_request_shared_secret(stun_handle_t *se) ...@@ -927,6 +929,7 @@ int stun_handle_request_shared_secret(stun_handle_t *se)
connect_timer = su_timer_create(su_root_task(se->st_root), connect_timer = su_timer_create(su_root_task(se->st_root),
STUN_TLS_CONNECT_TIMEOUT); STUN_TLS_CONNECT_TIMEOUT);
se->st_connect_timer = connect_timer;
su_timer_set(connect_timer, stun_tls_connect_timer_cb, se); su_timer_set(connect_timer, stun_tls_connect_timer_cb, se);
return 0; return 0;
...@@ -1120,11 +1123,9 @@ static void stun_sendto_timer_cb(su_root_magic_t *magic, ...@@ -1120,11 +1123,9 @@ static void stun_sendto_timer_cb(su_root_magic_t *magic,
{ {
int s = se->ss_sockfd; int s = se->ss_sockfd;
if (se->st_state == stun_bind_sending) { SU_DEBUG_9(("%s: entering.\n", __func__));
return;
}
if (se->st_state != stun_bind_receiving) { if ((se->st_state != stun_bind_sending) && (se->st_state != stun_bind_receiving)) {
su_timer_destroy(t); su_timer_destroy(t);
return; return;
} }
...@@ -1151,7 +1152,7 @@ static void stun_sendto_timer_cb(su_root_magic_t *magic, ...@@ -1151,7 +1152,7 @@ static void stun_sendto_timer_cb(su_root_magic_t *magic,
return; return;
} }
SU_DEBUG_3(("%s: Time out no. %d, retransmitting.\n", __func__, se->st_retry_count)); SU_DEBUG_3(("%s: Timeout no. %d, retransmitting.\n", __func__, se->st_retry_count));
if (stun_send_message(s, &se->st_pri_addr->su_sin, se->st_binding_request, &(se->st_passwd)) < 0) { if (stun_send_message(s, &se->st_pri_addr->su_sin, se->st_binding_request, &(se->st_passwd)) < 0) {
stun_free_message(se->st_binding_request); stun_free_message(se->st_binding_request);
......
...@@ -100,7 +100,7 @@ int stun_parse_message(stun_msg_t *msg) { ...@@ -100,7 +100,7 @@ int stun_parse_message(stun_msg_t *msg) {
while(len > 0) { while(len > 0) {
i = stun_parse_attribute(msg, p); i = stun_parse_attribute(msg, p);
if(i <= 0) { if(i <= 0) {
SU_DEBUG_0(("stun: Error parsing attribute.\n")); SU_DEBUG_3(("%s: Error parsing attribute.\n", __func__));
return -1; return -1;
} }
p += i; p += i;
...@@ -130,7 +130,7 @@ int stun_parse_attribute(stun_msg_t *msg, unsigned char *p) { ...@@ -130,7 +130,7 @@ int stun_parse_attribute(stun_msg_t *msg, unsigned char *p) {
p+=2; p+=2;
len = ntohs(tmp16); len = ntohs(tmp16);
SU_DEBUG_3(("%s: received attribute: Type %02X, Length %d - %s\n", __func__, SU_DEBUG_1(("%s: received attribute: Type %02X, Length %d - %s\n", __func__,
attr->attr_type, len, stun_attr_phrase(attr->attr_type))); attr->attr_type, len, stun_attr_phrase(attr->attr_type)));
switch(attr->attr_type) { switch(attr->attr_type) {
...@@ -213,7 +213,7 @@ int stun_parse_attr_address(stun_attr_t *attr, const unsigned char *p, unsigned ...@@ -213,7 +213,7 @@ int stun_parse_attr_address(stun_attr_t *attr, const unsigned char *p, unsigned
memcpy(&addr->sin_port, p+2, 2); memcpy(&addr->sin_port, p+2, 2);
memcpy(&addr->sin_addr.s_addr, p+4, 4); memcpy(&addr->sin_addr.s_addr, p+4, 4);
SU_DEBUG_3(("%s: address attribute: %s:%d\n", __func__, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port))); SU_DEBUG_1(("%s: address attribute: %s:%d\n", __func__, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)));
attr->pattr = addr; attr->pattr = addr;
stun_init_buffer(&attr->enc_buf); stun_init_buffer(&attr->enc_buf);
...@@ -553,7 +553,7 @@ int stun_free_message(stun_msg_t *msg) { ...@@ -553,7 +553,7 @@ int stun_free_message(stun_msg_t *msg) {
return 0; return 0;
} }
#if 1
int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd) int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
{ {
int z; int z;
...@@ -562,36 +562,12 @@ int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, ...@@ -562,36 +562,12 @@ int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg,
z = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size, z = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size,
0, (struct sockaddr *)to_addr, sizeof(*to_addr)); 0, (struct sockaddr *)to_addr, sizeof(*to_addr));
SU_DEBUG_5(("stun: message sent to %s:%u\n", SU_DEBUG_3(("%s: message sent to %s:%u\n", __func__,
inet_ntoa(to_addr->sin_addr), ntohs(to_addr->sin_port))); inet_ntoa(to_addr->sin_addr), ntohs(to_addr->sin_port)));
debug_print(&msg->enc_buf); debug_print(&msg->enc_buf);
return z; return z;
} }
#else
int stun_send_message(int sockfd, su_sockaddr_t *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
{
int err = -1;
char ipaddr[SU_ADDRSIZE + 2];
int namelen = sizeof(su_addrinfo_t);
stun_encode_message(msg, pwd);
inet_ntop(to_addr->su_family, SU_ADDR(to_addr), ipaddr, sizeof(ipaddr));
err = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size,
0, (struct sockaddr *) &to_addr->su_sin, namelen);
if (err != 0)
STUN_ERROR(err, sendto);
inet_ntop(to_addr->su_family, SU_ADDR(to_addr), ipaddr, sizeof(ipaddr));
SU_DEBUG_5(("stun: message sent to %s:%u\n",
ipaddr, ntohs(to_addr->su_port)));
debug_print(&msg->enc_buf);
return err;
}
#endif
/** send a STUN message. /** send a STUN message.
...@@ -720,7 +696,7 @@ char *stun_determine_ip_address(int family) ...@@ -720,7 +696,7 @@ char *stun_determine_ip_address(int family)
hints->li_family = family; hints->li_family = family;
hints->li_canonname = getenv("HOSTADDRESS"); hints->li_canonname = getenv("HOSTADDRESS");
if ((error = su_getlocalinfo(hints, &li)) < 0) { if ((error = su_getlocalinfo(hints, &li)) < 0) {
SU_DEBUG_0(("stun: stun_determine_ip_address, su_getlocalinfo: %s\n", su_gli_strerror(error))); SU_DEBUG_3(("%s: stun_determine_ip_address, su_getlocalinfo: %s\n", __func__, su_gli_strerror(error)));
return NULL; return NULL;
} }
......
...@@ -125,7 +125,7 @@ ...@@ -125,7 +125,7 @@
#define STUN_NAT_PORT_RES_CONE 7 #define STUN_NAT_PORT_RES_CONE 7
/* other protocol specific parameters */ /* other protocol specific parameters */
#define STUN_MAX_RETRX 8 #define STUN_MAX_RETRX 5 /* should be 8? */
#define STUN_MAX_RETRX_INT 1600 /**< max retrx interval in #define STUN_MAX_RETRX_INT 1600 /**< max retrx interval in
millisec */ millisec */
#define STUN_DEFAULT_PORT 3478 /**< from RFC3489 */ #define STUN_DEFAULT_PORT 3478 /**< from RFC3489 */
......
...@@ -73,7 +73,6 @@ void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_states_t event) ...@@ -73,7 +73,6 @@ void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_states_t event)
switch (event) { switch (event) {
case stun_tls_done: case stun_tls_done:
/* SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event))); */
su_root_break(stun_handle_root(en)); su_root_break(stun_handle_root(en));
break; break;
...@@ -90,8 +89,9 @@ void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_states_t event) ...@@ -90,8 +89,9 @@ void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_states_t event)
su_root_break(stun_handle_root(en)); su_root_break(stun_handle_root(en));
break; break;
case stun_bind_timeout:
case stun_tls_connection_failed:
case stun_error: case stun_error:
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
su_root_break(stun_handle_root(en)); su_root_break(stun_handle_root(en));
default: default:
......
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