Commit d66a5cca authored by Martti Mela's avatar Martti Mela

initial async stun support. Does not work with tport yet.

darcs-hash:20051213181132-1b897-50dc63545200b67990d903e9d55d7feb23b63c17.gz
parent 0647074d
This diff is collapsed.
......@@ -53,19 +53,41 @@ typedef STUN_MAGIC_T stun_magic_t;
extern char const stun_version[]; /**< Name and version of STUN software */
typedef enum stun_event_e {
stun_connect_error,
/**
* States of the STUN client->server query process.
*/
typedef enum stun_states_e {
/* stun engine errors */
stun_connect_error = -10,
stun_no_shared_secret_obtained = -9,
/* stun client errors */
stun_client_error = -5, /**< Sending query to server */
stun_client_timeout = -1,
/* stun_engine related */
stun_shared_secret_obtained = 0,
stun_connect_success,
stun_no_shared_secret_obtained,
stun_ready,
} stun_event_t;
/* 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_states_t;
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_event_t event);
stun_states_t event);
su_root_t *stun_engine_root(stun_engine_t *self);
......
......@@ -542,28 +542,46 @@ int stun_free_message(stun_msg_t *msg) {
return 0;
}
int stun_send_message(int sockfd, su_localinfo_t *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
#if 1
int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd)
{
int z;
stun_encode_message(msg, pwd);
z = sendto(sockfd, msg->enc_buf.data, msg->enc_buf.size,
0, (struct sockaddr *)to_addr, sizeof(*to_addr));
SU_DEBUG_5(("stun: message sent to %s:%u\n",
inet_ntoa(to_addr->sin_addr), ntohs(to_addr->sin_port)));
debug_print(&msg->enc_buf);
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->li_addr, sizeof(*to_addr));
0, (struct sockaddr *) &to_addr->su_sin, namelen);
if (err != 0)
STUN_ERROR(err, sendto);
inet_ntop(to_addr->li_family, to_addr->li_addr, ipaddr, sizeof(ipaddr));
#if 0 /* xxx mela */
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)));
#endif
debug_print(&msg->enc_buf);
return err;
}
#endif
/** send a STUN message.
* This will convert the stun_msg_t to the binary format based on the
......
......@@ -237,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, su_localinfo_t *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 stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd);
char const *stun_response_phrase(int status);
......
......@@ -74,22 +74,23 @@ int stun_set_uname_pwd(stun_engine_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, su_addrinfo_t *ai);
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,
su_localinfo_t *srvr_addr,
su_addrinfo_t *srvr_info,
su_sockaddr_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_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_process_response(stun_msg_t *msg);
int stun_process_binding_response(stun_msg_t *msg);
int stun_process_error_response(stun_msg_t *msg);
int stun_atoaddr(struct sockaddr_in *addr, char const *in);
int stun_atoaddr(int ai_family, su_addrinfo_t *info, su_sockaddr_t *addr, char const *in);
int stun_add_response_address(stun_msg_t *req, struct sockaddr_in *mapped_addr);
#endif /* !defined(STUN_INTERNAL_H) */
......@@ -63,14 +63,16 @@ struct stunc_s {
};
void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_event_t event);
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_event_t event)
void stunc_callback(stunc_t *stunc, stun_engine_t *en, stun_states_t event)
{
printf("event: %d\n", event); fflush(stdout);
su_root_break(stun_engine_root(en));
if (event < 0)
su_root_break(stun_engine_root(en));
return;
}
......@@ -81,6 +83,7 @@ int main(int argc, char *argv[])
int s, lifetime;
//socklen_t addrlen;
su_localinfo_t addr[1];
su_sockaddr_t sockaddr[1] = {{ 0 }};
stunc_t stunc[1];
su_root_t *root = su_root_create(stunc);
stun_engine_t *se;
......@@ -111,11 +114,15 @@ int main(int argc, char *argv[])
if (ss == NULL) { perror("stun_socket_create"); exit(1); }
memset(&addr, 0, sizeof(addr));
addr->li_addr = sockaddr;
addr->li_addrlen = sizeof(addr);
lifetime = 0;
result = stun_bind(ss, (su_localinfo_t *) &addr, &lifetime);
su_root_run(root);
if (result == -1) { perror("stun_bind"); exit(1); }
/*
if (stun_is_natted(ss)) {
......
......@@ -141,10 +141,10 @@ int main(int argc, char *argv[])
stun_engine_t *se;
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_event_t ev);
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_states_t ev);
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_event_t ev)
void torture_callback(torture_t *torturer, stun_engine_t *en, stun_states_t ev)
{
SU_DEBUG_3(("%s: called\n", __func__));
......
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