/
serverOLD.c
326 lines (279 loc) · 8.56 KB
/
serverOLD.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* @file
* @brief This file implements the storage server.
*
* The storage server should be named "server" and should take a single
* command line argument that refers to the configuration file.
*
* The storage server should be able to communicate with the client
* library functions declared in storage.h and implemented in storage.c.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include "utils.h"
#include "logconst.h"
#define LOGGING 2
#define MAX_LISTENQUEUELEN 20 ///< The maximum number of queued connections.
#define MAX_DATABASE_LEN 1000 ///< The maxinum number of tables, and entries in each table.
time_t rawtime;
struct tm * timeinfo2;
char buffer2 [40];
FILE *fp;
char message[100];
int n; //for sprintf
/**
* @brief Global array of entries.
*/
struct Entry {
char *key;
char *value;
};
typedef struct Entry Entry;
/**
* @brief Global array of tables.
*/
struct Table{
char *name;
Entry entry[MAX_DATABASE_LEN];
};
typedef struct Table Table;
//time_t tm = mktime(time(0)); //creates global time struct created at program execution
/**
* @brief Searches for a entry with specified key.
* @param key Specified key.
* @param database The pointer to root of the database.
* @return Returns the pointer to entry if found, NULL otherwise.
*/
Table* table_search(char* name, Table tables[MAX_DATABASE_LEN]){
int i=0;
for(i=0; i<MAX_DATABASE_LEN; i++){
if(tables[i].name==name){
return &tables[i];
}
}
return NULL;
}
/**
* @brief Searches for a entry with specified key.
* @param key Specified key.
* @param database The pointer to root of the database.
* @return Returns the pointer to entry if found, NULL otherwise.
*/
Entry* entry_search(char* key, Entry entries[MAX_DATABASE_LEN]){
int i=0;
for(i=0; i<MAX_DATABASE_LEN; i++){
if(entries[i].key==key){
return &entries[i];
}
}
return NULL;
}
/**
* @brief Process a command from the client.
*
* @param sock The socket connected to the client.
* @param cmd The command received from the client.
* @return Returns 0 on success, -1 otherwise.
*/
int handle_command(int sock, char *cmd, Table* database, char* username, char* password)
{
n = sprintf(message, "Processing command '%s'\n", cmd);
fp = fopen(buffer2, "a+");
logger(LOGGING, fp, message); // replace LOG commands with logger() calls
fclose(fp);
// For now, just send back the command to the client.
int isReadingCMD=0; //boolean that indicated whether it is reading the command indicator (AUTH, SET, GET)
char* split, arg1, arg2, arg3;
split=strtok(cmd, " ");
if(split=="AUTH")
{
arg1=strtok(NULL, " ");//Get username
arg2=strtok(NULL, " ");//Get password
if(username==arg1&&password==arg2){
sendall(sock, arg1, strlen(arg1));
return 0;
}
else return -1;
}
else if(split=="GET")
{
arg1=strtok(NULL, " ");//Get name of table
arg2=strtok(NULL, " ");//Get key of entry
Table* table=table_search(arg1, database);
if(table){
Entry* entry=entry_search(arg2, table->entry);
if(entry){
sendall(sock, entry->value, strlen(entry->value));
}
}
}
else if(split=="SET")
{
arg1=strtok(NULL, " ");//Get name of table
arg2=strtok(NULL, " ");//Get key of entry
arg3=strtok(NULL, " ");//Get value to set
Table* table=table_search(arg1, database);
if(!table){
int i;
for(i=0; i<MAX_DATABASE_LEN; i++){
if(database[i].name==""){
table=&database[i];
break;
}
}
}
table->name=arg1;
Entry* entry=entry_search(arg2, table->entry);
if(!entry){
int i;
for(i=0; i<MAX_DATABASE_LEN; i++){
if(table->entry[i].key==""){
entry=&(table->entry[i]);
break;
}
}
}
entry->key=arg2;
entry->value=arg3;
sendall(sock, entry->value, strlen(entry->value));
}
/*
sendall(sock, cmd, strlen(cmd));
sendall(sock, "\n", 1);*/
return 0;
}
/**
* @brief Creates the data structure to be used in our tables.
*
* @param key A string, the entry's key.
* @param value An integer, the entry's value. The data type of this parameter might be changed for later versions of the storage server.
*/
/**
* @brief Creates the data structure to berver.c -o server.o
server.c:69: error: expected identifier or ��before ‘[�token
server.c:70: error: expected ��before ‘int�
server.c:84: error: expected �� �� �� ‘asm�or ‘__attribute__�before ‘database�
make[1]: *** [server.o] Error 1
* e used in our tables.
*
* @param key A string, the entry's key.
* @param value An integer, the entry's value. The data type of this parameter might be changed for later versions of the storage server.
*/
/**
* @brief Start the storage server.
*
* This is the main entry point for the storage server. It reads the
* configuration file, starts listening on a port, and proccesses
* commands from clients.
*/
int main(int argc, char *argv[])
{
Table database[MAX_DATABASE_LEN];
//printf("Setting time\n");//remove
time ( &rawtime );
timeinfo2 = localtime ( &rawtime );
//printf("Finished rawtime stuff\n"); //remove
//printf("About to do strftime\n"); //remove
strftime (buffer2,40,"Server-%Y-%m-%d-%H-%M-%S.log",timeinfo2);
puts (buffer2);
//printf("Finished setting time\n");//remove
// Process command line arguments.
// This program expects exactly one argument: the config file name.
assert(argc > 0);
if (argc != 2) {
printf("Usage %s <config_file>\n", argv[0]);
exit(EXIT_FAILURE);
}
char *config_file = argv[1];
// Read the config file.
struct config_params params;
int status = read_config(config_file, ¶ms);
if (status != 0) {
printf("Error processing config file.\n");
exit(EXIT_FAILURE);
}
n = sprintf(message, "Server on %s:%d\n", params.server_host, params.server_port);
//printf("%i", n);
fp = fopen(buffer2, "a+");
logger(LOGGING, fp, message);
fclose(fp);
// Create a socket.
int listensock = socket(PF_INET, SOCK_STREAM, 0);
if (listensock < 0) {
printf("Error creating socket.\n");
exit(EXIT_FAILURE);
}
// Allow listening port to be reused if defunct.
int yes = 1;
status = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
if (status != 0) {
printf("Error configuring socket.\n");
exit(EXIT_FAILURE);
}
// Bind it to the listening port.
struct sockaddr_in listenaddr;
memset(&listenaddr, 0, sizeof listenaddr);
listenaddr.sin_family = AF_INET;
listenaddr.sin_port = htons(params.server_port);
inet_pton(AF_INET, params.server_host, &(listenaddr.sin_addr)); // bind to local IP address
status = bind(listensock, (struct sockaddr*) &listenaddr, sizeof listenaddr);
if (status != 0) {
printf("Error binding socket.\n");
exit(EXIT_FAILURE);
}
// Listen for connections.
status = listen(listensock, MAX_LISTENQUEUELEN);
if (status != 0) {
printf("Error listening on socket.\n");
exit(EXIT_FAILURE);
}
// Listen loop.
int wait_for_connections = 1;
while (wait_for_connections) {
// Wait for a connection.
struct sockaddr_in clientaddr;
socklen_t clientaddrlen = sizeof clientaddr;
int clientsock = accept(listensock, (struct sockaddr*)&clientaddr, &clientaddrlen);
if (clientsock < 0) {
printf("Error accepting a connection.\n");
exit(EXIT_FAILURE);
}
n = sprintf(message, "Got a connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
fp = fopen(buffer2, "a+");
logger(LOGGING, fp, message);
fclose(fp);
// Get commands from client.
int wait_for_commands = 1;
do {
// Read a line from the client.
char cmd[MAX_CMD_LEN];
int status = recvline(clientsock, cmd, MAX_CMD_LEN);
if (status != 0) {
// Either an error occurred or the client closed the connection.
wait_for_commands = 0;
} else {
// Handle the command from the client.
int status = handle_command(clientsock, cmd, database, params.username, params.password);
if (status != 0)
wait_for_commands = 0; // Oops. An error occured.
}
} while (wait_for_commands);
// Close the connection with the client.
close(clientsock);
n = sprintf(message, "Closed connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
fp = fopen(buffer2, "a+");
logger(LOGGING, fp, message);
fclose(fp);
}
// Stop listening for connections.
close(listensock);
return EXIT_SUCCESS;
}