-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.c
607 lines (568 loc) · 15.7 KB
/
transport.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
* $Id:$
*
* Copyright (C) 2005 Nils Ohlmeier
*
* This file belongs to sipsak, a free sip testing tool.
*
* sipsak is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* sipsak is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sipsak.h"
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif /* TIME_WITH_SYS_TIME */
#ifdef HAVE_UNISTD_H
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include "transport.h"
#include "shoot.h"
#ifdef RAW_SUPPORT
# ifdef HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
# endif
# ifdef HAVE_NETINET_IP_H
# include <netinet/ip.h>
# endif
# ifdef HAVE_NETINET_IP_ICMP_H
# include <netinet/ip_icmp.h>
# endif
# ifdef HAVE_NETINET_UDP_H
# define __FAVOR_BSD
# include <netinet/udp.h>
# endif
#endif /* RAW_SUPPORT */
#include "exit_code.h"
#include "helper.h"
#include "header_f.h"
#ifdef RAW_SUPPORT
int rawsock;
#endif
void create_sockets(struct sipsak_con_data *cd) {
socklen_t slen;
memset(&(cd->adr), 0, sizeof(struct sockaddr_in));
cd->adr.sin_family = AF_INET;
cd->adr.sin_addr.s_addr = htonl( INADDR_ANY);
cd->adr.sin_port = htons((short)lport);
if (transport == SIP_UDP_TRANSPORT) {
/* create the un-connected socket */
if (!symmetric) {
cd->usock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (cd->usock==-1) {
perror("unconnected UDP socket creation failed");
exit_code(2);
}
if (bind(cd->usock, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr_in) )==-1) {
perror("unconnected UDP socket binding failed");
exit_code(2);
}
}
#ifdef RAW_SUPPORT
/* try to create the raw socket */
rawsock = (int)socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
if (rawsock==-1) {
if (verbose>1)
fprintf(stderr, "warning: need raw socket (root privileges) to receive all ICMP errors\n");
#endif
/* create the connected socket as a primitve alternative to the
raw socket*/
cd->csock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (cd->csock==-1) {
perror("connected UDP socket creation failed");
exit_code(2);
}
if (!symmetric)
cd->adr.sin_port = htons((short)0);
if (bind(cd->csock, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr_in) )==-1) {
perror("connected UDP socket binding failed");
exit_code(2);
}
#ifdef RAW_SUPPORT
}
else if (symmetric) {
cd->csock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (cd->csock==-1) {
perror("connected UDP socket creation failed");
exit_code(2);
}
if (bind(cd->csock, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr_in) )==-1) {
perror("connected UDP socket binding failed");
exit_code(2);
}
}
#endif
}
else {
cd->csock = (int)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (cd->csock==-1) {
perror("TCP socket creation failed");
exit_code(2);
}
if (bind(cd->csock, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr_in) )==-1) {
perror("TCP socket binding failed");
exit_code(2);
}
}
/* for the via line we need our listening port number */
if (lport==0){
memset(&(cd->adr), 0, sizeof(struct sockaddr_in));
slen=sizeof(struct sockaddr_in);
if (symmetric || transport != SIP_UDP_TRANSPORT)
getsockname(cd->csock, (struct sockaddr *) &(cd->adr), &slen);
else
getsockname(cd->usock, (struct sockaddr *) &(cd->adr), &slen);
lport=ntohs(cd->adr.sin_port);
}
}
void send_message(char* mes, struct sipsak_con_data *cd,
struct sipsak_counter *sc, struct sipsak_sr_time *srt) {
struct timezone tz;
int ret, len = strlen(mes), flen = len;
char *tmsg;
if (cd->dontsend == 0) {
if (mes_body)
flen += mes_body->len;
tmsg = str_alloc(flen);
memcpy(tmsg, mes, len);
if (verbose > 2)
printf("\nrequest [%d bytes]:\n%s", flen, mes);
if (mes_body)
{
if (verbose > 2)
printf("<UCS-2BE encoded body>\n");
memcpy(tmsg + len, mes_body->data, mes_body->len);
}
/* lets fire the request to the server and store when we did */
if (cd->csock == -1) {
#ifdef DEBUG
printf("\nusing un-connected socket for sending\n");
#endif
ret = sendto(cd->usock, tmsg, flen, 0, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr));
}
else {
#ifdef DEBUG
printf("\nusing connected socket for sending\n");
#endif
ret = send(cd->csock, tmsg, flen, 0);
}
free(tmsg);
(void)gettimeofday(&(srt->sendtime), &tz);
if (ret==-1) {
if (verbose)
printf("\n");
perror("send failure");
exit_code(2);
}
#ifdef HAVE_INET_NTOP
if (verbose > 2) {
printf("\nsend to: %s:%s:%i [%d bytes]\n", transport_str, target_dot, rport, ret);
}
#endif
sc->send_counter++;
}
else {
cd->dontsend = 0;
}
}
void check_socket_error(int socket, int size) {
struct pollfd sockerr;
int ret = 0;
/* lets see if we at least received an icmp error */
sockerr.fd=socket;
sockerr.events=POLLERR;
ret = poll(&sockerr, 1, 10);
if (ret==1) {
if (sockerr.revents && POLLERR) {
recvfrom(socket, recv, size, 0, NULL, 0);
if (verbose)
printf("\n");
perror("send failure");
if (randtrash == 1)
printf ("last message before send failure:\n%s\n", req);
exit_code(3);
}
}
}
int check_for_message(char *recv, int size, struct sipsak_con_data *cd,
struct sipsak_sr_time *srt, struct sipsak_counter *count,
struct sipsak_delay *sd) {
fd_set fd;
struct timezone tz;
struct timeval tv;
double senddiff;
int ret = 0;
if (cd->dontrecv == 0) {
/* set the timeout and wait for a response */
tv.tv_sec = sd->retryAfter/1000;
tv.tv_usec = (sd->retryAfter % 1000) * 1000;
FD_ZERO(&fd);
if (cd->usock != -1)
FD_SET(cd->usock, &fd);
if (cd->csock != -1)
FD_SET(cd->csock, &fd);
#ifdef RAW_SUPPORT
if (rawsock != -1)
FD_SET(rawsock, &fd);
#endif
ret = select(FD_SETSIZE, &fd, NULL, NULL, &tv);
(void)gettimeofday(&(srt->recvtime), &tz);
}
else {
cd->dontrecv = 0;
}
/* store the time of our first send */
if (count->send_counter==1) {
memcpy(&(srt->firstsendt), &(srt->sendtime), sizeof(struct timeval));
}
if (sd->retryAfter == SIP_T1) {
memcpy(&(srt->starttime), &(srt->sendtime), sizeof(struct timeval));
}
if (ret == 0)
{
/* lets see if we at least received an icmp error */
if (cd->csock == -1)
check_socket_error(cd->usock, size);
else
check_socket_error(cd->csock, size);
/* printout that we did not received anything */
if (trace == 1) {
printf("%i: timeout after %i ms\n", namebeg, sd->retryAfter);
}
else if (usrloc == 1||invite == 1||message == 1) {
printf("timeout after %i ms\n", sd->retryAfter);
}
else if (verbose>0)
printf("** timeout after %i ms**\n", sd->retryAfter);
if (randtrash == 1) {
printf("did not get a response on this request:\n%s\n", req);
if (cseq_counter < nameend) {
if (count->randretrys == 2) {
printf("sended the following message three "
"times without getting a response:\n%s\n"
"give up further retransmissions...\n", req);
exit_code(3);
}
else {
printf("resending it without additional "
"random changes...\n\n");
(count->randretrys)++;
}
}
}
senddiff = deltaT(&(srt->starttime), &(srt->recvtime));
if (senddiff > (float)64 * (float)SIP_T1) {
if (timing == 0) {
if (verbose>0)
printf("*** giving up, no final response after %.3f ms\n", senddiff);
exit_code(3);
}
else {
timing--;
count->run++;
sd->all_delay += senddiff;
sd->big_delay = senddiff;
new_transaction(req);
sd->retryAfter = SIP_T1;
if (timing == 0) {
printf("%.3f/%.3f/%.3f ms\n", sd->small_delay, sd->all_delay / count->run, sd->big_delay);
exit_code(3);
}
}
}
else {
/* set retry time according to RFC3261 */
if ((inv_trans) || (sd->retryAfter *2 < SIP_T2)) {
sd->retryAfter = sd->retryAfter * 2;
}
else {
sd->retryAfter = SIP_T2;
}
}
(count->retrans_s_c)++;
if (srt->delaytime.tv_sec == 0)
memcpy(&(srt->delaytime), &(srt->sendtime), sizeof(struct timeval));
/* if we did not exit until here lets try another send */
return -1;
}
else if ( ret == -1 ) {
perror("select error");
exit_code(2);
}
else if (((cd->usock != -1) && FD_ISSET(cd->usock, &fd)) || ((cd->csock != -1) && FD_ISSET(cd->csock, &fd))) {
if ((cd->usock != -1) && FD_ISSET(cd->usock, &fd))
ret = cd->usock;
else if ((cd->csock != -1) && FD_ISSET(cd->csock, &fd))
ret = cd->csock;
else {
printf("unable to determine the socket which received something\n");
exit_code(2);
}
/* no timeout, no error ... something has happened :-) */
if (trace == 0 && usrloc ==0 && invite == 0 && message == 0 && randtrash == 0 && (verbose > 1))
printf ("\nmessage received");
}
#ifdef RAW_SUPPORT
else if ((rawsock != -1) && FD_ISSET(rawsock, &fd)) {
if (verbose > 1)
printf("\nreceived ICMP message");
ret = rawsock;
}
#endif
else {
printf("\nselect returned succesfuly, nothing received\n");
return -1;
}
return ret;
}
int complete_mes(char *mes, int size) {
int cl = 0, headers = 0, len = 0;
char *tmp = NULL;
cl = get_cl(mes);
#ifdef DEBUG
printf("CL: %i\n", cl);
#endif
if (cl < 0){
if (verbose > 0)
printf("missing CL header; waiting for more bytes...\n");
return 0;
}
tmp = get_body(mes);
#ifdef DEBUG
printf("body: '%s'\n", tmp);
#endif
headers = tmp - mes;
#ifdef DEBUG
printf("length: %i, headers: %i\n", size, headers);
#endif
len = headers + cl;
if (len == size) {
if (verbose > 0)
printf("message is complete\n");
return 1;
}
else if (len > size) {
if (verbose > 0)
printf("waiting for more bytes...\n");
return 0;
}
else {
/* we received more then the sender claims to sent
* for now we treat this as a complete message
* FIXME: should we store the extra bytes in a buffer and
* truncate the message at the calculated length !? */
if (verbose > 0)
printf("received too much bytes...\n");
return 1;
}
}
int recv_message(char *buf, int size, int inv_trans,
struct sipsak_delay *sd, struct sipsak_sr_time *srt,
struct sipsak_counter *count, struct sipsak_con_data *cd,
struct sipsak_regexp *reg) {
int ret = 0;
int sock = 0;
double tmp_delay;
char *ehp;
#ifdef HAVE_INET_NTOP
struct sockaddr_in peer_adr;
socklen_t psize = sizeof(peer_adr);
#endif
#ifdef RAW_SUPPORT
struct sockaddr_in faddr;
struct ip *r_ip_hdr, *s_ip_hdr;
struct icmp *icmp_hdr;
struct udphdr *udp_hdr;
size_t r_ip_len, s_ip_len, icmp_len;
int srcport, dstport;
unsigned int flen;
#endif
if (cd->buf_tmp) {
buf = cd->buf_tmp;
size = size - cd->buf_tmp_size;
}
sock = check_for_message(buf, size, cd, srt, count, sd);
if (sock <= 1) {
return -1;
}
#ifdef RAW_SUPPORT
if (sock != rawsock) {
#else
else {
#endif
check_socket_error(sock, size);
ret = recvfrom(sock, buf, size, 0, NULL, 0);
}
#ifdef RAW_SUPPORT
else {
/* lets check if the ICMP message matches with our
sent packet */
flen = sizeof(faddr);
memset(&faddr, 0, sizeof(struct sockaddr));
ret = recvfrom(rawsock, buf, size, 0, (struct sockaddr *)&faddr, &flen);
if (ret == -1) {
perror("error while trying to read from icmp raw socket");
exit_code(2);
}
r_ip_hdr = (struct ip *) buf;
r_ip_len = r_ip_hdr->ip_hl << 2;
icmp_hdr = (struct icmp *) (buf + r_ip_len);
icmp_len = ret - r_ip_len;
if (icmp_len < 8) {
if (verbose > 1)
printf(": ignoring (ICMP header length below 8 bytes)\n");
return -2;
}
else if (icmp_len < 36) {
if (verbose > 1)
printf(": ignoring (ICMP message too short to contain IP and UDP header)\n");
return -2;
}
s_ip_hdr = (struct ip *) ((char *)icmp_hdr + 8);
s_ip_len = s_ip_hdr->ip_hl << 2;
if (s_ip_hdr->ip_p == IPPROTO_UDP) {
udp_hdr = (struct udphdr *) ((char *)s_ip_hdr + s_ip_len);
srcport = ntohs(udp_hdr->uh_sport);
dstport = ntohs(udp_hdr->uh_dport);
#ifdef DEBUG
printf("\nlport: %i, rport: %i\n", lport, rport);
#endif
if ((srcport == lport) && (dstport == rport)) {
printf(" (type: %u, code: %u)", icmp_hdr->icmp_type, icmp_hdr->icmp_code);
#ifdef HAVE_INET_NTOP
if (inet_ntop(AF_INET, &faddr.sin_addr, &source_dot[0], INET_ADDRSTRLEN) != NULL)
printf(": from %s\n", source_dot);
else
printf("\n");
#else
printf("\n");
#endif // HAVE_INET_NTOP
exit_code(3);
}
else {
if (verbose > 2)
printf(": ignoring (ICMP message does not match used ports)\n");
return -2;
}
}
else {
if (verbose > 1)
printf(": ignoring (ICMP data is not a UDP packet)\n");
return -2;
}
}
#endif // RAW_SUPPORT
if (ret > 0) {
*(buf+ ret) = '\0';
// Cut off binary message body
if (mes_body && (ehp = strstr(buf, "\r\n\r\n")))
ehp[4] = 0;
if (transport != SIP_UDP_TRANSPORT) {
if (verbose > 0)
printf("\nchecking message for completness...\n");
if (complete_mes(buf, ret) == 1) {
cd->buf_tmp = NULL;
ret += cd->buf_tmp_size;
cd->buf_tmp_size = 0;
}
else {
if (cd->buf_tmp) {
cd->buf_tmp += ret;
cd->buf_tmp_size += ret;
}
else {
cd->buf_tmp = buf + ret;
cd->buf_tmp_size = ret;
}
cd->dontsend = 1;
ret = -1;
}
}
/* store the biggest delay if one occured */
if (srt->delaytime.tv_sec != 0) {
tmp_delay = deltaT(&(srt->delaytime), &(srt->recvtime));
if (tmp_delay > sd->big_delay)
sd->big_delay = tmp_delay;
if ((sd->small_delay == 0) || (tmp_delay < sd->small_delay))
sd->small_delay = tmp_delay;
srt->delaytime.tv_sec = 0;
srt->delaytime.tv_usec = 0;
}
if (timing > 0) {
tmp_delay = deltaT(&(srt->sendtime), &(srt->recvtime));
if (tmp_delay > sd->big_delay)
sd->big_delay = tmp_delay;
if ((sd->small_delay == 0) || (tmp_delay < sd->small_delay))
sd->small_delay = tmp_delay;
sd->all_delay += tmp_delay;
}
#ifdef HAVE_INET_NTOP
if ((verbose > 2) && (getpeername(sock, (struct sockaddr *)&peer_adr, &psize) == 0) && (inet_ntop(peer_adr.sin_family, &peer_adr.sin_addr, &source_dot[0], INET_ADDRSTRLEN) != NULL)) {
printf("\nreceived from: %s:%s:%i [%d bytes]\n", transport_str,
source_dot, ntohs(peer_adr.sin_port), ret);
}
else if (verbose > 1 && trace == 0 && usrloc == 0)
printf(":\n");
#else
if (trace == 0 && usrloc == 0)
printf(":\n");
#endif // HAVE_INET_NTOP
if (!inv_trans && ret > 0 && (regexec(&(reg->proexp), buf, 0, 0, 0) != REG_NOERROR)) {
sd->retryAfter = SIP_T1;
}
}
else {
check_socket_error(sock, size);
printf("nothing received, select returned error\n");
exit_code(2);
}
return ret;
}
/* clears the given sockaddr, fills it with the given data and if a
* socket is given connects the socket to the new target */
int set_target(struct sockaddr_in *adr, unsigned long target, int port, int socket, int connected) {
if (socket != -1 && transport != SIP_UDP_TRANSPORT && connected) {
if (shutdown(socket, SHUT_RDWR) != 0) {
perror("error while shutting down socket");
}
}
memset(adr, 0, sizeof(struct sockaddr_in));
adr->sin_addr.s_addr = target;
adr->sin_port = htons((short)port);
adr->sin_family = AF_INET;
#ifdef HAVE_INET_NTOP
inet_ntop(adr->sin_family, &adr->sin_addr, &target_dot[0], INET_ADDRSTRLEN);
#endif
if (socket != -1) {
if (connect(socket, (struct sockaddr *)adr, sizeof(struct sockaddr_in)) == -1) {
perror("connecting socket failed");
exit_code(2);
}
}
return 1;
}