-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
410 lines (385 loc) · 11.7 KB
/
server.cpp
File metadata and controls
410 lines (385 loc) · 11.7 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***** C knihovny *****/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
/***** C++ knihovny ******/
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
/****** POMOCNA MAKRA ******/
#define BUFF_SIZE 100
/*
* TO DO LIST:
* - dokumentace
*/
using namespace std;
/****************************************************************************************************
* Deklarace pomocnych funkci a promennych.
****************************************************************************************************
*/
typedef struct {
int u_or_l;
string parameters;
string search;
vector<string> goals;
int port;
} tOptions;
void initOptions(tOptions*);
int getPort(int, char* [], tOptions*);
int setNetwork(int*, struct sockaddr_in*, tOptions*);
int createSocket();
int acceptConnection(struct sockaddr_in*, int*, int*);
int communicate(int*, tOptions*);
string createAnswer(char *, tOptions*, int*);
int processRequest(string, tOptions*, int*);
int find(tOptions*);
/****************************************************************************************************
* Main.
****************************************************************************************************
*/
int main(int argc, char* argv[])
{
int welcome_soc, in_soc;
struct sockaddr_in sin;
tOptions options;
memset(&sin, 0, sizeof(sin));
initOptions(&options);
getPort(argc, argv, &options);
setNetwork(&welcome_soc, &sin, &options);
while (1)
{
int pid;
/* accepting new connection request from client, socket id for the new connection is returned in in_soc */
acceptConnection(&sin, &welcome_soc, &in_soc);
if( in_soc <= 0 ) continue;
/***** FORK PROCESS *****/
pid = fork(); // fork
if ( pid < 0 )
{
cerr << "Error on fork." << endl;
exit(EXIT_FAILURE);
}
if ( pid == 0 ) // child process
{
if (close(welcome_soc) < 0) // uzavreni welcome soketu, ktery u potomka uz neni potreba
{
cerr << "Close error." << endl;
return EXIT_FAILURE;
}
/***** SAMOTNA KOMUNIKACE *****/
communicate(&in_soc, &options);
if (close(in_soc) < 0) // uzavreni komunkacniho soketu
{
cerr << "Close error." << endl;
return EXIT_FAILURE;
}
break;
}
else // parent process
{
// closing communication socket
if ( close(in_soc) < 0 )
{
cerr << "Error on close." << endl;
exit(EXIT_FAILURE);
}
}
}
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce zpracovavajici argument, vracejici cislo portu.
****************************************************************************************************
* @param argc, argv - ...
* @return cislo portu z argumentu
*/
int getPort(int argc, char* argv[], tOptions* opts)
{
int option;
int length;
if ( argc != 3 )
{
cerr << "Error: Too few or Too many parameters." << endl;
exit(EXIT_FAILURE);
}
while ( (option = getopt (argc, argv, "p:")) != -1 ) // pouziti getoptu
{
switch(option)
{
case 'p':
length = strlen(optarg);
for (int j = 0; j < length; j++)
{
if ( !isdigit(optarg[j]) )
{
cerr << "Port has to be a number." << endl;
exit(EXIT_FAILURE);
}
}
opts->port = atoi(optarg);
break;
default:
exit(EXIT_FAILURE);
break;
}
}
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce pro nabindovani a prijmuti prvniho soketu.
****************************************************************************************************
* @param sin pointer na strukturu
* @param soc_p soket
* @param opts pomocna struktura
* @return se stejne zahodi
* - z prikladu k predmetu, jen vlozeno do funkce
*/
int setNetwork(int *soc_p, struct sockaddr_in* sin, tOptions* opts)
{
*soc_p = createSocket();
sin->sin_family = PF_INET; // set protocol family to Internet
sin->sin_port = opts->port; // sin->sin_port = htons(options->port);
sin->sin_addr.s_addr = INADDR_ANY;
if ( bind(*soc_p, (struct sockaddr *)sin, sizeof(*sin) ) < 0 )
{
cerr << "Error on bind." << endl;
exit(EXIT_FAILURE); // bind error
}
if ( listen(*soc_p, 5) )
{
cerr << "Error on listen." << endl; // listen error
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce pro vytvoreni socketu.
****************************************************************************************************
* @return - vytvoreny socket
*/
int createSocket()
{
int new_soc;
if ( (new_soc = socket(PF_INET, SOCK_STREAM, 0 )) < 0 ) // create socket
{
cerr << "Error on socket." << endl; // socket error
exit(EXIT_FAILURE);
}
return new_soc;
}
/****************************************************************************************************
* Funkce na prijeti pripojeni.
****************************************************************************************************
* @param sin struktura
* @param welcome_soc welcome socket
* @param in_soc komunikacni soket
* @return zahodi se
*/
int acceptConnection(struct sockaddr_in* sin, int* welcome_soc, int* in_soc)
{
int sin_len;
string hostname;
sin_len = sizeof(*sin);
*in_soc = accept(*welcome_soc, (struct sockaddr *)sin, (socklen_t*)&sin_len);
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce slouzici pro komunikaci s klientem.
****************************************************************************************************
* @param in_soc komunkikacni socket
* @param opts pomocna struktura s prepinaci
* @return se stejne zahodi
*/
int communicate(int* in_soc, tOptions* opts)
{
char recieved_message[BUFF_SIZE];
string request, answer;
bzero(recieved_message, BUFF_SIZE*sizeof(char) );
int i = 1;
while(i)
{
/***** READ REQUEST *****/
if ( read(*in_soc, recieved_message, BUFF_SIZE ) < 0 )
{
cerr << "Error on read." << endl; // read error
return EXIT_FAILURE;
}
/***** CREATE ANSWER *****/
answer = createAnswer(recieved_message, opts, &i); // vytvoreni odpovedi pro klienta
if ( i == 0 ) // pokud bylo zmeneno i na 0, byla ze strany klienta ukoncena komunikace
return EXIT_SUCCESS;
/***** SEND ANSWER *****/
if ( send(*in_soc, answer.c_str(), answer.length(), 0) < 0 ) // odeslani odpovedi klientovi
{
perror("Error on sending.\n"); // chyba pri odesilani zpravy
exit(EXIT_FAILURE);
}
opts->search.clear();
opts->parameters.clear();
}
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce pro vytvoreni zpravy.
****************************************************************************************************
* @param request obdrzena zprava
* @param opts ukazatel na strukturu s prepinaci
* @return vytvorena zprava
*/
string createAnswer(char* request, tOptions* opts, int* i)
{
string answer, s_request;
s_request = request;
processRequest(s_request, opts, i); // zpracovani pozadavku klienta
if ( *i == 0 ) // pokud bylo zmeneno i na 0, byla ze strany klienta ukoncena komunikace
return "END\n";
find(opts); // hledani pozadovanych informaci
if ( opts->goals.empty() ) // pokud nebyl nalezen vysledek
{
answer.append("N"); // zprava bude zacinat N jako "NOT FOUND"
if ( opts->u_or_l == 1 )
answer.append("L\n");
else if ( opts->u_or_l == 2 )
answer.append("U\n");
answer.append(opts->search);
answer.append("\n");
}
else // pokud odpovidajici informace byla nalezena
{
answer.append("F\n"); // zprava bude zacinat F jako "FOUND"
while ( !opts->goals.empty() ) // zkopirovani vsech vysledku z vektoru
{
answer.append( opts->goals.front() );
opts->goals.erase( opts->goals.begin() );
}
}
return answer;
}
/****************************************************************************************************
* Funkce pro hledani v souboru etc/passwd.
****************************************************************************************************
* @param opts ...
* @return ...
*/
int find(tOptions* opts)
{
string line;
string delimiter = ":";
string new_line = "\n";
ifstream etcPasswd("/etc/passwd"); // otevreni souboru /etc/passwd
if(!etcPasswd)
{
cerr << "Error on open: /etc/passwd." << endl;
exit(EXIT_FAILURE);
}
while( getline(etcPasswd, line) ) // nacitani soubotu po radcich
{
vector<string> items; // vektor do ktereho se oddelene ulozi vsechny informace na radku
size_t offset = 0;
int match = 0;
string item, goal;
while ( ( offset = line.find(delimiter) ) != string::npos ) // prochazeni radku, nalezeni pozice dvojtecky
{
item = line.substr(0, offset); // vytvoreni informace pomoci substringu
items.push_back(item);
line.erase( 0, offset+delimiter.length() ); // odmazani zpracovane informace z radku
}
items.push_back(line); // ulozeni posledni iformace, tzn. default shellu
if ( opts->u_or_l == 1 ) // case login
{
if ( opts->search.compare(items[0]) == 0 ) // pokud se ve vektoru na miste loginu nachazi hledany login, dochazi k matchi
match = 1;
}
else if ( opts->u_or_l == 2 ) // case uid
{
if ( opts->search.compare(items[2]) == 0 ) // match uid
match = 1;
}
if ( match )
{
for (size_t s = 0; s < opts->parameters.length(); s++) // prochazeni pozadovanych parametru v poradi v jakem prisli, pripadne vlozeni
{
switch(opts->parameters[s])
{
case 'L':
goal.append(items[0]);
goal.append(" ");
break;
case 'U':
goal.append(items[2]);
goal.append(" ");
break;
case 'G':
goal.append(items[3]);
goal.append(" ");
break;
case 'H':
goal.append(items[5]);
goal.append(" ");
break;
case 'S':
goal.append(items[6]);
goal.append(" ");
break;
case 'N':
goal.append(items[4]);
goal.append(" ");
break;
}
}
size_t dest;
dest = goal.rfind(" ");
goal.replace(dest, 1, new_line); // zmena posledniho znaku z mezery na novy radek
opts->goals.push_back(goal);
}
}
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce na zpracovani pozadavku.
****************************************************************************************************
* @param request zprava od klienta
* @param opts ...
* @return ...
*/
int processRequest(string request, tOptions* opts, int* i)
{
string line;
istringstream fStr(request);
if ( request[0] == 'L' ) // pokud v prichozi zprave je prvni znak L, bude se hledat podle loginu
opts->u_or_l = 1;
else if ( request[0] == 'U' ) // pokud U, hledat se bude podle uid
opts->u_or_l = 2;
else if ( request[0] == 'E' ) // pokud na prvnim miste je E, klient konci komunikaci
{
*i = 0;
return 0;
}
getline(fStr, line);
opts->search.append(line, 2, line.length()-2 ); // ulozeni loginu/uid zacinajicich na 2 pozici
getline(fStr, line);
opts->parameters.append(line); // ulozeni pozadovanych informaci
return EXIT_SUCCESS;
}
/****************************************************************************************************
* Funkce na inicializaci struktury s prepinaci.
****************************************************************************************************
* @param opts - uk. na strukturu k inicializaci
*/
void initOptions(tOptions* opts)
{
opts->port = -1;
opts->u_or_l = 0;
opts->parameters = "";
}