File: misc.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (246 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Copyright (c) 1997 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/* Modified from FreeBSD 2.1 sys/i386/boot/netboot */
/**************************************************************************
NETBOOT -  BOOTP/TFTP Bootstrap Program

Author: Martin Renters
  Date: Dec/93

**************************************************************************/

#include <oskit/c/arpa/inet.h>		/* htonl */
#include <oskit/c/string.h>		/* memcpy */
#include <oskit/c/strings.h>		/* strsep */
#include <oskit/c/stdarg.h>		/* va_list */
#include <oskit/c/stdio.h>		/* putchar */
#include <oskit/c/netinet/in.h>		/* INADDR_NONE */

#include "netboot.h"

/*
 * Convert IP address from net to machine order
 */
void
convert_ipaddr(void *dv, void *sv)
{
	char *d = dv, *s = sv;

	*(d+3) = *s;
	*(d+2) = *(s+1);
	*(d+1) = *(s+2);
	*d     = *(s+3);
}

/**************************************************************************
PRINTF and friends

	Formats:
		%X	- 4 byte ASCII (8 hex digits)
		%x	- 2 byte ASCII (4 hex digits)
		%b	- 1 byte ASCII (2 hex digits)
		%d	- decimal
		%c	- ASCII char
		%s	- ASCII string
		%I	- Internet address in x.x.x.x notation
		%L	- Binary long
		%S	- String (multiple of 4 bytes) preceded with 4 byte
			  binary length
		%M	- Copy memory.  Takes two args, len and ptr
**************************************************************************/
static char *
do_netprintf(char *buf, char *fmt, va_list args)
{
	static char hex[]="0123456789ABCDEF";
	register char *p;
	char tmp[16];

	while (*fmt) {
		if (*fmt == '%') {	/* switch() uses more space */
			fmt++;
			if (*fmt == 'L') {
				register int h = va_arg(args, int);
				*(buf++) = h>>24;
				*(buf++) = h>>16;
				*(buf++) = h>>8;
				*(buf++) = h;
			}
			if (*fmt == 'S') {
				register int len = 0;
				char *lenptr = buf;
				p = va_arg(args, char *);
				buf += 4;
				while (*p) {
					*(buf++) = *p++;
					len ++;
				}
				*(lenptr++) = len>>24;
				*(lenptr++) = len>>16;
				*(lenptr++) = len>>8;
				*lenptr = len;
				while (len & 3) {
					*(buf++) = 0;
					len ++;
				}
			}
			if (*fmt == 'M') {
				register int len = va_arg(args, int);
				memcpy(buf, va_arg(args, void *), len);
				buf += len;
			}
			if (*fmt == 'X') {
				register int h = va_arg(args, int);
				*(buf++) = hex[(h>>28)& 0x0F];
				*(buf++) = hex[(h>>24)& 0x0F];
				*(buf++) = hex[(h>>20)& 0x0F];
				*(buf++) = hex[(h>>16)& 0x0F];
				*(buf++) = hex[(h>>12)& 0x0F];
				*(buf++) = hex[(h>>8)& 0x0F];
				*(buf++) = hex[(h>>4)& 0x0F];
				*(buf++) = hex[h& 0x0F];
			}
			if (*fmt == 'x') {
				register int h = va_arg(args, int);
				*(buf++) = hex[(h>>12)& 0x0F];
				*(buf++) = hex[(h>>8)& 0x0F];
				*(buf++) = hex[(h>>4)& 0x0F];
				*(buf++) = hex[h& 0x0F];
			}
			if (*fmt == 'b') {
				register int h = va_arg(args, int);
				*(buf++) = hex[(h>>4)& 0x0F];
				*(buf++) = hex[h& 0x0F];
			}
			if (*fmt == 'd') {
				register int dec = va_arg(args, int);
				p = tmp;
				if (dec < 0) {
					*(buf++) = '-';
					dec = -dec;
				}
				do {
					*(p++) = '0' + (dec%10);
					dec = dec/10;
				} while(dec);
				while ((--p) >= tmp) *(buf++) = *p;
			}
			if (*fmt == 'I') {
				register int i = va_arg(args, int);
				buf = netsprintf(buf,"%d.%d.%d.%d",
					(i>>24) & 0x00FF,
					(i>>16) & 0x00FF,
					(i>>8) & 0x00FF,
					i & 0x00FF);
			}
			if (*fmt == 'c')
				*(buf++) = va_arg(args, int);
			if (*fmt == 's') {
				p = va_arg(args, char *);
				while (*p) *(buf++) = *p++;
			}
		} else *(buf++) = *fmt;
		fmt++;
	}
	va_end(args);
	*buf = 0;
	return(buf);
}

char *
netsprintf(char *buf, char *fmt, ...)
{
	va_list args;
	char *r;

	va_start(args, fmt);
	r = do_netprintf(buf, fmt, args);
	va_end(args);
	return r;
}

void
netprintf(char *fmt, ...)
{
	va_list args;
	char buf[512], *p;

	va_start(args, fmt);
	p = buf;
	do_netprintf(buf, fmt, args);
	while (*p) putchar(*p++);
	va_end(args);
}

/*
 * Host lookup kludge since it is hard to use the oskit's gethostbyname
 * since we don't use the BSD socket stuff.
 * If you fix this, fix the helpstring in main.c too.
 */
static unsigned long
hostlookup(char *host_or_ip)
{
	/* These are padded with NULs so the binary can be patched... */
	if (!strcmp(host_or_ip, "marker") || !strcmp(host_or_ip, "marker.cs"))
		host_or_ip = "155.99.212.61\0\0";
	else if (!strcmp(host_or_ip, "fast") || !strcmp(host_or_ip, "fast.cs"))
		host_or_ip = "155.99.212.1\0\0\0";
	else if (!strcmp(host_or_ip, "golden"))
		host_or_ip = "206.163.153.18\0";
	return ntohl(inet_addr(host_or_ip));
}

/*
 * Take the command line and set the IP, dir, and name components.
 * The cmdline is modified and things point into it.
 * The cmdline looks like "ip:/dir/file -foo bar", eg "155.99.212.1:/tmp/foo -l"
 */
int
parse_cmdline(char *cmdline, int cmdsize, unsigned int *ip, char **dir,
	      char **name, char **rest)
{
	char *p = cmdline;
	char *host_or_ip;

	p = strsep(&cmdline, " \t");
	*rest = cmdline;		/* ok if NULL */

	host_or_ip = strsep(&p, ":");
	if (! p)
		return 0;

	*ip = hostlookup(host_or_ip);
	if (*ip == INADDR_NONE)
		return 0;

	if (p[0] != '/')
		return 0;
	*dir = p;

	p = strrchr(*dir, '/');
	if (! p)
		return 0;
	*p++ = '\0';
	if (! *p)
		return 0;
	*name = p;

	/* Allow the case ip:/foo; we might have just NULified that slash. */
	if ((*dir)[0] == '\0')
		*dir = "/";

	return 1;
}