|
@@ -17,14 +17,17 @@
|
17
|
17
|
#include <stdio.h>
|
18
|
18
|
/* pour la gestion des erreurs */
|
19
|
19
|
#include <errno.h>
|
20
|
|
-#define QUEUE_SIZE 5
|
|
20
|
+#define QUEUE_SIZE 5 // Taille de la file d'attente du listen()
|
21
|
21
|
|
|
22
|
+// Remplit le message de lg caractères motif
|
22
|
23
|
void construire_message(char* message, char motif, int lg, int numero_envoi);
|
23
|
24
|
|
24
|
25
|
// Affichage distinct entre envoi et reception pour une facilité d'utilisation
|
25
|
26
|
void afficher_message_envoi(char *message, int lg, int numero_envoi);
|
26
|
27
|
void afficher_message_reception(char *message, int lg, int numero_envoi);
|
27
|
28
|
|
|
29
|
+// Affiche l'usage de l'outil
|
|
30
|
+void print_usage(char* arg0);
|
28
|
31
|
|
29
|
32
|
int main (int argc, char **argv)
|
30
|
33
|
{
|
|
@@ -44,7 +47,7 @@ int main (int argc, char **argv)
|
44
|
47
|
switch (c) {
|
45
|
48
|
case 'p':
|
46
|
49
|
if (source == 1) {
|
47
|
|
- printf("usage: cmd [-p|-s][-n ##]\n");
|
|
50
|
+ print_usage(argv[0]);
|
48
|
51
|
exit(1);
|
49
|
52
|
}
|
50
|
53
|
source = 0;
|
|
@@ -52,7 +55,7 @@ int main (int argc, char **argv)
|
52
|
55
|
|
53
|
56
|
case 's':
|
54
|
57
|
if (source == 0) {
|
55
|
|
- printf("usage: cmd [-p|-s][-n ##]\n");
|
|
58
|
+ print_usage(argv[0]);
|
56
|
59
|
exit(1) ;
|
57
|
60
|
}
|
58
|
61
|
source = 1;
|
|
@@ -62,11 +65,10 @@ int main (int argc, char **argv)
|
62
|
65
|
nb_message = atoi(optarg);
|
63
|
66
|
// Packets are numerotated with 5 figures, which means the number of
|
64
|
67
|
// packets send can't be bigger than (or equal) 10 ** 6
|
65
|
|
- if(nb_message >= 100000) {
|
66
|
|
- printf("Too many packets");
|
67
|
|
- exit(1);
|
|
68
|
+ if(nb_message >= 100000 || nb_message <= 0) {
|
|
69
|
+ print_usage(argv[0]);
|
|
70
|
+ exit(1);
|
68
|
71
|
}
|
69
|
|
-
|
70
|
72
|
break;
|
71
|
73
|
|
72
|
74
|
case 'u':
|
|
@@ -79,77 +81,85 @@ int main (int argc, char **argv)
|
79
|
81
|
// We provide an MTU of 1400 bytes.
|
80
|
82
|
// We also to send at least a message of 5 characters length for indice packet
|
81
|
83
|
if(lg >= 1400 || lg < 5) {
|
82
|
|
- printf("Messages too long");
|
|
84
|
+ print_usage(argv[0]);
|
83
|
85
|
exit(1);
|
84
|
86
|
}
|
85
|
87
|
break;
|
86
|
88
|
/* -c and -e options stand only for TCP exchange */
|
87
|
89
|
case 'C':
|
88
|
90
|
client=1;
|
89
|
|
- if (udp == 1){
|
90
|
|
- printf("No option -C for UDP");
|
|
91
|
+ if (udp || serveur) {
|
|
92
|
+ print_usage(argv[0]);
|
91
|
93
|
exit(0);
|
92
|
94
|
}
|
93
|
95
|
break;
|
94
|
96
|
case 'S':
|
95
|
97
|
serveur = 1;
|
96
|
|
- if (udp == 1){
|
97
|
|
- printf("No option -S for UDP");
|
|
98
|
+ if (udp || client) {
|
|
99
|
+ print_usage(argv[0]);
|
98
|
100
|
exit(0);
|
99
|
101
|
}
|
100
|
102
|
break;
|
101
|
103
|
case 'e':
|
102
|
104
|
emetteur=1;
|
103
|
|
- if (udp == 1 || recepteur == 1){
|
104
|
|
- printf("Missuse of -e option");
|
|
105
|
+ if (udp || recepteur) {
|
|
106
|
+ print_usage(argv[0]);
|
105
|
107
|
exit(0);
|
106
|
108
|
}
|
107
|
109
|
break;
|
108
|
110
|
case 'r':
|
109
|
111
|
recepteur=1;
|
110
|
|
- if (udp == 1 || emetteur == 1){
|
111
|
|
- printf("Missuse of -r option");
|
112
|
|
- exit(0);
|
|
112
|
+ if (udp || emetteur) {
|
|
113
|
+ print_usage(argv[0]);
|
|
114
|
+ exit(0);
|
113
|
115
|
}
|
114
|
116
|
break;
|
115
|
117
|
default:
|
116
|
|
- printf("usage: cmd [-p|-s][-n ##]\n");
|
|
118
|
+ print_usage(argv[0]);
|
117
|
119
|
break;
|
118
|
120
|
}
|
119
|
121
|
}
|
120
|
122
|
|
121
|
|
- if (!recepteur && !emetteur && !udp) {
|
122
|
|
- printf("Need to specify sender or receiver for TCP connexion");
|
123
|
|
- exit(0);
|
|
123
|
+ // Little verification of TCP arguments.
|
|
124
|
+ // Machine must be a server or a client as once,
|
|
125
|
+ // as well as a sender or receiver.
|
|
126
|
+ if (!udp) {
|
|
127
|
+ if ((!recepteur && !emetteur) || (!serveur && !client)) {
|
|
128
|
+ print_usage(argv[0]);
|
|
129
|
+ exit(0);
|
|
130
|
+ }
|
124
|
131
|
}
|
125
|
132
|
|
|
133
|
+ // Variable qui va stocker le nom logique de la machine avec qui on communique
|
126
|
134
|
char* nom_machine_distante;
|
127
|
135
|
|
|
136
|
+ //Allocation de la variable stockant un message, il est de la taille donnée en paramètre
|
|
137
|
+ char* message = malloc(lg * sizeof(char));
|
|
138
|
+
|
128
|
139
|
// Recuperation du port
|
129
|
140
|
int port=atoi(argv[argc-1]);
|
130
|
|
- //port = htons(port);
|
131
|
141
|
|
132
|
142
|
// Default set of packet length : 30 bytes
|
133
|
143
|
if (lg == -1) {
|
134
|
144
|
lg=30;
|
135
|
145
|
}
|
136
|
146
|
|
137
|
|
- //Allocation du message, il est de la taille donnée en paramètre
|
138
|
|
- char* message = malloc(lg * sizeof(char));
|
139
|
|
-
|
140
|
|
-
|
141
|
|
- // If number of messages is not fixed for the source, it is set to 10 messages
|
142
|
|
- if (nb_message == -1 && source ) {
|
|
147
|
+ // If number of messages is not fixed for the source, it is set to 10 messages
|
|
148
|
+ if (nb_message == -1 && (source || !udp)) {
|
143
|
149
|
nb_message = 10 ;
|
144
|
150
|
}
|
145
|
151
|
|
146
|
152
|
|
147
|
153
|
//Affichage des informations de communication initiée
|
148
|
|
- if (source || client) {
|
|
154
|
+ if (source || emetteur) {
|
149
|
155
|
//Recuperation du nom logique
|
150
|
156
|
nom_machine_distante=argv[argc-2];
|
151
|
|
- printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n",
|
152
|
|
- lg,port,nb_message,(udp)?"UDP":"TCP",nom_machine_distante);
|
|
157
|
+ printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s",
|
|
158
|
+ lg,port,nb_message,(udp)?"UDP":"TCP");
|
|
159
|
+ if (client) {
|
|
160
|
+ printf(",dest=%s",nom_machine_distante);
|
|
161
|
+ }
|
|
162
|
+ printf("\n");
|
153
|
163
|
} else {
|
154
|
164
|
printf("PUITS:lg_mesg-lu=%d,port=%d,nb_receptions=",lg,port);
|
155
|
165
|
if (nb_message!=-1 && !source) {
|
|
@@ -278,7 +288,7 @@ int main (int argc, char **argv)
|
278
|
288
|
}
|
279
|
289
|
|
280
|
290
|
|
281
|
|
- // Envoi des messages
|
|
291
|
+ // Envoi ou reception des messages
|
282
|
292
|
for (int i = 0; i < nb_message; i++) {
|
283
|
293
|
|
284
|
294
|
if (emetteur){
|
|
@@ -296,19 +306,12 @@ int main (int argc, char **argv)
|
296
|
306
|
}
|
297
|
307
|
}
|
298
|
308
|
|
299
|
|
-
|
300
|
|
-
|
301
|
|
-
|
302
|
309
|
// Close socket to avoid dangling connections
|
303
|
310
|
close(sock);
|
304
|
|
-
|
305
|
|
-
|
306
|
311
|
}
|
307
|
312
|
|
308
|
313
|
// The server shall accept the connexion
|
309
|
314
|
if (serveur) {
|
310
|
|
-
|
311
|
|
-
|
312
|
315
|
// Creation de l'adresse du socket local
|
313
|
316
|
struct sockaddr_in adr_locale;
|
314
|
317
|
socklen_t longueur_adr_locale = sizeof(adr_locale);
|
|
@@ -327,7 +330,7 @@ int main (int argc, char **argv)
|
327
|
330
|
exit(1);
|
328
|
331
|
}
|
329
|
332
|
|
330
|
|
- // set listening queue size
|
|
333
|
+ // Set listening queue size
|
331
|
334
|
if (listen(sock, QUEUE_SIZE) == -1) {
|
332
|
335
|
printf("échec et mat listen\n");
|
333
|
336
|
exit(1);
|
|
@@ -341,17 +344,17 @@ int main (int argc, char **argv)
|
341
|
344
|
printf("échec du accept\n") ;
|
342
|
345
|
exit(1) ;
|
343
|
346
|
}
|
344
|
|
- printf("socket accepte:%d\n",sock_bis);
|
345
|
|
-
|
346
|
347
|
|
|
348
|
+ // On crée un processus pour chaque connexion TCP acceptée
|
347
|
349
|
switch (fork() ) {
|
|
350
|
+
|
348
|
351
|
case - 1 : /* il y a une erreur */
|
349
|
352
|
printf("erreur fork\n") ; exit(1) ;
|
|
353
|
+
|
350
|
354
|
case 0 : /* on est dans le proc. fils */
|
351
|
355
|
close(sock) ; /* fermeture socket du proc. père */
|
352
|
356
|
for (int i=0 ; i < nb_message ; i ++) {
|
353
|
|
-
|
354
|
|
- // Server send packets
|
|
357
|
+ // Server sends packets with -e
|
355
|
358
|
if (emetteur){
|
356
|
359
|
// Construction du message
|
357
|
360
|
construire_message(message, 'a' + (i % 26), lg, i+1);
|
|
@@ -359,7 +362,7 @@ int main (int argc, char **argv)
|
359
|
362
|
// Envoi du message
|
360
|
363
|
write(sock_bis,message,longueur_message);
|
361
|
364
|
}
|
362
|
|
- // Server receives packets
|
|
365
|
+ // Server receives packets with -r
|
363
|
366
|
if (recepteur) {
|
364
|
367
|
if ((read(sock_bis, message, longueur_message)) < 0){
|
365
|
368
|
printf("échec du read\n") ; exit(1) ;}
|
|
@@ -406,4 +409,17 @@ void afficher_message_reception(char *message, int lg, int numero_envoi) {
|
406
|
409
|
|
407
|
410
|
}
|
408
|
411
|
|
|
412
|
+void print_usage(char* arg0) {
|
|
413
|
+ printf("usage: %s [-psuCSer] [-n nb_messages] [-l mess_length] [host] <port>\n", arg0);
|
|
414
|
+ printf("parameters: host With -s or -C, address of the host to connect to. Required with -s or -C.\n");
|
|
415
|
+ printf(" port Port to connect or bind to. Required.\n");
|
|
416
|
+ printf("options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30.\n");
|
|
417
|
+ printf(" -n nb_messages Number of messages to send. Min 1. Default 10. Ignored with -p.\n");
|
|
418
|
+ printf(" -p Runs a TCP/UDP sink. Incompatible with -s.\n");
|
|
419
|
+ printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n");
|
|
420
|
+ printf(" -C Client of a TCP connexion. Incompatible with -S and -u. Requires -e or -r.\n");
|
|
421
|
+ printf(" -S Server of a TCP connexion. Incompatible with -C and -u. Requires -e or -r.\n");
|
|
422
|
+ printf(" -e Packets issuer of a TCP connexion. Incompatible with -r and -u.\n");
|
|
423
|
+ printf(" -r Packets receiver of a TCP connexion. Incompatible with -e and -u.\n");
|
409
|
424
|
|
|
425
|
+}
|