summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/net.c32
-rw-r--r--src/net.h5
2 files changed, 28 insertions, 9 deletions
diff --git a/src/net.c b/src/net.c
index 0eea8a9..1b3811d 100644
--- a/src/net.c
+++ b/src/net.c
@@ -84,7 +84,11 @@ int* server_get_players(int serversock, const uint8_t count)
assert(count < MAX_PLAYERS && count > 0);
clientsocks = malloc(count*sizeof(int));
- assert(clientsocks != NULL);
+ if(clientsocks == NULL)
+ {
+ printf("server_get_players: Out of memory\n");
+ exit(EXIT_FAILURE);
+ }
// accept connections
for(i=0; i<count; i++)
@@ -117,8 +121,13 @@ void client_hello(int sock, const char* username)
uint8_t namelen = strlen(username);
buf = malloc(namelen+2); // type + len + username
+ if(buf == NULL)
+ {
+ printf("client_hello: Out of memory\n");
+ exit(EXIT_FAILURE);
+ }
- buf[0] = CLIENT_HELLO;
+ buf[0] = msg_type_hello;
buf[1] = namelen;
memcpy(buf+2, username, namelen);
@@ -187,7 +196,12 @@ void server_start_game(int* clients, const uint8_t clientcount, const char* user
buflen += strlen(usernames[i]);
buf = malloc(buflen);
- buf[pos++] = SERVER_START_GAME;
+ if(buf == NULL)
+ {
+ printf("server_start_game: Out of memory\n");
+ exit(EXIT_FAILURE);
+ }
+ buf[pos++] = msg_type_start_game;
buf[pos++] = clientcount;
// copy usernames with length to buffer
for(int i=0; i<usercount; i++)
@@ -217,11 +231,17 @@ char* server_recv_hello(int sock)
recv(sock, buf, 13, 0);
- assert(buf[0] == CLIENT_HELLO);
+ assert(buf[0] == msg_type_hello);
namelen = buf[1];
name = malloc(namelen+1);
- strncpy(name, buf+2, namelen);
+ if(name == NULL)
+ {
+ printf("sender_recv_hello: Out of memory\n");
+ exit(EXIT_FAILURE);
+ }
+
+ memcpy(name, buf+2, namelen);
name[namelen] = '\0';
return name;
@@ -247,7 +267,7 @@ void* server_recv(int sock, uint8_t wanted)
switch(type)
{
- case CLIENT_HELLO:
+ case msg_type_hello:
result = server_recv_hello(sock);
break;
}
diff --git a/src/net.h b/src/net.h
index 6a3d1a5..2fb28c0 100644
--- a/src/net.h
+++ b/src/net.h
@@ -3,13 +3,12 @@
#define MAX_PLAYERS 10
-enum packet_type { CLIENT_HELLO, SERVER_START_GAME };
-
typedef enum
{
// Specify message type identifier here
- msg_type_hello = 0x0
+ msg_type_hello = 0x0,
+ msg_type_start_game = 0x1
} msg_type_t;
typedef struct