Author: biurrun Date: Thu Nov 26 16:44:37 2009 New Revision: 1824 Log: Merge contents of create_send_headers.c into send.c. The file only contains two functions that are used nowhere else. Deleted: trunk/pairing/create_send_headers.c trunk/pairing/create_send_headers.h Modified: trunk/Makefile.am trunk/pairing/send.c Modified: trunk/Makefile.am ============================================================================== --- trunk/Makefile.am Thu Nov 26 16:40:05 2009 (r1823) +++ trunk/Makefile.am Thu Nov 26 16:44:37 2009 (r1824) @@ -86,7 +86,6 @@ pairing/packet_handler_accept.c pairing_send_SOURCES = $(pairing_ACCEPT_SEND_SRCS) \ - pairing/create_send_headers.c \ pairing/packet_handler_send.c \ pairing/send.c Modified: trunk/pairing/send.c ============================================================================== --- trunk/pairing/send.c Thu Nov 26 16:40:05 2009 (r1823) +++ trunk/pairing/send.c Thu Nov 26 16:44:37 2009 (r1824) @@ -23,7 +23,6 @@ #include "common.h" #include "packet_handler.h" #include "libconfig_wrapper.h" -#include "create_send_headers.h" // When these are updated, the user message in parse_options() should also be updated #define REQUEST_CONNECT 0 @@ -361,6 +360,137 @@ } +/** Creates a header_general structure which holds a header_password structure and fills both. + * + * @param password The password that should be stored in the structure. + * + * @return A pointer to the header_general structure. Returns NULL on error. + */ +static header_general* create_pwd_struct(char *password) +{ + if (password == NULL) + { + DEBUG("No password given. Password structure was not created."); + return NULL; + } + + header_general *gen_hdr = malloc(sizeof(header_general)); + gen_hdr->msg_type = MSG_PASSWORD; + strncpy((char*)&((gen_hdr->msg.header_password).password), password, LENGTH_PASSWORD); + + DEBUG_HIGH(LINE_BREAK); + DEBUG_HIGH("General header created."); + DEBUG_HIGH("\tMessage type: %d", gen_hdr->msg_type); + DEBUG_HIGH("Password structure created:"); + DEBUG_HIGH("\tPassword: \'%s\'", (gen_hdr->msg.header_password).password); + DEBUG_HIGH(LINE_BREAK); + + return gen_hdr; +} + + +/** Creates a header_general structure which holds a header_pwd_request structure and fills both. + * + * @return A pointer to the header_general structure. Returns NULL on error. + */ +static header_general* create_pwd_request_struct(void) +{ + char nickname[LENGTH_NICKNAME]; + char expiration1[LENGTH_TIMEOUT]; + char expiration2[LENGTH_TIMEOUT]; + long int expr1; + long int expr2; + int valid = 1; + + nickname[0] = '\0'; + + header_general *gen_hdr = malloc(sizeof(header_general)); + gen_hdr->msg_type = MSG_PWD_REQUEST; + + if (!global_send.force_options) + { + // We will send the time difference to avoid conflicts on non-synchronized networks + int current_time = (int)time(NULL); + + // Get a nickname for this friend + USER_MSG("Who would you like to give access to? You may only use alphanumeric characters for the nickname." \ + " Leave blank to user server defaults."); + do + { + if (!valid) + { + USER_MSG("Invalid nickname. Nicknames may only contain letters and numbers."); + USER_MSG("Please enter a nickname (alphanumeric characters only)."); + } + if (!fgets(nickname, LENGTH_NICKNAME, stdin)) + return NULL; + nickname[strlen(nickname)-1] = 0; + } + while (!(valid = valid_nickname(nickname))); + + // Get expiration times + USER_MSG("When should the password expire? Date format: dd-mm-yyyy hh:mm:ss (24 hour clock)."); + if (!fgets(expiration1, LENGTH_TIMEOUT, stdin)) + return NULL; + expiration1[strlen(expiration1)-1] = 0; + + USER_MSG("When should their access terminate? Date format: dd-mm-yyyy hh:mm:ss (24 hour clock)."); + if (!fgets(expiration2, LENGTH_TIMEOUT, stdin)) + return NULL; + expiration2[strlen(expiration2)-1] = 0; + + // Convert the first expiration date + if (!(get_expiration_time(expiration1, &expr1))) // If date conversion fails... + return NULL; + expr1 -= current_time; + + // Convert the second expiration date + if (!(get_expiration_time(expiration2, &expr2))) // If date conversion fails... + return NULL; + expr2 -= current_time; + + if (expr1 > expr2) + { + USER_MSG("Invalid dates specified (cannot have password expire before access). Exiting program."); + return NULL; + } + + if ((expr1 <= 0) || (expr2 <= 0)) + { + USER_MSG("Invalid dates specified (cannot be in the past). Exiting program."); + return NULL; + } + } + else // Get defaults + { + if (!get_config_setting(FILE_USER_CONFIG, "pwd_request_settings.default_pwd_lifetime", CONFIG_TYPE_INT, &expr1, 0)) + return NULL; + expr1 *= 60; // Convert from minutes to seconds + + if (!get_config_setting(FILE_USER_CONFIG, "pwd_request_settings.default_access_lifetime", CONFIG_TYPE_INT, &expr2, 0)) + return NULL; + expr2 *= 60; // Convert from minutes to seconds + } + + // Copy the nickname + strncpy((char*)&((gen_hdr->msg.header_pwd_request).nickname), nickname, LENGTH_NICKNAME); + // Copy the expirations + (gen_hdr->msg.header_pwd_request).expiration1 = expr1; + (gen_hdr->msg.header_pwd_request).expiration2 = expr2; + + DEBUG_HIGH(LINE_BREAK); + DEBUG_HIGH("General header created."); + DEBUG_HIGH("\tMessage type: %d", gen_hdr->msg_type); + DEBUG_HIGH("pwd_request structure created:"); + DEBUG_HIGH("\tNickname: %s", (gen_hdr->msg.header_pwd_request).nickname); + DEBUG_HIGH("\tExpiration 1: %ld", (gen_hdr->msg.header_pwd_request).expiration1); + DEBUG_HIGH("\tExpiration 2: %ld", (gen_hdr->msg.header_pwd_request).expiration2); + DEBUG_HIGH(LINE_BREAK); + + return gen_hdr; +} + + /** Main program that will be run on the user's computer. */ int main(int argc, char *argv[])