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

schweikert / fping / 23642076215

27 Mar 2026 10:31AM UTC coverage: 87.863% (-0.3%) from 88.152%
23642076215

Pull #463

github

web-flow
Merge 57545c9a9 into fd87284ca
Pull Request #463: New option --oiface for outgoing interface

49 of 59 new or added lines in 3 files covered. (83.05%)

2 existing lines in 1 file now uncovered.

1665 of 1895 relevant lines covered (87.86%)

346.68 hits per line

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

86.08
/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
int open_ping_socket_ipv6(int *socktype)
881✔
55
{
56
    struct protoent* proto;
57
    int s;
58

59
    /* confirm that ICMP6 is available on this machine */
60
    if ((proto = getprotobyname("ipv6-icmp")) == NULL)
881✔
61
        crash_and_burn("ipv6-icmp: unknown protocol");
×
62

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

77
        ICMP6_FILTER_SETBLOCKALL(&recv_filter);
869✔
78
        ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &recv_filter);
869✔
79
        ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &recv_filter);
869✔
80
        ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &recv_filter);
869✔
81
        ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &recv_filter);
869✔
82
        ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &recv_filter);
869✔
83

84
        if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &recv_filter, sizeof(recv_filter))) {
869✔
85
            errno_crash_and_burn("cannot set icmp6 message type filter");
×
86
        }
87
    }
88

89
    /* Make sure that we use non-blocking IO */
90
    {
91
        int flags;
92

93
        if ((flags = fcntl(s, F_GETFL, 0)) < 0)
881✔
94
            perror("fcntl");
×
95

96
        if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
881✔
97
            perror("fcntl");
×
98
    }
99

100
    return s;
881✔
101
}
239✔
102

103
void socket_set_outgoing_iface_ipv6(int s, const char *iface_name)
4✔
104
{
105
    unsigned int idx = if_nametoindex(iface_name);
4✔
106
    if (idx == 0) {
4✔
NEW
107
        fprintf(stderr, "fping: unknown interface '%s'\n", iface_name);
×
NEW
108
        exit(1);
×
109
    }
110
    outgoing_iface_idx_ipv6 = (int)idx;
4✔
111

112
    int on = 1;
4✔
113
    if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0) {
4✔
NEW
114
        perror("setsockopt IPV6_RECVPKTINFO");
×
NEW
115
        exit(1);
×
116
    }
117
}
4✔
118

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196
        n = sendmsg(s, &msg, 0);
2✔
197
    } else {
198
        n = sendto(s, icp, ping_pkt_size_ipv6, 0, saddr, saddr_len);
121✔
199
    }
200

201
    return n;
123✔
202
}
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