Commit 7bbb41a1 authored by Martti Mela's avatar Martti Mela

working async stun support

darcs-hash:20051214143210-1b897-8d70bb7b9fc5c516c9f535076d874922eb569ffe.gz
parent d66a5cca
This diff is collapsed.
......@@ -109,9 +109,11 @@ void stun_socket_destroy(stun_socket_t *ss);
/** Bind a socket using STUN. */
int stun_bind(stun_socket_t *ss,
su_localinfo_t *my_addr,
/* su_localinfo_t *my_addr, */
int *return_lifetime);
su_localinfo_t *stun_get_local_addr(stun_engine_t *en);
int stun_get_nattype(stun_socket_t *ss,
su_localinfo_t *my_addr,
int *addrlen);
......
......@@ -450,33 +450,33 @@ int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd) {
unsigned char dig[20]; /* received sha1 digest */
unsigned char *padded_text;
if(pwd->data == NULL) {
return 0; /* no need to check */
}
if (pwd->data == NULL)
return 0;
/* message integrity not received */
if(stun_get_attr(msg->stun_attr, MESSAGE_INTEGRITY)==NULL) {
fprintf(stderr, "Message integrity missing.\n");
if (stun_get_attr(msg->stun_attr, MESSAGE_INTEGRITY) == NULL) {
SU_DEBUG_5(("%s: error: message integrity missing.\n", __func__));
return -1;
}
/* zero padding */
len = msg->enc_buf.size-24;
padded_len = len + (len%64==0? 0:64 - (len%64));
padded_text = (unsigned char *)malloc(padded_len);
padded_len = len + (len % 64 == 0 ? 0 : 64 - (len % 64));
padded_text = (unsigned char *) malloc(padded_len);
memset(padded_text, 0, padded_len);
memcpy(padded_text, msg->enc_buf.data, len);
memcpy(dig, HMAC(EVP_sha1(), pwd->data, pwd->size, padded_text, padded_len, NULL, &dig_len), 20);
if(memcmp(dig, msg->enc_buf.data+msg->enc_buf.size-20, 20)!=0) {
if (memcmp(dig, msg->enc_buf.data+msg->enc_buf.size-20, 20) != 0) {
/* does not match, but try the test server's password */
if(memcmp(msg->enc_buf.data+msg->enc_buf.size-20, "hmac-not-implemented", 20)!=0) {
fprintf(stderr, "Error: message digest problem\n");
if (memcmp(msg->enc_buf.data+msg->enc_buf.size-20, "hmac-not-implemented", 20) != 0) {
SU_DEBUG_5(("%s: error: message digest problem.\n", __func__));
return -1;
}
}
else {
SU_DEBUG_5(("Message integrity validated.\n"));
SU_DEBUG_5(("%s: message integrity validated.\n", __func__));
}
free(padded_text);
......@@ -486,10 +486,14 @@ int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd) {
void debug_print(stun_buffer_t *buf) {
int i;
for(i=0; i<buf->size/4; i++) {
for(i = 0; i < buf->size/4; i++) {
SU_DEBUG_9(("%02x %02x %02x %02x\n",
*(buf->data+i*4), *(buf->data+i*4+1), *(buf->data+i*4+2), *(buf->data+i*4+3)));
if(i==4) SU_DEBUG_9(("---------------------\n"));
*(buf->data + i*4),
*(buf->data + i*4 +1),
*(buf->data + i*4 +2),
*(buf->data + i*4 +3)));
if (i == 4)
SU_DEBUG_9(("---------------------\n"));
}
SU_DEBUG_9(("\n"));
}
......
......@@ -68,11 +68,23 @@ 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_states_t event)
{
su_localinfo_t *li = NULL;
printf("event: %d\n", event); fflush(stdout);
if (event < 0)
su_root_break(stun_engine_root(en));
if (event == stun_client_done) {
char ipaddr[48];
li = stun_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_root_break(stun_engine_root(en));
}
else if (event == stun_client_error) {
SU_DEBUG_3(("%s: no nat detected\n", __func__));
su_root_break(stun_engine_root(en));
}
return;
}
......@@ -82,8 +94,6 @@ int main(int argc, char *argv[])
int result;
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;
......@@ -113,28 +123,12 @@ 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);
result = stun_bind(ss, &lifetime);
su_root_run(root);
if (result == -1) { perror("stun_bind"); exit(1); }
/*
if (stun_is_natted(ss)) {
char ipaddr[48];
printf("natted as %s:%u\n",
inet_ntop(addr.su_family, SU_ADDR(&addr), ipaddr, sizeof(ipaddr)),
ntohs(addr.su_port));
}
else {
printf("no nat detected\n");
}
*/
stun_socket_destroy(ss);
stun_engine_destroy(se);
......
......@@ -212,7 +212,7 @@ int test_sync_stun(char *localaddr)
*/
addrlen = sizeof(*my_addr);
result = stun_bind(ss, &addr, &lifetime); TEST(result, 0);
result = stun_bind(ss, &lifetime); TEST(result, 0);
/* Just a check that getsockname() returns same address as stun_bind */
memset(&local, 0, sizeof(local)); locallen = sizeof(local);
......
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