-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
339 lines (289 loc) · 9.53 KB
/
server.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef DEBUG
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif // DEBUG
#ifdef DEBUG_PRINT_ENABLED
#define DEBUG_PRINT(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( 0 )
#else
#define DEBUG_PRINT(...) do{ } while ( 0 )
#endif // DEBUG_PRINT_ENABLED
#ifdef DEBUG_LOG_ENABLED
#define DEBUG_LOG(...) do{ fprintf( stderr, "[%s] %s\n", __VA_ARGS__ ); } while( 0 )
#else
#define DEBUG_LOG(...) do{ } while ( 0 )
#endif // DEBUG_LOG_ENABLED
#ifdef DEBUG_ERR_ENABLED
#define DEBUG_ERR(...) do{ fprintf( stderr, "[%s] ERR %s\n", __VA_ARGS__ ); } while( 0 )
#else
#define DEBUG_ERR(...) do{ } while ( 0 )
#endif // DEBUG_ERR_ENABLED
#define ERR(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( 0 )
#define OUTPUT(...) do{ fprintf( stdout, __VA_ARGS__ ); } while( 0 )
int main(int argc, char **argv)
{
DEBUG_LOG("SRV-INIT", "Starting server...");
FILE *src;
struct sockaddr_in server_address;
unsigned short port_number = 0;
short c;
src = fopen("/etc/passwd", "r");
DEBUG_LOG("SRV-INIT", "Processing options...");
while ((c = getopt(argc, argv, "p:")) != -1)
{
switch (c)
{
case 'p':
DEBUG_LOG("SRV-ARG", "Port option registered:");
char *garbage;
port_number = strtoul(optarg, &garbage, 0);
if (strcmp(garbage, "") != 0 || port_number <= 0)
{
ERR("Port '%s' is not valid!\n", optarg);
exit(6);
}
DEBUG_PRINT("\tvalue: %d\n", port_number);
break;
case '?':
if (isprint(optopt))
ERR("Unknown option '-%c' or missing it's argument.\n", optopt);
else
ERR("Unknown option character `\\x%x'.\n", optopt);
exit(5);
default:
ERR("Unknown option character %c.\n", c);
exit(5);
}
}
if (port_number == 0)
{
ERR("Missing option -p: port of remote server is not specified.\n");
exit(7);
}
// Creating socket: IPv4, TCP
int server = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (server <= 0)
{
DEBUG_ERR("SRV-INIT", "Failed to create socket!");
DEBUG_PRINT("result: %d\n", server);
ERR("Socket failed to be created.\n");
return 1;
}
DEBUG_LOG("SRV-INIT", "Socket created.");
int optval = 1;
int dc_timeout = 10;
// Setting something XD
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, (const void*) &optval, sizeof(int));
setsockopt(server, SOL_SOCKET, SO_LINGER, (const void*) &dc_timeout, sizeof(int));
// Clean server_address contents
memset(&server_address, 0, sizeof(server_address));
// IPv4
server_address.sin_family = AF_INET;
// Accepts from any IP
server_address.sin_addr.s_addr = INADDR_ANY;
// Application port
server_address.sin_port = htons(port_number);
// Bind to address and port
int bind_res = bind(server, (struct sockaddr *) &server_address, sizeof(server_address));
if (bind_res < 0)
{
DEBUG_ERR("SRV-INIT", "Failed to bind!");
DEBUG_PRINT("result: %d\n", bind_res);
ERR("Failed to bind the server to port '%d' and any IP address.\n", port_number);
return 2;
}
DEBUG_LOG("SRV-INIT", "Socket bind successful.");
int listen_res = listen(server, 5);
if (listen_res < 0)
{
DEBUG_ERR("SRV-INIT", "Failed to listen!");
DEBUG_PRINT("result: %d\n", listen_res);
ERR("Failed to start listening.\n");
return 3;
}
DEBUG_LOG("SRV-INIT", "Socket listen successful.");
struct sockaddr_in client;
unsigned int client_size = sizeof(client);
char buffer[256];
while (1)
{
rewind(src);
DEBUG_LOG("SRV-CONN", "Waiting for incomming transmission...");
int socket = accept(server, (struct sockaddr *) &client, &client_size);
DEBUG_LOG("SRV-CONN", "Connection with client established!");
if (socket < 0)
{
DEBUG_ERR("SRV-CONN", "Error on accept!");
DEBUG_PRINT("result: %d\n", socket);
}
else
{
DEBUG_LOG("SRV-CONN", "Preparing to read incomming data...");
while (1)
{
memset(&buffer, 0, sizeof(buffer));
DEBUG_LOG("SRV-COMMS", "Reading data...");
int read_res = read(socket, buffer, sizeof(buffer));
if (read_res < 0)
{
DEBUG_ERR("SRV-COMMS", "Error on read from received socket!");
DEBUG_PRINT("result: %d\n", read_res);
close(socket);
break;
}
else
{
int write_res;
DEBUG_PRINT("received: %s\n", buffer);
DEBUG_ERR("SRV-COMMS", "Parsing command and search info.");
char *command;
char *search;
command = strtok(buffer, ":");
search = strtok(NULL, ":");
DEBUG_PRINT("\tcmd: %s\n", command);
DEBUG_PRINT("\tsearch: %s\n", search);
unsigned short cmd_target = 0;
if (command == NULL || (strcmp(command, "list") != 0 && search == NULL))
{
ERR("Received invalid command from client.\n");
write(socket, "!err", 5);
write(socket, "Received empty command.", strlen("Received empty command.") + 1);
DEBUG_LOG("SRV-COMMS", "Terminating connection...");
close(socket);
break;
}
else if (strcmp(command, "info") == 0)
{
// -n
cmd_target = 5;
}
else if (strcmp(command, "home") == 0)
{
// -f
cmd_target = 6;
}
else if (strcmp(command, "list") == 0)
{
// -l
cmd_target = 1;
if (search == NULL)
search = "";
}
else
{
ERR("Received invalid command from client.\n");
write(socket, "!err", 5);
write(socket, "Received unknown command.", strlen("Received unknown command.") + 1);
DEBUG_LOG("SRV-COMMS", "Terminating connection...");
close(socket);
break;
}
DEBUG_LOG("SRV-COMMS", "Making system command call...");
unsigned c_idx = 0;
unsigned r_idx = 0;
unsigned colon_idx = 256;
char results[4096][256];
char *result;
char c;
int matches = 1;
while ((c = fgetc(src)) && c != EOF)
{
if (c == '\n' || matches == 0)
{
if (c == '\n')
{
if (matches == 1)
{
r_idx++;
}
matches = 1;
c_idx = 0;
colon_idx = 256;
}
continue;
}
results[r_idx][c_idx] = c;
//results[r_idx][c_idx + 1] = '\0';
if (c == ':' && c_idx < colon_idx)
{
// Označení indexu první dvojtečky (konec loginu)
colon_idx = c_idx;
}
if (c_idx < colon_idx)
{
// Aktuální znak je součástí tohoto loginu
if (c_idx >= strlen(search))
{
// Aktuální znak je mimo vyhledávací řetězec
if (cmd_target > 1)
{
matches = 0;
continue;
}
}
else if (c != search[c_idx])
{
// Aktuální znak již neshoduje
matches = 0;
continue;
}
}
else if (c_idx < strlen(search))
{
// Aktuální znak není mimo vyhledávací řetězec
// ale už není součástí loginu
matches = 0;
continue;
}
c_idx++;
}
DEBUG_LOG("SRV-COMMS", "Search completed!");
if (r_idx == 0 || (r_idx > 1 && cmd_target > 1))
{
DEBUG_LOG("SRV-COMMS", "Search empty, sending special empty response!");
write_res = write(socket, "!empty", 7);
if (write_res < 0)
{
DEBUG_ERR("WHILE", "Error on write to socket!");
DEBUG_PRINT("\tresult: %d\n", write_res);
break;
}
}
else
{
for (unsigned int i = 0; i < r_idx; i++)
{
DEBUG_LOG("SRV-COMMS", "Sending response...");
DEBUG_PRINT("\tfull: '%s'\n", results[i]);
result = strtok(results[i], ":");
for (unsigned int x = 1; x < cmd_target; x++)
result = strtok(NULL, ":");
DEBUG_PRINT("\tdata: '%s'\n", result);
write_res = write(socket, result, strlen(result) + 1);
if (write_res < 0)
{
DEBUG_ERR("WHILE", "Error on write to socket!");
DEBUG_PRINT("\tresult: %d\n", write_res);
break;
}
}
}
DEBUG_LOG("SRV-COMMS", "Result emptied. Sending bye...");
write_res = write(socket, "!bye", 5);
DEBUG_LOG("SRV-COMMS", "Terminating connection...");
close(socket);
break;
}
}
}
}
return 0;
}