1 /**************************************************************************
3 **************************************************************************/
8 /**************************************************************************
9 IPCHKSUM - Checksum IP Header
10 **************************************************************************/
11 uint16_t ipchksum(const void *data, unsigned long length)
17 /* In the most straight forward way possible,
18 * compute an ip style checksum.
22 for(i = 0; i < length; i++) {
28 /* Add the new value */
30 /* Wrap around the carry */
32 sum = (sum + (sum >> 16)) & 0xFFFF;
35 return (~cpu_to_le16(sum)) & 0xFFFF;
38 uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
40 unsigned long checksum;
44 /* byte swap the sum if it came from an odd offset
45 * since the computation is endian independant this
51 if (checksum > 0xFFFF) {
54 return (~checksum) & 0xFFFF;
59 /**************************************************************************
60 RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
61 **************************************************************************/
64 static int32_t seed = 0;
66 if (!seed) /* Initialize linear congruential generator */
68 /* simplified version of the LCG given in Bruce Schneier's
69 "Applied Cryptography" */
71 if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
75 /**************************************************************************
77 **************************************************************************/
82 for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
86 /**************************************************************************
88 **************************************************************************/
89 void interruptible_sleep(int secs)
95 /**************************************************************************
97 **************************************************************************/
102 static const char tiddles[]="-\\|/";
103 static unsigned long lastticks = 0;
106 if ( ! as_main_program ) return;
108 /* Limit the maximum rate at which characters are printed */
110 if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
114 putchar(tiddles[(count++)&3]);
118 #endif /* BAR_PROGRESS */
121 /**************************************************************************
122 STRCASECMP (not entirely correct, but this will do for our purposes)
123 **************************************************************************/
124 int strcasecmp(const char *a, const char *b)
126 while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
127 return((*a & ~0x20) - (*b & ~0x20));
130 /**************************************************************************
131 INET_ATON - Convert an ascii x.x.x.x to binary form
132 **************************************************************************/
133 int inet_aton ( const char *cp, struct in_addr *inp ) {
135 const char *digits_start;
136 unsigned long ip = 0;
139 for(j = 0; j <= 3; j++) {
141 val = strtoul(p, ( char ** ) &p, 10);
142 if ((p == digits_start) || (val > 255)) return 0;
143 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
144 ip = (ip << 8) | val;
147 inp->s_addr = htonl(ip);
153 unsigned long strtoul ( const char *p, char **endp, int base ) {
154 unsigned long ret = 0;
155 unsigned int charval;
162 if ( ( *p | 0x20 ) == 'x' ) {
171 if ( charval >= 'a' ) {
172 charval = ( charval - 'a' + 10 );
173 } else if ( charval >= 'A' ) {
174 charval = ( charval - 'A' + 10 );
176 charval = ( charval - '0' );
178 if ( charval >= ( unsigned int ) base )
180 ret = ( ( ret * base ) + charval );
185 *endp = ( char * ) p;