#include #include #include #include #include #include #include #include #include #include struct client_args { int fd; struct in_addr ip; char ip_string[16]; }; #define WORDCOUNT 104334 char words[WORDCOUNT][32]; // Not very efficient; some words are shorter than this void *client_function(void* arg){ struct client_args *ca = arg; printf("Connection made from address %s\n", ca->ip_string); // Write a lurk type 11 instead uint8_t type = 11; write(ca->fd, &type, 1); uint16_t next_number; // Initial Points next_number = random() % 1000; write(ca->fd, &next_number, 2); // Stat limit next_number = random() % 1000; write(ca->fd, &next_number, 2); char description[1024]; sprintf(description, "%s %s %s %s %s", words[random() % WORDCOUNT], words[random() % WORDCOUNT], words[random() % WORDCOUNT], words[random() % WORDCOUNT], words[random() % WORDCOUNT]); next_number = strlen(description); write(ca->fd, &next_number, 2); write(ca->fd, description, next_number); sleep(10); close(ca->fd); free(ca); } int main(int argc, char ** argv){ struct sockaddr_in sad; if(argc > 1) sad.sin_port = htons(atoi(argv[1])); else sad.sin_port = htons(5143); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; srandom(time(0)); FILE *wordfile = fopen("/usr/share/dict/american-english", "r"); char *current_word = malloc(32); for(int i = 0; i < WORDCOUNT; i++){ size_t wordlen = 32; getline(¤t_word, &wordlen, wordfile); strcpy(words[i], current_word); } free(current_word); fclose(wordfile); int skt = socket(AF_INET, SOCK_STREAM, 0); // Step 1 if(skt == -1){ perror("socket"); return 1; } if( bind(skt, (struct sockaddr*)(&sad), sizeof(struct sockaddr_in)) ){ // step 2 perror("bind"); return 1; } if( listen(skt, 5) ){ // step 3 perror("listen"); return 1; } while(1){ // Main thread int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); pthread_t thread_reference; struct client_args *ca = malloc(sizeof(struct client_args)); ca->fd = client_fd; ca->ip = client_address.sin_addr; strcpy(ca->ip_string, inet_ntoa(ca->ip)); pthread_create(&thread_reference, 0, client_function, ca); } return 0; }