-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
428 lines (384 loc) · 11.4 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#define _GNU_SOURCE
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <netdb.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <linux/if_link.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include "iniparser.h"
//https://github.com/ndevilla/iniparser
#define MAC_STRING_LENGTH 13
#define ERROR(fmt, ...) do { printf(fmt, __VA_ARGS__); return -1; } while(0)
void *connection_handler(void *);
const char *printData(char *);
typedef struct collectData {
char *interface_name;
}client;
//skatalogowane interfejsy danego servera - nazwy
struct collectData interfaces[5];
int licznik = 0;
char *filename = "config";
int port, max_users;
int limit_users = 0;
//sprawdza/tworzy plik config
void create_config_file(char * name_config){
FILE * ini ;
if(!access(name_config, F_OK)){
}else{
//domyslny config
ini=fopen(name_config, "w");
fprintf(ini,
"#\n"
"# Config server\n"
"#\n"
"\n"
"[Config]\n"
"\n"
"PORT = 8888 ;\n"
"MAX_USERS = 5 ;\n"
"\n");
fclose(ini);
}
}
//parsuje plik config
int parse_config_file(char * ini_name){
dictionary * ini ;
ini = iniparser_load(ini_name);
if (ini==NULL) {
fprintf(stderr, "cannot parse file: %s\n", ini_name);
return -1 ;
}
iniparser_dump(ini, stderr);
/* Ustawienia w config'u */
printf("Configuration Server:\n");
port = iniparser_getint(ini, "config:port", -1);
printf("PORT: [%d]\n", port);
max_users = iniparser_getint(ini, "config:max_users", -1);
printf("MAX_USERS: [%d]\n", max_users);
iniparser_freedict(ini);
return 0 ;
}
//funkcja sprawdza czy interface jest już w tabeli z interfaces
int already_there(char *interfacename) {
for(int i=0; i<5; i++){
if(interfaces[i].interface_name == NULL){
return 0;
}else if(strcmp(interfaces[i].interface_name, interfacename)== 0){
return 1;
}
}
}
//funkcja sprawdza status danego interfejsu
int check_status(char *ifname) {
int state = -1;
int socId = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (socId < 0) ERROR("Socket failed. Errno = %d\n", errno);
struct ifreq if_req;
(void) strncpy(if_req.ifr_name, ifname, sizeof(if_req.ifr_name));
int rv = ioctl(socId, SIOCGIFFLAGS, &if_req);
close(socId);
if ( rv == -1) ERROR("Ioctl failed. Errno = %d\n", errno);
return (if_req.ifr_flags & IFF_UP) && (if_req.ifr_flags & IFF_RUNNING);
}
//f.pobiera mac adres
char *getmac(char *iface){
char *ret = malloc(MAC_STRING_LENGTH);
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, iface);
if (fd >= 0 && ret && 0 == ioctl(fd, SIOCGIFHWADDR, &s))
{
int i;
for (i = 0; i < 6; ++i)
snprintf(ret+i*2,MAC_STRING_LENGTH-i*2,"%02x",(unsigned char) s.ifr_addr.sa_data[i]);
}
else
{
perror("malloc/socket/ioctl failed");
exit(1);
}
return(ret);
}
//f. ustawia ip i maske adres interfejsu
void set_new_interface_data(char *inter_data){
char nazwa[8], ip[16], mask[16];
strcpy(nazwa, strtok(inter_data, ":"));
strcpy(ip, strtok(NULL, ":"));
strcpy(mask, strtok(NULL, ":"));
struct ifreq ifr;
struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
const char * name = nazwa;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
int ip_check, mask_check;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
ip_check = inet_pton(AF_INET, ip , ifr.ifr_addr.sa_data + 2);
ioctl(fd, SIOCSIFADDR, &ifr);
mask_check = inet_pton(AF_INET, mask , ifr.ifr_addr.sa_data + 2);
ioctl(fd, SIOCSIFNETMASK, &ifr);
//do rozbudowy
if(ip_check <=0 || mask_check <= 0){
printData("error");
}
ioctl(fd, SIOCGIFFLAGS, &ifr);
strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
ioctl(fd, SIOCSIFFLAGS, &ifr);
printData("ok");
}
//tworzy tablice z dostepnymi interfejsami - interfaces
char *list_interfaces(char *msg){
char *recv_msg =(char *) malloc(sizeof(char) *(100+1));
struct ifaddrs *ifaddr, *ifa;
int family, s, n;
char host[NI_MAXHOST];
char mask[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6 || family == AF_PACKET) {
if(already_there(ifa->ifa_name)==0){
interfaces[n].interface_name = ifa->ifa_name;
licznik ++;
}
}
}
if(strcmp (msg, "list") == 0){
for(int i=0; i<licznik; i++){
recv_msg = strcat(strcat(recv_msg, interfaces[i].interface_name),"\n");
}
}
else if(strcmp (msg, "ip") == 0){
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
for(int i=0; i<licznik; i++){
if((strcmp(ifa->ifa_name,interfaces[i].interface_name)==0)&&(ifa->ifa_addr->sa_family==AF_INET))
{
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
recv_msg = strcat(strcat(strcat(strcat(recv_msg, ifa->ifa_name), "\t"),host),"\n");
}
}
}
}
else if(strcmp (msg, "mac") == 0){
for(int i =0; i<licznik; i++){
char *mac = getmac(interfaces[i].interface_name);
recv_msg = strcat(strcat(strcat(strcat(recv_msg, interfaces[i].interface_name), "\t"),mac), "\t\n");
free(mac);
}
} else if(strcmp (msg, "mask") == 0){
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_netmask == NULL)
continue;
s=getnameinfo(ifa->ifa_netmask,sizeof(struct sockaddr_in),mask, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
for(int i=0; i<licznik; i++){
if((strcmp(ifa->ifa_name,interfaces[i].interface_name)==0)&&(ifa->ifa_addr->sa_family==AF_INET))
{
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
recv_msg = strcat(strcat(strcat(strcat(recv_msg, ifa->ifa_name), "\t"),mask),"\n");
}
}
}
}
else if(strcmp (msg, "stat") == 0){
char *status_s =malloc(sizeof(char) *100);
char up[] = "\tUP\t";
char down[] = "\tDOWN\t";
for(int i =0; i<licznik; i++){
if(check_status(interfaces[i].interface_name) == 0){
status_s = strcat(strcpy(status_s, interfaces[i].interface_name), down);
status_s = strcat(status_s, "\n");
}else{
status_s = strcat(strcpy(status_s, interfaces[i].interface_name), up);
status_s = strcat(status_s, "\n");
}
recv_msg = strcat(recv_msg, status_s);
}
free(status_s);
}
else if(strcmp (msg, "help") == 0){
recv_msg = strcpy(recv_msg,"list\nip\nmask\nstat\nmac\nsetinter(set new ip and mask addres) \nquit\n");
}
else if(strcmp (msg, "quit") == 0){
recv_msg = strcat(recv_msg, "quit");
}else{
char komu[30] = "Error - bledne polecenie!!!\n";
recv_msg = strcpy(recv_msg, komu);
}
freeifaddrs(ifaddr);
return recv_msg;
}
//wyświetlanie danych
const char * printData(char *client_m){
char *rec_msg =(char *) malloc(sizeof(char) *(1999+1));
char *odpow;
if(strcmp (client_m, "help") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "list") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "ip") == 0){
odpow = list_interfaces( client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "mask") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "stat") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "quit") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else if(strcmp (client_m, "setinter") == 0){
// odpow = list_interfaces(client_m);
// rec_msg = strcpy(rec_msg, odpow);
// free(odpow);
}else if(strcmp (client_m, "mac") == 0){
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
else{
odpow = list_interfaces(client_m);
rec_msg = strcpy(rec_msg, odpow);
free(odpow);
}
return rec_msg;
}
//funkcja zarzadza polaczeniami
void *connection_handler(void *socket_desc){
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
char *odp;
//wiadomość dla klienta
message = "Witaj!\n";
write(sock, message, strlen(message));
//Odpowiedź klienta
while( ( read_size = recv(sock , client_message , 2000 ,0))>0 ) {
for (int i=0; client_message[i]; i++){
client_message[i] = tolower(client_message[i]);
}
if(strcmp(client_message, "setinter")== 0){
message = "Podaj nazwe interface, IP i Maske w formacie nazwa:xxx.xxx.xxx.xxx:xxx.xxx.xxx.xxx";
write(sock, message, strlen(message)+1);
while ((read_size = recv(sock , client_message , 2000 ,0))>0){
set_new_interface_data(client_message);
}
}else{
odp = printData(client_message);
write(sock, odp, strlen(odp)+1);
memset(client_message, 0, sizeof(client_message));
free(odp);
fflush(stdout);
}
}
if(read_size == 0)
{
puts("Klient rozłączył się");
limit_users --;
fflush(stdout);
}
else if(read_size == -1)
{
perror("odpowiedź błędna");
}
//zwolnić gniazdo
free(socket_desc);
return 0;
}
int main(int argc, char *argv[]){
create_config_file(filename);
parse_config_file(filename);
int socket_desc, client_sock, c , *new_sock;
struct sockaddr_in server , client;
//tworzymy gniazdo
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Nie mogę utworzyć gniazda");
}
puts("Gniazdo utworzone");
//Struktura socket_in
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
//bind
if( bind(socket_desc,(struct sockaddr *)&server, sizeof(server)) <0)
{
perror("Bind fail");
return 1;
}
puts("Bind OK");
//nasłuch
listen(socket_desc , 1);
puts("Czekam na połączenie...");
c = sizeof(struct sockaddr_in);
//akceptacja lub odrzucenie połączenia
while((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)))
{
if(limit_users > max_users){
perror("Limit");
}else{
puts("Połączenie zaakceptowane");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if(pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("Nie udało się utworzyć wątku");
return 1;
}
puts("Utworzono wątek");
limit_users++;
}
if (client_sock < 0)
{
perror("Błąd połączenia");
return 1;
}
}
return 0;
}