Skip to content

Commit 6600b04

Browse files
tychoschweikert
authored andcommitted
only use getsockname() for identity with SOCK_DGRAM sockets
Signed-off-by: Steven Noonan <[email protected]>
1 parent 8152668 commit 6600b04

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

src/fping.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,13 @@ char* prog;
260260
int ident4 = 0; /* our icmp identity field */
261261
int ident6 = 0;
262262
int socket4 = -1;
263+
int socktype4 = -1;
263264
int using_sock_dgram4 = 0;
264265
#ifndef IPV6
265266
int hints_ai_family = AF_INET;
266267
#else
267268
int socket6 = -1;
269+
int socktype6 = -1;
268270
int hints_ai_family = AF_UNSPEC;
269271
#endif
270272

@@ -406,9 +408,16 @@ int main(int argc, char** argv)
406408
usage(0);
407409
}
408410

409-
socket4 = open_ping_socket_ipv4(&using_sock_dgram4);
411+
socket4 = open_ping_socket_ipv4(&socktype4);
412+
#ifdef __linux__
413+
/* We only treat SOCK_DGRAM differently on Linux, where the IPv4 header
414+
* structure is missing in the message.
415+
*/
416+
using_sock_dgram4 = (socktype4 == SOCK_DGRAM);
417+
#endif
418+
410419
#ifdef IPV6
411-
socket6 = open_ping_socket_ipv6();
420+
socket6 = open_ping_socket_ipv6(&socktype6);
412421
/* if called (sym-linked) via 'fping6', imply '-6'
413422
* for backward compatibility */
414423
if (strstr(prog, "fping6")) {
@@ -428,7 +437,7 @@ int main(int argc, char** argv)
428437
}
429438

430439
optparse_init(&optparse_state, argv);
431-
ident4 = ident6 = getpid() & 0xFFFF;
440+
ident4 = ident6 = htons(getpid() & 0xFFFF);
432441
verbose_flag = 1;
433442
backoff_flag = 1;
434443
opterr = 1;
@@ -1023,11 +1032,11 @@ int main(int argc, char** argv)
10231032
}
10241033

10251034
if (socket4 >= 0) {
1026-
socket_set_src_addr_ipv4(socket4, &src_addr, &ident4);
1035+
socket_set_src_addr_ipv4(socket4, &src_addr, (socktype4 == SOCK_DGRAM) ? &ident4 : NULL);
10271036
}
10281037
#ifdef IPV6
10291038
if (socket6 >= 0) {
1030-
socket_set_src_addr_ipv6(socket6, &src_addr6, &ident6);
1039+
socket_set_src_addr_ipv6(socket6, &src_addr6, (socktype6 == SOCK_DGRAM) ? &ident6 : NULL);
10311040
}
10321041
#endif
10331042

src/fping.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ int in_cksum( unsigned short *p, int n );
1414
extern int random_data_flag;
1515

1616
/* socket.c */
17-
int open_ping_socket_ipv4(int *using_sock_dgram);
17+
int open_ping_socket_ipv4(int *socktype);
1818
void init_ping_buffer_ipv4(size_t ping_data_size);
1919
void socket_set_src_addr_ipv4(int s, struct in_addr *src_addr, int *ident);
2020
int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, uint16_t icmp_seq, uint16_t icmp_id);
2121
#ifdef IPV6
22-
int open_ping_socket_ipv6();
22+
int open_ping_socket_ipv6(int *socktype);
2323
void init_ping_buffer_ipv6(size_t ping_data_size);
2424
void socket_set_src_addr_ipv6(int s, struct in6_addr *src_addr, int *ident);
2525
int socket_sendto_ping_ipv6(int s, struct sockaddr *saddr, socklen_t saddr_len, uint16_t icmp_seq, uint16_t icmp_id);

src/socket4.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
char* ping_buffer_ipv4 = 0;
4848
size_t ping_pkt_size_ipv4;
4949

50-
int open_ping_socket_ipv4(int *using_sock_dgram)
50+
int open_ping_socket_ipv4(int *socktype)
5151
{
5252
struct protoent* proto;
5353
int s;
@@ -56,23 +56,16 @@ int open_ping_socket_ipv4(int *using_sock_dgram)
5656
if ((proto = getprotobyname("icmp")) == NULL)
5757
crash_and_burn("icmp: unknown protocol");
5858

59-
*using_sock_dgram = 0;
60-
6159
/* create raw socket for ICMP calls (ping) */
62-
s = socket(AF_INET, SOCK_RAW, proto->p_proto);
60+
*socktype = SOCK_RAW;
61+
s = socket(AF_INET, *socktype, proto->p_proto);
6362
if (s < 0) {
6463
/* try non-privileged icmp (works on Mac OSX without privileges, for example) */
65-
s = socket(AF_INET, SOCK_DGRAM, proto->p_proto);
64+
*socktype = SOCK_DGRAM;
65+
s = socket(AF_INET, *socktype, proto->p_proto);
6666
if (s < 0) {
6767
return -1;
6868
}
69-
70-
#ifdef __linux__
71-
/* We only treat SOCK_DGRAM differently on Linux, where the IPv4 header
72-
* structure is missing in the message.
73-
*/
74-
*using_sock_dgram = 1;
75-
#endif
7669
}
7770

7871
/* Make sure that we use non-blocking IO */
@@ -109,12 +102,14 @@ void socket_set_src_addr_ipv4(int s, struct in_addr* src_addr, int *ident)
109102
if (bind(s, (struct sockaddr*)&sa, len) < 0)
110103
errno_crash_and_burn("cannot bind source address");
111104

112-
memset(&sa, 0, len);
113-
if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
114-
errno_crash_and_burn("can't get ICMP socket identity");
105+
if (ident) {
106+
memset(&sa, 0, len);
107+
if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
108+
errno_crash_and_burn("can't get ICMP socket identity");
115109

116-
if (sa.sin_port)
117-
*ident = sa.sin_port;
110+
if (sa.sin_port)
111+
*ident = sa.sin_port;
112+
}
118113
}
119114

120115
unsigned short calcsum(unsigned short* buffer, int length)

src/socket6.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
char* ping_buffer_ipv6 = 0;
4747
size_t ping_pkt_size_ipv6;
4848

49-
int open_ping_socket_ipv6()
49+
int open_ping_socket_ipv6(int *socktype)
5050
{
5151
struct protoent* proto;
5252
int s;
@@ -56,10 +56,12 @@ int open_ping_socket_ipv6()
5656
crash_and_burn("icmp: unknown protocol");
5757

5858
/* create raw socket for ICMP calls (ping) */
59-
s = socket(AF_INET6, SOCK_RAW, proto->p_proto);
59+
*socktype = SOCK_RAW;
60+
s = socket(AF_INET6, *socktype, proto->p_proto);
6061
if (s < 0) {
6162
/* try non-privileged icmp (works on Mac OSX without privileges, for example) */
62-
s = socket(AF_INET6, SOCK_DGRAM, proto->p_proto);
63+
*socktype = SOCK_DGRAM;
64+
s = socket(AF_INET6, *socktype, proto->p_proto);
6365
if (s < 0) {
6466
return -1;
6567
}
@@ -99,12 +101,14 @@ void socket_set_src_addr_ipv6(int s, struct in6_addr* src_addr, int *ident)
99101
if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0)
100102
errno_crash_and_burn("cannot bind source address");
101103

102-
memset(&sa, 0, len);
103-
if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
104-
errno_crash_and_burn("can't get ICMP socket identity");
104+
if (ident) {
105+
memset(&sa, 0, len);
106+
if (getsockname(s, (struct sockaddr *)&sa, &len) < 0)
107+
errno_crash_and_burn("can't get ICMP socket identity");
105108

106-
if (sa.sin6_port)
107-
*ident = sa.sin6_port;
109+
if (sa.sin6_port)
110+
*ident = sa.sin6_port;
111+
}
108112
}
109113

110114
int socket_sendto_ping_ipv6(int s, struct sockaddr* saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr)

0 commit comments

Comments
 (0)