From 313a10a1251cfd588cebf6389395617bc2672b0c Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sat, 9 May 2015 11:50:36 -0400 Subject: long-overdue code cleanup --- chatbot.c | 127 ++++++++--------- gtnw.c | 466 +++++++++++++++++++++++++++++++------------------------------- joshua.c | 163 +++++++++++++--------- server.c | 398 ++++++++++++++++++++++++++++++----------------------- util.h | 10 +- 5 files changed, 625 insertions(+), 539 deletions(-) diff --git a/chatbot.c b/chatbot.c index d48c432..b8ce71b 100644 --- a/chatbot.c +++ b/chatbot.c @@ -27,101 +27,104 @@ #include void do_chatbot(void) { - int stage=0; /* stage 0: i'm fine how are you... -> - stage 1: people sometimes make mistakes -> - stage 2: love to. how about global thermonuclear war? -> - stage 3: no lets play global thermonuclear war -> - stage 4: GLOBAL THERMONUCLEAR WAR!!! */ - while(1) + int stage = 0; /* stage 0: i'm fine how are you... -> + stage 1: people sometimes make mistakes -> + stage 2: love to. how about global thermonuclear war? -> + stage 3: no lets play global thermonuclear war -> + stage 4: GLOBAL THERMONUCLEAR WAR!!! */ + while(1) { - char buf[513]; - int ret=getnstr(buf, 512); - usleep(SLEEP_TIME*100); - if(ret==ERR) + char buf[128]; + int ret = getnstr(buf, sizeof(buf)); + usleep(SLEEP_TIME * 100); + + if(ret == ERR) { - print_string("\n\n"); - print_string("SORRY?"); - print_string("\n\n"); + print_string("\n\n"); + print_string("SORRY?"); + print_string("\n\n"); } - else + else { - allLower(buf); - remove_punct(buf); - bool valid=false; - switch(stage) + allLower(buf); + remove_punct(buf); + bool valid = false; + switch(stage) { case 0: - for(int i=0;ib?a:b; + return a>b?a:b; } /* simulate a missile launch */ static void fire_missile(struct location_t* city) { - int random=rand()%100; /* leave this at 100 for future adjustments */ - int x=city->x, y=city->y; - if(random>=90) /* crit */ + int random=rand()%100; /* leave this at 100 for future adjustments */ + int x=city->x, y=city->y; + if(random>=90) /* crit */ { - map[y][x]='!'; - city->population=0; + map[y][x]='!'; + city->population=0; } - else if(random>=60) /* major */ + else if(random>=60) /* major */ { - map[y][x]='X'; - city->population=max((double)city->population*(double).4-5000, 0); + map[y][x]='X'; + city->population=max((double)city->population*(double).4-5000, 0); } - else if(random>=30) /* minor */ + else if(random>=30) /* minor */ { - map[y][x]='*'; - city->population=max((double)city->population*(double).6-2500, 0); + map[y][x]='*'; + city->population=max((double)city->population*(double).6-2500, 0); } - else if(random>=10) /* marginal */ + else if(random>=10) /* marginal */ { - map[y][x]='x'; - city->population=max((double)city->population*(double).8-1000, 0); + map[y][x]='x'; + city->population=max((double)city->population*(double).8-1000, 0); } - else /* miss */ + else /* miss */ { - map[y][x]='O'; + map[y][x]='O'; } } /* calculate populations of US+USSR by totaling the populations of each of their cities */ static void calc_pops(long long* us_pop, long long* ussr_pop) { - *us_pop=0; - *ussr_pop=0; - /* calculate populations */ - for(int i=0;iussr_back) + else if(us_back>ussr_back) { - while(us_back!=ussr_back) + while(us_back!=ussr_back) { - ussr_cities[ussr_back].print=false; - ++ussr_back; + ussr_cities[ussr_back].print=false; + ++ussr_back; } } - us_cities[us_back].print=true; - us_cities[us_back].print_name="Total"; - us_cities[us_back].population=us_pop; - ussr_cities[ussr_back].print=true; - ussr_cities[ussr_back].print_name="Total"; - ussr_cities[ussr_back].population=ussr_pop; - ++us_back; - ++ussr_back; - print_string("\n\n"); - char buf[512]; - for(int i=0;i0 && move<5) - good=true; + print_string("PLEASE CHOOSE ONE: "); + char buf[32]; + getnstr(buf, 32); + sscanf(buf, "%u", &move); + if(move>0 && move<5) + good=true; } - switch(move) + switch(move) { case 1: - do_missile_launch(side); - break; + do_missile_launch(side); + break; case 2: - do_peace_talks(side); - break; + do_peace_talks(side); + break; case 3: - surrender=true; - winner=side==1?2:1; - break; + surrender=true; + winner=side==1?2:1; + break; case 4: - break; + break; } } /* play a game of Global Thermonuclear War! */ void global_thermonuclear_war(void) { - srand(time(0)); // might want to move to main()... - surrender=false; - clear(); - for(int i=0;i #include #include @@ -34,273 +35,322 @@ #include #include #include + #define DEFAULT_PORT 23 #define LOG_LOCATION "/var/log/wopr" + int server_socket; uint16_t port; + int pipes[FD_SETSIZE][2]; + struct connection_data_t connection_data[FD_SETSIZE]; + int debugf(const char* fmt, ...) { - va_list l; - va_start(l, fmt); - int ret=vprintf(fmt, l); - va_end(l); - return ret; + va_list l; + va_start(l, fmt); + int ret = vprintf(fmt, l); + va_end(l); + return ret; } + int make_server_socket(uint16_t port) { - int sock; - struct sockaddr_in name; - sock=socket(AF_INET, SOCK_STREAM, 0); - if(sock<0) + int sock; + struct sockaddr_in name; + sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock < 0) { - debugf("FATAL: Error opening socket.\n"); - return -1; + debugf("FATAL: Error opening socket.\n"); + return -1; } - name.sin_family=AF_INET; - name.sin_port=htons(port); - name.sin_addr.s_addr=htonl(INADDR_ANY); - int ret=bind(sock, (struct sockaddr*) &name, sizeof(name)); - if(ret<0) + + const int opt_val = 1; + + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val)); + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &opt_val, sizeof(opt_val)); + + name.sin_family = AF_INET; + name.sin_port = htons(port); + name.sin_addr.s_addr = htonl(INADDR_ANY); + int ret = bind(sock, (struct sockaddr*) &name, sizeof(name)); + if(ret < 0) { - debugf("FATAL: Error binding to port %d\n", port); - return -1; + debugf("FATAL: Error binding to port %d\n", port); + return -1; } - return sock; + + return sock; } + char pending_buffer[1024]; + void handle_command(unsigned char* buf, int buflen, int connection) { - unsigned char cmd, opt; - if(buflen<2) + unsigned char cmd, opt; + if(buflen < 2) { - debugf("Invalid command.\n"); - return; + debugf("Invalid command.\n"); + return; } - cmd=buf[1]; - /* handle two-byte commands */ - switch(cmd) + cmd = buf[1]; + + /* handle two-byte commands */ + switch(cmd) { case AYT: - { - unsigned char iac_nop[]={IAC, NOP}; + { + unsigned char iac_nop[] = {IAC, NOP}; write(connection, iac_nop, sizeof(iac_nop)); return; - } } - if(buflen<3) + } + + if(buflen < 3) { - debugf("Invalid command.\n"); - return; + debugf("Invalid command.\n"); + return; } - opt=buf[2]; - switch(cmd) + + opt = buf[2]; + switch(cmd) { case SB: - { + { switch(opt) - { - case NAWS: + { + case NAWS: + { + printf("NAWS command recieved.\n"); + /* format of NAWS data: IAC SB NAWS W W H H IAC SE */ + uint16_t height, width; + uint8_t height_hi, height_lo, width_hi, width_lo; + + if(buflen < 9) { - printf("NAWS command recieved.\n"); - /* format of NAWS data: IAC SB NAWS W W H H IAC SE */ - uint16_t height, width; - uint8_t height_hi, height_lo, width_hi, width_lo; - if(buflen<9) - { - debugf("IAC SB NAWS command too short!\n"); - return; - } - width_hi=buf[3]; - width_lo=buf[4]; - height_hi=buf[5]; - height_lo=buf[6]; - height=(height_hi<<8)|height_lo; - width=(width_hi<<8)|width_lo; - connection_data[connection].know_termsize=(height==0 || width==0)?0:1; - connection_data[connection].term_height=height; - connection_data[connection].term_width=width; - return; + debugf("IAC SB NAWS command too short!\n"); + return; } - } + + width_hi = buf[3]; + width_lo = buf[4]; + height_hi = buf[5]; + height_lo = buf[6]; + height = (height_hi << 8) | height_lo; + width = (width_hi << 8) | width_lo; + connection_data[connection].know_termsize = (height == 0 || width == 0) ? 0 : 1; + connection_data[connection].term_height = height; + connection_data[connection].term_width = width; + return; + } + } break; - } } - /* unimplemented command, just deny it */ - unsigned char deny_cmd[3]={IAC, WONT, opt}; - if(opt==LINEMODE) + } + + if((opt == LINEMODE && cmd == WILL) || (opt == NAWS)) { - deny_cmd[1]=WILL; + /* avoid an endless negiociation loop */ + return; } - /* - write(connection, deny_cmd, sizeof(deny_cmd)); - fsync(connection); - */ - return; + + /* unimplemented command, just deny it */ + unsigned char deny_cmd[3] = {IAC, WONT, opt}; + + write(connection, deny_cmd, sizeof(deny_cmd)); + fsync(connection); + return; } + int process_data(int fd) { - unsigned char buf[1024]; - memset(buf, 0, sizeof(buf)); - int ret=read(fd, buf, sizeof(buf)); - debugf("Client %d sends: %s\n", fd, buf); - debugf("Byte dump of data: "); - for(int i=0;buf[i];++i) + unsigned char buf[1024]; + memset(buf, 0, sizeof(buf)); + int ret = read(fd, buf, sizeof(buf)); + + debugf("Client %d sends: %s\n", fd, buf); + debugf("Byte dump of data: "); + + for(int i = 0; buf[i]; ++i) { - debugf("%d ", buf[i]); + debugf("%02x ", buf[i]); } - debugf("\n"); - char ctrl_c[]={0xff, 0xf4, 0xff, 0xfd, 0x06, 0x00}; - if(strcmp(ctrl_c, buf)==0) + debugf("\n"); + + char ctrl_c[] = {0xff, 0xf4, 0xff, 0xfd, 0x06, 0x00}; + if(strcmp(ctrl_c, buf) == 0) { - debugf("Got CTRL-C from client %d.\n", fd); - return -1; + debugf("Got CTRL-C from client %d.\n", fd); + return -1; } - if(ret<0) /* error */ + + if(ret < 0) /* error */ { - debugf("Error in read()\n"); - return -1; + debugf("Error in read()\n"); + return -1; } - if(ret==0) + + if(ret == 0) { - debugf("EOF from client %d.\n", fd); - return -1; + debugf("EOF from client %d.\n", fd); + return -1; } - else + else { - int buflen=strlen(buf); - if(buflen>0) /* no need to write nothing to the input stream :D */ + int buflen = strlen(buf); + if(buflen > 0) { - if(buf[0]==0xff) + if(buf[0] == 0xff) { - handle_command(buf, buflen, fd); + handle_command(buf, buflen, fd); } - else if(strlen(buf)>0) + else if(strlen(buf) > 0) { - write(pipes[fd][1],buf,strlen(buf)); + write(pipes[fd][1], buf, strlen(buf)); } - } - return 0; + } } - return 0; + + return 0; } + void serv_cleanup() { - debugf("\nPreparing to exit...\n"); - fflush(stdout); - shutdown(server_socket, SHUT_RDWR); + debugf("\nPreparing to exit...\n"); + fflush(stdout); + shutdown(server_socket, SHUT_RDWR); } + void setup_new_connection(int fd) { - unsigned char will_naws[]={IAC, DO, NAWS}; - write(fd, will_naws, sizeof(will_naws)); - will_naws[1]=WILL; - write(fd, will_naws, sizeof(will_naws)); + unsigned char will_naws[] = {IAC, DO, NAWS}; + write(fd, will_naws, sizeof(will_naws)); + will_naws[1] = WILL; + write(fd, will_naws, sizeof(will_naws)); - unsigned char dont_echo[]={IAC, DONT, ECHO}; - write(fd, dont_echo, sizeof(dont_echo)); - dont_echo[1]=WONT; - write(fd, dont_echo, sizeof(dont_echo)); + unsigned char dont_echo[] = {IAC, DONT, ECHO}; + write(fd, dont_echo, sizeof(dont_echo)); + dont_echo[1] = WONT; + write(fd, dont_echo, sizeof(dont_echo)); - unsigned char dont_sga[]={IAC, WONT, SGA}; - write(fd, dont_sga, sizeof(dont_sga)); + unsigned char dont_sga[] = {IAC, WONT, SGA}; + write(fd, dont_sga, sizeof(dont_sga)); - unsigned char will_linemode[]={IAC, WILL, LINEMODE}; - write(fd, will_linemode, sizeof(will_linemode)); + unsigned char will_linemode[] = {IAC, WILL, LINEMODE}; + write(fd, will_linemode, sizeof(will_linemode)); - memset(&connection_data[fd], 0, sizeof(struct connection_data_t)); - debugf("New connection set up.\n"); + memset(&connection_data[fd], 0, sizeof(struct connection_data_t)); + debugf("New connection set up.\n"); } + int main(int argc, char* argv[]) { - if(argc!=2) + if(argc != 2) { - debugf("Listening on default port: %d\n", DEFAULT_PORT); - port=DEFAULT_PORT; + debugf("Listening on default port: %d\n", DEFAULT_PORT); + port = DEFAULT_PORT; } - else + else { - int port2=atoi(argv[1]); - if(port2<0 || port2>65535) + int port2 = atoi(argv[1]); + if(port2 < 0 || port2 > 65535) { - debugf("FATAL: Port out of range.\n"); - return 2; + debugf("FATAL: Port out of range.\n"); + return 2; } - port=atoi(argv[1]); + port = atoi(argv[1]); } - debugf("Initializing server on port %u...\n", port); - signal(SIGINT, &serv_cleanup); - int sock=make_server_socket(port); - server_socket=sock; - fd_set active_fd_set, read_fd_set; - struct sockaddr_in client; - if(listen(sock, 1)<0) + + debugf("Initializing server on port %u...\n", port); + signal(SIGINT, &serv_cleanup); + + int sock = make_server_socket(port); + server_socket = sock; + + fd_set active_fd_set, read_fd_set; + + struct sockaddr_in client; + + if(listen(sock, 1) < 0) { - debugf("FATAL: Error opening socket.\n"); - return 1; + debugf("FATAL: Error opening socket.\n"); + return 1; } - FD_ZERO(&active_fd_set); - FD_SET(sock, &active_fd_set); - debugf("Server ready, listening on port %d.\n", port); - debugf("Maximum clients: %d\n", FD_SETSIZE); - while(1) + + FD_ZERO(&active_fd_set); + FD_SET(sock, &active_fd_set); + + debugf("Server ready, listening on port %d.\n", port); + debugf("Maximum clients: %d\n", FD_SETSIZE); + + while(1) { - read_fd_set=active_fd_set; - int ret=select(FD_SETSIZE, &read_fd_set, 0,0,0); - if(ret<0) + read_fd_set = active_fd_set; + int ret = select(FD_SETSIZE, &read_fd_set, 0, 0, 0); + if(ret < 0) { - debugf("Error in select().\n"); - return 1; + debugf("Error in select().\n"); + return 1; } - for(int i=0;i #include + struct connection_data_t { - int naws_enable:1; - int know_termsize:1; + int naws_enable : 1; + int know_termsize : 1; uint16_t term_height; uint16_t term_width; }; + extern struct connection_data_t connection_data[FD_SETSIZE]; + void allLower(char*); void print_string(const char*); void remove_punct(char*); @@ -36,6 +40,8 @@ void clear(void); int getnstr(char*, int); void echo_off(void); void echo_on(void); + #define ERR 1 #define OK 0 + extern int out_fd; -- cgit v1.1