xref: /freebsd/libexec/rbootd/utils.c (revision 0559b33149171859c5c0a8c618eede70e44da85d)
1 /*	$NetBSD: utils.c,v 1.6 1995/11/14 08:41:47 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1992 The University of Utah and the Center
5  *	for Software Science (CSS).
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Center for Software Science of the University of Utah Computer
11  * Science Department.  CSS requests users of this software to return
12  * to css-dist@cs.utah.edu any improvements that they make and grant
13  * CSS redistribution rights.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *	from: @(#)utils.c	8.1 (Berkeley) 6/4/93
44  *
45  * From: Utah Hdr: utils.c 3.1 92/07/06
46  * Author: Jeff Forys, University of Utah CSS
47  */
48 
49 #ifndef lint
50 /*static char sccsid[] = "@(#)utils.c	8.1 (Berkeley) 6/4/93";*/
51 static char rcsid[] = "$NetBSD: utils.c,v 1.6 1995/11/14 08:41:47 thorpej Exp $";
52 #endif /* not lint */
53 
54 #include <sys/param.h>
55 
56 #include <fcntl.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <time.h>
63 #include <unistd.h>
64 #include "defs.h"
65 
66 /*
67 **  DispPkt -- Display the contents of an RMPCONN packet.
68 **
69 **	Parameters:
70 **		rconn - packet to be displayed.
71 **		direct - direction packet is going (DIR_*).
72 **
73 **	Returns:
74 **		Nothing.
75 **
76 **	Side Effects:
77 **		None.
78 */
79 void
80 DispPkt(rconn, direct)
81 	RMPCONN *rconn;
82 	int direct;
83 {
84 	static char BootFmt[] = "\t\tRetCode:%u SeqNo:%lx SessID:%x Vers:%u";
85 	static char ReadFmt[] = "\t\tRetCode:%u Offset:%lx SessID:%x\n";
86 
87 	struct tm *tmp;
88 	register struct rmp_packet *rmp;
89 	int i, omask;
90 	u_int32_t t;
91 
92 	/*
93 	 *  Since we will be working with RmpConns as well as DbgFp, we
94 	 *  must block signals that can affect either.
95 	 */
96 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2));
97 
98 	if (DbgFp == NULL) {			/* sanity */
99 		(void) sigsetmask(omask);
100 		return;
101 	}
102 
103 	/* display direction packet is going using '>>>' or '<<<' */
104 	fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp);
105 
106 	/* display packet timestamp */
107 	tmp = localtime((time_t *)&rconn->tstamp.tv_sec);
108 	fprintf(DbgFp, "%02d:%02d:%02d.%06ld   ", tmp->tm_hour, tmp->tm_min,
109 	        tmp->tm_sec, rconn->tstamp.tv_usec);
110 
111 	/* display src or dst addr and information about network interface */
112 	fprintf(DbgFp, "Addr: %s   Intf: %s\n", EnetStr(rconn), IntfName);
113 
114 	rmp = &rconn->rmp;
115 
116 	/* display IEEE 802.2 Logical Link Control header */
117 	(void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n",
118                rmp->hp_llc.dsap, rmp->hp_llc.ssap, ntohs(rmp->hp_llc.cntrl));
119 
120 	/* display HP extensions to 802.2 Logical Link Control header */
121 	(void) fprintf(DbgFp, "\tHP Ext:    DXSAP:%x SXSAP:%x\n",
122 	               ntohs(rmp->hp_llc.dxsap), ntohs(rmp->hp_llc.sxsap));
123 
124 	/*
125 	 *  Display information about RMP packet using type field to
126 	 *  determine what kind of packet this is.
127 	 */
128 	switch(rmp->r_type) {
129 		case RMP_BOOT_REQ:		/* boot request */
130 			(void) fprintf(DbgFp, "\tBoot Request:");
131 			GETWORD(rmp->r_brq.rmp_seqno, t);
132 			if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
133 				if (WORDZE(rmp->r_brq.rmp_seqno))
134 					fputs(" (Send Server ID)", DbgFp);
135 				else
136 					fprintf(DbgFp," (Send Filename #%u)",t);
137 			}
138 			(void) fputc('\n', DbgFp);
139 			(void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode,
140 			        t, ntohs(rmp->r_brq.rmp_session),
141 			        ntohs(rmp->r_brq.rmp_version));
142 			(void) fprintf(DbgFp, "\n\t\tMachine Type: ");
143 			for (i = 0; i < RMP_MACHLEN; i++)
144 				(void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp);
145 			DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm);
146 			break;
147 		case RMP_BOOT_REPL:		/* boot reply */
148 			fprintf(DbgFp, "\tBoot Reply:\n");
149 			GETWORD(rmp->r_brpl.rmp_seqno, t);
150 			(void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode,
151 			        t, ntohs(rmp->r_brpl.rmp_session),
152 			        ntohs(rmp->r_brpl.rmp_version));
153 			DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm);
154 			break;
155 		case RMP_READ_REQ:		/* read request */
156 			(void) fprintf(DbgFp, "\tRead Request:\n");
157 			GETWORD(rmp->r_rrq.rmp_offset, t);
158 			(void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode,
159 			        t, ntohs(rmp->r_rrq.rmp_session));
160 			(void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n",
161 			        ntohs(rmp->r_rrq.rmp_size));
162 			break;
163 		case RMP_READ_REPL:		/* read reply */
164 			(void) fprintf(DbgFp, "\tRead Reply:\n");
165 			GETWORD(rmp->r_rrpl.rmp_offset, t);
166 			(void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode,
167 			        t, ntohs(rmp->r_rrpl.rmp_session));
168 			(void) fprintf(DbgFp, "\t\tNoOfBytesSent: %d\n",
169 			        rconn->rmplen - RMPREADSIZE(0));
170 			break;
171 		case RMP_BOOT_DONE:		/* boot complete */
172 			(void) fprintf(DbgFp, "\tBoot Complete:\n");
173 			(void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n",
174 			        rmp->r_done.rmp_retcode,
175 			        ntohs(rmp->r_done.rmp_session));
176 			break;
177 		default:			/* ??? */
178 			(void) fprintf(DbgFp, "\tUnknown Type:(%d)\n",
179 				rmp->r_type);
180 	}
181 	(void) fputc('\n', DbgFp);
182 	(void) fflush(DbgFp);
183 
184 	(void) sigsetmask(omask);		/* reset old signal mask */
185 }
186 
187 
188 /*
189 **  GetEtherAddr -- convert an RMP (Ethernet) address into a string.
190 **
191 **	An RMP BOOT packet has been received.  Look at the type field
192 **	and process Boot Requests, Read Requests, and Boot Complete
193 **	packets.  Any other type will be dropped with a warning msg.
194 **
195 **	Parameters:
196 **		addr - array of RMP_ADDRLEN bytes.
197 **
198 **	Returns:
199 **		Pointer to static string representation of `addr'.
200 **
201 **	Side Effects:
202 **		None.
203 **
204 **	Warnings:
205 **		- The return value points to a static buffer; it must
206 **		  be copied if it's to be saved.
207 */
208 char *
209 GetEtherAddr(addr)
210 	u_int8_t *addr;
211 {
212 	static char Hex[] = "0123456789abcdef";
213 	static char etherstr[RMP_ADDRLEN*3];
214 	register int i;
215 	register char *cp;
216 
217 	/*
218 	 *  For each byte in `addr', convert it to "<hexchar><hexchar>:".
219 	 *  The last byte does not get a trailing `:' appended.
220 	 */
221 	i = 0;
222 	cp = etherstr;
223 	for(;;) {
224 		*cp++ = Hex[*addr >> 4 & 0xf];
225 		*cp++ = Hex[*addr++ & 0xf];
226 		if (++i == RMP_ADDRLEN)
227 			break;
228 		*cp++ = ':';
229 	}
230 	*cp = '\0';
231 
232 	return(etherstr);
233 }
234 
235 
236 /*
237 **  DispFlnm -- Print a string of bytes to DbgFp (often, a file name).
238 **
239 **	Parameters:
240 **		size - number of bytes to print.
241 **		flnm - address of first byte.
242 **
243 **	Returns:
244 **		Nothing.
245 **
246 **	Side Effects:
247 **		- Characters are sent to `DbgFp'.
248 */
249 void
250 DspFlnm(size, flnm)
251 	register u_int size;
252 	register char *flnm;
253 {
254 	register int i;
255 
256 	(void) fprintf(DbgFp, "\n\t\tFile Name (%u): <", size);
257 	for (i = 0; i < size; i++)
258 		(void) fputc(*flnm++, DbgFp);
259 	(void) fputs(">\n", DbgFp);
260 }
261 
262 
263 /*
264 **  NewClient -- allocate memory for a new CLIENT.
265 **
266 **	Parameters:
267 **		addr - RMP (Ethernet) address of new client.
268 **
269 **	Returns:
270 **		Ptr to new CLIENT or NULL if we ran out of memory.
271 **
272 **	Side Effects:
273 **		- Memory will be malloc'd for the new CLIENT.
274 **		- If malloc() fails, a log message will be generated.
275 */
276 CLIENT *
277 NewClient(addr)
278 	u_int8_t *addr;
279 {
280 	CLIENT *ctmp;
281 
282 	if ((ctmp = (CLIENT *) malloc(sizeof(CLIENT))) == NULL) {
283 		syslog(LOG_ERR, "NewClient: out of memory (%s)",
284 		       GetEtherAddr(addr));
285 		return(NULL);
286 	}
287 
288 	bzero(ctmp, sizeof(CLIENT));
289 	bcopy(addr, &ctmp->addr[0], RMP_ADDRLEN);
290 	return(ctmp);
291 }
292 
293 /*
294 **  FreeClient -- free linked list of Clients.
295 **
296 **	Parameters:
297 **		None.
298 **
299 **	Returns:
300 **		Nothing.
301 **
302 **	Side Effects:
303 **		- All malloc'd memory associated with the linked list of
304 **		  CLIENTS will be free'd; `Clients' will be set to NULL.
305 **
306 **	Warnings:
307 **		- This routine must be called with SIGHUP blocked.
308 */
309 void
310 FreeClients()
311 {
312 	register CLIENT *ctmp;
313 
314 	while (Clients != NULL) {
315 		ctmp = Clients;
316 		Clients = Clients->next;
317 		FreeClient(ctmp);
318 	}
319 }
320 
321 /*
322 **  NewStr -- allocate memory for a character array.
323 **
324 **	Parameters:
325 **		str - null terminated character array.
326 **
327 **	Returns:
328 **		Ptr to new character array or NULL if we ran out of memory.
329 **
330 **	Side Effects:
331 **		- Memory will be malloc'd for the new character array.
332 **		- If malloc() fails, a log message will be generated.
333 */
334 char *
335 NewStr(str)
336 	char *str;
337 {
338 	char *stmp;
339 
340 	if ((stmp = (char *)malloc((unsigned) (strlen(str)+1))) == NULL) {
341 		syslog(LOG_ERR, "NewStr: out of memory (%s)", str);
342 		return(NULL);
343 	}
344 
345 	(void) strcpy(stmp, str);
346 	return(stmp);
347 }
348 
349 /*
350 **  To save time, NewConn and FreeConn maintain a cache of one RMPCONN
351 **  in `LastFree' (defined below).
352 */
353 
354 static RMPCONN *LastFree = NULL;
355 
356 /*
357 **  NewConn -- allocate memory for a new RMPCONN connection.
358 **
359 **	Parameters:
360 **		rconn - initialization template for new connection.
361 **
362 **	Returns:
363 **		Ptr to new RMPCONN or NULL if we ran out of memory.
364 **
365 **	Side Effects:
366 **		- Memory may be malloc'd for the new RMPCONN (if not cached).
367 **		- If malloc() fails, a log message will be generated.
368 */
369 RMPCONN *
370 NewConn(rconn)
371 	RMPCONN *rconn;
372 {
373 	RMPCONN *rtmp;
374 
375 	if (LastFree == NULL) {		/* nothing cached; make a new one */
376 		if ((rtmp = (RMPCONN *) malloc(sizeof(RMPCONN))) == NULL) {
377 			syslog(LOG_ERR, "NewConn: out of memory (%s)",
378 			       EnetStr(rconn));
379 			return(NULL);
380 		}
381 	} else {			/* use the cached RMPCONN */
382 		rtmp = LastFree;
383 		LastFree = NULL;
384 	}
385 
386 	/*
387 	 *  Copy template into `rtmp', init file descriptor to `-1' and
388 	 *  set ptr to next elem NULL.
389 	 */
390 	bcopy((char *)rconn, (char *)rtmp, sizeof(RMPCONN));
391 	rtmp->bootfd = -1;
392 	rtmp->next = NULL;
393 
394 	return(rtmp);
395 }
396 
397 /*
398 **  FreeConn -- Free memory associated with an RMPCONN connection.
399 **
400 **	Parameters:
401 **		rtmp - ptr to RMPCONN to be free'd.
402 **
403 **	Returns:
404 **		Nothing.
405 **
406 **	Side Effects:
407 **		- Memory associated with `rtmp' may be free'd (or cached).
408 **		- File desc associated with `rtmp->bootfd' will be closed.
409 */
410 void
411 FreeConn(rtmp)
412 	register RMPCONN *rtmp;
413 {
414 	/*
415 	 *  If the file descriptor is in use, close the file.
416 	 */
417 	if (rtmp->bootfd >= 0) {
418 		(void) close(rtmp->bootfd);
419 		rtmp->bootfd = -1;
420 	}
421 
422 	if (LastFree == NULL)		/* cache for next time */
423 		rtmp = LastFree;
424 	else				/* already one cached; free this one */
425 		free((char *)rtmp);
426 }
427 
428 /*
429 **  FreeConns -- free linked list of RMPCONN connections.
430 **
431 **	Parameters:
432 **		None.
433 **
434 **	Returns:
435 **		Nothing.
436 **
437 **	Side Effects:
438 **		- All malloc'd memory associated with the linked list of
439 **		  connections will be free'd; `RmpConns' will be set to NULL.
440 **		- If LastFree is != NULL, it too will be free'd & NULL'd.
441 **
442 **	Warnings:
443 **		- This routine must be called with SIGHUP blocked.
444 */
445 void
446 FreeConns()
447 {
448 	register RMPCONN *rtmp;
449 
450 	while (RmpConns != NULL) {
451 		rtmp = RmpConns;
452 		RmpConns = RmpConns->next;
453 		FreeConn(rtmp);
454 	}
455 
456 	if (LastFree != NULL) {
457 		free((char *)LastFree);
458 		LastFree = NULL;
459 	}
460 }
461 
462 /*
463 **  AddConn -- Add a connection to the linked list of connections.
464 **
465 **	Parameters:
466 **		rconn - connection to be added.
467 **
468 **	Returns:
469 **		Nothing.
470 **
471 **	Side Effects:
472 **		- RmpConn will point to new connection.
473 **
474 **	Warnings:
475 **		- This routine must be called with SIGHUP blocked.
476 */
477 void
478 AddConn(rconn)
479 	register RMPCONN *rconn;
480 {
481 	if (RmpConns != NULL)
482 		rconn->next = RmpConns;
483 	RmpConns = rconn;
484 }
485 
486 /*
487 **  FindConn -- Find a connection in the linked list of connections.
488 **
489 **	We use the RMP (Ethernet) address as the basis for determining
490 **	if this is the same connection.  According to the Remote Maint
491 **	Protocol, we can only have one connection with any machine.
492 **
493 **	Parameters:
494 **		rconn - connection to be found.
495 **
496 **	Returns:
497 **		Matching connection from linked list or NULL if not found.
498 **
499 **	Side Effects:
500 **		None.
501 **
502 **	Warnings:
503 **		- This routine must be called with SIGHUP blocked.
504 */
505 RMPCONN *
506 FindConn(rconn)
507 	register RMPCONN *rconn;
508 {
509 	register RMPCONN *rtmp;
510 
511 	for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
512 		if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
513 		         (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0)
514 			break;
515 
516 	return(rtmp);
517 }
518 
519 /*
520 **  RemoveConn -- Remove a connection from the linked list of connections.
521 **
522 **	Parameters:
523 **		rconn - connection to be removed.
524 **
525 **	Returns:
526 **		Nothing.
527 **
528 **	Side Effects:
529 **		- If found, an RMPCONN will cease to exist and it will
530 **		  be removed from the linked list.
531 **
532 **	Warnings:
533 **		- This routine must be called with SIGHUP blocked.
534 */
535 void
536 RemoveConn(rconn)
537 	register RMPCONN *rconn;
538 {
539 	register RMPCONN *thisrconn, *lastrconn;
540 
541 	if (RmpConns == rconn) {		/* easy case */
542 		RmpConns = RmpConns->next;
543 		FreeConn(rconn);
544 	} else {				/* must traverse linked list */
545 		lastrconn = RmpConns;			/* set back ptr */
546 		thisrconn = lastrconn->next;		/* set current ptr */
547 		while (thisrconn != NULL) {
548 			if (rconn == thisrconn) {		/* found it */
549 				lastrconn->next = thisrconn->next;
550 				FreeConn(thisrconn);
551 				break;
552 			}
553 			lastrconn = thisrconn;
554 			thisrconn = thisrconn->next;
555 		}
556 	}
557 }
558