File: probe_unix.c

package info (click to toggle)
mtr 0.95-1.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 900 kB
  • sloc: ansic: 10,325; python: 572; makefile: 164; sh: 141
file content (1045 lines) | stat: -rw-r--r-- 31,352 bytes parent folder | download | duplicates (2)
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/*
    mtr  --  a network diagnostic tool
    Copyright (C) 2016  Matt Kimball

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as
    published by the Free Software Foundation.

    This program 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.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "probe.h"

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_ERROR_H
#include <error.h>
#else
#include "portability/error.h"
#endif
#ifdef HAVE_LINUX_ERRQUEUE_H
#include <linux/errqueue.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include "platform.h"
#include "protocols.h"
#include "sockaddr.h"
#include "construct_unix.h"
#include "deconstruct_unix.h"
#include "timeval.h"

/*  A wrapper around sendto for mixed IPv4 and IPv6 sending  */
static
int send_packet(
    const struct net_state_t *net_state,
    const struct probe_param_t *param,
    int sequence,
    const char *packet,
    int packet_size,
    const struct sockaddr_storage *sockaddr)
{
    struct sockaddr_storage dst;
    int send_socket = 0;
    int sockaddr_length;

    memcpy(&dst, sockaddr, sizeof(struct sockaddr_storage));

    if (sockaddr->ss_family == AF_INET6) {
        sockaddr_length = sizeof(struct sockaddr_in6);

        if (param->protocol == IPPROTO_ICMP) {
            if (net_state->platform.ip6_socket_raw) {
                send_socket = net_state->platform.icmp6_send_socket;
            } else {
                send_socket = net_state->platform.ip6_txrx_icmp_socket;
            }
        } else if (param->protocol == IPPROTO_UDP) {
            if (net_state->platform.ip6_socket_raw) {
                send_socket = net_state->platform.udp6_send_socket;
                /* we got a ipv6 udp raw socket
                 * the remote port is in the payload
                 * we do not set in the sockaddr
                 */
                *sockaddr_port_offset(&dst) = 0;
            } else {
                send_socket = net_state->platform.ip6_txrx_udp_socket;
                if (param->dest_port) {
                    *sockaddr_port_offset(&dst) = htons(param->dest_port);
                } else {
                    *sockaddr_port_offset(&dst) = sequence;
                }
            }
        }
    } else if (sockaddr->ss_family == AF_INET) {
        sockaddr_length = sizeof(struct sockaddr_in);

        if (net_state->platform.ip4_socket_raw) {
            send_socket = net_state->platform.ip4_send_socket;
        } else {
            if (param->protocol == IPPROTO_ICMP) {
                if (param->is_probing_byte_order) {
                    send_socket = net_state->platform.ip4_tmp_icmp_socket;;
                } else {
                    send_socket = net_state->platform.ip4_txrx_icmp_socket;
                }
            } else if (param->protocol == IPPROTO_UDP) {
                send_socket = net_state->platform.ip4_txrx_udp_socket;
                if (param->dest_port) {
                    *sockaddr_port_offset(&dst) = htons(param->dest_port);
                } else {
                    *sockaddr_port_offset(&dst) = sequence;
                }
            }
        }
    }

    if (send_socket == 0) {
        errno = EINVAL;
        return -1;
    }

    return sendto(send_socket, packet, packet_size, 0,
                  (struct sockaddr *) &dst, sockaddr_length);
}

/*
    Nearly all fields in the IP header should be encoded in network byte
    order prior to passing to send().  However, the required byte order of
    the length field of the IP header is inconsistent between operating
    systems and operating system versions.  FreeBSD 11 requires the length
    field in network byte order, but some older versions of FreeBSD
    require host byte order.  OS X requires the length field in host
    byte order.  Linux will accept either byte order.

    Test for a byte order which works by sending a ping to localhost.
*/
static
void check_length_order(
    struct net_state_t *net_state)
{
    char packet[PACKET_BUFFER_SIZE];
    struct probe_param_t param;
    struct probe_t p0 = {.sequence = MIN_PORT };
    ssize_t bytes_sent;
    int packet_size;

#ifdef __linux__
    /*  Linux will accept either byte order and check below fails to work
     *  in some cases due to sendto() returning EPERM. */
    return;
#endif

    memset(&param, 0, sizeof(struct probe_param_t));
    param.ip_version = 4;
    param.protocol = IPPROTO_ICMP;
    param.ttl = 255;
    param.remote_address = "127.0.0.1";
    param.is_probing_byte_order = true;


    if (resolve_probe_addresses(net_state, &param, &p0.remote_addr,
                &p0.local_addr)) {
        fprintf(stderr, "Error decoding localhost address (%s/%s)\n",
                probe_err, strerror (errno));
        exit(EXIT_FAILURE);
    }

    /*  First attempt to ping the localhost with network byte order  */
    net_state->platform.ip_length_host_order = false;

    packet_size = construct_packet(net_state, NULL, &p0,
                                   packet, PACKET_BUFFER_SIZE,
                                   &param);
    if (packet_size < 0) {
      error(EXIT_FAILURE, errno, "Unable to send to localhost");
    }

    bytes_sent =
        send_packet(net_state, &param, MIN_PORT, packet, packet_size,
                    &p0.remote_addr);
    if (bytes_sent > 0) {
        return;
    }

    /*  Since network byte order failed, try host byte order  */
    net_state->platform.ip_length_host_order = true;

    packet_size = construct_packet(net_state, NULL, &p0,
                                   packet, PACKET_BUFFER_SIZE,
                                   &param);
    if (packet_size < 0) {
        error(EXIT_FAILURE, errno, "Unable to send to localhost");
    }

    bytes_sent =
        send_packet(net_state, &param, MIN_PORT, packet, packet_size,
                    &p0.remote_addr);
    if (bytes_sent < 0) {
        error(EXIT_FAILURE, errno, "Unable to send with swapped length");
    }
}

/*
    Check to see if SCTP is support.  We can't just rely on checking
    if IPPROTO_SCTP is defined, because while that is necessary,
    MacOS as of "Sierra" defines IPPROTO_SCTP, but creating an SCTP
    socket results in an error.
*/
static
void check_sctp_support(
    struct net_state_t *net_state)
{
#ifdef IPPROTO_SCTP
    int sctp_socket;

    sctp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    if (sctp_socket != -1) {
        close(sctp_socket);

        net_state->platform.sctp_support = true;
    }
#endif
}

/*  Set a socket to non-blocking mode  */
void set_socket_nonblocking(
    int socket)
{
    int flags;

    flags = fcntl(socket, F_GETFL, 0);
    if (flags == -1) {
        error(EXIT_FAILURE, errno, "Unexpected socket F_GETFL error");
    }

    if (fcntl(socket, F_SETFL, flags | O_NONBLOCK)) {
        error(EXIT_FAILURE, errno, "Unexpected socket F_SETFL O_NONBLOCK error");
    }
}

/*  Open the raw sockets for sending/receiving IPv4 packets  */
static
int open_ip4_sockets_raw(
    struct net_state_t *net_state)
{
    int send_socket;
    int recv_socket;
    int trueopt = 1;

    send_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
    if (send_socket == -1) {
        send_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        if (send_socket == -1) {
            return -1;
        }
    }

    /*
       We will be including the IP header in transmitted packets.
       Linux doesn't require this, but BSD derived network stacks do.
     */
    if (setsockopt
        (send_socket, IPPROTO_IP, IP_HDRINCL, &trueopt, sizeof(int))) {

        close(send_socket);
        return -1;
    }

    /*
       Open a second socket with IPPROTO_ICMP because we are only
       interested in receiving ICMP packets, not all packets.
     */
    recv_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if (recv_socket == -1) {
        close(send_socket);
        return -1;
    }

    net_state->platform.ip4_present = true;
    net_state->platform.ip4_socket_raw = true;
    net_state->platform.ip4_send_socket = send_socket;
    net_state->platform.ip4_recv_socket = recv_socket;

    return 0;
}

#ifdef HAVE_LINUX_ERRQUEUE_H
/*  Open DGRAM sockets for sending/receiving IPv4 packets  */
static
int open_ip4_sockets_dgram(
    struct net_state_t *net_state)
{
    int udp_socket;
    int icmp_socket, icmp_tmp_socket;
#ifdef HAVE_LINUX_ERRQUEUE_H
    int val = 1;
#endif

    icmp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
    if (icmp_socket == -1) {
        return -1;
    }
#ifdef HAVE_LINUX_ERRQUEUE_H
    if (setsockopt(icmp_socket, SOL_IP, IP_RECVERR, &val, sizeof(val)) < 0) {
        return -1;
    }
#endif

    udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (udp_socket == -1) {
        close(icmp_socket);
        return -1;
    }
#ifdef HAVE_LINUX_ERRQUEUE_H
    if (setsockopt(udp_socket, SOL_IP, IP_RECVERR, &val, sizeof(val)) < 0) {
        close(icmp_socket);
        close(udp_socket);
        return -1;
    }
#endif

    icmp_tmp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
    if (icmp_tmp_socket == -1) {
        close(icmp_socket);
        close(udp_socket);
        return -1;
    }

    net_state->platform.ip4_present = true;
    net_state->platform.ip4_socket_raw = false;
    net_state->platform.ip4_txrx_icmp_socket = icmp_socket;
    net_state->platform.ip4_tmp_icmp_socket = icmp_tmp_socket;
    net_state->platform.ip4_txrx_udp_socket = udp_socket;

    return 0;
}
#endif

/*  Open the raw sockets for sending/receiving IPv6 packets  */
static
int open_ip6_sockets_raw(
    struct net_state_t *net_state)
{
    int send_socket_icmp;
    int send_socket_udp;
    int recv_socket;

    send_socket_icmp = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
    if (send_socket_icmp == -1) {
        return -1;
    }

    send_socket_udp = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
    if (send_socket_udp == -1) {
        close(send_socket_icmp);

        return -1;
    }

    recv_socket = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
    if (recv_socket == -1) {
        close(send_socket_icmp);
        close(send_socket_udp);

        return -1;
    }

    net_state->platform.ip6_present = true;
    net_state->platform.ip6_socket_raw = true;
    net_state->platform.icmp6_send_socket = send_socket_icmp;
    net_state->platform.udp6_send_socket = send_socket_udp;
    net_state->platform.ip6_recv_socket = recv_socket;

    return 0;
}

#ifdef HAVE_LINUX_ERRQUEUE_H
/*  Open DGRAM sockets for sending/receiving IPv6 packets  */
static
int open_ip6_sockets_dgram(
    struct net_state_t *net_state)
{
    int icmp_socket;
    int udp_socket;
#ifdef HAVE_LINUX_ERRQUEUE_H
    int val = 1;
#endif

    icmp_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
    if (icmp_socket == -1) {
        return -1;
    }
#ifdef HAVE_LINUX_ERRQUEUE_H
    if (setsockopt(icmp_socket, SOL_IPV6, IPV6_RECVERR, &val, sizeof(val)) < 0) {
        return -1;
    }
#endif

    udp_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (udp_socket == -1) {
        close(icmp_socket);
        return -1;
    }
#ifdef HAVE_LINUX_ERRQUEUE_H
    if (setsockopt(udp_socket, SOL_IPV6, IPV6_RECVERR, &val, sizeof(val)) < 0) {
        close(icmp_socket);
        close(udp_socket);
        return -1;
    }
#endif

    net_state->platform.ip6_present = true;
    net_state->platform.ip6_socket_raw = false;
    net_state->platform.ip6_txrx_icmp_socket = icmp_socket;
    net_state->platform.ip6_txrx_udp_socket = udp_socket;

    return 0;
}
#endif

/*
    The first half of the net state initialization.  Since this
    happens with elevated privileges, this is kept as minimal
    as possible to minimize security risk.
*/
void init_net_state_privileged(
    struct net_state_t *net_state)
{
    int ip4_err = 0;
    int ip6_err = 0;

    memset(net_state, 0, sizeof(struct net_state_t));

    net_state->platform.next_sequence = MIN_PORT;

    if (open_ip4_sockets_raw(net_state)) {
#ifdef HAVE_LINUX_ERRQUEUE_H
        /* fall back to using unprivileged sockets */
        if (open_ip4_sockets_dgram(net_state)) {
            ip4_err = errno;
        }
#endif
    }
    if (open_ip6_sockets_raw(net_state)) {
#ifdef HAVE_LINUX_ERRQUEUE_H
        /* fall back to using unprivileged sockets */
        if (open_ip6_sockets_dgram(net_state)) {
            ip6_err = errno;
        }
#endif
    }

    /*
       If we couldn't open either IPv4 or IPv6 sockets, we can't do
       much, so print errors and exit.
     */
    if (!net_state->platform.ip4_present
        && !net_state->platform.ip6_present) {
        error(0, ip4_err, "Failure to open IPv4 sockets");
        error(0, ip6_err, "Failure to open IPv6 sockets");
        exit(EXIT_FAILURE);
    }
}

/*
    The second half of net state initialization, which is run
    at normal privilege levels.
*/
void init_net_state(
    struct net_state_t *net_state)
{
    if (net_state->platform.ip4_socket_raw) {
        set_socket_nonblocking(net_state->platform.ip4_recv_socket);
    } else {
        set_socket_nonblocking(net_state->platform.ip4_txrx_icmp_socket);
        set_socket_nonblocking(net_state->platform.ip4_txrx_udp_socket);
    }
    if (net_state->platform.ip6_socket_raw) {
        set_socket_nonblocking(net_state->platform.ip6_recv_socket);
    } else {
        set_socket_nonblocking(net_state->platform.ip6_txrx_icmp_socket);
        set_socket_nonblocking(net_state->platform.ip6_txrx_udp_socket);
    }

    if (net_state->platform.ip4_present) {
        check_length_order(net_state);
    }

    check_sctp_support(net_state);
}

/*
    Returns true if we were able to open sockets for a particular
    IP protocol version.
*/
bool is_ip_version_supported(
    struct net_state_t *net_state,
    int ip_version)
{
    if (ip_version == 4) {
        return net_state->platform.ip4_present;
    } else if (ip_version == 6) {
        return net_state->platform.ip6_present;
    } else {
        return false;
    }
}

/*  Returns true if we can transmit probes using the specified protocol  */
bool is_protocol_supported(
    struct net_state_t * net_state,
    int protocol)
{
    if (protocol == IPPROTO_ICMP) {
        return true;
    }

    if (protocol == IPPROTO_UDP) {
        return true;
    }

    if (protocol == IPPROTO_TCP) {
        return true;
    }
#ifdef IPPROTO_SCTP
    if (protocol == IPPROTO_SCTP) {
        return net_state->platform.sctp_support;
    }
#endif

    return false;
}

/*  Report an error during send_probe based on the errno value  */
static
void report_packet_error(
    int command_token)
{
    if (errno == EINVAL) {
        printf("%d invalid-argument\n", command_token);
    } else if (errno == ENETDOWN) {
        printf("%d network-down\n", command_token);
    } else if (errno == ENETUNREACH) {
        printf("%d no-route\n", command_token);
    } else if (errno == EHOSTUNREACH) {
        printf("%d no-route\n", command_token);
    } else if (errno == EPERM) {
        printf("%d permission-denied\n", command_token);
    } else if (errno == EADDRINUSE) {
        printf("%d address-in-use\n", command_token);
    } else if (errno == EADDRNOTAVAIL) {
        printf("%d address-not-available\n", command_token);
    } else {
        printf("%d unexpected-error errno %d\n", command_token, errno);
    }
}

/*  Craft a custom ICMP packet for a network probe.  */
void send_probe(
    struct net_state_t *net_state,
    const struct probe_param_t *param)
{
    char packet[PACKET_BUFFER_SIZE];
    struct probe_t *probe;
    int trytimes;
    int packet_size;

    probe = alloc_probe(net_state, param->command_token);
    if (probe == NULL) {
        printf("%d probes-exhausted\n", param->command_token);
        return;
    }

    if (resolve_probe_addresses(net_state, param, &probe->remote_addr,
                &probe->local_addr)) {
        printf("%d invalid-argument\n", param->command_token);
        free_probe(net_state, probe);
        return;
    }

    if (gettimeofday(&probe->platform.departure_time, NULL)) {
        error(EXIT_FAILURE, errno, "gettimeofday failure");
    }

    // there might be an off-by-one in the number of tries here.
    // this is intentional.  It is no use exhausting the very last
    // open port. Max 10 retries would've been acceptable too I think.
    for (trytimes=MIN_PORT; trytimes < MAX_PORT; trytimes++) {

        packet_size = construct_packet(net_state, &probe->platform.socket,
                         probe, packet, PACKET_BUFFER_SIZE,
                         param);

        if (packet_size > 0) break; // no retry if we succeed.

        if ((param->protocol != IPPROTO_TCP) &&
            (param->protocol != IPPROTO_SCTP)) break; // no retry if not TCP/SCTP

        if ((errno != EADDRINUSE) && (errno != EADDRNOTAVAIL)) {
            break; // no retry
        }

     	probe->sequence = net_state->platform.next_sequence++;

       	if (net_state->platform.next_sequence > MAX_PORT) {
            net_state->platform.next_sequence = MIN_PORT;
        }
    }

    if (packet_size < 0) {
        /*
           When using a stream protocol, FreeBSD will return ECONNREFUSED
           when connecting to localhost if the port doesn't exist,
           even if the socket is non-blocking, so we should be
           prepared for that.
         */
        if (errno == ECONNREFUSED) {
            receive_probe(net_state, probe, ICMP_ECHOREPLY,
                          &probe->remote_addr, NULL, 0, NULL);
        } else {
            report_packet_error(param->command_token);
            free_probe(net_state, probe);
        }

        return;
    }

    if (packet_size > 0) {
        if (send_packet(net_state, param, probe->sequence,
                        packet, packet_size, &probe->remote_addr) == -1) {

            report_packet_error(param->command_token);
            free_probe(net_state, probe);
            return;
        }
    }

    probe->platform.timeout_time = probe->platform.departure_time;
    probe->platform.timeout_time.tv_sec += param->timeout;
}

/*  When allocating a probe, assign it a unique port number  */
void platform_alloc_probe(
    struct net_state_t *net_state,
    struct probe_t *probe)
{
    probe->sequence = net_state->platform.next_sequence++;

    if (net_state->platform.next_sequence > MAX_PORT) {
        net_state->platform.next_sequence = MIN_PORT;
    }
}

/*
    When freeing the probe, close the socket for the probe,
    if one has been opened
*/
void platform_free_probe(
    struct probe_t *probe)
{
    if (probe->platform.socket) {
        close(probe->platform.socket);
        probe->platform.socket = 0;
    }
}

/*
    Compute the round trip time of a just-received probe and pass it
    to the platform agnostic response handling.
*/
void receive_probe(
    struct net_state_t *net_state,
    struct probe_t *probe,
    int icmp_type,
    const struct sockaddr_storage *remote_addr,
    struct timeval *timestamp,
    int mpls_count,
    struct mpls_label_t *mpls)
{
    unsigned int round_trip_us;
    struct timeval *departure_time = &probe->platform.departure_time;
    struct timeval now;

    if (timestamp == NULL) {
        if (gettimeofday(&now, NULL)) {
            error(EXIT_FAILURE, errno, "gettimeofday failure");
        }

        timestamp = &now;
    }

    round_trip_us =
        (timestamp->tv_sec - departure_time->tv_sec) * 1000000 +
        timestamp->tv_usec - departure_time->tv_usec;

    respond_to_probe(net_state, probe, icmp_type,
                     remote_addr, round_trip_us, mpls_count, mpls);
}

/*
    Read all available packets through our receiving raw socket, and
    handle any responses to probes we have previously sent.
*/
static
void receive_replies_from_recv_socket(
    struct net_state_t *net_state,
    int socket,
    received_packet_func_t handle_received_packet)
{
    char packet[PACKET_BUFFER_SIZE];
    int packet_length;
    struct sockaddr_storage remote_addr;
    struct timeval timestamp;
    int flag = 0;
#ifdef HAVE_LINUX_ERRQUEUE_H
    struct cmsghdr *cm;
    struct sock_extended_err *ee = NULL;
    bool icmp_connrefused_received = false;
    bool icmp_hostunreach_received = false;
#endif

    /*  Read until no more packets are available  */
    while (true) {
        struct iovec iov;
        struct msghdr msg;
        char control[1024];

        memset(&msg, 0, sizeof(msg));
        memset(&iov, 0, sizeof(iov));
        iov.iov_base = packet;
        iov.iov_len = sizeof(packet);
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
        msg.msg_name = (struct sockaddr*) &remote_addr;
        msg.msg_namelen = sizeof(remote_addr);
        msg.msg_control = control;
        msg.msg_controllen = sizeof(control);
        packet_length = recvmsg(socket, &msg, flag);

        /*
           Get the time immediately after reading the packet to
           keep the timing as precise as we can.
         */
        if (gettimeofday(&timestamp, NULL)) {
            error(EXIT_FAILURE, errno, "gettimeofday failure");
        }

        if (packet_length == -1) {
            /*
               EAGAIN will be returned if there is no current packet
               available.
             */
            if (errno == EAGAIN) {
                return;
            }

            /*
               EINTER will be returned if we received a signal during
               receive.
             */
            if (errno == EINTR) {
                /* clear error */
                int so_err;
                socklen_t so_err_size = sizeof(so_err);
                int err;

                do {
                  err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &so_err, &so_err_size);
                } while (err < 0 && errno == EINTR);
                continue;
            }

            /* handle error received in error queue */
            if (errno == EHOSTUNREACH) {
                /* potential error caused by ttl, read inner icmp hdr from err queue */
#ifdef HAVE_LINUX_ERRQUEUE_H
                icmp_hostunreach_received = true;
                flag |= MSG_ERRQUEUE;
#endif
                continue;
            }

            if (errno == ECONNREFUSED) {
                /* udp packet reached dst, read inner udp hdr from err queue */
#ifdef HAVE_LINUX_ERRQUEUE_H
                icmp_connrefused_received = true;
                flag |= MSG_ERRQUEUE;
#endif
                continue;
            }

            error(EXIT_FAILURE, errno, "Failure receiving replies");
        }

#ifdef HAVE_LINUX_ERRQUEUE_H
        /* get src ip for packets read from err queue */
        if (flag & MSG_ERRQUEUE) {
            for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
                if (cm->cmsg_level == SOL_IP) {
                    if (cm->cmsg_type == IP_RECVERR) {
                        ee = (struct sock_extended_err *) CMSG_DATA(cm);
                    }
                }
                else if (cm->cmsg_level == SOL_IPV6) {
                    if (cm->cmsg_type == IPV6_RECVERR) {
                        ee = (struct sock_extended_err *) CMSG_DATA(cm);
                    }
                }
            }
            if (ee) {
                memcpy(&remote_addr, SO_EE_OFFENDER(ee), sizeof(remote_addr));
            }
        }

#ifdef SO_PROTOCOL
        if (icmp_connrefused_received) {
            /* using ICMP type ICMP_ECHOREPLY is not a bug, it is an
               indication of successfully reaching dst host.
             */
            handle_error_queue_packet(net_state, &remote_addr, ICMP_ECHOREPLY, IPPROTO_UDP,
                    packet, packet_length, &timestamp);
        } else if (icmp_hostunreach_received) {
            /* handle packet based on send socket protocol */
            int proto, length = sizeof(int);

            if (getsockopt(socket, SOL_SOCKET, SO_PROTOCOL, &proto, &length) < 0) {
                error(EXIT_FAILURE, errno, "getsockopt SO_PROTOCOL error");
            }
            handle_error_queue_packet(net_state, &remote_addr, ICMP_TIME_EXCEEDED, proto,
                    packet, packet_length, &timestamp);
        } else {
#endif
#endif
            /* ICMP packets received from raw socket */
            handle_received_packet(net_state, &remote_addr, packet,
                                   packet_length, &timestamp);
#ifdef HAVE_LINUX_ERRQUEUE_H
#ifdef SO_PROTOCOL
        }
#endif
#endif
    }
}

/*
    Attempt to send using the probe's socket, in order to check whether
    the connection has completed, for stream oriented protocols such as
    TCP.
*/
static
void receive_replies_from_probe_socket(
    struct net_state_t *net_state,
    struct probe_t *probe)
{
    int probe_socket;
    struct timeval zero_time;
    int err;
    int err_length = sizeof(int);
    fd_set write_set;

    probe_socket = probe->platform.socket;
    if (!probe_socket) {
        return;
    }

    FD_ZERO(&write_set);
    FD_SET(probe_socket, &write_set);

    zero_time.tv_sec = 0;
    zero_time.tv_usec = 0;

    if (select(probe_socket + 1, NULL, &write_set, NULL, &zero_time) == -1) {
        if (errno == EAGAIN) {
            return;
        } else {
            error(EXIT_FAILURE, errno, "probe socket select error");
        }
    }

    /*
       If the socket is writable, the connection attempt has completed.
     */
    if (!FD_ISSET(probe_socket, &write_set)) {
        return;
    }

    if (getsockopt(probe_socket, SOL_SOCKET, SO_ERROR, &err, &err_length)) {
        error(EXIT_FAILURE, errno, "probe socket SO_ERROR");
    }

    /*
       If the connection complete successfully, or was refused, we can
       assume our probe arrived at the destination.
     */
    if (!err || err == ECONNREFUSED) {
        receive_probe(net_state, probe, ICMP_ECHOREPLY,
                      &probe->remote_addr, NULL, 0, NULL);
    } else {
        errno = err;
        report_packet_error(probe->token);
        free_probe(net_state, probe);
    }
}

/*  Check both the IPv4 and IPv6 sockets for incoming packets  */
void receive_replies(
    struct net_state_t *net_state)
{
    struct probe_t *probe;
    struct probe_t *probe_safe_iter;

    if (net_state->platform.ip4_present) {
        if (net_state->platform.ip4_socket_raw) {
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip4_recv_socket,
                                             handle_received_ip4_packet);
        } else {
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip4_txrx_icmp_socket,
                                             handle_received_ip4_packet);
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip4_txrx_udp_socket,
                                             handle_received_ip4_packet);
        }
    }

    if (net_state->platform.ip6_present) {
        if (net_state->platform.ip6_socket_raw) {
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip6_recv_socket,
                                             handle_received_ip6_packet);
        } else {
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip6_txrx_icmp_socket,
                                             handle_received_ip6_packet);
            receive_replies_from_recv_socket(net_state,
                                             net_state->platform.
                                             ip6_txrx_udp_socket,
                                             handle_received_ip6_packet);
        }
    }

    LIST_FOREACH_SAFE(probe, &net_state->outstanding_probes,
                      probe_list_entry, probe_safe_iter) {

        receive_replies_from_probe_socket(net_state, probe);
    }
}

/*
    Put all of our probe sockets in the read set used for an upcoming
    select so we can wake when any of them become readable.
*/
int gather_probe_sockets(
    const struct net_state_t *net_state,
    fd_set * write_set)
{
    int probe_socket;
    int nfds;
    const struct probe_t *probe;

    nfds = 0;

    LIST_FOREACH(probe, &net_state->outstanding_probes, probe_list_entry) {
        probe_socket = probe->platform.socket;

        if (probe_socket) {
            FD_SET(probe_socket, write_set);
            if (probe_socket >= nfds) {
                nfds = probe_socket + 1;
            }
        }
    }

    return nfds;
}

/*
    Check for any probes for which we have not received a response
    for some time, and report a time-out, assuming that we won't
    receive a future reply.
*/
void check_probe_timeouts(
    struct net_state_t *net_state)
{
    struct timeval now;
    struct probe_t *probe;
    struct probe_t *probe_safe_iter;

    if (gettimeofday(&now, NULL)) {
        error(EXIT_FAILURE, errno, "gettimeofday failure");
    }

    LIST_FOREACH_SAFE(probe, &net_state->outstanding_probes,
                      probe_list_entry, probe_safe_iter) {

        if (compare_timeval(probe->platform.timeout_time, now) < 0) {
            /*  Report timeout to the command stream  */
            printf("%d no-reply\n", probe->token);

            free_probe(net_state, probe);
        }
    }
}

/*
    Find the remaining time until the next probe times out.
    This may be a negative value if the next probe timeout has
    already elapsed.

    Returns false if no probes are currently outstanding, and true
    if a timeout value for the next probe exists.
*/
bool get_next_probe_timeout(
    const struct net_state_t *net_state,
    struct timeval *timeout)
{
    bool have_timeout;
    const struct probe_t *probe;
    struct timeval now;
    struct timeval probe_timeout;

    if (gettimeofday(&now, NULL)) {
        error(EXIT_FAILURE, errno, "gettimeofday failure");
    }

    have_timeout = false;
    LIST_FOREACH(probe, &net_state->outstanding_probes, probe_list_entry) {
        probe_timeout.tv_sec =
            probe->platform.timeout_time.tv_sec - now.tv_sec;
        probe_timeout.tv_usec =
            probe->platform.timeout_time.tv_usec - now.tv_usec;

        normalize_timeval(&probe_timeout);
        if (have_timeout) {
            if (compare_timeval(probe_timeout, *timeout) < 0) {
                /*  If this probe has a sooner timeout, store it instead  */
                *timeout = probe_timeout;
            }
        } else {
            *timeout = probe_timeout;
            have_timeout = true;
        }
    }

    return have_timeout;
}