summaryrefslogtreecommitdiff
path: root/surplus.c
blob: 6dfbea42a373ab86abfb0e4bed9b6721924b8228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * 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 */