Author: biurrun Date: Thu Nov 26 13:57:43 2009 New Revision: 1791 Log: Move functions around to avoid forward declarations. Modified: trunk/pairing/accept.c trunk/pairing/accept.h trunk/pairing/send.c trunk/pairing/send.h Modified: trunk/pairing/accept.c ============================================================================== --- trunk/pairing/accept.c Thu Nov 26 13:51:01 2009 (r1790) +++ trunk/pairing/accept.c Thu Nov 26 13:57:43 2009 (r1791) @@ -18,81 +18,6 @@ struct global_accept_variables global_accept = {0}; -/** Main program that will be run on the relay. - */ -int main(void) -{ - struct sockaddr_in6 peer_addr; - unsigned int addrlen; - header_general *gen_hdr = malloc(sizeof(header_general)); - int chars_received; // Number of characters received on the socket - char address_string[INET6_ADDRSTRLEN]; // Human-readable address of sender - int msg_type, return_value; - - if (!open_socket_accept()) - { - DEBUG("Socket could not be created. Leaving program.\n"); - return 1; - } - - pisa_cfg_authorized_hosts_setup_file("authorized_hosts.cfg"); - - addrlen = sizeof(struct sockaddr_in6); - - if (signal(SIGALRM, check_expiration_dates) == SIG_ERR) - { - DEBUG("Cannot register signal handler"); - } - check_expiration_dates(SIGALRM); - - - while (1) - { - DEBUG_MED(LINE_BREAK LINE_BREAK); - - // Receive message - DEBUG_MED("Waiting for incoming message..."); - chars_received = recvfrom(global_accept.socket_desc, gen_hdr, sizeof(header_general), 0, (struct sockaddr *)&peer_addr, &addrlen); - DEBUG_MED("Bytes received: %d", chars_received); - - if (chars_received > 0) - { - - - // Get peer address, determine if it's a HIT - inet_ntop(AF_INET6, &peer_addr.sin6_addr, address_string, sizeof(address_string)); - DEBUG("Address of sender is %s", address_string); - if (!pisa_ipv6_addr_is_hit(&peer_addr.sin6_addr)) - DEBUG_HIGH("Address is IPv6."); - else - { - DEBUG_HIGH("Address is a HIT!"); - - // Handle the packet (and react accordingly) - handle_packet(&peer_addr, gen_hdr, &msg_type, &return_value); - - print_msg_info(msg_type, return_value); - } - - DEBUG_MED(LINE_BREAK LINE_BREAK); - } - else - perror("receive"); - - } - - free(gen_hdr); - - pisa_cfg_authorized_hosts_cleanup(); - - // Close the socket - close(global_accept.socket_desc); - - return 0; -} - - - int open_socket_accept(void) { int optval=1; @@ -188,3 +113,75 @@ alarm(TIMER_INTERVAL); } +/** Main program that will be run on the relay. + */ +int main(void) +{ + struct sockaddr_in6 peer_addr; + unsigned int addrlen; + header_general *gen_hdr = malloc(sizeof(header_general)); + int chars_received; // Number of characters received on the socket + char address_string[INET6_ADDRSTRLEN]; // Human-readable address of sender + int msg_type, return_value; + + if (!open_socket_accept()) + { + DEBUG("Socket could not be created. Leaving program.\n"); + return 1; + } + + pisa_cfg_authorized_hosts_setup_file("authorized_hosts.cfg"); + + addrlen = sizeof(struct sockaddr_in6); + + if (signal(SIGALRM, check_expiration_dates) == SIG_ERR) + { + DEBUG("Cannot register signal handler"); + } + check_expiration_dates(SIGALRM); + + + while (1) + { + DEBUG_MED(LINE_BREAK LINE_BREAK); + + // Receive message + DEBUG_MED("Waiting for incoming message..."); + chars_received = recvfrom(global_accept.socket_desc, gen_hdr, sizeof(header_general), 0, (struct sockaddr *)&peer_addr, &addrlen); + DEBUG_MED("Bytes received: %d", chars_received); + + if (chars_received > 0) + { + + + // Get peer address, determine if it's a HIT + inet_ntop(AF_INET6, &peer_addr.sin6_addr, address_string, sizeof(address_string)); + DEBUG("Address of sender is %s", address_string); + if (!pisa_ipv6_addr_is_hit(&peer_addr.sin6_addr)) + DEBUG_HIGH("Address is IPv6."); + else + { + DEBUG_HIGH("Address is a HIT!"); + + // Handle the packet (and react accordingly) + handle_packet(&peer_addr, gen_hdr, &msg_type, &return_value); + + print_msg_info(msg_type, return_value); + } + + DEBUG_MED(LINE_BREAK LINE_BREAK); + } + else + perror("receive"); + + } + + free(gen_hdr); + + pisa_cfg_authorized_hosts_cleanup(); + + // Close the socket + close(global_accept.socket_desc); + + return 0; +} Modified: trunk/pairing/accept.h ============================================================================== --- trunk/pairing/accept.h Thu Nov 26 13:51:01 2009 (r1790) +++ trunk/pairing/accept.h Thu Nov 26 13:57:43 2009 (r1791) @@ -40,8 +40,4 @@ #define TIMER_INTERVAL 60 -// Function prototypes -int open_socket_accept(void); -void check_expiration_dates(int signr); - #endif /* PISA_ACCEPT_H */ Modified: trunk/pairing/send.c ============================================================================== --- trunk/pairing/send.c Thu Nov 26 13:51:01 2009 (r1790) +++ trunk/pairing/send.c Thu Nov 26 13:57:43 2009 (r1791) @@ -16,72 +16,6 @@ /** @todo Make sure that the address we're sending from is a HIT **/ -/** Main program that will be run on the user's computer. - */ -int main(int argc, char *argv[]) -{ - struct sockaddr_in6 *dest_addr; // Our peer - char password[LENGTH_PASSWORD], // Information passed from the command line - peer_hit[INET6_ADDRSTRLEN], - peer_ipv6_addr[INET6_ADDRSTRLEN], - nickname[LENGTH_NICKNAME]; - struct extras options; // Holds integers which indicate various options - int msg_type, return_value; // Information returned from the handle_packet function - header_general *gen_hdr; - int connected = 0; - - memset(&global_send, 0, sizeof(global_send)); - - - // Parse command line options - if (!parse_options(argc, argv, password, peer_hit, peer_ipv6_addr, nickname, &options)) - return 1; - // Open a socket based on the HIT and IPv6 address given - dest_addr = open_socket_send(peer_hit, peer_ipv6_addr); - if (dest_addr == NULL) - return 1; - - // Connect to relay - gen_hdr = create_pwd_struct(password); - CHECK_FOR_NULL_HDR(gen_hdr); - send_and_recv_msg(dest_addr, gen_hdr, peer_hit, &msg_type, &return_value); - if (msg_type == MSG_ACK_1) - connected = 1; - else - { - USER_MSG("Could not connect to relay. Exiting program."); - return 1; - } - - // Do what the user wants - switch(options.request_type) - { - case REQUEST_CONNECT: - break; // There's not really anything to do since we've already connected above - case REQUEST_GET_PWD: - { - // Send pwd_request struct - gen_hdr = create_pwd_request_struct(); - CHECK_FOR_NULL_HDR(gen_hdr); - send_and_recv_msg(dest_addr, gen_hdr, peer_hit, &msg_type, &return_value); - break; - } - default: - USER_MSG("Unknown request type. Program will be terminated."); - return 1; - } - - // Close the socket - close(global_send.socket_desc); - - // Clean up - free(dest_addr); - free(gen_hdr); - - return 0; -} - - /** Sends the message given in gen_hdr and waits for a reply (quits after receiving * MAX_RECV_TRIES bad packets). Calls handle_packet() upon receipt of a valid packet. * @@ -397,3 +331,69 @@ return dest_addr; } + + +/** Main program that will be run on the user's computer. + */ +int main(int argc, char *argv[]) +{ + struct sockaddr_in6 *dest_addr; // Our peer + char password[LENGTH_PASSWORD], // Information passed from the command line + peer_hit[INET6_ADDRSTRLEN], + peer_ipv6_addr[INET6_ADDRSTRLEN], + nickname[LENGTH_NICKNAME]; + struct extras options; // Holds integers which indicate various options + int msg_type, return_value; // Information returned from the handle_packet function + header_general *gen_hdr; + int connected = 0; + + memset(&global_send, 0, sizeof(global_send)); + + + // Parse command line options + if (!parse_options(argc, argv, password, peer_hit, peer_ipv6_addr, nickname, &options)) + return 1; + // Open a socket based on the HIT and IPv6 address given + dest_addr = open_socket_send(peer_hit, peer_ipv6_addr); + if (dest_addr == NULL) + return 1; + + // Connect to relay + gen_hdr = create_pwd_struct(password); + CHECK_FOR_NULL_HDR(gen_hdr); + send_and_recv_msg(dest_addr, gen_hdr, peer_hit, &msg_type, &return_value); + if (msg_type == MSG_ACK_1) + connected = 1; + else + { + USER_MSG("Could not connect to relay. Exiting program."); + return 1; + } + + // Do what the user wants + switch(options.request_type) + { + case REQUEST_CONNECT: + break; // There's not really anything to do since we've already connected above + case REQUEST_GET_PWD: + { + // Send pwd_request struct + gen_hdr = create_pwd_request_struct(); + CHECK_FOR_NULL_HDR(gen_hdr); + send_and_recv_msg(dest_addr, gen_hdr, peer_hit, &msg_type, &return_value); + break; + } + default: + USER_MSG("Unknown request type. Program will be terminated."); + return 1; + } + + // Close the socket + close(global_send.socket_desc); + + // Clean up + free(dest_addr); + free(gen_hdr); + + return 0; +} Modified: trunk/pairing/send.h ============================================================================== --- trunk/pairing/send.h Thu Nov 26 13:51:01 2009 (r1790) +++ trunk/pairing/send.h Thu Nov 26 13:57:43 2009 (r1791) @@ -48,10 +48,4 @@ // Global variables extern struct global_send_variables global_send; - -// Function prototypes -int send_and_recv_msg(struct sockaddr_in6 *dest_addr, header_general *gen_hdr, char *peer_hit, int *msg_type, int *return_value); -int parse_options(int argc, char *argv[], char *password, char *hit, char *ipv6_addr, char *nickname, struct extras* options); -struct sockaddr_in6 *open_socket_send(char *hit, char *ipv6_addr); - #endif /* PISA_SEND_H */