diff --git a/common.c b/common.c index 5e3fb4a..43814b8 100644 --- a/common.c +++ b/common.c @@ -17,15 +17,13 @@ char * strndup (const char *s, size_t n) { } #endif -void buildCommands() { - //char** commands = NULL; -} - -//void addCommand(const char *command_string, void(*function)()) { -void addCommand(const char *command_string, void(*function)) { - int hash = generateHash(command_string, COMMAND_HASH_SIZE); - printf("command '%s' added with hash %d\n", command_string, hash); - commands_table[hash] = function; +int generateHash(const char* string, int table_size) { + int i, sum; + size_t string_length = strlen(string); + for (sum=0, i=0; i < string_length; i++) { + sum += string[i]; + } + return sum % table_size; } char **getCommand(const char* string) { @@ -63,26 +61,19 @@ char **getCommand(const char* string) { command_array[0] = (void*)(intptr_t)(j-1); return command_array; } - +void addCommand(const char *command_string, void(*function)) { + int hash = generateHash(command_string, COMMAND_HASH_SIZE); + printf("command '%s' added with hash %d\n", command_string, hash); + commands_table[hash] = function; +} void freeCommand(char **command_array) { - //printf("res[0] = %d\n", command_array[0]); int j; for (j = 1; j < (intptr_t)command_array[0]+1; j++) { - //printf("res[%d] = %s\n", j, command_array[j]); free(command_array[j]); } free(command_array); } -int generateHash(const char* string, int table_size) { - int i, sum; - size_t string_length = strlen(string); - for (sum=0, i=0; i < string_length; i++) { - sum += string[i]; - } - return sum % table_size; -} - void setConfig(const char* variable, const char* value) { if (variable && value) { int hash = generateHash(variable, CONFIG_HASH_SIZE); @@ -99,7 +90,6 @@ void setConfig(const char* variable, const char* value) { printf("usage: %s", SET_SYNTAX); } } - void freeConfig() { int i = 0; for (i = 0;i < CONFIG_HASH_SIZE;i++) { @@ -115,7 +105,6 @@ void addHelp(const char* variable, const char* value) { help[hash] = realloc(help[hash], strlen(value)); strcpy(help[hash], value); } - void freeHelp() { int i; for (i = 0;i < COMMAND_HASH_SIZE;i++) { @@ -130,7 +119,6 @@ void addSyntax(const char* variable, const char* value) { syntax[hash] = realloc(syntax[hash], strlen(value)); strcpy(syntax[hash], value); } - void freeSyntax() { int i; for (i = 0;i < COMMAND_HASH_SIZE;i++) { diff --git a/common.h b/common.h index ed98cef..a88f58d 100644 --- a/common.h +++ b/common.h @@ -18,48 +18,30 @@ Common #if __APPLE__ -char * strndup (const char *s, size_t n); + char * strndup (const char *s, size_t n); #endif -/* etc */ - -char *start_message[3]; - -char* command_array; - -char** getCommand(const char* string); -void freeCommand(char **command_array); int generateHash(const char* string, int table_size); -//void addCommand(const char *command_string, void(*function)(const void*, const void*, const void*)); -//void addCommand(const char *command_string, void(*function)()); -void addCommand(const char *command_string, void(*function)); -//void (*commands_table[64]) (const void*, const void*); -void (*commands_table[COMMAND_HASH_SIZE]) (); -//void(*function)(const void*, const void*, const void*) **commands[64]; // array of command strings, e.g., "quit", "save", etc. -//int *commands_table; // array of command hashes (integers). -int commands_length; // total of commands available int hash_size; // err, change this. +void (*commands_table[COMMAND_HASH_SIZE]) (); // pointer to array of command strings, e.g., "quit", "save", etc. +void addCommand(const char *command_string, void(*function)); +void freeCommand(char **command_array); +char** getCommand(const char* string); + char *help[COMMAND_HASH_SIZE]; +void addHelp(const char* variable, const char* value); +void freeHelp(); + char *syntax[COMMAND_HASH_SIZE]; +void addSyntax(const char* variable, const char* value); +void freeSyntax(); char *config_name[CONFIG_HASH_SIZE]; char (*config[CONFIG_HASH_SIZE]); void setConfig(const char* variable, const char* value); +void freeConfig(); int is_running; void quitProgram(); - - -void addHelp(const char* variable, const char* value); -void addSyntax(const char* variable, const char* value); -void freeHelp(); -void freeSyntax(); -void freeConfig(); - -typedef struct { - int length; - char *arg; -} t_command; - #endif diff --git a/main.c b/main.c index c191572..07206d5 100644 --- a/main.c +++ b/main.c @@ -59,7 +59,6 @@ int main() { addCommand(START_COMMAND, startSockets); addCommand(STOP_COMMAND, stopSockets); - addCommand("open", sendOpen); addHelp(SET_COMMAND, SET_HELP); @@ -82,7 +81,6 @@ int main() { } while(is_running) { - //for(;;) { read_fds = master_fds; if (select(max_fd+1, &read_fds, NULL, NULL, NULL) == -1) { perror("select"); @@ -103,7 +101,6 @@ int main() { } } closeInterface(); - //freeCommands(); stopSockets(NULL); freeInterfaces(); freeHelp(); diff --git a/net.c b/net.c index 64c2779..ffc798e 100644 --- a/net.c +++ b/net.c @@ -1,6 +1,7 @@ #include "net.h" int initNetwork() { + // Load in Windows' socket networking support #if defined(WIN32) WORD wVersionRequested; WSADATA wsaData; @@ -15,14 +16,13 @@ int initNetwork() { } #endif + // @@TODO: move port initialization elswhere setConfig("port", "7332"); - char *hostname[15]; - //char hostname; - gethostname(&hostname, 16); - + // Get the env USER and hostname then build user@hostname nickname char *username = getenv("USER"); - + char hostname[15]; // cap hostname to 16 chars, I guess + gethostname(hostname, 16); size_t nick_length = strlen(hostname)+strlen(username)+1; char nick[nick_length]; int i = 0; @@ -36,23 +36,23 @@ int initNetwork() { nick[i++] = hostname[j++]; } nick[i] = '\0'; - setConfig("nick", nick); return 0; } int openListenSocket() { + int ip_hash = generateHash("ip", CONFIG_HASH_SIZE); + int port_hash = generateHash("port", CONFIG_HASH_SIZE); setAttribute(MAGENTA); setAttribute(UNDERSCORE); printf("--openListenSocket--\n"); clearAttributes(); - int ip_hash = generateHash("ip", CONFIG_HASH_SIZE); - int port_hash = generateHash("port", CONFIG_HASH_SIZE); setAttribute(GREEN); printf("IP: %s\n", config[ip_hash]); printf("port: %s\n", config[port_hash]); clearAttributes(); printf("open: "); + // Create our IPv4 datagram socket listen_socket = socket(AF_INET, SOCK_DGRAM, 0); if (listen_socket < 0) { perror("error"); @@ -60,14 +60,7 @@ int openListenSocket() { } else { printf("success\n"); } - -/* printf("non-blocking set: "); - if (fcntl(listen_socket, F_SETFL, O_NONBLOCK)) { - printf("success\n"); - } else { - perror("fcntl() error"); - }*/ - + // Allow the socket to be reusable int reuse = 1; printf("SO_REUSEADDR set: "); if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)) < 0) { @@ -76,7 +69,7 @@ int openListenSocket() { } else { printf("success\n"); } - + // Join the broadcast group for 226.1.1.1 if (!is_group_socket_set) { memset((char *) &group_socket, 0, sizeof(group_socket)); group_socket.sin_family = AF_INET; @@ -85,7 +78,7 @@ int openListenSocket() { group_socket.sin_addr.s_addr = inet_addr("226.1.1.1"); is_group_socket_set = 1; } - + // Bind to the socket w/ our group information printf("bind: "); if (bind(listen_socket, (struct sockaddr*)&group_socket, sizeof(group_socket))) { perror("error"); @@ -94,10 +87,9 @@ int openListenSocket() { printf("success\n"); } + // join our group to user's selected IP group.imr_multiaddr.s_addr = inet_addr("226.1.1.1"); group.imr_interface.s_addr = inet_addr(config[ip_hash]); - //group.imr_interface.s_addr = inet_addr("192.168.182.51"); - printf("IP_ADD_MEMBERSHIP: "); if (setsockopt(listen_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) { perror("error"); @@ -126,12 +118,12 @@ int closeListenSocket() { } int openSendSocket() { + int ip_hash = generateHash("ip", CONFIG_HASH_SIZE); + int port_hash = generateHash("port", CONFIG_HASH_SIZE); setAttribute(MAGENTA); setAttribute(UNDERSCORE); printf("--openSendSocket--\n"); clearAttributes(); - int ip_hash = generateHash("ip", CONFIG_HASH_SIZE); - int port_hash = generateHash("port", CONFIG_HASH_SIZE); setAttribute(GREEN); printf("IP: %s\n", config[ip_hash]); printf("port: %s\n", config[port_hash]); @@ -186,14 +178,6 @@ int closeSendSocket() { return 0; } -int setIp(const char string[]) { - int ip_hash = generateHash("ip", CONFIG_HASH_SIZE); -// strcpy(&config[ip_hash], string); - setString(config[ip_hash], string); - printf("ip: %s", config[ip_hash]); - return 0; -} - int setString(char *destination, const char source[]) { size_t len = strlen(source); size_t i = 0; @@ -334,10 +318,9 @@ int handleOpenMessage(const char packet[]) { void sendOpen(char buffer[]) { int packet_size = 0; - + // Build our packet's length in bytes first uint8_t message_type = MESSAGE_OPEN; packet_size++; // for message_type - int nick_hash = generateHash("nick", CONFIG_HASH_SIZE); uint8_t nick_size = strlen(config[nick_hash])+1; // +1 for \0 packet_size++; // for nick_length; @@ -347,14 +330,12 @@ void sendOpen(char buffer[]) { uint8_t payload_type = 1; // chat type packet_size++; // for payload type -// char str[] = "testing 1 2 3"; uint8_t buffer_size = strlen(buffer)+1; -// uint8_t string_length = strlen(str)+1; // +1 for \0, since strlen sucks packet_size++; // for string_length char packet_size += buffer_size; // for payload itself + //printf("packet size will be %d\n", packet_size); -// printf("packet size will be %d\n", packet_size); - + // Populate our packet int i = 0; int packet_offset = 0; char packet[packet_size]; @@ -398,25 +379,20 @@ void getInterfaces() { return; } ifconf.ifc_buf = (char *) ifreq; - // Set ifconf's ifc_len to the length of our array of interface ifreqs. ifconf.ifc_len = sizeof ifreq; - // Populate ifconf.ifc_buf (ifreq) with a list of interface names and addresses. if (ioctl(test_socket, SIOCGIFCONF, &ifconf) == -1) return; // Divide the length of the interface list by the size of each entry. // This gives us the number of interfaces on the system. interfaces = ifconf.ifc_len / sizeof(ifreq[0]); - for (i = 0; i < interfaces; i++) { char ip[INET_ADDRSTRLEN]; struct sockaddr_in *address = (struct sockaddr_in *) &ifreq[i].ifr_addr; - // Convert the binary IP address into a readable string. if (!inet_ntop(AF_INET, &address->sin_addr, ip, sizeof(ip))) - return; - + return; ip_list[i] = malloc(strlen(ip)); setString(ip_list[i], ip); printf("%s - %s\n", ifreq[i].ifr_name, ip); diff --git a/net.h b/net.h index e4b5989..735b0c6 100644 --- a/net.h +++ b/net.h @@ -72,8 +72,6 @@ int data_buffer_length; char ip_address[15]; char port[15]; -int setIp(const char string[]); - int initNetwork(); void getInterfaces(); // same v