diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-20 01:36:52 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-20 01:36:52 -0400 |
commit | af44220248c7da4c2f66f5bd7ff018acfe623b03 (patch) | |
tree | 9c206a5cfebf35e3307813c1ee5ed49febb0e6af /snowcast_server_concurrent.c | |
parent | ea7aff51be44884e22c8bdabef917c77c291951e (diff) |
add code to send invalid messages
Diffstat (limited to 'snowcast_server_concurrent.c')
-rw-r--r-- | snowcast_server_concurrent.c | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 2c74cab..4fa6d40 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -67,7 +67,8 @@ void *update_user_station(int sockfd, int stationNum); void *print_user_data(int sockfd); void destroy_user(int sockfd); -void send_announce_message(int fd, int station_num); +void send_announce_reply(int fd, int station_num); +void send_invalid_command_reply(int fd, size_t message_size, char* message); // void *load_file(void* arg); @@ -164,7 +165,7 @@ int main(int argc, char *argv[]) pthread_create(&print_info_thread, NULL, print_info_routine, print_fd); // note - this file descriptor is closed in the thread } - else if (strncmp("u\n", input, LINE_MAX) == 0) + else if (!strcmp(command, "u")) { // print all user data for (int i = 0; i < max_active_users; i++) @@ -415,7 +416,7 @@ void *synchronization_thread(void *arg) { // update the station of each user if (user_data[i].stationNum == i) { - send_announce_message(user_data[i].sockfd, i); + send_announce_reply(user_data[i].sockfd, i); } } } @@ -552,7 +553,7 @@ void *select_thread(void *arg) { // we got some data from a client if (command.commandType == 0) { // hello message with udpPort - printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number)); + // printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number)); // update udp port of user update_user_udpPort(i, ntohs(command.number)); @@ -565,22 +566,37 @@ void *select_thread(void *arg) { perror("send"); } else if (command.commandType == 1) { + // check if user has a udpPort + if (user_data[sockfd_to_user[i]].udpPort == -1) { + // send back in invalid command + char * message = "Must send Hello message first"; + send_invalid_command_reply(i, strlen(message), message); + // drop connection upon invalid command + close(i); + FD_CLR(i, &master); + destroy_user(i); + } + int station_num = ntohs(command.number); - printf("setting station to %d\n", ntohs(command.number)); + if (station_num >= num_stations || station_num < 0) { + // send back in invalid command + char * message = "Station number out of range"; + send_invalid_command_reply(i, strlen(message), message); + // drop connection upon invalid command + close(i); + FD_CLR(i, &master); + destroy_user(i); + } + + // printf("setting station to %d\n", ntohs(command.number)); // update station of user update_user_station(i, ntohs(command.number)); - - send_announce_message(i, station_num); + send_announce_reply(i, station_num); } else { // send back in invalid command - struct InvalidCommand invalid; - invalid.replyType = 4; - invalid.replyStringSize = 21; - // make a string with the command.commmandType type in it - invalid.replyString = "Invalid command type"; - if ((send(i, &invalid, sizeof(struct InvalidCommand), 0)) == -1) - perror("send"); + char * message = "Invalid command"; + send_invalid_command_reply(i, strlen(message), message); // drop connection upon invalid command close(i); @@ -671,7 +687,7 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } -void send_announce_message(int fd, int station_num) { +void send_announce_reply(int fd, int station_num) { char* file_path = station_data[station_num].filePath; int len_file_path = strlen(file_path); @@ -692,6 +708,26 @@ void send_announce_message(int fd, int station_num) { free(send_buffer); } +void send_invalid_command_reply(int fd, size_t message_size, char* message) { + char *send_buffer = malloc(message_size+2); + + // type and payload size + send_buffer[0] = 4; + send_buffer[1] = message_size; + + memcpy(send_buffer + 2, message, message_size); + + // printf("buffer: %s\n", send_buffer); + + int bytessent; + if ((bytessent = send(fd, send_buffer, message_size + 2, 0)) == -1) + perror("send"); + // print the number of bytes sent + // printf("sent %d bytes\n", bytessent); + + free(send_buffer); +} + // Parses a buffer into tokens, from cs33 :) int parse(char buffer[LINE_MAX], char *tokens[LINE_MAX / 2]) { |