-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
281 lines (260 loc) · 8.67 KB
/
main.c
File metadata and controls
281 lines (260 loc) · 8.67 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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
#include <ctype.h>
#include "tcp.h"
#include "udp.h"
#include "list.h"
#include "threadsoper.h"
#include "tcpclient.h"
#include "fileoper.h"
#define BUFSIZE 2048
pthread_mutex_t locker;
void showHelp();
void runtimeHelp();
int main(int argc, char **argv) {
//wartosci domyslne parametrow
int verbose = 0;
int tcpPort = 34000;
int udpPort = 9000;
int c;
//obsluga parametrow wejsciowych - biblioteka getopt
while ((c = getopt (argc, argv, "a:ht:u:v")) != -1) {
switch (c)
{
case 'a':
addToEnd("shared.txt", optarg);
return 0;
case 'h':
showHelp();
return 0;
case 't':
tcpPort = atoi(optarg);
break;
case 'u':
udpPort = atoi(optarg);
break;
case 'v':
verbose = 1;
break;
case '?':
if (optopt == 't' || optopt == 'u')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr,"Unknown option '-%c'.\n", optopt);
else
fprintf (stderr,"Unknown option character '%c'.\n", optopt);
return 1;
default:
abort ();
}
}
//argument "wolny" - folder przechowywania plikow
if (optind>=argc) {
fprintf (stderr,"You need to specify destination directory\n");
return 1;
}
char* destDir = argv[optind];
struct stat st;
lstat(destDir, &st);
//czy to katalog
if(!S_ISDIR(st.st_mode)){
fprintf (stderr,"%s is not a directory\n", destDir);
return 1;
}
//czy prawo do zapisu
if(access(destDir, W_OK)) {
perror("Destination directory error");
return 1;
}
//tworzenie list
int err=0;
List* adresses;
adresses = malloc(sizeof(List));
if (!adresses){
perror("Cannot allocate memory");
return 1;
}
listInit(adresses);
List* ipadr = malloc(sizeof(List));
if (!ipadr){
perror("Cannot allocate memory");
return 1;
}
List* broadadr = malloc(sizeof(List));
if (!broadadr){
perror("Cannot allocate memory");
return 1;
}
listInit(ipadr);
listInit(broadadr);
//pobranie konfiguracji sieci
ipbroadcastAdresses(ipadr, broadadr);
//zamaskowanie sygnalu SIGPIPE - dopuszczamy pisanie do zamknietego socketa
sigset_t mask;
sigemptyset (&mask);
sigaddset (&mask, SIGPIPE);
sigprocmask (SIG_BLOCK, &mask, NULL);
//inicjalizacja mutexa - potrzebne do sekcji krytycznej
err = pthread_mutex_init(&locker, NULL);
if (err<0){
perror("Cannot create locker");
return 1;
}
//tworenie watkow UDP i TCP
pthread_t udpListener, tcpListener;
ArgListIntInt tcpArg, udpArg;
fillArgStruct(&udpArg, adresses, ipadr, broadadr, udpPort, verbose);
fillArgStruct(&tcpArg, ipadr, NULL, NULL, tcpPort, verbose);
err = pthread_create(&udpListener,NULL,udpListenDance, &udpArg);
if (err<0){
perror("Cannot create UDP listener");
return 1;
}
err = pthread_create(&tcpListener,NULL,tcpListenDance, &tcpArg);
if (err<0){
perror("Cannot create TCP listener");
return 1;
}
//obsluga polecen uzytkownika
int k=1;
while(k){
//bezpieczne odczytywanie stdin
char buff[200];
int readed = read(STDIN_FILENO, buff, sizeof(buff)-1);
buff[readed-1] = 0;
char *words[readed/2], *ptr;
for(int i=0;i<readed/2;++i) words[i]=0;
//wlasna implementacja tokenizacji stringa
int params=1;
ptr=words[0]=buff;
while ((ptr = strchr(ptr,' '))) {
*ptr='\0';
words[params]=++ptr;
++params;
}
//polecenie shared (s)
if(!strcmp(words[0],"shared") || !strcmp(words[0],"s")) {
//sprawdzenie liczby parametrow
if(params<2)
printf("Too few parameters. Type 'help' to get information\n");
else {
//sprawdzenie poprawnosci adresu IP
struct sockaddr_in remote;
err=inet_pton(AF_INET, words[1], &remote.sin_addr);
if(err==0)
printf("%s is not a valid IP adress\n",words[1]);
else if (err==-1)
perror("IP Conversion Error");
else {
//sprawdzenie, czy zadany adres na liscie obslugujacych i ew. nawiazanie polaczenia
pthread_mutex_lock(&locker);
if(find(adresses,&remote)) {
pthread_mutex_unlock(&locker);
getShared(&remote, tcpPort, verbose);
}
else {
pthread_mutex_unlock(&locker);
printf("%s don't want to dance with us\n", words[1]);
}
}
}
}
//polecenie get (g)
else if(!strcmp(words[0],"get") || !strcmp(words[0],"g")) {
//sprawdzaie liczby parametrow
if(params<3)
printf("Too few parameters. Type 'help' to get information\n");
else {
//sprawdzanie poprawnosci adresu IP
struct sockaddr_in remote;
err=inet_pton(AF_INET, words[1], &remote.sin_addr);
if(err==0)
printf("%s is not a valid IP adress\n",words[1]);
else if (err==-1)
perror("IP Conversion Error");
else {
//sprawdzenie, czy zadany adres na liscie obslugujacych i ew. nawiazanie polaczenia
pthread_mutex_lock(&locker);
if(find(adresses,&remote)) {
pthread_mutex_unlock(&locker);
getFile(&remote, tcpPort, words[2], destDir, verbose);
}
else {
pthread_mutex_unlock(&locker);
printf("%s don't want to dance with us\n", words[1]);
}
}
}
}
//polecenie list (l)
else if(!strcmp(words[0],"list") || !strcmp(words[0],"l")) {
//wyswietlanie adresow w sekcji krytycznej
pthread_mutex_lock(&locker);
printAll(adresses);
pthread_mutex_unlock(&locker);
}
//polecenie exit (e)
else if(!strcmp(words[0],"exit") || !strcmp(words[0],"e")) {
//kończenie watkow
signal(SIGUSR1,sig_udpListenDance);
closeThread(&udpListener);
signal(SIGUSR1,sig_tcpListenDance);
closeThread(&tcpListener);
k=0;
}
//polecenie help (h)
else if(!strcmp(words[0],"help") || !strcmp(words[0],"h")) {
runtimeHelp();
}
//nieprawidlowy parametr
else {
printf("'%s' is not a dancer command. Type 'help' to get information\n", words[0]);
}
}
//zwalnianie pamieci
deleteAllAllocate(adresses);
free(adresses);
deleteAllAllocate(ipadr);
free(ipadr);
deleteAllAllocate(broadadr);
free(broadadr);
return 0;
}
//pomoc ogolna
void showHelp() {
printf("Dancer 0.00001, Rumba(v.0.00001) sharing protocol client\n"
"Syntax: dancer [OPTIONS] destination\n"
"\n"
"destination - directory, where you want to save downloaded files\n"
"\n"
"Running:\n"
"-a PATH\tAdd PATH to shared resources\n"
"-h\tShow this help\n"
"-t\tSpecify TCP port. Default: 24000\n"
"-u\tSpecify UDP port. Default: 9000\n"
"-v\tSwitch to verbose mode (it's really verbose)\n"
"\n"
"Runtime:\n");
runtimeHelp();
}
//pomoc po uruchomieniu
void runtimeHelp() {
printf("Program handle options:\n"
"e\texit\t\tEnd the program\n"
"g\tget IP FILENUM\tFrom IP (see list) get file with FILENUM (see shared)\n"
"h\thelp\t\tShow this info\n"
"l\tlist\t\tShow list of computers, which use awesome Rumba protocol\n"
"s\tshared IP\tGet list of shared resources from IP (see list)\n");
}