1 /**************************************************************************
3 **************************************************************************/
9 /**************************************************************************
10 IPCHKSUM - Checksum IP Header
11 **************************************************************************/
12 uint16_t ipchksum(const void *data, unsigned long length)
18 /* In the most straight forward way possible,
19 * compute an ip style checksum.
23 for(i = 0; i < length; i++) {
29 /* Add the new value */
31 /* Wrap around the carry */
33 sum = (sum + (sum >> 16)) & 0xFFFF;
36 return (~cpu_to_le16(sum)) & 0xFFFF;
39 uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
41 unsigned long checksum;
45 /* byte swap the sum if it came from an odd offset
46 * since the computation is endian independant this
52 if (checksum > 0xFFFF) {
55 return (~checksum) & 0xFFFF;
60 /**************************************************************************
61 RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
62 **************************************************************************/
65 static int32_t seed = 0;
67 if (!seed) /* Initialize linear congruential generator */
69 /* simplified version of the LCG given in Bruce Schneier's
70 "Applied Cryptography" */
72 if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
76 /**************************************************************************
78 **************************************************************************/
79 unsigned int sleep(unsigned int secs)
83 for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
88 /**************************************************************************
90 **************************************************************************/
91 void interruptible_sleep(int secs)
97 /**************************************************************************
99 **************************************************************************/
104 static const char tiddles[]="-\\|/";
105 static unsigned long lastticks = 0;
108 if ( ! as_main_program ) return;
110 /* Limit the maximum rate at which characters are printed */
112 if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
116 putchar(tiddles[(count++)&3]);
120 #endif /* BAR_PROGRESS */
123 /**************************************************************************
124 STRCASECMP (not entirely correct, but this will do for our purposes)
125 **************************************************************************/
126 int strcasecmp(const char *a, const char *b)
128 while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
129 return((*a & ~0x20) - (*b & ~0x20));
132 /**************************************************************************
133 INET_ATON - Convert an ascii x.x.x.x to binary form
134 **************************************************************************/
135 int inet_aton ( const char *cp, struct in_addr *inp ) {
137 const char *digits_start;
138 unsigned long ip = 0;
141 for(j = 0; j <= 3; j++) {
143 val = strtoul(p, ( char ** ) &p, 10);
144 if ((p == digits_start) || (val > 255)) return 0;
145 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
146 ip = (ip << 8) | val;
149 inp->s_addr = htonl(ip);
155 unsigned long strtoul ( const char *p, char **endp, int base ) {
156 unsigned long ret = 0;
157 unsigned int charval;
164 if ( ( *p | 0x20 ) == 'x' ) {
173 if ( charval >= 'a' ) {
174 charval = ( charval - 'a' + 10 );
175 } else if ( charval >= 'A' ) {
176 charval = ( charval - 'A' + 10 );
178 charval = ( charval - '0' );
180 if ( charval >= ( unsigned int ) base )
182 ret = ( ( ret * base ) + charval );
187 *endp = ( char * ) p;