/* * LOKI3 * * [ surplus.c ] * * 1996/7 Guild Corporation Worldwide [daemon9] */ #include "loki.h" extern int verbose; extern jmp_buf env; /* * Domain names / dotted-decimals --> network byte order. */ uint32_t name_resolve(char *hostname) { struct in_addr addr; struct hostent *hostEnt; /* name lookup failure */ if ((addr.s_addr = inet_addr(hostname)) == -1) { if (!(hostEnt = gethostbyname(hostname))) { return 0; // TODO this probably isnt right. } bcopy(hostEnt->h_addr, (char *)&addr.s_addr, hostEnt -> h_length); } return addr.s_addr; } /* * Network byte order --> dotted-decimals. */ char *host_lookup(uint32_t in) { char hostname[BUFSIZ] = {0}; struct in_addr addr; addr.s_addr = in; strcpy(hostname, inet_ntoa(addr)); return strdup(hostname); } #ifdef X86FAST_CHECK /* * Fast x86 based assembly implementation of the IP checksum routine. * TODO: Do we still need this? */ u_short i_check(u_short *buff, int len) { u_long sum = 0; if (len > 3) { __asm__("clc\n" "1:\t" "lodsl\n\t" "adcl %%eax, %%ebx\n\t" "loop 1b\n\t" "adcl $0, %%ebx\n\t" "movl %%ebx, %%eax\n\t" "shrl $16, %%eax\n\t" "addw %%ax, %%bx\n\t" "adcw $0, %%bx" : "=b" (sum) , "=S" (buff) : "0" (sum), "c" (len >> 2) ,"1" (buff) : "ax", "cx", "si", "bx"); } if (len & 2) { __asm__("lodsw\n\t" "addw %%ax, %%bx\n\t" "adcw $0, %%bx" : "=b" (sum) , "=S" (buff) : "0" (sum), "c" (len >> 2) ,"1" (buff) : "ax", "cx", "si", "bx"); } if (len & 2) { __asm__("lodsw\n\t" "addw %%ax, %%bx\n\t" "adcw $0, %%bx" : "=b" (sum), "=S" (buff) : "0" (sum), "1" (buff) : "bx", "ax", "si"); } if (len & 1) { __asm__("lodsb\n\t" "movb $0, %%ah\n\t" "addw %%ax, %%bx\n\t" "adcw $0, %%bx" : "=b" (sum), "=S" (buff) : "0" (sum), "1" (buff) : "bx", "ax", "si"); } if (len & 1) { __asm__("lodsb\n\t" "movb $0, %%ah\n\t" "addw %%ax, %%bx\n\t" "adcw $0, %%bx" : "=b" (sum), "=S" (buff) : "0" (sum), "1" (buff) : "bx", "ax", "si"); } sum = ~sum; return (sum & 0xffff); } #else /* * Standard IP Family checksum routine. */ uint16_t i_check(uint16_t *ptr, int nbytes) { register long sum = 0; uint16_t oddbyte = 0; register uint16_t answer = 0; while (nbytes > 1) { sum += *ptr++; nbytes -= 2; } if (nbytes == 1) { oddbyte = 0; *((uint8_t *)&oddbyte) =* (uint8_t *)ptr; sum += oddbyte; } sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); answer = ~sum; return answer; } #endif /* X86FAST_CHECK */ /* * Generic exit with error function. If checkerrno is true, errno should * be looked at and we call perror, otherwise, just dump to stderr. * Additionally, we have the option of suppressing the error messages by * zeroing verbose. */ void err_exit(int exitstatus, int checkerrno, int verbalkint, char *errstr) { if (verbalkint) { if (checkerrno) { perror(errstr); } else { fprintf(stderr, "%s\n", errstr); } } clean_exit(exitstatus); } /* * Clean exit handler */ void clean_exit(int status) { extern int tsock; extern int ripsock; close(ripsock); close(tsock); exit(status); } #ifdef DEBUG /* * Bulk of this function taken from Stevens APUE... * got this from Mooks (LTC) */ void fd_status(int fd, int newline) { int accmode = 0, val = 0; val = fcntl(fd, F_GETFL, 0); #if !defined(pyr) && !defined(ibm032) && !defined(sony_news) && !defined(NeXT) accmode = val & O_ACCMODE; #else /* pyramid */ accmode = val; /* kludge */ #endif /* pyramid */ if (accmode == O_RDONLY) fprintf(stderr, " read only"); else if (accmode == O_WRONLY) fprintf(stderr, " write only"); else if (accmode == O_RDWR) fprintf(stderr, " read write"); if (val & O_APPEND) fprintf(stderr, " append"); if (val & O_NONBLOCK) fprintf(stderr, " nonblocking"); else fprintf(stderr, " blocking"); #if defined(O_SYNC) if (val & O_SYNC) fprintf(stderr, " sync writes"); #else #if defined(O_FSYNC) if (val & O_FSYNC) fprintf(stderr, " sync writes"); #endif /* O_FSYNC */ #endif /* O_SYNC */ if (newline) fprintf(stderr, "\r\n"); } #endif /* DEBUG */ /* EOF */