From 22e4ad996fc6114fa8daf15a31ba60d2ecc05a1a Mon Sep 17 00:00:00 2001 From: David Schweikert Date: Fri, 24 Jul 2015 22:33:43 +0200 Subject: [PATCH] further unification work --- src/fping.c | 28 +--------------------------- src/fping.h | 4 +++- src/socket.c | 11 +++++++++++ src/socket4.c | 33 ++++++++++++++++++++++++++++++++- src/socket6.c | 36 ++++++++++++++++++++++++++++++++++-- 5 files changed, 81 insertions(+), 31 deletions(-) diff --git a/src/fping.c b/src/fping.c index 8651080..7f41314 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1393,44 +1393,19 @@ void print_global_stats( void ) int send_ping( int s, HOST_ENTRY *h ) { - char *buffer; - FPING_ICMPHDR *icp; int n; int myseq; int ret = 1; - buffer = ( char* )malloc( ( size_t )ping_pkt_size ); - if( !buffer ) - crash_and_burn( "can't malloc ping packet" ); - - memset( buffer, 0, ping_pkt_size * sizeof( char ) ); - icp = ( FPING_ICMPHDR* )buffer; - gettimeofday( &h->last_send_time, &tz ); myseq = seqmap_add(h->i, h->num_sent, &h->last_send_time); -#ifndef IPV6 - icp->icmp_type = ICMP_ECHO; - icp->icmp_code = 0; - icp->icmp_cksum = 0; - icp->icmp_seq = htons(myseq); - icp->icmp_id = htons(ident); - - icp->icmp_cksum = in_cksum( ( unsigned short* )icp, ping_pkt_size ); -#else - icp->icmp6_type = ICMP6_ECHO_REQUEST; - icp->icmp6_code = 0; - icp->icmp6_seq = htons(myseq); - icp->icmp6_id = htons(ident); - - icp->icmp6_cksum = 0; // The IPv6 stack calculates the checksum for us... -#endif #if defined(DEBUG) || defined(_DEBUG) if( trace_flag ) printf( "sending [%d] to %s\n", h->num_sent, h->host ); #endif /* DEBUG || _DEBUG */ - n = sendto( s, buffer, ping_pkt_size, 0, (struct sockaddr *) &h->saddr, h->saddr_len); + n = socket_sendto_ping(s, (struct sockaddr *) &h->saddr, h->saddr_len, ping_pkt_size, myseq, ident); if( (n < 0 || n != ping_pkt_size) @@ -1463,7 +1438,6 @@ int send_ping( int s, HOST_ENTRY *h ) h->waiting++; num_pingsent++; last_send_time = h->last_send_time; - free( buffer ); return(ret); } diff --git a/src/fping.h b/src/fping.h index 74875d2..d42691d 100644 --- a/src/fping.h +++ b/src/fping.h @@ -18,9 +18,11 @@ /* fping.c */ void crash_and_burn( char *message ); void errno_crash_and_burn( char *message ); +int in_cksum( unsigned short *p, int n ); /* socket.c */ -int open_ping_socket(); +int open_ping_socket(); void socket_set_src_addr(int s, FPING_INADDR src_addr); +int socket_sendto_ping(int s, struct sockaddr *saddr, socklen_t saddr_len, size_t ping_pkg_size, uint16_t icmp_seq, uint16_t icmp_id); #endif diff --git a/src/socket.c b/src/socket.c index 1cb00d4..54ee1ec 100644 --- a/src/socket.c +++ b/src/socket.c @@ -37,6 +37,8 @@ int open_ping_socket_ipv4(); int open_ping_socket_ipv6(); void socket_set_src_addr_ipv4(int s, FPING_INADDR src_addr); void socket_set_src_addr_ipv6(int s, FPING_INADDR src_addr); +int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, size_t ping_pkg_size, uint16_t icmp_seq_nr, uint16_t icmp_id_nr); +int socket_sendto_ping_ipv6(int s, struct sockaddr *saddr, socklen_t saddr_len, size_t ping_pkg_size, uint16_t icmp_seq_nr, uint16_t icmp_id_nr); int open_ping_socket() { @@ -55,3 +57,12 @@ void socket_set_src_addr(int s, FPING_INADDR src_addr) socket_set_src_addr_ipv6(s, src_addr); #endif } + +int socket_sendto_ping(int s, struct sockaddr *saddr, socklen_t saddr_len, size_t ping_pkt_size, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) +{ +#ifndef IPV6 + return socket_sendto_ping_ipv4(s, saddr, saddr_len, ping_pkt_size, icmp_seq_nr, icmp_id_nr); +#else + return socket_sendto_ping_ipv6(s, saddr, saddr_len, ping_pkt_size, icmp_seq_nr, icmp_id_nr); +#endif +} diff --git a/src/socket4.c b/src/socket4.c index 40141b4..51ba9bd 100644 --- a/src/socket4.c +++ b/src/socket4.c @@ -35,11 +35,17 @@ #include #include +#include +#include #include #include #include +#include -int open_ping_socket_ipv4() +char *ping_buffer = 0; +size_t ping_pkt_size; + +int open_ping_socket_ipv4(size_t ping_data_size) { struct protoent *proto; int s; @@ -58,6 +64,12 @@ int open_ping_socket_ipv4() } } + /* allocate ping buffer */ + ping_pkt_size = ping_data_size + ICMP_MINLEN; + ping_buffer = (char *) calloc(1, ping_pkt_size); + if(!ping_buffer) + crash_and_burn( "can't malloc ping packet" ); + return s; } @@ -71,3 +83,22 @@ void socket_set_src_addr_ipv4(int s, FPING_INADDR src_addr) if ( bind( s, (struct sockaddr *)&sa, sizeof( sa ) ) < 0 ) errno_crash_and_burn( "cannot bind source address" ); } + +int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, size_t ping_pkt_size, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) +{ + struct icmp *icp; + int n; + + icp = (struct icmp *) ping_buffer; + + icp->icmp_type = ICMP_ECHO; + icp->icmp_code = 0; + icp->icmp_cksum = 0; + icp->icmp_seq = htons(icmp_seq_nr); + icp->icmp_id = htons(icmp_id_nr); + icp->icmp_cksum = in_cksum((unsigned short*) icp, ping_pkt_size ); + + n = sendto(s, icp, ping_pkt_size, 0, saddr, saddr_len); + + return n; +} diff --git a/src/socket6.c b/src/socket6.c index 8641c99..361612a 100644 --- a/src/socket6.c +++ b/src/socket6.c @@ -38,10 +38,14 @@ #include #include #include +#include -#include +#include -int open_ping_socket_ipv6() +char *ping_buffer = 0; +size_t ping_pkt_size; + +int open_ping_socket_ipv6(size_t ping_data_size) { struct protoent *proto; int opton = 1; @@ -61,6 +65,12 @@ int open_ping_socket_ipv6() } } + /* allocate ping buffer */ + ping_pkt_size = ping_data_size + sizeof(struct icmp6_hdr); + ping_buffer = (char *) calloc(1, ping_pkt_size); + if(!ping_buffer) + crash_and_burn( "can't malloc ping packet" ); + return s; } @@ -74,3 +84,25 @@ void socket_set_src_addr_ipv6(int s, FPING_INADDR src_addr) if ( bind( s, (struct sockaddr *)&sa, sizeof( sa ) ) < 0 ) errno_crash_and_burn( "cannot bind source address" ); } + +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; + + // FIXME: randomization + icp = (struct icmp6_hdr *) ping_buffer; + if(!icp) { + crash_and_burn("can't malloc ping packet"); + } + + icp->icmp6_type = ICMP6_ECHO_REQUEST; + icp->icmp6_code = 0; + icp->icmp6_seq = htons(icmp_seq_nr); + icp->icmp6_id = htons(icmp_id_nr); + icp->icmp6_cksum = 0; // The IPv6 stack calculates the checksum for us... + + n = sendto(s, icp, ping_pkt_size, 0, saddr, saddr_len); + + return n; +} -- 2.43.0