Author: biurrun Date: Thu Nov 26 18:03:17 2009 New Revision: 1835 Log: Move handle_packet_ack_1 / handle_packet_ack_2 (and dependencies) to the only place they are used and mark them as static. Deleted: trunk/pairing/packet_handler_send.c trunk/pairing/packet_handler_send.h Modified: trunk/Makefile.am trunk/pairing/packet_handler.c Modified: trunk/Makefile.am ============================================================================== --- trunk/Makefile.am Thu Nov 26 17:57:34 2009 (r1834) +++ trunk/Makefile.am Thu Nov 26 18:03:17 2009 (r1835) @@ -84,7 +84,6 @@ pairing/packet_handler_accept.c pairing_send_SOURCES = $(pairing_ACCEPT_SEND_SRCS) \ - pairing/packet_handler_send.c \ pairing/send.c pairing_management_SOURCES = $(pairing_COMMON_SRCS) \ Modified: trunk/pairing/packet_handler.c ============================================================================== --- trunk/pairing/packet_handler.c Thu Nov 26 17:57:34 2009 (r1834) +++ trunk/pairing/packet_handler.c Thu Nov 26 18:03:17 2009 (r1835) @@ -10,9 +10,203 @@ * @date Sep. 2008 */ +#include <libconfig.h> +#include <stdio.h> +#include <string.h> +#include <netinet/in.h> +#include "common.h" #include "packet_handler.h" +/** Stores the contact information of a relay in FILE_KNOWN_RELAYS. + * + * @param nickname Pointer to the string containing the suggested nickname of the relay. + * @param hit Pointer to the string containing the HIT of the relay. + * @param ipv4_addr Pointer to the string containing the IPv4 address of the relay. + * @param ipv6_addr Pointer to the string containing the IPv6 address of the relay. + * + * @return 1 if write successful; 0 otherwise + */ +static int store_relay_info(char *nickname, char *hit, char *ipv4_addr, + char *ipv6_addr) +{ + config_t cfg; + config_setting_t *root; + config_setting_t *this_relay; + config_setting_t *new_hit, *new_ipv4_addr, *new_ipv6_addr; + char new_nickname[LENGTH_NICKNAME]; + + assert(hit != NULL); + + config_init(&cfg); + + if (!config_read_file(&cfg, FILE_KNOWN_RELAYS)) + { + DEBUG("Could not read file \'%s\'.", FILE_KNOWN_RELAYS); + return 0; + } + + root = config_root_setting(&cfg); + + if (!global_send.force_options) + { int valid = 1; + do + { + if (!valid) + USER_MSG("Invalid nickname. Nicknames may only contain letters and numbers."); + USER_MSG("Please enter a nickname for this relay (suggest: \'%s\'). Nicknames may only contain alphanumeric characters. Leave blank to skip storing the information.", nickname); + if (!fgets(new_nickname, LENGTH_NICKNAME, stdin)) + return 0; + new_nickname[strlen(new_nickname)-1] = 0; + } + while (!(valid = valid_nickname(new_nickname))); + } + else + strcpy(new_nickname, nickname); // Use suggested nickname by default + + if (strlen(new_nickname) <= 0) + { + USER_MSG("No nickname given. No relay information will be written."); + return 0; + } + + DEBUG_HIGH("Nickname: \'%s\'", new_nickname); + + // See if we already have information on this relay + this_relay = config_lookup(&cfg, new_nickname); + if (this_relay != NULL) // If a relay with that nickname already exists, make an overwrite choice + { + char c; + char temp[LENGTH_NICKNAME + 20]; + if (!global_send.force_options) // If options are not forced, ask before overwriting + { + do + { + USER_MSG("Host with that nickname already exists. Overwrite? (y/n) "); + c = getc(stdin); + } + while ((c != 'y') && (c != 'Y') && (c != 'n') && (c != 'N')); + + if ((c == 'n') || (c == 'N')) + { + USER_MSG("No relay information will be updated.\n"); + return 0; + } + } + + + // Since a relay with that nickname already exists, look up the fields + // and associate them with the appropriate pointers + strcpy(temp, new_nickname); + strcat(temp, ".hit"); + new_hit = config_lookup(&cfg, temp); + + strcpy(temp, new_nickname); + strcat(temp, ".ipv4_addr"); + new_ipv4_addr = config_lookup(&cfg, temp); + + strcpy(temp, new_nickname); + strcat(temp, ".ipv6_addr"); + new_ipv6_addr = config_lookup(&cfg, temp); + } + else // ... create a new group for this relay + { + this_relay = config_setting_add(root, new_nickname, CONFIG_TYPE_GROUP); + if (this_relay == NULL) + { + DEBUG("Setting could not be created. Exiting function."); + return 0; + } + + // Since no relay with that nickname existed, we must create the fields + new_hit = config_setting_add(this_relay, "hit", CONFIG_TYPE_STRING); + new_ipv4_addr = config_setting_add(this_relay, "ipv4_addr", CONFIG_TYPE_STRING); + new_ipv6_addr = config_setting_add(this_relay, "ipv6_addr", CONFIG_TYPE_STRING); + } + + DEBUG("Writing new relay information."); + + // Write relay information + // Write HIT + if (new_hit) + config_setting_set_string(new_hit, hit); + else + DEBUG("Error writing HIT."); + // Write IPv4 address + if (new_ipv4_addr) + config_setting_set_string(new_ipv4_addr, ipv4_addr); + else + DEBUG("Error writing IPv4 address."); + // Write IPv6 address + if (new_ipv6_addr) + config_setting_set_string(new_ipv6_addr, ipv6_addr); + else + DEBUG("Error writing IPv6 address."); + + // Write the configuration back to the file + DEBUG("Write file"); + config_write_file(&cfg, FILE_KNOWN_RELAYS); + config_destroy(&cfg); + + return 1; +} + +/** Handles an ack_1 packet + * + * @param socket_addr Pointer to sockaddr_in6 structure connected to peer. + * @param hdr_ack_1 Pointer to the header_ack_1 structure + * + * @return -1 on failure; 0 if no IP addresses sent; 1 if only IPv4 address sent; 2 if only IPv6 address sent; 3 if both addresses sent + */ +static int handle_packet_ack_1(struct sockaddr_in6 *socket_addr, + header_ack_1 *hdr_ack_1) +{ + int has_ipv4 = 0; + int has_ipv6 = 0; + char hit[INET6_ADDRSTRLEN]; + + assert(socket_addr != NULL); + assert(hdr_ack_1 != NULL); + + // Show the addresses + DEBUG_HIGH("IPv4 address: %s", hdr_ack_1->ipv4_addr); + DEBUG_HIGH("IPv6 address: %s", hdr_ack_1->ipv6_addr); + + // Test to see if the addresses were included + has_ipv4 = hdr_ack_1->ipv4_addr[0] ? 1 : 0; + has_ipv6 = hdr_ack_1->ipv6_addr[0] ? 1 : 0; + + inet_ntop(AF_INET6, &(socket_addr->sin6_addr), hit, sizeof(hit)); + + // Write out addresses (somewhere) if we don't already know this relay + if (!global_send.nickname_given) + store_relay_info((char*)hdr_ack_1->nickname, hit, (char*)hdr_ack_1->ipv4_addr, (char*)hdr_ack_1->ipv6_addr); + // Return a value indicating which addrs were received + // 0 = none; 1 = ipv4; 2 = ipv6; 3 = both + return has_ipv4*1 + has_ipv6*2; +} + + + +/** Handles an ACK 2 packet. + * + * @param socket_addr Pointer to sockaddr_in6 structure connected to peer. + * @param hdr_ack_2 Pointer to the header_pwd_request structure. + * + * @return 1 if password is of nonzero length; 0 otherwise + */ +static int handle_packet_ack_2(struct sockaddr_in6 *socket_addr, + header_ack_2 *hdr_ack_2) +{ + assert(socket_addr != NULL); + assert(hdr_ack_2 != NULL); + + USER_MSG("Password for your friend: %s", hdr_ack_2->password); + + return (strlen((char*)hdr_ack_2->password) > 0); +} + + /** Handles an error packet * * @param socket_addr Pointer to sockaddr_in6 structure connected to peer.