Commit 5c534998 authored by Martti Mela's avatar Martti Mela

stun asyncing contd. NOT WORKING

darcs-hash:20051209153230-1b897-85af91f2938ea94c83342240dbf8a3f1945817ed.gz
parent 7d322326
This diff is collapsed.
......@@ -39,6 +39,8 @@
#endif
#include "stun_common.h"
#include <su_localinfo.h>
typedef struct stun_engine_s stun_engine_t;
typedef struct stun_socket_s stun_socket_t;
......@@ -65,7 +67,7 @@ typedef void (*stun_event_f)(stun_magic_t *magic,
stun_engine_t *se,
stun_event_t event);
su_root_t *stun_root(stun_engine_t *self);
su_root_t *stun_engine_root(stun_engine_t *self);
stun_engine_t *stun_engine_tcreate(stun_magic_t *context,
su_root_t *root,
......@@ -85,19 +87,15 @@ void stun_socket_destroy(stun_socket_t *ss);
/** Bind a socket using STUN. */
int stun_bind(stun_socket_t *ss,
#if 1
su_sockaddr_t *my_addr,
#else
struct sockaddr *addr,
socklen_t *return_addrlen,
#endif
su_localinfo_t *my_addr,
int *return_lifetime);
int stun_get_nattype(stun_socket_t *ss,
struct sockaddr *my_addr, int *addrlen);
su_localinfo_t *my_addr,
int *addrlen);
int stun_get_lifetime(stun_socket_t *ss,
struct sockaddr *my_addr, int *addrlen,
su_localinfo_t *my_addr, int *addrlen,
int *lifetime);
/** other functions */
......
......@@ -540,16 +540,21 @@ int stun_free_message(stun_msg_t *msg) {
return 0;
}
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, su_localinfo_t *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
{
int z;
char ipaddr[SU_ADDRSIZE + 2];
stun_encode_message(msg, pwd);
z = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size,
0, (struct sockaddr *)to_addr, sizeof(*to_addr));
0, (struct sockaddr *) &to_addr->li_addr, sizeof(*to_addr));
inet_ntop(to_addr->li_family, SU_ADDR(to_addr->li_addr), ipaddr, sizeof(ipaddr));
#if 0 /* xxx mela */
SU_DEBUG_5(("stun: message sent to %s:%u\n",
inet_ntoa(to_addr->sin_addr), ntohs(to_addr->sin_port)));
ipaddr, ntohs(to_addr->su_port)));
#endif
debug_print(&msg->enc_buf);
return z;
......
......@@ -55,6 +55,9 @@
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <su.h>
#include <su_localinfo.h>
#ifdef WIN32
#ifndef uint16_t
#define uint16_t unsigned short
......@@ -234,7 +237,7 @@ int stun_free_buffer(stun_buffer_t *p);
int stun_free_message(stun_msg_t *msg);
int stun_init_message(stun_msg_t *msg);
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, su_localinfo_t *to_addr, stun_msg_t *msg, stun_buffer_t *pwd);
int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd);
char const *stun_response_phrase(int status);
......
......@@ -51,7 +51,7 @@ 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,
struct sockaddr *my_addr, int *addrlen,
su_localinfo_t *my_addr, int *addrlen,
int *lifetime);
/** other functions */
......@@ -62,8 +62,11 @@ int stun_set_uname_pwd(stun_engine_t *se, const char *uname, int len_uname,
int stun_connect_start(stun_engine_t *se, su_addrinfo_t *ai);
int stun_make_sharedsecret_req(stun_msg_t *msg);
int stun_bind_test(stun_socket_t *ss, struct sockaddr_in *srvr, struct sockaddr_in *cli,
int chg_ip, int chg_port);
int stun_bind_test(stun_socket_t *ss,
su_localinfo_t *srvr_addr,
su_localinfo_t *clnt_addr,
int chg_ip,
int chg_port);
int stun_send_message2(stun_socket_t *ss, struct sockaddr_in *srvr, stun_msg_t *msg); /* client version */
int stun_make_binding_req(stun_socket_t *ss, stun_msg_t *msg, int chg_ip, int chg_port);
int stun_process_response(stun_msg_t *msg);
......
......@@ -42,6 +42,13 @@ typedef struct stunc_s stunc_t;
#define STUN_MAGIC_T stunc_t
#include "stun.h"
#include <su.h>
#ifndef SU_DEBUG
#define SU_DEBUG 3
#endif
#define SU_LOG (stun_log)
#include <su_debug.h>
char const *name = "stunc";
......@@ -63,7 +70,7 @@ void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_event_t event)
{
printf("event: %d\n", event); fflush(stdout);
su_root_break(stun_root(root));
su_root_break(stun_engine_root(en));
return;
}
......@@ -73,8 +80,7 @@ int main(int argc, char *argv[])
int result;
int s, lifetime;
//socklen_t addrlen;
//su_sockaddr_t addr;
su_addrinfo_t addr[1];
su_localinfo_t addr[1];
stunc_t stunc[1];
su_root_t *root = su_root_create(stunc);
stun_engine_t *se;
......@@ -85,10 +91,12 @@ int main(int argc, char *argv[])
usage(1);
/* Running this test requires a local STUN server on default port */
se = stun_engine_create(stunc,
root,
stunc_callback,
argv[1], argv[2] != NULL);
se = stun_engine_create(stunc, root, stunc_callback, argv[1], atoi(argv[2]));
if (!se) {
SU_DEBUG_3(("%s: %s", __func__, "stun_engine_create"));
return -1;
}
su_root_run(root);
......@@ -103,15 +111,11 @@ int main(int argc, char *argv[])
if (ss == NULL) { perror("stun_socket_create"); exit(1); }
memset(&addr, 0, sizeof(addr));
addr->su_len = sizeof(addr);
addr->li_addrlen = sizeof(addr);
lifetime = 0;
#if 0
result = stun_bind(ss, &addr.su_sa, &addrlen, &lifetime);
#else
result = stun_bind(ss, &addr, &lifetime);
#endif
result = stun_bind(ss, (su_localinfo_t *) &addr, &lifetime);
if (result == -1) { perror("stun_bind"); exit(1); }
/*
if (stun_is_natted(ss)) {
......
......@@ -148,7 +148,7 @@ void torture_callback(torture_t *torturer, stun_engine_t *en, stun_event_t ev)
{
SU_DEBUG_3(("%s: called\n", __func__));
su_root_break(stun_root(en));
su_root_break(stun_engine_root(en));
return;
}
......@@ -173,7 +173,7 @@ int test_sync_stun(char *localaddr)
int result;
int s, lifetime;
socklen_t addrlen, locallen;
su_sockaddr_t addr;
su_localinfo_t addr;
stun_socket_t *ss;
struct sockaddr_in *my_addr, local;
......@@ -198,30 +198,33 @@ int test_sync_stun(char *localaddr)
}
}
memset(&addr, 0, sizeof(addr)); /* addrlen = sizeof(addr); */
memset(&addr, 0, sizeof(addr));
/* addrlen = sizeof(addr); */
lifetime = 0;
my_addr = (struct sockaddr_in *)&addr.su_sa;
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;*/ /* wildcard */
my_addr->sin_port = 0;
*/
addrlen = sizeof(*my_addr);
result = stun_bind(ss, &addr.su_sa, &addrlen, &lifetime); TEST(result, 0);
result = stun_bind(ss, &addr, &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.su_sa;
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.su_sa, 8), 0);
TEST(memcmp(&local, (struct sockaddr_in *)&addr.li_addr, 8), 0);
su_close(s);
......@@ -232,9 +235,9 @@ int test_get_lifetime(char *localaddr)
{
int result, lifetime;
int s, addrlen;
su_sockaddr_t addr;
su_localinfo_t addr;
stun_socket_t *ss;
struct sockaddr_in *my_addr;
su_localinfo_t *my_addr;
BEGIN();
......@@ -259,11 +262,11 @@ int test_get_lifetime(char *localaddr)
memset(&addr, 0, sizeof(addr)); /* addrlen = sizeof(addr); */
my_addr = (struct sockaddr_in *)&addr.su_sa;
my_addr = &addr.li_addr;
atonetaddr(my_addr, localaddr);
addrlen = sizeof(*my_addr);
result = stun_get_lifetime(ss, &addr.su_sa, &addrlen, &lifetime); TEST(result, 0);
result = stun_get_lifetime(ss, &addr.li_addr, &addrlen, &lifetime); TEST(result, 0);
printf("Binding Lifetime determined to be: %d seconds\n", lifetime);
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