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