Commit a1280a33 authored by Martti Mela's avatar Martti Mela

STUN TLS connection now works

darcs-hash:20051218164341-1b897-e0fba2aad98649fd9f080fcbe911b621b86dc2f2.gz
parent 690f45b6
This diff is collapsed.
......@@ -58,36 +58,40 @@ extern char const stun_version[]; /**< Name and version of STUN software */
*/
typedef enum stun_states_e {
/* stun handle errors */
stun_client_connection_timeout = -10,
stun_client_connection_failed = -9,
stun_no_shared_secret_obtained = -8,
/* TLS events */
stun_tls_connecting,
stun_tls_ssl_connecting,
stun_tls_writing,
stun_tls_closing,
stun_tls_reading,
stun_tls_done,
/* STUN bind events */
stun_bind_init, /**< Initial state */
stun_bind_started, /**< Discovery process started */
stun_bind_sending, /**< Sending query to server */
stun_bind_sent, /**< Query sent */
stun_bind_receiving, /**< Waiting for server to answer */
stun_bind_processing, /**< Processing server reply */
stun_bind_done, /**< Initial state */
/* STUN errors */
/* Do not change the order! */
stun_error,
stun_tls_connection_timeout,
stun_tls_connection_failed,
stun_tls_ssl_connect_failed,
/* stun client errors */
stun_client_error = -5,
stun_client_timeout = -1,
/* stun_handle related */
stun_shared_secret_obtained = 0,
stun_client_connecting,
stun_client_connected,
/* client: stun socket */
stun_client_init, /**< Initial state */
stun_client_started, /**< Discovery process started */
stun_client_sending, /**< Sending query to server */
stun_client_sent, /**< Query sent */
stun_client_receiving, /**< Waiting for server to answer */
stun_client_received, /**< Server answered */
stun_client_processing, /**< Processing server reply */
stun_client_done, /**< Initial state */
stun_bind_error,
stun_bind_timeout,
} stun_states_t;
/* Return the socket associated with the stun_socket_t structure */
int stun_socket_get_socket(stun_socket_t *ss);
int stun_handle_get_bind_socket(stun_handle_t *se);
char const *stun_str_state(stun_states_t state);
......@@ -96,7 +100,6 @@ int stun_is_requested(tag_type_t tag, tag_value_t value, ...);
typedef void (*stun_event_f)(stun_magic_t *magic,
stun_handle_t *se,
stun_socket_t *ss,
stun_states_t event);
su_root_t *stun_handle_root(stun_handle_t *self);
......@@ -112,32 +115,35 @@ stun_handle_t *stun_handle_create(stun_magic_t *context,
char const *server,
int use_msgint);
int stun_connect_start(stun_handle_t *se);
int stun_handle_request_shared_secret(stun_handle_t *se);
void stun_handle_destroy(stun_handle_t *);
stun_socket_t *stun_socket_create(stun_handle_t *se, int sockfd);
void stun_socket_destroy(stun_socket_t *ss);
int stun_handle_set_bind_socket(stun_handle_t *se, int sockfd);
/** Bind a socket using STUN. */
int stun_bind(stun_socket_t *ss,
/* su_localinfo_t *my_addr, */
int *return_lifetime);
int stun_handle_bind(stun_handle_t *se,
/* su_localinfo_t *my_addr, */
int *return_lifetime);
su_localinfo_t *stun_get_local_addr(stun_handle_t *en);
su_localinfo_t *stun_handle_get_local_addr(stun_handle_t *en);
int stun_get_nattype(stun_socket_t *ss,
su_localinfo_t *my_addr,
int *addrlen);
int stun_handle_get_nattype(stun_handle_t *se,
su_localinfo_t *my_addr,
int *addrlen);
int stun_get_lifetime(stun_socket_t *ss,
su_localinfo_t *my_addr, int *addrlen,
int *lifetime);
int stun_handle_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,
const char *pwd, int len_pwd);
int stun_handle_set_uname_pwd(stun_handle_t *se,
const char *uname,
int len_uname,
const char *pwd,
int len_pwd);
char const *stun_nattype(stun_handle_t *se);
......
......@@ -65,7 +65,7 @@ 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_socket_t *ss,
int stun_get_lifetime(stun_handle_t *se,
su_localinfo_t *my_addr, int *addrlen,
int *lifetime);
......@@ -76,14 +76,14 @@ 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_bind_test(stun_socket_t *ss,
int stun_bind_test(stun_handle_t *se,
su_addrinfo_t *srvr_info,
su_sockaddr_t *srvr_addr,
su_localinfo_t *clnt_addr,
int chg_ip,
int chg_port);
int stun_send_message(su_socket_t ss, struct sockaddr_in *srvr, stun_msg_t *msg, stun_buffer_t *pwd); /* client version */
int stun_make_binding_req(stun_socket_t *ss, stun_msg_t *msg, int chg_ip, int chg_port);
int stun_make_binding_req(stun_handle_t *se, stun_msg_t *msg, int chg_ip, int chg_port);
int stun_process_response(stun_msg_t *msg);
int stun_process_binding_response(stun_msg_t *msg);
......
......@@ -64,32 +64,39 @@ struct stunc_s {
};
void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_socket_t *ss, stun_states_t event)
void stunc_callback(stunc_t *stunc, stun_handle_t *en, stun_states_t event)
{
su_localinfo_t *li = NULL;
char ipaddr[48];
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
if (event == stun_client_done) {
char ipaddr[48];
li = stun_get_local_addr(en);
switch (event) {
case stun_tls_done:
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
su_root_break(stun_handle_root(en));
break;
case stun_bind_done:
li = stun_handle_get_local_addr(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_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 (event == stun_client_error) {
break;
case stun_bind_error:
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_handle_root(en));
}
break;
if (event == stun_client_done || event == stun_client_error ||
event == stun_client_connection_timeout || event ||
stun_client_connection_failed) {
case stun_error:
SU_DEBUG_3(("%s: %s\n", __func__, stun_str_state(event)));
su_root_break(stun_handle_root(en));
}
default:
break;
}
return;
}
......@@ -103,7 +110,6 @@ int main(int argc, char *argv[])
stunc_t stunc[1];
su_root_t *root = su_root_create(stunc);
stun_handle_t *se;
stun_socket_t *ss;
if (argc != 3)
......@@ -124,7 +130,7 @@ int main(int argc, char *argv[])
return -1;
}
if (msg_integrity == 1 && stun_connect_start(se) < 0) {
if (msg_integrity == 1 && stun_handle_request_shared_secret(se) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_connect_start()"));
return -1;
}
......@@ -138,23 +144,22 @@ int main(int argc, char *argv[])
return -1;
}
ss = stun_socket_create(se, s);
if (ss == NULL) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_socket_create()"));
if (stun_handle_set_bind_socket(se, s) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_handle_set_bind_socket()"));
return -1;
}
lifetime = 0;
if (stun_bind(ss, &lifetime) < 0) {
if (stun_handle_bind(se, &lifetime) < 0) {
SU_DEBUG_3(("%s: %s failed\n", __func__, "stun_bind()"));
return -1;
}
su_root_run(root);
stun_socket_destroy(ss);
stun_handle_destroy(se);
return 0;
......
......@@ -70,7 +70,6 @@ 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);
......@@ -141,7 +140,7 @@ int main(int argc, char *argv[])
}
if (se && !bypass_msg_int) {
stun_connect_start(se);
stun_handle_request_shared_secret(se);
su_root_run(root);
}
......@@ -171,7 +170,7 @@ int main(int argc, char *argv[])
}
void torture_callback(torture_t *torturer, stun_handle_t *en, stun_socket_t *ss, stun_states_t ev)
void torture_callback(torture_t *torturer, stun_handle_t *en, stun_states_t ev)
{
char ipaddr[48];
int s = -1;
......@@ -179,16 +178,16 @@ void torture_callback(torture_t *torturer, stun_handle_t *en, stun_socket_t *ss,
SU_DEBUG_3(("%s: called by event \"%s\"\n", __func__, stun_str_state(ev)));
if (ev == stun_client_done) {
li = stun_get_local_addr(en);
s = stun_socket_get_socket(ss);
if (ev == stun_bind_done) {
li = stun_handle_get_local_addr(en);
s = stun_handle_get_bind_socket(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));
}
else if (ev == stun_client_error || ev == stun_client_connection_timeout || ev == stun_client_connection_failed) {
else if (ev >= stun_error) {
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_handle_root(en));
}
......@@ -233,13 +232,13 @@ int torture_test_stun_bind(char *localaddr)
{
int result;
int s, lifetime;
stun_socket_t *ss;
BEGIN();
s = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(s != -1);
ss = stun_socket_create(se, s); TEST_1(ss != NULL);
result = stun_handle_set_bind_socket(se, s);
TEST_1(result != -1);
/* test message integrity? */
if(fake_msg_int) {
......@@ -250,7 +249,7 @@ int torture_test_stun_bind(char *localaddr)
char username[256], password[256];
if(fscanf(pwd, "\"%[^\"]\",\"%[^\"]\"", username, password)) {
printf("Read username, password from pwd.txt: \"%s\", \"%s\"\n", username, password);
stun_set_uname_pwd(se, username, (int)strlen(username), password, (int)strlen(password));
stun_handle_set_uname_pwd(se, username, (int)strlen(username), password, (int)strlen(password));
}
fclose(pwd);
}
......@@ -258,7 +257,7 @@ int torture_test_stun_bind(char *localaddr)
lifetime = 0;
result = stun_bind(ss, &lifetime); TEST(result, 0);
result = stun_handle_bind(se, &lifetime); TEST(result, 0);
END();
}
......@@ -268,14 +267,14 @@ int torture_test_get_lifetime(char *localaddr)
int result, lifetime;
int s, addrlen;
su_localinfo_t addr;
stun_socket_t *ss;
su_localinfo_t *my_addr;
BEGIN();
s = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(s != -1);
ss = stun_socket_create(se, s); TEST_1(ss != NULL);
result = stun_handle_set_bind_socket(se, s);
TEST_1(result != -1);
/* test message integrity? */
if(fake_msg_int) {
......@@ -286,7 +285,7 @@ int torture_test_get_lifetime(char *localaddr)
char username[256], password[256];
if(fscanf(pwd, "\"%[^\"]\",\"%[^\"]\"", username, password)) {
printf("Read username, password from pwd.txt: \"%s\", \"%s\"\n", username, password);
stun_set_uname_pwd(se, username, strlen(username), password, strlen(password));
stun_handle_set_uname_pwd(se, username, strlen(username), password, strlen(password));
}
fclose(pwd);
}
......@@ -298,7 +297,7 @@ int torture_test_get_lifetime(char *localaddr)
atonetaddr(my_addr, localaddr);
addrlen = sizeof(*my_addr);
result = stun_get_lifetime(ss, &addr.li_addr, &addrlen, &lifetime); TEST(result, 0);
result = stun_handle_get_lifetime(se, &addr.li_addr, &addrlen, &lifetime); TEST(result, 0);
printf("Binding Lifetime determined to be: %d seconds\n", lifetime);
su_close(s);
......@@ -313,14 +312,14 @@ int torture_test_get_nattype(char *localaddr)
int result;
int s, addrlen;
su_sockaddr_t addr;
stun_socket_t *ss;
struct sockaddr_in *my_addr;
BEGIN();
s = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(s != -1);
ss = stun_socket_create(se, s); TEST_1(ss != NULL);
result = stun_handle_set_bind_socket(se, s);
TEST_1(result != -1);
/* test message integrity? */
/* test message integrity? */
......@@ -332,7 +331,7 @@ int torture_test_get_nattype(char *localaddr)
char username[256], password[256];
if(fscanf(pwd, "\"%[^\"]\",\"%[^\"]\"", username, password)) {
printf("Read username, password from pwd.txt: \"%s\", \"%s\"\n", username, password);
stun_set_uname_pwd(se, username, strlen(username), password, strlen(password));
stun_handle_set_uname_pwd(se, username, strlen(username), password, strlen(password));
}
fclose(pwd);
}
......@@ -344,7 +343,7 @@ int torture_test_get_nattype(char *localaddr)
atonetaddr(my_addr, localaddr);
addrlen = sizeof(*my_addr);
result = stun_get_nattype(ss, &addr.su_sa, &addrlen); TEST(result, 0);
result = stun_handle_get_nattype(se, &addr.su_sa, &addrlen); TEST(result, 0);
printf("NAT type is: %s\n", stun_nattype(se));
su_close(s);
......
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