Use more readably cksum variant and move function into v4 sockets code.

pull/82/head
Andrey Jr. Melnikov 9 years ago
parent 7885effce1
commit 4e14f407aa

@ -316,7 +316,6 @@ char *na_cat( char *name, struct in_addr ipaddr );
void crash_and_burn( char *message );
void errno_crash_and_burn( char *message );
char *get_host_by_address( struct in_addr in );
int in_cksum( unsigned short *p, int n );
void u_sleep( int u_sec );
int recvfrom_wto( int s, char *buf, int len, struct sockaddr *saddr, socklen_t *saddr_len, long timo );
void remove_job( HOST_ENTRY *h );
@ -1799,54 +1798,6 @@ int handle_random_icmp(FPING_ICMPHDR *p, struct sockaddr *addr, socklen_t addr_l
} /* handle_random_icmp() */
/************************************************************
Function: in_cksum
*************************************************************
Inputs: unsigned short *p, int n
Returns: int
Description:
Checksum routine for Internet Protocol family headers (C Version)
From ping examples in W.Richard Stevens "UNIX NETWORK PROGRAMMING" book.
************************************************************/
int in_cksum( unsigned short *p, int n )
{
register unsigned short answer;
register long sum = 0;
unsigned short odd_byte = 0;
while( n > 1 )
{
sum += *p++;
n -= 2;
}/* WHILE */
/* mop up an odd byte, if necessary */
if( n == 1 )
{
*( unsigned char* )( &odd_byte ) = *( unsigned char* )p;
sum += odd_byte;
}/* IF */
sum = ( sum >> 16 ) + ( sum & 0xffff ); /* add hi 16 to low 16 */
sum += ( sum >> 16 ); /* add carry */
answer = ~sum; /* ones-complement, truncate*/
return ( answer );
} /* in_cksum() */
/************************************************************
Function: add_name

@ -87,6 +87,23 @@ void socket_set_src_addr_ipv4(int s, FPING_INADDR src_addr)
errno_crash_and_burn( "cannot bind source address" );
}
unsigned short calcsum(unsigned short *buffer, int length)
{
unsigned long sum;
// initialize sum to zero and loop until length (in words) is 0
for (sum=0; length>1; length-=2) // sizeof() returns number of bytes, we're interested in number of words
sum += *buffer++; // add 1 word of buffer to sum and proceed to the next
// we may have an extra byte
if (length==1)
sum += (char)*buffer;
sum = (sum >> 16) + (sum & 0xFFFF); // add high 16 to low 16
sum += (sum >> 16); // add carry
return ~sum;
}
int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr)
{
struct icmp *icp;
@ -106,7 +123,7 @@ int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len,
}
}
icp->icmp_cksum = in_cksum((unsigned short*) icp, ping_pkt_size );
icp->icmp_cksum = calcsum((unsigned short *) icp, ping_pkt_size);
n = sendto(s, icp, ping_pkt_size, 0, saddr, saddr_len);

Loading…
Cancel
Save