Bonjour à tous !

Dans le cadre d'un projet, j'ai le besoin de réaliser un programme qui enverrai un certain nombre de segment UDP par seconde. (débit)
Cette phase du programme fonctionne correctement. Le soucis réside dans la reception de la réponse. La fonction recvfrom() me renvoi constement la valeur -1.

La réponse est le retour de mes segments UDP envoyé en direction d'un port UDP bien en écoute je précise.

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <sys/socket.h>
 
#include <netinet/in.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <signal.h>
 
#include <arpa/inet.h>
 
#include <netdb.h>
 
#include <unistd.h>
 
#include <math.h>
 
#include <time.h>
 
 
 
 
 
 
 
#define PORT 443
 
#define TAILLE 1400
 
 
 
 
 
 
 
void warning(char* message)//***Avertissement d'une erreur
 
{
 
    printf("Erreur %s\n",message);
 
    perror("Message d'erreur: ");
 
  exit(1);
 
}
 
 
 
int torpille_udp (int broadcast)//***Créer un socket UDP
 
{
 
  int sock;
 
  if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
 
    warning ("création torpille");
 
    return (-1);
 
  }
 
  setsockopt(sock,SOL_SOCKET,SO_BROADCAST, &broadcast,sizeof(broadcast));
 
  return (sock);
 
}
 
 
 
void help()//***Aide à l'utilisation
 
{
 
  printf("Flood [-a destination] [-p port] [-d debit] [-h] [destination]\n");
 
  printf("Programme permettant le flood d'un équipement par l'envoi massif de segments UDP\n");
 
  exit(2);
 
}
 
 
 
 
 
int main(int argc,char** argv)
 
{
 
  int torpille, j = 0, i = 0, octetSend = 0, octetRecv = 0;
 
double debit = 0;
 
  struct sockaddr_in adresse, source, msgSource;
 
  struct hostent *   hostent  = NULL;
 
  struct protoent *  protoent = NULL;
 
 
 
 
 
  char *hote = NULL, msg[500] = {0};
 
  int prt = PORT;
 
  int option;
 
  while ((option = getopt(argc,argv,"d:p:a:h")) != -1) {
 
    switch(option) {
 
    case 'a':
 
      hote = optarg;
 
      break;
 
    case 'p':
 
      sscanf(optarg,"%d",&prt);
 
      break;
 
	case 'd':
 
	sscanf(optarg,"%lf",&debit);
 
	j = (int)(debit / TAILLE);	
 
	break;
 
    case 'h':
 
    case '?':
 
    default:
 
      help();
 
      break;
 
    }
 
  }
 
 
 
  if ((hote == NULL) && (optind < argc)) hote = argv[optind];
 
  if (hote == NULL) help();
 
  if (debit == 0) help();
 
 
 
  torpille = torpille_udp(1);
 
 
 
  if (hote != NULL)
 
    if ((hostent = gethostbyname (hote)) == NULL) {
 
      warning ("gethostbyname");
 
      return (-1);
 
    }
 
 
 
  if ((protoent = getprotobyname ("udp")) == NULL) {
 
    warning ("getprotobyname");
 
    return (-1);
 
  }
 
 
 
  memset (&adresse, 0, sizeof (struct sockaddr_in));
 
	memset (&source, 0, sizeof (struct sockaddr_in));
 
	memset (&msgSource, 0, sizeof (struct sockaddr_in));
 
 
 
  adresse.sin_family = AF_INET;
 
  if (prt != 0)
 
	adresse.sin_port = htons(prt);
 
  else
 
	adresse.sin_port = htons (PORT);
 
 
 
  if (hostent != NULL)
 
    adresse.sin_addr . s_addr = ((struct in_addr *) (hostent -> h_addr)) -> s_addr;
 
  else 
 
	warning("pas de destinataire");
 
 
 
  source.sin_family = AF_INET;
 
  source.sin_port = htons(40500);
 
  source.sin_addr . s_addr = INADDR_ANY;
 
 
 
  if (bind(torpille, (struct sockaddr*)&source, sizeof(source)) < 0)
 
	warning("Bind");
 
 
 
printf("***************************************\n");
 
printf("*              Flood UDP              *\n");
 
printf("***************************************\n\n");
 
printf("Envoi de segments UDP en direction de %s:%d ...\n\n", hote, prt);
 
 
 
while (1) 
 
{
 
	octetSend = 0;
 
	octetRecv = 0;
 
	for (i = 0; i < j; i++)
 
	{	
 
	octetSend += sendto(torpille,"Torpillage",TAILLE,0,(struct sockaddr *) &adresse,sizeof (struct sockaddr_in));
 
	}
 
 
 
	if ((int)(octetSend / 1000000) >= 1)
 
	{
 
		printf("Debit envoye : %.3lf Mbps (Total segments : %d)\n", (double)octetSend/1000000, j);
 
	}
 
	else if ((int)(octetSend / 1000) >= 1)
 
	{
 
		printf("Debit envoye : %.3lf Kbps (Total segments : %d)\n", (double)octetSend/1000, j);
 
	}
 
	else
 
		printf("Debit envoye : %d bps (Total segments : %d)\n", octetSend, j);
 
 
 
	usleep(1000000);
 
	while ((i = recvfrom(torpille, msg, 50, 0, (struct sockaddr *) &msgSource, (int*) sizeof(msgSource))) > 0)
 
	{
 
		printf("Nombre d'octets capturés : %d", i);
 
	}   
 
	printf("valeur de i = %d\n", i);
 
}
 
  close(torpille);
 
  exit(0);
 
}