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

schweikert / fping / 24923853954

25 Apr 2026 05:42AM UTC coverage: 88.0% (-0.09%) from 88.091%
24923853954

push

github

gsnw-sebast
Fixes the indentation of the blocks

5 of 5 new or added lines in 1 file covered. (100.0%)

151 existing lines in 3 files now uncovered.

1672 of 1900 relevant lines covered (88.0%)

344.84 hits per line

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

87.34
/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
#include "flags.h"
36

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

46
#include <netinet/icmp6.h>
47

48
char* ping_buffer_ipv6 = 0;
49
size_t ping_pkt_size_ipv6;
50

51
/* Interface index for outgoing packets (0 = not set, use routing table) */
52
static int outgoing_iface_idx_ipv6 = 0;
53

54
/* Source address for outgoing packets (used to populate ipi6_addr) */
55
static struct in6_addr outgoing_src_addr_ipv6;
56
static int outgoing_src_addr_set_ipv6 = 0;
57

58
int open_ping_socket_ipv6(int *socktype)
881✔
59
{
60
    struct protoent* proto;
61
    int s;
62

63
    /* confirm that ICMP6 is available on this machine */
64
    if ((proto = getprotobyname("ipv6-icmp")) == NULL)
881✔
UNCOV
65
        crash_and_burn("ipv6-icmp: unknown protocol");
×
66

67
    /* create raw socket for ICMP6 calls (ping) */
68
    *socktype = SOCK_RAW;
881✔
69
    s = socket(AF_INET6, *socktype, proto->p_proto);
881✔
70
    if (s < 0) {
881✔
71
        /* try non-privileged icmp6 (works on Mac OSX without privileges, for example) */
72
        *socktype = SOCK_DGRAM;
12✔
73
        s = socket(AF_INET6, *socktype, proto->p_proto);
12✔
74
        if (s < 0) {
12✔
UNCOV
75
            return -1;
×
76
        }
77
    } else {
78
        /* receive only ICMP6 messages relevant for fping on raw socket */
79
        struct icmp6_filter recv_filter;
80

81
        ICMP6_FILTER_SETBLOCKALL(&recv_filter);
869✔
82
        ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &recv_filter);
869✔
83
        ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &recv_filter);
869✔
84
        ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &recv_filter);
869✔
85
        ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &recv_filter);
869✔
86
        ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &recv_filter);
869✔
87

88
        if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &recv_filter, sizeof(recv_filter))) {
869✔
UNCOV
89
            errno_crash_and_burn("cannot set icmp6 message type filter");
×
90
        }
91
    }
92

93
    /* Make sure that we use non-blocking IO */
94
    {
95
        int flags;
96

97
        if ((flags = fcntl(s, F_GETFL, 0)) < 0)
881✔
UNCOV
98
            perror("fcntl");
×
99

100
        if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
881✔
UNCOV
101
            perror("fcntl");
×
102
    }
103

104
    return s;
881✔
105
}
239✔
106

107
void socket_set_outgoing_iface_ipv6(const char *iface_name)
4✔
108
{
109
    unsigned int idx = if_nametoindex(iface_name);
4✔
110
    if (idx == 0) {
4✔
UNCOV
111
        fprintf(stderr, "fping: unknown interface '%s'\n", iface_name);
×
UNCOV
112
        exit(1);
×
113
    }
114
    outgoing_iface_idx_ipv6 = (int)idx;
4✔
115
}
4✔
116

117
void init_ping_buffer_ipv6(size_t ping_data_size)
392✔
118
{
119
    /* allocate ping buffer */
120
    ping_pkt_size_ipv6 = ping_data_size + sizeof(struct icmp6_hdr);
392✔
121
    ping_buffer_ipv6 = (char*)calloc(1, ping_pkt_size_ipv6);
392✔
122
    if (!ping_buffer_ipv6)
392✔
UNCOV
123
        crash_and_burn("can't malloc ping packet");
×
124
}
392✔
125

126
void socket_set_src_addr_ipv6(int s, struct in6_addr* src_addr, int *ident)
16✔
127
{
128
    struct sockaddr_in6 sa;
129
    socklen_t len = sizeof(sa);
16✔
130

131
    outgoing_src_addr_ipv6 = *src_addr;
16✔
132
    outgoing_src_addr_set_ipv6 = 1;
16✔
133

134
    memset(&sa, 0, sizeof(sa));
16✔
135
    sa.sin6_family = AF_INET6;
16✔
136
    sa.sin6_addr = *src_addr;
16✔
137
    if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0)
16✔
138
        errno_crash_and_burn("cannot bind source address");
2✔
139

140
    if (ident) {
14✔
141
        memset(&sa, 0, len);
12✔
142
        if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
12✔
UNCOV
143
            errno_crash_and_burn("can't get ICMP6 socket identity");
×
144

145
        if (sa.sin6_port)
12✔
146
            *ident = sa.sin6_port;
12✔
147
    }
148
}
14✔
149

150
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✔
151
{
152
    struct icmp6_hdr* icp;
153
    int n;
154

155
    icp = (struct icmp6_hdr*)ping_buffer_ipv6;
123✔
156
    icp->icmp6_type = ICMP6_ECHO_REQUEST;
123✔
157
    icp->icmp6_code = 0;
123✔
158
    icp->icmp6_seq = htons(icmp_seq_nr);
123✔
159
    icp->icmp6_id = icmp_id_nr;
123✔
160

161
    if (opt_random_data_on) {
123✔
162
        for (n = sizeof(struct icmp6_hdr); n < ping_pkt_size_ipv6; ++n) {
342✔
163
            ping_buffer_ipv6[n] = random() & 0xFF;
336✔
164
        }
165
    }
166

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

169
    if (outgoing_iface_idx_ipv6 > 0) {
123✔
170
        struct iovec iov = {
2✔
171
            .iov_base = icp,
172
            .iov_len  = ping_pkt_size_ipv6
173
        };
174

175
        char cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
176
        memset(cmsg_buf, 0, sizeof(cmsg_buf));
2✔
177

178
        struct msghdr msg = {
2✔
179
            .msg_name       = saddr,
180
            .msg_namelen    = saddr_len,
181
            .msg_iov        = &iov,
182
            .msg_iovlen     = 1,
183
            .msg_control    = cmsg_buf,
184
            .msg_controllen = sizeof(cmsg_buf),
185
            .msg_flags      = 0
186
        };
187

188
        struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
2✔
189
        cmsg->cmsg_level = IPPROTO_IPV6;
2✔
190
        cmsg->cmsg_type  = IPV6_PKTINFO;
2✔
191
        cmsg->cmsg_len   = CMSG_LEN(sizeof(struct in6_pktinfo));
2✔
192

193
        struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
2✔
194
        memset(pktinfo, 0, sizeof(*pktinfo));
2✔
195
        pktinfo->ipi6_ifindex = outgoing_iface_idx_ipv6;
2✔
196

197
        if (outgoing_src_addr_set_ipv6) {
2✔
UNCOV
198
            pktinfo->ipi6_addr = outgoing_src_addr_ipv6;
×
199
        }
200

201
        n = sendmsg(s, &msg, 0);
2✔
202
    } else {
203
        n = sendto(s, icp, ping_pkt_size_ipv6, 0, saddr, saddr_len);
121✔
204
    }
205

206
    return n;
123✔
207
}
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