Commit 959e3c67 authored by Pekka Pessi's avatar Pekka Pessi

Bugs in stun client code leading to failures in the discovery process. (kv)

* stun.h (stun_set_uname_pwd): Changed from unsigned to signed
  char pointers for passing username and password.
* stun.c: Print the error state code if discovery process fails.
* stunc.c (stun_bind_test): The fd_set was not properly reset between calls to select().

darcs-hash:20051128172432-65a35-71519257e0fe9bb246c2d7dc0d642e9da45da09c.gz
parent a305baa9
This diff is collapsed.
......@@ -13,6 +13,17 @@ client library.
@section stun_usage Using Sofia STUN Library
<i>To be written.</i>
To be written.
@section todo Todo
- merge NAT-type check and stun_bind_tests (no
use in using STUN if the NAT type is symmetric) (added 20051118)
- output a summary of results at the end of
stund_bind_test() (added 20051118)
- stun_bind_test does not detect, if the server does
_not_ honor the change-{address,port} requests (i.e. that
response should be send from a different port/address),
which leads to incorrect analysis of NAT behaviour (added 20051119)
*/
......@@ -74,8 +74,8 @@ int stun_get_lifetime(stun_socket_t *ss,
int *lifetime);
/** other functions */
int stun_set_uname_pwd(stun_engine_t *se, const unsigned char *uname, int len_uname,
const unsigned char *pwd, int len_pwd);
int stun_set_uname_pwd(stun_engine_t *se, const char *uname, int len_uname,
const char *pwd, int len_pwd);
/* internal functions declaration */
......
......@@ -44,7 +44,13 @@
#define LARGEST_ATTRIBUTE TURN_LARGEST_ATTRIBUTE
#endif
#ifndef SU_DEBUG
#define SU_DEBUG 3
#endif
#define SU_LOG (stun_log)
#include <su_debug.h>
#include "stun_common.h"
const char stun_400_Bad_request[] = "Bad Request",
......@@ -77,11 +83,13 @@ int stun_parse_message(stun_msg_t *msg) {
msg->stun_hdr.msg_type = ntohs(tmp16);
memcpy(&tmp16, p, 2); p+=2;
msg->stun_hdr.msg_len = ntohs(tmp16);
for(i=0; i<8; i++) {
memcpy(&tmp16, p, 2); p+=2;
msg->stun_hdr.tran_id[i] = ntohs(tmp16);
}
fprintf(stderr, "Parse STUN message: Length = %d\n", msg->stun_hdr.msg_len);
SU_DEBUG_5(("stun: Parse STUN message: Length = %d\n", msg->stun_hdr.msg_len));
/* parse attributes */
len = msg->stun_hdr.msg_len;
......@@ -90,7 +98,7 @@ int stun_parse_message(stun_msg_t *msg) {
while(len > 0) {
i = stun_parse_attribute(msg, p);
if(i <= 0) {
fprintf(stderr, "Error parsing attribute.\n");
SU_DEBUG_0(("stun: Error parsing attribute.\n"));
return -1;
}
p += i;
......@@ -120,8 +128,8 @@ int stun_parse_attribute(stun_msg_t *msg, unsigned char *p) {
p+=2;
len = ntohs(tmp16);
fprintf(stderr, "Parsing attribute: Type %02X, Length %d - %s\n",
attr->attr_type, len, stun_attr_phrase(attr->attr_type));
SU_DEBUG_3(("stun: received attribute: Type %02X, Length %d - %s\n",
attr->attr_type, len, stun_attr_phrase(attr->attr_type)));
switch(attr->attr_type) {
case MAPPED_ADDRESS:
case RESPONSE_ADDRESS:
......@@ -202,7 +210,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_addr.s_addr, p+4, 4);
fprintf(stderr, "parsing address attribute: %s:%d\n", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
SU_DEBUG_3(("stun: address attribute: %s:%d\n", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)));
attr->pattr = addr;
stun_init_buffer(&attr->enc_buf);
......@@ -223,7 +231,7 @@ int stun_parse_attr_error_code(stun_attr_t *attr, const unsigned char *p, unsign
error->phrase = (char *) malloc(len-4);
strncpy(error->phrase, p+4, len-4);
strncpy(error->phrase, (char*)p+4, len-4);
attr->pattr = error;
stun_init_buffer(&attr->enc_buf);
......@@ -397,7 +405,8 @@ int stun_encode_buffer(stun_attr_t *attr) {
}
int stun_encode_message_integrity(stun_attr_t *attr, unsigned char *buf, int len, stun_buffer_t *pwd) {
int dig_len, padded_len;
int padded_len;
size_t dig_len;
unsigned char *padded_text;
if(stun_encode_type_len(attr, 20)<0) {
......@@ -434,7 +443,8 @@ int stun_encode_type_len(stun_attr_t *attr, uint16_t len) {
*/
int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd) {
int dig_len, padded_len, len;
int padded_len, len;
size_t dig_len;
unsigned char dig[20]; /* received sha1 digest */
unsigned char *padded_text;
......@@ -464,7 +474,7 @@ int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd) {
}
}
else {
fprintf(stderr, "Message integrity validated.\n");
SU_DEBUG_5(("Message integrity validated.\n"));
}
free(padded_text);
......@@ -538,8 +548,8 @@ 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,
0, (struct sockaddr *)to_addr, sizeof(*to_addr));
fprintf(stderr, "STUN message sent to %s:%u\n",
inet_ntoa(to_addr->sin_addr), ntohs(to_addr->sin_port));
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;
......@@ -614,17 +624,17 @@ int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd) {
msg->stun_hdr.msg_len = len;
buf_len = 20+msg->stun_hdr.msg_len;
buf = (char *)malloc(buf_len);
buf = (unsigned char *)malloc(buf_len);
/* convert to binary format for transmission */
len = 0;
tmp16 = htons(msg->stun_hdr.msg_type);
memcpy(buf, (char *)&tmp16, 2); len+=2;
memcpy(buf, (unsigned char *)&tmp16, 2); len+=2;
tmp16 = htons(msg->stun_hdr.msg_len);
memcpy(buf+len, (char *)&tmp16, 2); len+=2;
memcpy(buf+len, (unsigned char *)&tmp16, 2); len+=2;
for(i=0; i<8; i++) {
tmp16 = htons(msg->stun_hdr.tran_id[i]);
memcpy(buf+len, (char *)&tmp16, 2); len+=2;
memcpy(buf+len, (unsigned char *)&tmp16, 2); len+=2;
}
/* attaching encoded attributes */
......
......@@ -50,7 +50,8 @@ void usage(int exitcode)
int main(int argc, char *argv[])
{
int result;
int s, addrlen, lifetime;
int s, lifetime;
socklen_t addrlen;
su_sockaddr_t addr;
stun_engine_t *se;
......
......@@ -52,7 +52,6 @@ char const *name = "torture_stun";
static int test_init(char *addr);
static int test_sync_stun(char *addr);
static int test_async_stun(void);
static int test_get_nattype(char *addr);
static int test_get_lifetime(char *addr);
......@@ -140,7 +139,8 @@ int test_init(char *server)
int test_sync_stun(char *localaddr)
{
int result;
int s, addrlen, locallen, lifetime;
int s, lifetime;
socklen_t addrlen, locallen;
su_sockaddr_t addr;
stun_socket_t *ss;
struct sockaddr_in *my_addr, local;
......@@ -160,7 +160,7 @@ int test_sync_stun(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_set_uname_pwd(se, username, (int)strlen(username), password, (int)strlen(password));
}
fclose(pwd);
}
......@@ -287,6 +287,8 @@ int test_get_nattype(char *localaddr)
#include <poll.h>
/* XXX: Not used in the test set yet. */
#if 0
/*
* Run test asynchronously (with non-blocking socket).
* stun_bind() is called repeteadly until it returns 0 (or -1 with errno
......@@ -296,7 +298,8 @@ int test_get_nattype(char *localaddr)
int test_async_stun(void)
{
int result;
int s, addrlen, locallen, lifetime;
int s, lifetime;
socklen_t addrlen, locallen;
su_sockaddr_t addr, local;
stun_socket_t *ss;
......@@ -341,6 +344,7 @@ int test_async_stun(void)
END();
}
#endif
static int test_deinit(void)
{
......
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