-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathsocket6.c
More file actions
214 lines (180 loc) · 6.57 KB
/
Copy pathsocket6.c
File metadata and controls
214 lines (180 loc) · 6.57 KB
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
/*
* fping: fast-ping, file-ping, favorite-ping, funky-ping
*
* Ping a list of target hosts in a round robin fashion.
* A better ping overall.
*
* fping website: http://www.fping.org
*
* Current maintainer of fping: David Schweikert
* Please send suggestions and patches to: david@schweikert.ch
*
*
* Original author: Roland Schemers <schemers@stanford.edu>
* IPv6 Support: Jeroen Massar <jeroen@unfix.org / jeroen@ipng.nl>
* Improved main loop: David Schweikert <david@schweikert.ch>
* Debian Merge, TOS settings: Tobi Oetiker <tobi@oetiker.ch>
* Bugfixes, byte order & senseful seq.-numbers: Stephan Fuhrmann (stephan.fuhrmann AT 1und1.de)
*
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by Stanford University. The name of the University may not be used
* to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "config.h"
#include "fping.h"
#include "flags.h"
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/icmp6.h>
char* ping_buffer_ipv6 = 0;
size_t ping_pkt_size_ipv6;
/* Interface index for outgoing packets (0 = not set, use routing table) */
static int outgoing_iface_idx_ipv6 = 0;
/* Source address for outgoing packets (used to populate ipi6_addr) */
static struct in6_addr outgoing_src_addr_ipv6;
static int outgoing_src_addr_set_ipv6 = 0;
int open_ping_socket_ipv6(int *socktype)
{
int s = -1;
int p_proto = IPPROTO_ICMPV6;
#ifdef USE_GETPROTOBYNAME
{
/* confirm that ICMP6 is available on this machine */
struct protoent* proto = getprotobyname("ipv6-icmp");
if (proto == NULL) {
crash_and_burn("ipv6-icmp: unknown protocol");
}
p_proto = proto->p_proto;
}
#endif
/* create raw socket for ICMP6 calls (ping) */
*socktype = SOCK_RAW;
s = socket(AF_INET6, *socktype, p_proto);
if (s < 0) {
/* try non-privileged icmp6 (works on Mac OSX without privileges, for example) */
*socktype = SOCK_DGRAM;
s = socket(AF_INET6, *socktype, p_proto);
if (s < 0) {
return -1;
}
} else {
/* receive only ICMP6 messages relevant for fping on raw socket */
struct icmp6_filter recv_filter;
ICMP6_FILTER_SETBLOCKALL(&recv_filter);
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &recv_filter);
ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &recv_filter);
ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &recv_filter);
ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &recv_filter);
ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &recv_filter);
if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &recv_filter, sizeof(recv_filter))) {
errno_crash_and_burn("cannot set icmp6 message type filter");
}
}
/* Make sure that we use non-blocking IO */
{
int flags;
if ((flags = fcntl(s, F_GETFL, 0)) < 0)
perror("fcntl");
if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
perror("fcntl");
}
return s;
}
void socket_set_outgoing_iface_ipv6(const char *iface_name)
{
unsigned int idx = if_nametoindex(iface_name);
if (idx == 0) {
fprintf(stderr, "fping: unknown interface '%s'\n", iface_name);
exit(1);
}
outgoing_iface_idx_ipv6 = (int)idx;
}
void init_ping_buffer_ipv6(size_t ping_data_size)
{
/* allocate ping buffer */
ping_pkt_size_ipv6 = ping_data_size + sizeof(struct icmp6_hdr);
ping_buffer_ipv6 = (char*)calloc(1, ping_pkt_size_ipv6);
if (!ping_buffer_ipv6)
crash_and_burn("can't malloc ping packet");
}
void socket_set_src_addr_ipv6(int s, struct in6_addr* src_addr, int *ident)
{
struct sockaddr_in6 sa;
socklen_t len = sizeof(sa);
outgoing_src_addr_ipv6 = *src_addr;
outgoing_src_addr_set_ipv6 = 1;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_addr = *src_addr;
if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0)
errno_crash_and_burn("cannot bind source address");
if (ident) {
memset(&sa, 0, len);
if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
errno_crash_and_burn("can't get ICMP6 socket identity");
if (sa.sin6_port)
*ident = sa.sin6_port;
}
}
int socket_sendto_ping_ipv6(int s, struct sockaddr* saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr)
{
struct icmp6_hdr* icp;
int n;
icp = (struct icmp6_hdr*)ping_buffer_ipv6;
icp->icmp6_type = ICMP6_ECHO_REQUEST;
icp->icmp6_code = 0;
icp->icmp6_seq = htons(icmp_seq_nr);
icp->icmp6_id = icmp_id_nr;
if (opt_random_data_on) {
for (n = sizeof(struct icmp6_hdr); n < ping_pkt_size_ipv6; ++n) {
ping_buffer_ipv6[n] = random() & 0xFF;
}
}
icp->icmp6_cksum = 0; /* The IPv6 stack calculates the checksum for us... */
if (outgoing_iface_idx_ipv6 > 0) {
struct iovec iov = {
.iov_base = icp,
.iov_len = ping_pkt_size_ipv6
};
char cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
memset(cmsg_buf, 0, sizeof(cmsg_buf));
struct msghdr msg = {
.msg_name = saddr,
.msg_namelen = saddr_len,
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_buf,
.msg_controllen = sizeof(cmsg_buf),
.msg_flags = 0
};
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
memset(pktinfo, 0, sizeof(*pktinfo));
pktinfo->ipi6_ifindex = outgoing_iface_idx_ipv6;
if (outgoing_src_addr_set_ipv6) {
pktinfo->ipi6_addr = outgoing_src_addr_ipv6;
}
n = sendmsg(s, &msg, 0);
} else {
n = sendto(s, icp, ping_pkt_size_ipv6, 0, saddr, saddr_len);
}
return n;
}