aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-21 08:03:14 +0000
committersotech117 <michael_foiani@brown.edu>2023-09-21 08:03:14 +0000
commitebac9a74847dfbffa85e0fb2c184abfeb4aea06f (patch)
tree0dd0b2537a3606c5e369d612275f60ace278624e
parent9ba49f755e152d0996a43e8da7d146b8d2069051 (diff)
death with handshake first
-rw-r--r--client.c2
-rw-r--r--server.c83
-rwxr-xr-xsnowcast_controlbin22384 -> 22384 bytes
-rw-r--r--snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_controlbin15157 -> 15199 bytes
-rw-r--r--snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listenerbin11651 -> 11649 bytes
-rwxr-xr-xsnowcast_serverbin44360 -> 44576 bytes
-rw-r--r--snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_serverbin28332 -> 28763 bytes
7 files changed, 67 insertions, 18 deletions
diff --git a/client.c b/client.c
index 760b494..74a3f5d 100644
--- a/client.c
+++ b/client.c
@@ -93,6 +93,8 @@ int main(int argc, char *argv[])
usleep(1000);
+ // sleep(1);
+
struct Hello hello;
hello.commandType = 0;
// convert updPort to an int
diff --git a/server.c b/server.c
index c2d8208..ed92486 100644
--- a/server.c
+++ b/server.c
@@ -546,10 +546,36 @@ void *select_thread(void *arg) {
if (newfd == -1) {
perror("accept");
} else {
- FD_SET(newfd, &master); // add to master set
- if (newfd > fdmax) { // keep track of the max
- fdmax = newfd;
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = TIMEOUT;
+ if (setsockopt(newfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
+ perror("setsockopt");
}
+
+ uint8_t command_type = -1;
+ if ((nbytes = recv(newfd, &command_type, 1, 0)) <= 0)
+ {
+ // got error or connection closed by client
+ if (nbytes == 0) {
+ // connection closed
+ // printf("selectserver: socket %d hung up\n", i);
+ } else {
+ perror("recv");
+ }
+ // remove user from data structures
+ close(newfd);
+ // destroy_user(i);
+ continue;
+ }
+
+ if (command_type != 0) {
+ char * message = "must send a Hello message first";
+ send_invalid_command_reply(newfd, strlen(message), message);
+ close(newfd);
+ continue;
+ }
+
// printf("selectserver: new connection from %s on "
// "socket %d\n.",
// inet_ntop(remoteaddr.ss_family,
@@ -557,8 +583,43 @@ void *select_thread(void *arg) {
// remoteIP, INET6_ADDRSTRLEN),
// newfd);
// init user with this newfd
+
+ // get the udp port
+ uint16_t udp_port = -1;
+ int bytes_to_read = sizeof(uint16_t);
+ if (recv_all(newfd, &udp_port, &bytes_to_read) == -1) {
+ perror("recv_all");
+ close(newfd);
+ // destroy_user(i);
+ continue;
+ }
+ udp_port = ntohs(udp_port);
+
+ // printf("udpPort (from Hello) for new connection is %d.\n", udp_port);
+ // update udp port of user
+
+ // add a timeout to the socket
+ // struct timeval tv;
+ // tv.tv_sec = 0;
+ // tv.tv_usec = 0;
+ // if (setsockopt(newfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
+ // perror("setsockopt");
+ // }
+ FD_SET(newfd, &master); // add to master set
+ if (newfd > fdmax) { // keep track of the max
+ fdmax = newfd;
+ }
init_user(newfd);
- }
+ update_user_udpPort(newfd, udp_port);
+
+ // send the welcome message to client
+ struct Welcome welcome;
+ welcome.replyType = 2;
+ welcome.numStations = htons(num_stations);
+ int bytes_to_send = sizeof(struct Welcome);
+ if (send_all(newfd, &welcome, &bytes_to_send) == -1)
+ perror("send_all");
+ }
} else {
// handle data from a client
uint8_t command_type = -1;
@@ -591,9 +652,7 @@ void *select_thread(void *arg) {
destroy_user(i);
continue;
}
- udp_port = ntohs(udp_port);
- // check if user has a udpPort, if so, close connection
if (user_data[sockfd_to_user[i]].udpPort != -1) {
// send back in invalid command
char * message = "must not sent more than one Hello message";
@@ -604,18 +663,6 @@ void *select_thread(void *arg) {
destroy_user(i);
continue;
}
-
- // printf("udpPort (from Hello) for new connection is %d.\n", udp_port);
- // update udp port of user
- update_user_udpPort(i, udp_port);
-
- // send the welcome message to client
- struct Welcome welcome;
- welcome.replyType = 2;
- welcome.numStations = htons(num_stations);
- int bytes_to_send = sizeof(struct Welcome);
- if (send_all(i, &welcome, &bytes_to_send) == -1)
- perror("send_all");
}
else if (command_type == 1) { // we got a SetStation command
// get the station number
diff --git a/snowcast_control b/snowcast_control
index 2b99cdb..e8e8ad0 100755
--- a/snowcast_control
+++ b/snowcast_control
Binary files differ
diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
index 3a596f0..43f119b 100644
--- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
+++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
Binary files differ
diff --git a/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener b/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener
index b6206fa..f811857 100644
--- a/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener
+++ b/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener
Binary files differ
diff --git a/snowcast_server b/snowcast_server
index f829e64..2c41418 100755
--- a/snowcast_server
+++ b/snowcast_server
Binary files differ
diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
index 2428c04..df5780d 100644
--- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
+++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
Binary files differ