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 );
} /* 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
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;
}
}
- 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);