From 117a6b2f87eab48523f3ecd0d72d28ad9903c849 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 7 Oct 2015 09:51:33 +0000 Subject: stylistic fixes --- src/nat/nat.c | 6 +- src/nat/nat_stun.c | 718 +++++++++++++++++++++++++---------------------------- src/nat/nat_stun.h | 103 ++++---- 3 files changed, 398 insertions(+), 429 deletions(-) (limited to 'src/nat') diff --git a/src/nat/nat.c b/src/nat/nat.c index 709a13f03..3573ec497 100644 --- a/src/nat/nat.c +++ b/src/nat/nat.c @@ -1734,7 +1734,9 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client"); if ((GNUNET_YES == h->enable_nat_client) && (GNUNET_YES != - GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42"))) // none of these parameters are actually used in privilege testing mode + GNUNET_OS_check_helper_binary (binary, + GNUNET_YES, + "-d 127.0.0.1 127.0.0.2 42"))) /* none of these parameters are actually used in privilege testing mode */ { h->enable_nat_client = GNUNET_NO; LOG (GNUNET_ERROR_TYPE_WARNING, @@ -1814,7 +1816,7 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) GNUNET_SCHEDULER_cancel (h->dns_task); h->dns_task = NULL; } - if (NULL != h->stun_task) + if (NULL != h->stun_task) { GNUNET_SCHEDULER_cancel (h->stun_task); h->stun_task = NULL; diff --git a/src/nat/nat_stun.c b/src/nat/nat_stun.c index 5c819356c..66b4265a8 100644 --- a/src/nat/nat_stun.c +++ b/src/nat/nat_stun.c @@ -17,9 +17,7 @@ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - /** - * * This code provides some support for doing STUN transactions. * We send simplest possible packet ia REQUEST with BIND to a STUN server. * @@ -32,7 +30,6 @@ * * This code was based on ministun.c. * - * * @file nat/nat_stun.c * @brief Functions for STUN functionality * @author Bruno Souza Cabral @@ -56,51 +53,52 @@ * the request prior to the timeout or successful execution. Also * used to track our internal state for the request. */ -struct GNUNET_NAT_STUN_Handle { - - /** - * Handle to a pending DNS lookup request. - */ - struct GNUNET_RESOLVER_RequestHandle *dns_active; - - - /** - * Handle to the listen socket - */ - struct GNUNET_NETWORK_Handle * sock; - - /** - * Stun server address - */ - char *stun_server ; - - /** - * STUN port - */ - int stun_port; - - /** - * Function to call when a error occours - */ - GNUNET_NAT_STUN_ErrorCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Do we got a DNS resolution successfully ? - */ - int dns_success; +struct GNUNET_NAT_STUN_Handle +{ + /** + * Handle to a pending DNS lookup request. + */ + struct GNUNET_RESOLVER_RequestHandle *dns_active; + + /** + * Handle to the listen socket + */ + struct GNUNET_NETWORK_Handle *sock; + + /** + * Stun server address + */ + char *stun_server; + + /** + * Function to call when a error occours + */ + GNUNET_NAT_STUN_ErrorCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Do we got a DNS resolution successfully? + */ + int dns_success; + + /** + * STUN port + */ + uint16_t stun_port; }; - -/* here we store credentials extracted from a message */ -struct StunState { +/** + * here we store credentials extracted from a message +*/ +struct StunState +{ uint16_t attr; }; @@ -114,8 +112,8 @@ struct StunState { static int decode_class(int msg) { - /* Sorry for the magic, but this maps the class according to rfc5245 */ - return ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); + /* Sorry for the magic, but this maps the class according to rfc5245 */ + return ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); } /** @@ -127,9 +125,10 @@ decode_class(int msg) static int decode_method(int msg) { - return (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); + return (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); } + /** * Encode a class and method to a compatible STUN format * @@ -138,12 +137,14 @@ decode_method(int msg) * @return message in a STUN compatible format */ static int -encode_message(StunClasses msg_class, StunMethods method) +encode_message (enum StunClasses msg_class, + enum StunMethods method) { - return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | - (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); + return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | + (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); } + /** * Print a class and method from a STUN message * @@ -153,44 +154,46 @@ encode_message(StunClasses msg_class, StunMethods method) static const char * stun_msg2str(int msg) { - - const struct { enum StunClasses value; const char *name; } classes[] = { - { STUN_REQUEST, "Request" }, - { STUN_INDICATION, "Indication" }, - { STUN_RESPONSE, "Response" }, - { STUN_ERROR_RESPONSE, "Error Response" }, - { 0, NULL } - }; - - const struct { enum StunMethods value; const char *name; } methods[] = { - { STUN_BINDING, "Binding" }, - { 0, NULL } - }; - - static char result[32]; - const char *msg_class = NULL; - const char *method = NULL; - int i; - int value; - - value = decode_class(msg); - for (i = 0; classes[i].name; i++) { - msg_class = classes[i].name; - if (classes[i].value == value) - break; - } - value = decode_method(msg); - for (i = 0; methods[i].name; i++) { - method = methods[i].name; - if (methods[i].value == value) - break; - } - GNUNET_snprintf(result, sizeof(result), "%s %s", - method ? : "Unknown Method", - msg_class ? : "Unknown Class Message"); - return result; + static const struct { enum StunClasses value; const char *name; } classes[] = { + { STUN_REQUEST, "Request" }, + { STUN_INDICATION, "Indication" }, + { STUN_RESPONSE, "Response" }, + { STUN_ERROR_RESPONSE, "Error Response" }, + { 0, NULL } + }; + static const struct { enum StunMethods value; const char *name; } methods[] = { + { STUN_BINDING, "Binding" }, + { 0, NULL } + }; + static char result[32]; + const char *msg_class = NULL; + const char *method = NULL; + int i; + int value; + + value = decode_class(msg); + for (i = 0; classes[i].name; i++) + { + msg_class = classes[i].name; + if (classes[i].value == value) + break; + } + value = decode_method(msg); + for (i = 0; methods[i].name; i++) + { + method = methods[i].name; + if (methods[i].value == value) + break; + } + GNUNET_snprintf (result, + sizeof(result), + "%s %s", + method ? : "Unknown Method", + msg_class ? : "Unknown Class Message"); + return result; } + /** * Print attribute name * @@ -200,69 +203,39 @@ stun_msg2str(int msg) static const char * stun_attr2str(int msg) { - const struct { enum StunAttributes value; const char *name; } attrs[] = { - { STUN_MAPPED_ADDRESS, "Mapped Address" }, - { STUN_RESPONSE_ADDRESS, "Response Address" }, - { STUN_CHANGE_ADDRESS, "Change Address" }, - { STUN_SOURCE_ADDRESS, "Source Address" }, - { STUN_CHANGED_ADDRESS, "Changed Address" }, - { STUN_USERNAME, "Username" }, - { STUN_PASSWORD, "Password" }, - { STUN_MESSAGE_INTEGRITY, "Message Integrity" }, - { STUN_ERROR_CODE, "Error Code" }, - { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" }, - { STUN_REFLECTED_FROM, "Reflected From" }, - { STUN_REALM, "Realm" }, - { STUN_NONCE, "Nonce" }, - { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" }, - { STUN_MS_VERSION, "MS Version" }, - { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" }, - { STUN_SOFTWARE, "Software" }, - { STUN_ALTERNATE_SERVER, "Alternate Server" }, - { STUN_FINGERPRINT, "Fingerprint" }, - { 0, NULL } - }; - int i; - - for (i = 0; attrs[i].name; i++) { - if (attrs[i].value == msg) - return attrs[i].name; - } - return "Unknown Attribute"; -} - - -/** - * Fill the stun_header with a random request_id - * - * @param state, STUN attribute type - * @param attr , the actual attribute - * - * @param req, stun header to be filled - */ -static int -stun_process_attr(struct StunState *state, struct stun_attr *attr) -{ - LOG (GNUNET_ERROR_TYPE_INFO, - "Found STUN Attribute %s (%04x), length %d\n", - stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len)); - - switch (ntohs(attr->attr)) { - case STUN_MAPPED_ADDRESS: - case STUN_XOR_MAPPED_ADDRESS: - case STUN_MS_XOR_MAPPED_ADDRESS: - break; - default: - LOG (GNUNET_ERROR_TYPE_INFO, - "Ignoring STUN Attribute %s (%04x), length %d\n", - stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len)); - - } - return 0; + const struct { enum StunAttributes value; const char *name; } attrs[] = { + { STUN_MAPPED_ADDRESS, "Mapped Address" }, + { STUN_RESPONSE_ADDRESS, "Response Address" }, + { STUN_CHANGE_ADDRESS, "Change Address" }, + { STUN_SOURCE_ADDRESS, "Source Address" }, + { STUN_CHANGED_ADDRESS, "Changed Address" }, + { STUN_USERNAME, "Username" }, + { STUN_PASSWORD, "Password" }, + { STUN_MESSAGE_INTEGRITY, "Message Integrity" }, + { STUN_ERROR_CODE, "Error Code" }, + { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" }, + { STUN_REFLECTED_FROM, "Reflected From" }, + { STUN_REALM, "Realm" }, + { STUN_NONCE, "Nonce" }, + { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" }, + { STUN_MS_VERSION, "MS Version" }, + { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" }, + { STUN_SOFTWARE, "Software" }, + { STUN_ALTERNATE_SERVER, "Alternate Server" }, + { STUN_FINGERPRINT, "Fingerprint" }, + { 0, NULL } + }; + unsigned int i; + + for (i = 0; attrs[i].name; i++) + { + if (attrs[i].value == msg) + return attrs[i].name; + } + return "Unknown Attribute"; } - /** * Fill the stun_header with a random request_id * @@ -271,11 +244,12 @@ stun_process_attr(struct StunState *state, struct stun_attr *attr) static void generate_request_id(struct stun_header *req) { - unsigned int x; - req->magic = htonl(STUN_MAGIC_COOKIE); - for (x = 0; x < 3; x++) - req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, - UINT32_MAX); + unsigned int x; + + req->magic = htonl(STUN_MAGIC_COOKIE); + for (x = 0; x < 3; x++) + req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, + UINT32_MAX); } @@ -292,38 +266,41 @@ generate_request_id(struct stun_header *req) * @return 0 on success, other value otherwise */ static int -stun_get_mapped(struct StunState *st, struct stun_attr *attr,struct sockaddr_in *arg, unsigned int magic) +stun_get_mapped (struct StunState *st, + struct stun_attr *attr, + struct sockaddr_in *arg, + unsigned int magic) { - struct stun_addr *returned_addr = (struct stun_addr *)(attr + 1); - struct sockaddr_in *sa = (struct sockaddr_in *)arg; - unsigned short type = ntohs(attr->attr); - - switch (type) { - case STUN_MAPPED_ADDRESS: - if (st->attr == STUN_XOR_MAPPED_ADDRESS || - st->attr == STUN_MS_XOR_MAPPED_ADDRESS) - return 1; - magic = 0; - break; - case STUN_MS_XOR_MAPPED_ADDRESS: - if (st->attr == STUN_XOR_MAPPED_ADDRESS) - return 1; - break; - case STUN_XOR_MAPPED_ADDRESS: - break; - default: - return 1; - } - if (ntohs(attr->len) < 8 && returned_addr->family != 1) { - return 1; - } + struct stun_addr *returned_addr = (struct stun_addr *)(attr + 1); + struct sockaddr_in *sa = (struct sockaddr_in *)arg; + unsigned short type = ntohs(attr->attr); - st->attr = type; - - sa->sin_family = AF_INET; - sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16); - sa->sin_addr.s_addr = returned_addr->addr ^ magic; - return 0; + switch (type) + { + case STUN_MAPPED_ADDRESS: + if (st->attr == STUN_XOR_MAPPED_ADDRESS || + st->attr == STUN_MS_XOR_MAPPED_ADDRESS) + return 1; + magic = 0; + break; + case STUN_MS_XOR_MAPPED_ADDRESS: + if (st->attr == STUN_XOR_MAPPED_ADDRESS) + return 1; + break; + case STUN_XOR_MAPPED_ADDRESS: + break; + default: + return 1; + } + if (ntohs(attr->len) < 8 && returned_addr->family != 1) + { + return 1; + } + st->attr = type; + sa->sin_family = AF_INET; + sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16); + sa->sin_addr.s_addr = returned_addr->addr ^ magic; + return 0; } @@ -334,123 +311,122 @@ stun_get_mapped(struct StunState *st, struct stun_attr *attr,struct sockaddr_in * the externally visible address of the request. * If a callback is specified, invoke it with the attribute. * - * @param data, the packet - * @param len, the length of the packet - * @param arg, sockaddr_in where we will set our discovered packet + * @param data the packet + * @param len the length of the packet + * @param arg sockaddr_in where we will set our discovered packet * - * @return, #GNUNET_OK on OK, #GNUNET_NO if the packet is invalid ( not a stun packet) + * @return, #GNUNET_OK on OK, #GNUNET_NO if the packet is invalid (not a stun packet) */ int -GNUNET_NAT_stun_handle_packet(const void *data, size_t len, struct sockaddr_in *arg) +GNUNET_NAT_stun_handle_packet (const void *data, + size_t len, + struct sockaddr_in *arg) { - const struct stun_header *hdr = (const struct stun_header *)data; - struct stun_attr *attr; - struct StunState st; - int ret = GNUNET_OK; - - uint32_t advertised_message_size; - uint32_t message_magic_cookie; - - - /* On entry, 'len' is the length of the udp payload. After the - * initial checks it becomes the size of unprocessed options, - * while 'data' is advanced accordingly. - */ - if (len < sizeof(struct stun_header)) { - LOG (GNUNET_ERROR_TYPE_INFO, - "STUN packet too short (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header)); - GNUNET_break_op (0); - return GNUNET_NO; - } - /* Skip header as it is already in hdr */ - len -= sizeof(struct stun_header); - data += sizeof(struct stun_header); - - /* len as advertised in the message */ - advertised_message_size = ntohs(hdr->msglen); - - message_magic_cookie = ntohl(hdr->magic); - /* Compare if the cookie match */ - if(STUN_MAGIC_COOKIE != message_magic_cookie){ - LOG (GNUNET_ERROR_TYPE_INFO, - "Invalid magic cookie \n"); - return GNUNET_NO; - } + const struct stun_header *hdr = (const struct stun_header *)data; + struct stun_attr *attr; + struct StunState st; + int ret = GNUNET_OK; + uint32_t advertised_message_size; + uint32_t message_magic_cookie; + + /* On entry, 'len' is the length of the udp payload. After the + * initial checks it becomes the size of unprocessed options, + * while 'data' is advanced accordingly. + */ + if (len < sizeof(struct stun_header)) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "STUN packet too short (only %d, wanting at least %d)\n", + (int) len, + (int) sizeof(struct stun_header)); + GNUNET_break_op (0); + return GNUNET_NO; + } + /* Skip header as it is already in hdr */ + len -= sizeof(struct stun_header); + data += sizeof(struct stun_header); + /* len as advertised in the message */ + advertised_message_size = ntohs(hdr->msglen); - LOG (GNUNET_ERROR_TYPE_INFO, "STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), - ntohs(hdr->msgtype), - advertised_message_size); + message_magic_cookie = ntohl(hdr->magic); + /* Compare if the cookie match */ + if(STUN_MAGIC_COOKIE != message_magic_cookie) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Invalid magic cookie \n"); + return GNUNET_NO; + } + LOG (GNUNET_ERROR_TYPE_INFO, + "STUN Packet, msg %s (%04x), length: %d\n", + stun_msg2str(ntohs(hdr->msgtype)), + ntohs(hdr->msgtype), + advertised_message_size); + if (advertised_message_size > len) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Scrambled STUN packet length (got %d, expecting %d)\n", + advertised_message_size, + (int)len); + return GNUNET_NO; + } + else + { + len = advertised_message_size; + } + memset (&st,0, sizeof(st)); - if (advertised_message_size > len) { - LOG (GNUNET_ERROR_TYPE_INFO, "Scrambled STUN packet length (got %d, expecting %d)\n", advertised_message_size, - (int)len); - return GNUNET_NO; - } else { - len = advertised_message_size; + while (len > 0) + { + if (len < sizeof(struct stun_attr)) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Attribute too short (got %d, expecting %d)\n", + (int)len, + (int) sizeof(struct stun_attr)); + break; } + attr = (struct stun_attr *)data; - memset(&st,0, sizeof(st)); - - while (len > 0) { - if (len < sizeof(struct stun_attr)) { - LOG (GNUNET_ERROR_TYPE_INFO, "Attribute too short (got %d, expecting %d)\n", (int)len, - (int) sizeof(struct stun_attr)); - break; - } - attr = (struct stun_attr *)data; - - /* compute total attribute length */ - advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); - - /* Check if we still have space in our buffer */ - if (advertised_message_size > len ) { - LOG (GNUNET_ERROR_TYPE_INFO, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", advertised_message_size, - (int)len); - break; - } - - - stun_get_mapped(&st, attr, arg, hdr->magic); - - if (stun_process_attr(&st, attr)) { - LOG (GNUNET_ERROR_TYPE_INFO, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), - ntohs(attr->attr)); - break; - } - /** Clear attribute id: in case previous entry was a string, - * this will act as the terminator for the string. - **/ - attr->attr = 0; - data += advertised_message_size; - len -= advertised_message_size; - ret = GNUNET_OK; - } + /* compute total attribute length */ + advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); - return ret; + /* Check if we still have space in our buffer */ + if (advertised_message_size > len ) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", + advertised_message_size, + (int)len); + break; + } + stun_get_mapped (&st, attr, arg, hdr->magic); + /* Clear attribute id: in case previous entry was a string, + * this will act as the terminator for the string. + */ + attr->attr = 0; + data += advertised_message_size; + len -= advertised_message_size; + ret = GNUNET_OK; + } + return ret; } - /** * Clean-up used memory * - * @param cls our `struct GNUNET_NAT_STUN_Handle *` + * @param handle handle to release memory for */ static void -clean(struct GNUNET_NAT_STUN_Handle * handle) +clean (struct GNUNET_NAT_STUN_Handle *handle) { - if(handle->stun_server) - { - GNUNET_free(handle->stun_server); - } - GNUNET_free(handle); - + GNUNET_free (handle->stun_server); + GNUNET_free (handle); } - /** * Try to establish a connection given the specified address. * @@ -461,120 +437,112 @@ clean(struct GNUNET_NAT_STUN_Handle * handle) static void stun_dns_callback (void *cls, const struct sockaddr *addr, - socklen_t addrlen) { - - - struct GNUNET_NAT_STUN_Handle *request = cls; - - struct stun_header *req; - uint8_t reqdata[1024]; - int reqlen; - struct sockaddr_in server; - - - if(NULL == request) { - - if( GNUNET_NO == request->dns_success){ - LOG (GNUNET_ERROR_TYPE_INFO, "Empty request\n"); - request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); - clean(request); - - } - return; - } - - if (NULL == addr) { - request->dns_active = NULL; - - if( GNUNET_NO == request->dns_success){ - LOG (GNUNET_ERROR_TYPE_INFO, "Error resolving host %s\n", request->stun_server); - request->cb(request->cb_cls, GNUNET_NAT_ERROR_NOT_ONLINE); - clean(request); - - } + socklen_t addrlen) +{ + struct GNUNET_NAT_STUN_Handle *request = cls; + struct stun_header *req; + uint8_t reqdata[1024]; + int reqlen; + struct sockaddr_in server; - return; + if (NULL == request) + { + if (GNUNET_NO == request->dns_success) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Empty request\n"); + request->cb (request->cb_cls, + GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); + clean (request); } - - - request->dns_success= GNUNET_YES; - memset(&server,0, sizeof(server)); - server.sin_family = AF_INET; - server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr; - server.sin_port = htons(request->stun_port); - - - /*Craft the simplest possible STUN packet. A request binding*/ - req = (struct stun_header *)reqdata; - generate_request_id(req); - reqlen = 0; - req->msgtype = 0; - req->msglen = 0; - req->msglen = htons(reqlen); - req->msgtype = htons(encode_message(STUN_REQUEST, STUN_BINDING)); - - /* Send the packet */ - if (-1 == GNUNET_NETWORK_socket_sendto (request->sock, req, ntohs(req->msglen) + sizeof(*req), - (const struct sockaddr *) &server, sizeof (server))) + return; + } + if (NULL == addr) + { + request->dns_active = NULL; + if (GNUNET_NO == request->dns_success) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "Fail to sendto"); - request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); - clean(request); - return; + LOG (GNUNET_ERROR_TYPE_INFO, + "Error resolving host %s\n", + request->stun_server); + request->cb (request->cb_cls, + GNUNET_NAT_ERROR_NOT_ONLINE); + clean (request); } + return; + } - + request->dns_success= GNUNET_YES; + memset(&server,0, sizeof(server)); + server.sin_family = AF_INET; + server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr; + server.sin_port = htons(request->stun_port); + + /*Craft the simplest possible STUN packet. A request binding*/ + req = (struct stun_header *)reqdata; + generate_request_id(req); + reqlen = 0; + req->msgtype = 0; + req->msglen = 0; + req->msglen = htons(reqlen); + req->msgtype = htons(encode_message(STUN_REQUEST, STUN_BINDING)); + + /* Send the packet */ + if (-1 == GNUNET_NETWORK_socket_sendto (request->sock, + req, + ntohs(req->msglen) + sizeof(*req), + (const struct sockaddr *) &server, + sizeof (server))) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, + "Fail to sendto"); + request->cb (request->cb_cls, + GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); + clean (request); + return; + } } - - - /** - * Make Generic STUN request and - * Send a generic stun request to the server specified using the specified socket. - * possibly waiting for a reply and filling the 'reply' field with - * the externally visible address. + * Make Generic STUN request. Sends a generic stun request to the + * server specified using the specified socket, possibly waiting for + * a reply and filling the 'reply' field with the externally visible + * address. * - * @param server, the address of the stun server - * @param port, port of the stun server + * @param server the address of the stun server + * @param port port of the stun server * @param sock the socket used to send the request - * @param cb, callback in case of error + * @param cb callback in case of error + * @param cb_cls closure for @a cb * @return #GNUNET_OK success, #GNUNET_NO on error. */ int -GNUNET_NAT_stun_make_request(char * server, - int port, - struct GNUNET_NETWORK_Handle * sock, - GNUNET_NAT_STUN_ErrorCallback cb, - void *cb_cls) +GNUNET_NAT_stun_make_request (const char *server, + uint16_t port, + struct GNUNET_NETWORK_Handle *sock, + GNUNET_NAT_STUN_ErrorCallback cb, + void *cb_cls) { - - struct GNUNET_NAT_STUN_Handle *rh; - - rh = GNUNET_malloc (sizeof (struct GNUNET_NAT_STUN_Handle)); - rh->sock = sock; - - char * server_copy = GNUNET_strdup (server); - - rh->cb = cb; - rh->cb_cls = cb_cls; - rh->stun_server = server_copy; - rh->stun_port = port; - rh->dns_success = GNUNET_NO; - - rh->dns_active = GNUNET_RESOLVER_ip_get (server_copy, AF_INET, - TIMEOUT, - &stun_dns_callback, rh); - - if (rh->dns_active == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "Failed DNS"); - GNUNET_free(rh); - GNUNET_free(server_copy); - - return GNUNET_NO; - } - - return GNUNET_OK; + struct GNUNET_NAT_STUN_Handle *rh; + + rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle); + rh->sock = sock; + rh->cb = cb; + rh->cb_cls = cb_cls; + rh->stun_server = GNUNET_strdup (server); + rh->stun_port = port; + rh->dns_success = GNUNET_NO; + rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server, + AF_INET, + TIMEOUT, + &stun_dns_callback, rh); + if (NULL == rh->dns_active) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, + "Failed DNS"); + clean (rh); + return GNUNET_NO; + } + return GNUNET_OK; } diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h index 1dbb2a6ac..d79fd9ad2 100644 --- a/src/nat/nat_stun.h +++ b/src/nat/nat_stun.h @@ -36,76 +36,75 @@ typedef struct { uint32_t id[3]; } GNUNET_PACKED stun_trans_id; -struct stun_header { - uint16_t msgtype; - uint16_t msglen; - uint32_t magic; - stun_trans_id id; - +struct stun_header +{ + uint16_t msgtype; + uint16_t msglen; + uint32_t magic; + stun_trans_id id; } GNUNET_PACKED; -struct stun_attr { - uint16_t attr; - uint16_t len; - +struct stun_attr +{ + uint16_t attr; + uint16_t len; } GNUNET_PACKED; /* * The format normally used for addresses carried by STUN messages. */ -struct stun_addr { - uint8_t unused; - uint8_t family; - uint16_t port; - uint32_t addr; +struct stun_addr +{ + uint8_t unused; + uint8_t family; + uint16_t port; + uint32_t addr; } GNUNET_PACKED; /* STUN message classes */ -typedef enum StunClasses { +enum StunClasses { INVALID_CLASS = 0, STUN_REQUEST = 0x0000, STUN_INDICATION = 0x0001, STUN_RESPONSE = 0x0002, STUN_ERROR_RESPONSE = 0x0003 -} StunClasses; - -typedef enum StunMethods { - INVALID_METHOD = 0, - STUN_BINDING = 0x0001, - STUN_SHARED_SECRET = 0x0002, - STUN_ALLOCATE = 0x0003, - STUN_REFRESH = 0x0004, - STUN_SEND = 0x0006, - STUN_DATA = 0x0007, - STUN_CREATE_PERMISSION = 0x0008, - STUN_CHANNEL_BIND = 0x0009 -} StunMethods; +}; + +enum StunMethods { + INVALID_METHOD = 0, + STUN_BINDING = 0x0001, + STUN_SHARED_SECRET = 0x0002, + STUN_ALLOCATE = 0x0003, + STUN_REFRESH = 0x0004, + STUN_SEND = 0x0006, + STUN_DATA = 0x0007, + STUN_CREATE_PERMISSION = 0x0008, + STUN_CHANNEL_BIND = 0x0009 +}; /* Basic attribute types in stun messages. * Messages can also contain custom attributes (codes above 0x7fff) */ - -typedef enum StunAttributes { - STUN_MAPPED_ADDRESS = 0x0001, - STUN_RESPONSE_ADDRESS = 0x0002, - STUN_CHANGE_ADDRESS = 0x0003, - STUN_SOURCE_ADDRESS = 0x0004, - STUN_CHANGED_ADDRESS = 0x0005, - STUN_USERNAME = 0x0006, - STUN_PASSWORD = 0x0007, - STUN_MESSAGE_INTEGRITY = 0x0008, - STUN_ERROR_CODE = 0x0009, - STUN_UNKNOWN_ATTRIBUTES = 0x000a, - STUN_REFLECTED_FROM = 0x000b, - STUN_REALM = 0x0014, - STUN_NONCE = 0x0015, - STUN_XOR_MAPPED_ADDRESS = 0x0020, - STUN_MS_VERSION = 0x8008, - STUN_MS_XOR_MAPPED_ADDRESS = 0x8020, - STUN_SOFTWARE = 0x8022, - STUN_ALTERNATE_SERVER = 0x8023, - STUN_FINGERPRINT = 0x8028 -} StunAttributes; - +enum StunAttributes { + STUN_MAPPED_ADDRESS = 0x0001, + STUN_RESPONSE_ADDRESS = 0x0002, + STUN_CHANGE_ADDRESS = 0x0003, + STUN_SOURCE_ADDRESS = 0x0004, + STUN_CHANGED_ADDRESS = 0x0005, + STUN_USERNAME = 0x0006, + STUN_PASSWORD = 0x0007, + STUN_MESSAGE_INTEGRITY = 0x0008, + STUN_ERROR_CODE = 0x0009, + STUN_UNKNOWN_ATTRIBUTES = 0x000a, + STUN_REFLECTED_FROM = 0x000b, + STUN_REALM = 0x0014, + STUN_NONCE = 0x0015, + STUN_XOR_MAPPED_ADDRESS = 0x0020, + STUN_MS_VERSION = 0x8008, + STUN_MS_XOR_MAPPED_ADDRESS = 0x8020, + STUN_SOFTWARE = 0x8022, + STUN_ALTERNATE_SERVER = 0x8023, + STUN_FINGERPRINT = 0x8028 +}; -- cgit v1.2.3