aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'server.c')
-rw-r--r--server.c40
1 files changed, 26 insertions, 14 deletions
diff --git a/server.c b/server.c
index 59cd4e7..f7697a3 100644
--- a/server.c
+++ b/server.c
@@ -578,7 +578,7 @@ void *select_thread(void *arg) {
else
{
// we got some data from a client
- if (command_type == 0) { // we got a hello message
+ if (command_type == 0) { // we got a Hello commmand
// get the udp port
uint16_t udp_port = -1;
@@ -589,7 +589,19 @@ void *select_thread(void *arg) {
}
udp_port = ntohs(udp_port);
- printf("udpPort (from Hello) for new connection is %d.\n", 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";
+ send_invalid_command_reply(i, strlen(message), message);
+ // drop connection upon invalid command
+ close(i);
+ FD_CLR(i, &master);
+ 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);
@@ -601,8 +613,17 @@ void *select_thread(void *arg) {
if (send_all(i, &welcome, &bytes_to_send) == -1)
perror("send_all");
}
- else if (command_type == 1) { // we got a setStation command
- // check if user has a udpPort
+ else if (command_type == 1) { // we got a SetStation command
+ // get the station number
+ uint16_t station_number = -1;
+ int bytes_to_read = sizeof(uint16_t);
+ if (recv_all(i, &station_number, &bytes_to_read) == -1) {
+ perror("recv_all");
+ exit(1);
+ }
+ station_number = ntohs(station_number);
+
+ // check if user has a udpPort to stream to
if (user_data[sockfd_to_user[i]].udpPort == -1) {
// send back in invalid command
char * message = "must send Hello message first";
@@ -614,16 +635,7 @@ void *select_thread(void *arg) {
continue;
}
- // get the station number
- uint16_t station_number = -1;
- int bytes_to_read = sizeof(uint16_t);
- if (recv_all(i, &station_number, &bytes_to_read) == -1) {
- perror("recv_all");
- exit(1);
- }
- station_number = ntohs(station_number);
-
- // printf("station_num: %d\n", station_num);
+ // check if station num is in range
if (station_number >= num_stations || station_number < 0) {
// send back in invalid command
char * message = "station number out of range";