3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * server.c - Client Server Code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
13 #include <sys/socket.h>
16 #define MAX_CONNECTION_QUEUE 5
17 #define INPUT_BUFFER_SIZE 128
19 #define MSG_STR_TOO_LONG "499 Command too long (limit "EXPSTR(INPUT_BUFFER_SIZE)")\n"
22 typedef struct sClient
29 void Server_Start(void);
30 void Server_HandleClient(int Socket);
31 char *Server_ParseClientCommand(tClient *Client, char *CommandString);
32 char *Server_Cmd_USER(tClient *Client, char *Args);
35 int giServer_Port = 1020;
36 int giServer_NextClientID = 1;
38 struct sClientCommand {
40 char *(*Function)(tClient *Client, char *Arguments);
41 } gaServer_Commands[] = {
42 {"USER", Server_Cmd_USER}
44 #define NUM_COMMANDS (sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0]))
48 * \brief Open listenting socket and serve connections
50 void Server_Start(void)
56 * \brief Reads from a client socket and parses the command strings
58 void Server_HandleClient(int Socket)
60 char inbuf[INPUT_BUFFER_SIZE];
62 int remspace = INPUT_BUFFER_SIZE-1;
64 tClient clientInfo = {0};
67 while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
70 buf[bytes] = '\0'; // Allow us to use stdlib string functions on it
74 while( (eol = strchr(start, '\n')) )
78 ret = Server_ParseClientCommand(&clientInfo, start);
79 // `ret` is a string on the heap
80 send(Socket, ret, strlen(ret), 0);
85 // Check if there was an incomplete line
86 if( *start != '\0' ) {
87 int tailBytes = bytes - (start-buf);
88 // Roll back in buffer
89 memcpy(inbuf, start, tailBytes);
90 remspace -= tailBytes;
92 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
94 remspace = INPUT_BUFFER_SIZE - 1;
99 remspace = INPUT_BUFFER_SIZE - 1;
105 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
111 * \brief Parses a client command and calls the required helper function
112 * \param Client Pointer to client state structure
114 char *Server_ParseClientCommand(tClient *Client, char *CommandString)
119 // Split at first space
120 space = strchr(CommandString, ' ');
130 for( i = 0; i < NUM_COMMANDS; i++ )
132 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0)
133 return gaServer_Commands[i].Function(Client, args);
136 return strdup("400 Unknown Command\n");
143 * \brief Set client username
145 char *Server_Cmd_USER(tClient *Client, char *Args)
151 printf("Client %i authenticating as '%s'\n", Args);
155 free(Client->Username);
156 Client->Username = strdup(Args);
158 // Create a salt (that changes if the username is changed)
160 Client->Salt = malloc(9);
161 Client->Salt[0] = 0x21 + (rand()&0x3F);
162 Client->Salt[1] = 0x21 + (rand()&0x3F);
163 Client->Salt[2] = 0x21 + (rand()&0x3F);
164 Client->Salt[3] = 0x21 + (rand()&0x3F);
165 Client->Salt[4] = 0x21 + (rand()&0x3F);
166 Client->Salt[5] = 0x21 + (rand()&0x3F);
167 Client->Salt[6] = 0x21 + (rand()&0x3F);
168 Client->Salt[7] = 0x21 + (rand()&0x3F);
170 // "100 Salt xxxxXXXX\n"
171 ret = strdup("100 SALT xxxxXXXX\n");
172 sprintf(ret, "100 SALT %s\n", Client->Salt);