• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

schweikert / fping / 14822772522

04 May 2025 03:42PM UTC coverage: 86.609% (-1.3%) from 87.902%
14822772522

Pull #391

github

web-flow
Merge 0032fbfd9 into 913982a8c
Pull Request #391: fix ICMPv6 error message handling and reporting

7 of 41 new or added lines in 2 files covered. (17.07%)

4 existing lines in 1 file now uncovered.

1397 of 1613 relevant lines covered (86.61%)

325.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/socket6.c
1
/*
2
 * fping: fast-ping, file-ping, favorite-ping, funky-ping
3
 *
4
 *   Ping a list of target hosts in a round robin fashion.
5
 *   A better ping overall.
6
 *
7
 * fping website:  http://www.fping.org
8
 *
9
 * Current maintainer of fping: David Schweikert
10
 * Please send suggestions and patches to: david@schweikert.ch
11
 *
12
 *
13
 * Original author:  Roland Schemers  <schemers@stanford.edu>
14
 * IPv6 Support:     Jeroen Massar    <jeroen@unfix.org / jeroen@ipng.nl>
15
 * Improved main loop: David Schweikert <david@schweikert.ch>
16
 * Debian Merge, TOS settings: Tobi Oetiker <tobi@oetiker.ch>
17
 * Bugfixes, byte order & senseful seq.-numbers: Stephan Fuhrmann (stephan.fuhrmann AT 1und1.de)
18
 *
19
 *
20
 * Redistribution and use in source and binary forms are permitted
21
 * provided that the above copyright notice and this paragraph are
22
 * duplicated in all such forms and that any documentation,
23
 * advertising materials, and other materials related to such
24
 * distribution and use acknowledge that the software was developed
25
 * by Stanford University.  The name of the University may not be used
26
 * to endorse or promote products derived from this software without
27
 * specific prior written permission.
28
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31
 */
32

33
#include "config.h"
34
#include "fping.h"
35

36
#include <fcntl.h>
37
#include <netdb.h>
38
#include <netinet/in.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <sys/socket.h>
43

44
#include <netinet/icmp6.h>
45

46
char* ping_buffer_ipv6 = 0;
47
size_t ping_pkt_size_ipv6;
48

49
int open_ping_socket_ipv6(int *socktype)
611✔
50
{
51
    struct protoent* proto;
52
    int s;
53

54
    /* confirm that ICMP6 is available on this machine */
55
    if ((proto = getprotobyname("ipv6-icmp")) == NULL)
611✔
56
        crash_and_burn("ipv6-icmp: unknown protocol");
×
57

58
    /* create raw socket for ICMP6 calls (ping) */
59
    *socktype = SOCK_RAW;
611✔
60
    s = socket(AF_INET6, *socktype, proto->p_proto);
611✔
61
    if (s < 0) {
611✔
62
        /* try non-privileged icmp6 (works on Mac OSX without privileges, for example) */
63
        *socktype = SOCK_DGRAM;
10✔
64
        s = socket(AF_INET6, *socktype, proto->p_proto);
10✔
65
        if (s < 0) {
10✔
66
            return -1;
×
67
        }
68
    } else {
69
        /* receive only ICMP6 messages relevant for fping on raw socket */
70
        struct icmp6_filter recv_filter;
71

72
        ICMP6_FILTER_SETBLOCKALL(&recv_filter);
601✔
73
        ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &recv_filter);
601✔
74
        ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &recv_filter);
601✔
75
        ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &recv_filter);
601✔
76
        ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &recv_filter);
601✔
77
        ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &recv_filter);
601✔
78

79
        if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &recv_filter, sizeof(recv_filter))) {
601✔
NEW
80
            errno_crash_and_burn("cannot set icmp6 message type filter");
×
81
        }
82
    }
83

84
    /* Make sure that we use non-blocking IO */
85
    {
86
        int flags;
87

88
        if ((flags = fcntl(s, F_GETFL, 0)) < 0)
611✔
89
            perror("fcntl");
×
90

91
        if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
611✔
92
            perror("fcntl");
×
93
    }
94

95
    return s;
611✔
96
}
161✔
97

98
void init_ping_buffer_ipv6(size_t ping_data_size)
309✔
99
{
100
    /* allocate ping buffer */
101
    ping_pkt_size_ipv6 = ping_data_size + sizeof(struct icmp6_hdr);
309✔
102
    ping_buffer_ipv6 = (char*)calloc(1, ping_pkt_size_ipv6);
309✔
103
    if (!ping_buffer_ipv6)
309✔
104
        crash_and_burn("can't malloc ping packet");
×
105
}
309✔
106

107
void socket_set_src_addr_ipv6(int s, struct in6_addr* src_addr, int *ident)
14✔
108
{
109
    struct sockaddr_in6 sa;
110
    socklen_t len = sizeof(sa);
14✔
111

112
    memset(&sa, 0, sizeof(sa));
14✔
113
    sa.sin6_family = AF_INET6;
14✔
114
    sa.sin6_addr = *src_addr;
14✔
115
    if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0)
14✔
116
        errno_crash_and_burn("cannot bind source address");
2✔
117

118
    if (ident) {
12✔
119
        memset(&sa, 0, len);
10✔
120
        if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
10✔
121
            errno_crash_and_burn("can't get ICMP6 socket identity");
×
122

123
        if (sa.sin6_port)
10✔
124
            *ident = sa.sin6_port;
10✔
125
    }
126
}
12✔
127

128
int socket_sendto_ping_ipv6(int s, struct sockaddr* saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr)
123✔
129
{
130
    struct icmp6_hdr* icp;
131
    int n;
132

133
    icp = (struct icmp6_hdr*)ping_buffer_ipv6;
123✔
134
    icp->icmp6_type = ICMP6_ECHO_REQUEST;
123✔
135
    icp->icmp6_code = 0;
123✔
136
    icp->icmp6_seq = htons(icmp_seq_nr);
123✔
137
    icp->icmp6_id = icmp_id_nr;
123✔
138

139
    if (random_data_flag) {
123✔
140
        for (n = sizeof(struct icmp6_hdr); n < ping_pkt_size_ipv6; ++n) {
342✔
141
            ping_buffer_ipv6[n] = random() & 0xFF;
336✔
142
        }
143
    }
144

145
    icp->icmp6_cksum = 0; /* The IPv6 stack calculates the checksum for us... */
123✔
146

147
    n = sendto(s, icp, ping_pkt_size_ipv6, 0, saddr, saddr_len);
123✔
148

149
    return n;
123✔
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc