-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetTool.c
353 lines (334 loc) · 12.1 KB
/
NetTool.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<libgen.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
/********************************************************************/
/* A simple set of colored message functions. (blue,green,red) */
/********************************************************************/
void promptM(char * message){
fprintf(stdout,ANSI_COLOR_CYAN "[*]" ANSI_COLOR_RESET " %s\n",message);
}
void promptS(char * message){
fprintf(stdout,ANSI_COLOR_GREEN "[*]" ANSI_COLOR_RESET " %s\n",message);
}
void promptE(char * message){
fprintf(stderr,ANSI_COLOR_RED "[*] %s\n" ANSI_COLOR_RESET ,message);
}
/********************************************************************/
/*Return the string containing the hostname of a machine */
/********************************************************************/
char * getHost(struct sockaddr aE,socklen_t lg){
char * hbuf = malloc(NI_MAXHOST* sizeof(char));
getnameinfo((struct sockaddr*)&aE,lg,hbuf,sizeof(hbuf),NULL,0,0);
return hbuf;
}
/********************************************************************/
/* Create a socket in SOCK_STREAM or SOCK_DGRAM */
/* 0 = TCP ; 1 = UDP */
/********************************************************************/
int CreateSocket(int protocol){
promptM("Creating socket...");
int dS = -1;
switch(protocol){
case 0:
dS = socket(AF_INET,SOCK_STREAM,0);
break;
case 1:
dS = socket(AF_INET,SOCK_DGRAM,0);
break;
default:
fprintf(stderr,ANSI_COLOR_RED "[*] Error please set correct numbers for the function (0 = TCP, 1 = UDP)\n" ANSI_COLOR_RESET);
exit(1);
break;
}
if(dS == -1){
perror(ANSI_COLOR_RED "[*] Error while creating socket : " ANSI_COLOR_RESET);
exit(1);
}
promptS("Done.");
return dS;
}
/********************************************************************/
/* Bind a socket with the givin port */
/********************************************************************/
int BindSocket(int dS,char * argv){
promptM("Binding...");
struct sockaddr_in ad;
ad.sin_family = AF_INET;
ad.sin_addr.s_addr = INADDR_ANY;
ad.sin_port = htons((short)atoi(argv));
int dB = bind(dS,(struct sockaddr*)&ad,sizeof(ad));
if(dB == -1){
perror("Error while binding socket : ");
exit(1);
}
promptS("Done.");
return dB;
}
/********************************************************************/
/* Listen on a giving socket */
/********************************************************************/
int Listen(int dS){
promptS("Starting listener...\n");
int dL = listen(dS,10);
if(dL == -1){
perror("Error listening on the socket : ");
exit(1);
}
return dL;
}
/********************************************************************/
/* Accepting a TCP connection on a socket with an empty addrinfo */
/********************************************************************/
int AcceptTCP(int dS,struct sockaddr_storage adCli, socklen_t adCliLength){
int dSC = accept(dS,(struct sockaddr *)&adCli,&adCliLength);
if(dSC == -1){
perror("Error accepting a client : ");
exit(1);
}
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
getnameinfo((struct sockaddr *)&adCli,adCliLength,hbuf,sizeof(hbuf), sbuf,sizeof(sbuf),0);
printf("[*] Starting interaction with %s:%s...\n",hbuf,sbuf);
return dSC;
}
/********************************************************************/
/* Connect to a TCP server with a hostname/IP and a port */
/********************************************************************/
int CConnect(int dS,char* server, char* port){
struct addrinfo * addressInfo;
getaddrinfo(server,port,NULL,&addressInfo);
int dC = connect(dS,addressInfo->ai_addr,addressInfo->ai_addrlen);
if(dC == -1){
perror("Error while connecting : ");
exit(1);
}
return dC;
}
/********************************************************************/
/* Send "message" string to a TCP server */
/********************************************************************/
void SendMessageTCP(int dS, char * message,int sizeM){
char buffer[sizeM+1];
strcpy(buffer,message);
buffer[sizeM+1] = '\0';
int dSend = send(dS,buffer,sizeM,0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
return;
}
/********************************************************************/
/* Send an integer to a TCP server */
/********************************************************************/
void SendIntTcp(int dS,int entier){
int dSend = send(dS,&entier,sizeof(int),0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
return;
}
/********************************************************************/
/* Recive an integer from a TCP server/client */
/********************************************************************/
int ReciveInt(int dS){
int entier;
int dR = recv(dS,&entier,sizeof(int),0);
if(dR == -1){
perror("Error while reciving confirmation : ");
exit(1);
}
return entier;
}
/********************************************************************/
/* Send "message" string to a UDP server with the Hostname/IP and */
/* port of the UDP server */
/********************************************************************/
void SendMessageUDP(int dS,char * server, char * port,char * message,int sizeM){
struct sockaddr_in aD;
aD.sin_family = AF_INET;
inet_pton(AF_INET,server,&(aD.sin_addr));
aD.sin_port = htons(atoi(port));
socklen_t lgA = sizeof(aD);
int dSend;
dSend = sendto(dS,message,sizeM,0,(struct sockaddr*)&aD,lgA);
if(dSend == -1){
perror(ANSI_COLOR_RED "Error while sending message :" ANSI_COLOR_RESET);
exit(1);
}
promptS("Success.");
return;
}
/********************************************************************/
/* Recive one message from a UDP client/server */
/********************************************************************/
int ReciveUDP(int dS,char * buffer){
struct sockaddr_in aE;
socklen_t lg = sizeof(aE);
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int dR = recvfrom(dS,buffer,sizeof(buffer),0,(struct sockaddr*)&aE,&lg);
if(dR == -1){
perror(ANSI_COLOR_RED "Error while sending message :" ANSI_COLOR_RESET);
exit(1);
}
getnameinfo((struct sockaddr*)&aE,lg,hbuf,sizeof(hbuf), sbuf,sizeof(sbuf),0);
printf("Receving message from %s:%s. (size : %d)\n",hbuf,sbuf,dR);
printf("Data : %s\n",buffer);
return dR;
}
/********************************************************************/
/* Recive a message in TCP form a server/client */
/* length is the size of your receving buffer, if set to 0 the size */
/* will be the highest that your socket can recv. */
/********************************************************************/
char * ReciveMessage(int dS,socklen_t length){
if(length == 0){
int dG = getsockopt(dS,SOL_SOCKET,SO_RCVBUF,NULL,&length);
if(dG == -1){
perror("Error while getting socketMAXLENGTH :");
exit(1);
}
}
char * buffer = malloc(length*sizeof(char));
memset(buffer,0,length*sizeof(char));
int dR = recv(dS,buffer,length,0);
if(dR == -1){
perror("Error while reciving confirmation : ");
exit(1);
}
printf("[RECIVING] %d Bytes > %s \n",dR,buffer);
return buffer;
}
/********************************************************************/
/* CURRENTLY IN DEV DO NOT LOOK AT THIS MESS XD */
/********************************************************************/
//Send A file to a server, respecting our protocol
void sendFile(int dS, char *filename){
FILE *handler = fopen(filename, "r");
filename = basename(filename);
char ligne[256];
bzero(ligne,sizeof(ligne));
strcat(strcat(ligne,filename),"|");
//Send the name of the file first...
int dSend = send(dS,ligne,strlen(ligne),0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
bzero(ligne,sizeof(ligne));
if(handler){
int i = 0;
char c = ' ';
fseek(handler, 0, SEEK_END);
//Offset from the first to the last byte, or in other words, filesize
int file_size = ftell(handler); //Get the file size
//go back to the start of the file
rewind(handler);
//CHECK IF OUR FILESIZE IS HIGHER THAN THE BUFFER
if(file_size <=256){
char * buffer = (char*) malloc(sizeof(char) * (file_size + 1) );
int read_size = fread(buffer, sizeof(char), file_size, handler);
if (file_size != read_size){
// Something went wrong, throw away the memory and set the buffer to NULL
free(buffer);
buffer = NULL;
}
else{
strcat(ligne,buffer);
int dSend = send(dS,ligne,strlen(ligne),0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
}
}
//WE SEND 256 BYTES OF THE FILE UNTIL IT'S EMPTY
else {
while((c = fgetc(handler)) != EOF){
if(i == 255){
int dSend = send(dS,ligne,strlen(ligne),0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
printf("[SENDING] %s",ligne);
bzero(ligne,sizeof(ligne));
i = 0;
}
ligne[i] = c;
i++;
}
int dSend = send(dS,ligne,strlen(ligne),0);
if(dSend == -1){
perror("Error while sending message : ");
exit(1);
}
printf("[SENDING] %s",ligne);
}
fclose(handler);
}
}
//IN OUR FTP,THE NAME OF THE FILE IS LOCATED AT THE BEGENNING FOLLOWED BY '|'
char * getFileName(char * buffer){
char * fileName = malloc(256*sizeof(char));
for(int i = 0;i<strlen(buffer);i++){
if(buffer[i] == '|'){
return fileName;
}
fileName[i] = buffer[i];
}
return fileName;
}
//Recive a file respecting our protocol
void Recivefile(int dS){
char buffer[256];
bzero(buffer,sizeof(buffer));
//We know the the first 256 bytes will contain the filename because we are in TCP !
int dR = recv(dS,buffer,256,0);
if(dR == -1){
perror("Error while reciving confirmation : ");
exit(1);
}
char * filename = getFileName(buffer);
char rest[strlen(buffer)];
bzero(rest,sizeof(rest));
int j = 0;
//Getting the rest of the buffer without the name
for(int i = strlen(filename)+1; i<strlen(buffer);i++){
rest[j] = buffer[i];
j++;
}
char path[261];
bzero(path,sizeof(path));
strcat(strcat(path,"./loot/"),filename);
FILE * handler = fopen(path,"w");
bzero(buffer,sizeof(buffer));
strcat(buffer,rest);
if(handler){
while(dR != 0){
printf("[RECIVING] %d Bytes > %s \n",(int)strlen(buffer),buffer);
fprintf(handler,"%s",buffer);
bzero(buffer,sizeof(buffer));
dR = recv(dS,buffer,256,0);
if(dR == -1){
perror("Error while reciving confirmation : ");
exit(1);
}
}
fclose(handler);
}
else{
perror("Error while opening file : ");
exit(1);
}
}