xref: /freebsd/libexec/rbootd/utils.c (revision a6fe717c2a876105123214c05176cd74106fb94b)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
40559b331SSteve Price  * Copyright (c) 1988, 1992 The University of Utah and the Center
50559b331SSteve Price  *	for Software Science (CSS).
60559b331SSteve Price  * Copyright (c) 1992, 1993
70559b331SSteve Price  *	The Regents of the University of California.  All rights reserved.
80559b331SSteve Price  *
90559b331SSteve Price  * This code is derived from software contributed to Berkeley by
100559b331SSteve Price  * the Center for Software Science of the University of Utah Computer
110559b331SSteve Price  * Science Department.  CSS requests users of this software to return
120559b331SSteve Price  * to css-dist@cs.utah.edu any improvements that they make and grant
130559b331SSteve Price  * CSS redistribution rights.
140559b331SSteve Price  *
150559b331SSteve Price  * Redistribution and use in source and binary forms, with or without
160559b331SSteve Price  * modification, are permitted provided that the following conditions
170559b331SSteve Price  * are met:
180559b331SSteve Price  * 1. Redistributions of source code must retain the above copyright
190559b331SSteve Price  *    notice, this list of conditions and the following disclaimer.
200559b331SSteve Price  * 2. Redistributions in binary form must reproduce the above copyright
210559b331SSteve Price  *    notice, this list of conditions and the following disclaimer in the
220559b331SSteve Price  *    documentation and/or other materials provided with the distribution.
235efaea4cSChristian Brueffer  * 3. Neither the name of the University nor the names of its contributors
240559b331SSteve Price  *    may be used to endorse or promote products derived from this software
250559b331SSteve Price  *    without specific prior written permission.
260559b331SSteve Price  *
270559b331SSteve Price  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
280559b331SSteve Price  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
290559b331SSteve Price  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
300559b331SSteve Price  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
310559b331SSteve Price  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
320559b331SSteve Price  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
330559b331SSteve Price  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
340559b331SSteve Price  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
350559b331SSteve Price  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
360559b331SSteve Price  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
370559b331SSteve Price  * SUCH DAMAGE.
380559b331SSteve Price  *
390559b331SSteve Price  * From: Utah Hdr: utils.c 3.1 92/07/06
400559b331SSteve Price  * Author: Jeff Forys, University of Utah CSS
410559b331SSteve Price  */
420559b331SSteve Price 
430559b331SSteve Price #include <sys/param.h>
445c8709fdSSteve Price #include <sys/time.h>
4540905e90SStefan Farfeleder #include <netinet/in.h>
460559b331SSteve Price 
470559b331SSteve Price #include <fcntl.h>
480559b331SSteve Price #include <signal.h>
490559b331SSteve Price #include <stdio.h>
500559b331SSteve Price #include <stdlib.h>
510559b331SSteve Price #include <string.h>
520559b331SSteve Price #include <syslog.h>
530559b331SSteve Price #include <time.h>
540559b331SSteve Price #include <unistd.h>
550559b331SSteve Price #include "defs.h"
560559b331SSteve Price 
570559b331SSteve Price /*
580559b331SSteve Price **  DispPkt -- Display the contents of an RMPCONN packet.
590559b331SSteve Price **
600559b331SSteve Price **	Parameters:
610559b331SSteve Price **		rconn - packet to be displayed.
620559b331SSteve Price **		direct - direction packet is going (DIR_*).
630559b331SSteve Price **
640559b331SSteve Price **	Returns:
650559b331SSteve Price **		Nothing.
660559b331SSteve Price **
670559b331SSteve Price **	Side Effects:
680559b331SSteve Price **		None.
690559b331SSteve Price */
700559b331SSteve Price void
DispPkt(RMPCONN * rconn,int direct)71266ebcd3SWarner Losh DispPkt(RMPCONN *rconn, int direct)
720559b331SSteve Price {
732f3739b8SDimitry Andric 	static const char BootFmt[] = "\t\tRetCode:%u SeqNo:%x SessID:%x Vers:%u";
742f3739b8SDimitry Andric 	static const char ReadFmt[] = "\t\tRetCode:%u Offset:%x SessID:%x\n";
750559b331SSteve Price 
760559b331SSteve Price 	struct tm *tmp;
7711fe7d5eSSteve Price 	struct rmp_packet *rmp;
780559b331SSteve Price 	int i, omask;
790559b331SSteve Price 	u_int32_t t;
800559b331SSteve Price 
810559b331SSteve Price 	/*
820559b331SSteve Price 	 *  Since we will be working with RmpConns as well as DbgFp, we
830559b331SSteve Price 	 *  must block signals that can affect either.
840559b331SSteve Price 	 */
850559b331SSteve Price 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2));
860559b331SSteve Price 
870559b331SSteve Price 	if (DbgFp == NULL) {			/* sanity */
880559b331SSteve Price 		(void) sigsetmask(omask);
890559b331SSteve Price 		return;
900559b331SSteve Price 	}
910559b331SSteve Price 
920559b331SSteve Price 	/* display direction packet is going using '>>>' or '<<<' */
930559b331SSteve Price 	fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp);
940559b331SSteve Price 
950559b331SSteve Price 	/* display packet timestamp */
960559b331SSteve Price 	tmp = localtime((time_t *)&rconn->tstamp.tv_sec);
970559b331SSteve Price 	fprintf(DbgFp, "%02d:%02d:%02d.%06ld   ", tmp->tm_hour, tmp->tm_min,
980559b331SSteve Price 	        tmp->tm_sec, rconn->tstamp.tv_usec);
990559b331SSteve Price 
1000559b331SSteve Price 	/* display src or dst addr and information about network interface */
1010559b331SSteve Price 	fprintf(DbgFp, "Addr: %s   Intf: %s\n", EnetStr(rconn), IntfName);
1020559b331SSteve Price 
1030559b331SSteve Price 	rmp = &rconn->rmp;
1040559b331SSteve Price 
1050559b331SSteve Price 	/* display IEEE 802.2 Logical Link Control header */
1060559b331SSteve Price 	(void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n",
1070559b331SSteve Price                rmp->hp_llc.dsap, rmp->hp_llc.ssap, ntohs(rmp->hp_llc.cntrl));
1080559b331SSteve Price 
1090559b331SSteve Price 	/* display HP extensions to 802.2 Logical Link Control header */
1100559b331SSteve Price 	(void) fprintf(DbgFp, "\tHP Ext:    DXSAP:%x SXSAP:%x\n",
1110559b331SSteve Price 	               ntohs(rmp->hp_llc.dxsap), ntohs(rmp->hp_llc.sxsap));
1120559b331SSteve Price 
1130559b331SSteve Price 	/*
1140559b331SSteve Price 	 *  Display information about RMP packet using type field to
1150559b331SSteve Price 	 *  determine what kind of packet this is.
1160559b331SSteve Price 	 */
1170559b331SSteve Price 	switch(rmp->r_type) {
1180559b331SSteve Price 		case RMP_BOOT_REQ:		/* boot request */
1190559b331SSteve Price 			(void) fprintf(DbgFp, "\tBoot Request:");
1200559b331SSteve Price 			GETWORD(rmp->r_brq.rmp_seqno, t);
1210559b331SSteve Price 			if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
1220559b331SSteve Price 				if (WORDZE(rmp->r_brq.rmp_seqno))
1230559b331SSteve Price 					fputs(" (Send Server ID)", DbgFp);
1240559b331SSteve Price 				else
1250559b331SSteve Price 					fprintf(DbgFp," (Send Filename #%u)",t);
1260559b331SSteve Price 			}
1270559b331SSteve Price 			(void) fputc('\n', DbgFp);
1280559b331SSteve Price 			(void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode,
1290559b331SSteve Price 			        t, ntohs(rmp->r_brq.rmp_session),
1300559b331SSteve Price 			        ntohs(rmp->r_brq.rmp_version));
1310559b331SSteve Price 			(void) fprintf(DbgFp, "\n\t\tMachine Type: ");
1320559b331SSteve Price 			for (i = 0; i < RMP_MACHLEN; i++)
1330559b331SSteve Price 				(void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp);
1340559b331SSteve Price 			DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm);
1350559b331SSteve Price 			break;
1360559b331SSteve Price 		case RMP_BOOT_REPL:		/* boot reply */
1370559b331SSteve Price 			fprintf(DbgFp, "\tBoot Reply:\n");
1380559b331SSteve Price 			GETWORD(rmp->r_brpl.rmp_seqno, t);
1390559b331SSteve Price 			(void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode,
1400559b331SSteve Price 			        t, ntohs(rmp->r_brpl.rmp_session),
1410559b331SSteve Price 			        ntohs(rmp->r_brpl.rmp_version));
1420559b331SSteve Price 			DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm);
1430559b331SSteve Price 			break;
1440559b331SSteve Price 		case RMP_READ_REQ:		/* read request */
1450559b331SSteve Price 			(void) fprintf(DbgFp, "\tRead Request:\n");
1460559b331SSteve Price 			GETWORD(rmp->r_rrq.rmp_offset, t);
1470559b331SSteve Price 			(void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode,
1480559b331SSteve Price 			        t, ntohs(rmp->r_rrq.rmp_session));
1490559b331SSteve Price 			(void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n",
1500559b331SSteve Price 			        ntohs(rmp->r_rrq.rmp_size));
1510559b331SSteve Price 			break;
1520559b331SSteve Price 		case RMP_READ_REPL:		/* read reply */
1530559b331SSteve Price 			(void) fprintf(DbgFp, "\tRead Reply:\n");
1540559b331SSteve Price 			GETWORD(rmp->r_rrpl.rmp_offset, t);
1550559b331SSteve Price 			(void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode,
1560559b331SSteve Price 			        t, ntohs(rmp->r_rrpl.rmp_session));
1572f3739b8SDimitry Andric 			(void) fprintf(DbgFp, "\t\tNoOfBytesSent: %zu\n",
1580559b331SSteve Price 			        rconn->rmplen - RMPREADSIZE(0));
1590559b331SSteve Price 			break;
1600559b331SSteve Price 		case RMP_BOOT_DONE:		/* boot complete */
1610559b331SSteve Price 			(void) fprintf(DbgFp, "\tBoot Complete:\n");
1620559b331SSteve Price 			(void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n",
1630559b331SSteve Price 			        rmp->r_done.rmp_retcode,
1640559b331SSteve Price 			        ntohs(rmp->r_done.rmp_session));
1650559b331SSteve Price 			break;
1660559b331SSteve Price 		default:			/* ??? */
1670559b331SSteve Price 			(void) fprintf(DbgFp, "\tUnknown Type:(%d)\n",
1680559b331SSteve Price 				rmp->r_type);
1690559b331SSteve Price 	}
1700559b331SSteve Price 	(void) fputc('\n', DbgFp);
1710559b331SSteve Price 	(void) fflush(DbgFp);
1720559b331SSteve Price 
1730559b331SSteve Price 	(void) sigsetmask(omask);		/* reset old signal mask */
1740559b331SSteve Price }
1750559b331SSteve Price 
1760559b331SSteve Price 
1770559b331SSteve Price /*
1780559b331SSteve Price **  GetEtherAddr -- convert an RMP (Ethernet) address into a string.
1790559b331SSteve Price **
1800559b331SSteve Price **	An RMP BOOT packet has been received.  Look at the type field
1810559b331SSteve Price **	and process Boot Requests, Read Requests, and Boot Complete
1820559b331SSteve Price **	packets.  Any other type will be dropped with a warning msg.
1830559b331SSteve Price **
1840559b331SSteve Price **	Parameters:
1850559b331SSteve Price **		addr - array of RMP_ADDRLEN bytes.
1860559b331SSteve Price **
1870559b331SSteve Price **	Returns:
1880559b331SSteve Price **		Pointer to static string representation of `addr'.
1890559b331SSteve Price **
1900559b331SSteve Price **	Side Effects:
1910559b331SSteve Price **		None.
1920559b331SSteve Price **
1930559b331SSteve Price **	Warnings:
1940559b331SSteve Price **		- The return value points to a static buffer; it must
1950559b331SSteve Price **		  be copied if it's to be saved.
1960559b331SSteve Price */
1970559b331SSteve Price char *
GetEtherAddr(u_int8_t * addr)198266ebcd3SWarner Losh GetEtherAddr(u_int8_t *addr)
1990559b331SSteve Price {
2000559b331SSteve Price 	static char Hex[] = "0123456789abcdef";
2010559b331SSteve Price 	static char etherstr[RMP_ADDRLEN*3];
20211fe7d5eSSteve Price 	int i;
20311fe7d5eSSteve Price 	char *cp;
2040559b331SSteve Price 
2050559b331SSteve Price 	/*
2060559b331SSteve Price 	 *  For each byte in `addr', convert it to "<hexchar><hexchar>:".
2070559b331SSteve Price 	 *  The last byte does not get a trailing `:' appended.
2080559b331SSteve Price 	 */
2090559b331SSteve Price 	i = 0;
2100559b331SSteve Price 	cp = etherstr;
2110559b331SSteve Price 	for(;;) {
2120559b331SSteve Price 		*cp++ = Hex[*addr >> 4 & 0xf];
2130559b331SSteve Price 		*cp++ = Hex[*addr++ & 0xf];
2140559b331SSteve Price 		if (++i == RMP_ADDRLEN)
2150559b331SSteve Price 			break;
2160559b331SSteve Price 		*cp++ = ':';
2170559b331SSteve Price 	}
2180559b331SSteve Price 	*cp = '\0';
2190559b331SSteve Price 
2200559b331SSteve Price 	return(etherstr);
2210559b331SSteve Price }
2220559b331SSteve Price 
2230559b331SSteve Price 
2240559b331SSteve Price /*
2250559b331SSteve Price **  DispFlnm -- Print a string of bytes to DbgFp (often, a file name).
2260559b331SSteve Price **
2270559b331SSteve Price **	Parameters:
2280559b331SSteve Price **		size - number of bytes to print.
2290559b331SSteve Price **		flnm - address of first byte.
2300559b331SSteve Price **
2310559b331SSteve Price **	Returns:
2320559b331SSteve Price **		Nothing.
2330559b331SSteve Price **
2340559b331SSteve Price **	Side Effects:
2350559b331SSteve Price **		- Characters are sent to `DbgFp'.
2360559b331SSteve Price */
2370559b331SSteve Price void
DspFlnm(u_int size,char * flnm)238266ebcd3SWarner Losh DspFlnm(u_int size, char *flnm)
2390559b331SSteve Price {
24011fe7d5eSSteve Price 	int i;
2410559b331SSteve Price 
2420559b331SSteve Price 	(void) fprintf(DbgFp, "\n\t\tFile Name (%u): <", size);
2430559b331SSteve Price 	for (i = 0; i < size; i++)
2440559b331SSteve Price 		(void) fputc(*flnm++, DbgFp);
2450559b331SSteve Price 	(void) fputs(">\n", DbgFp);
2460559b331SSteve Price }
2470559b331SSteve Price 
2480559b331SSteve Price 
2490559b331SSteve Price /*
2500559b331SSteve Price **  NewClient -- allocate memory for a new CLIENT.
2510559b331SSteve Price **
2520559b331SSteve Price **	Parameters:
2530559b331SSteve Price **		addr - RMP (Ethernet) address of new client.
2540559b331SSteve Price **
2550559b331SSteve Price **	Returns:
2560559b331SSteve Price **		Ptr to new CLIENT or NULL if we ran out of memory.
2570559b331SSteve Price **
2580559b331SSteve Price **	Side Effects:
2590559b331SSteve Price **		- Memory will be malloc'd for the new CLIENT.
2600559b331SSteve Price **		- If malloc() fails, a log message will be generated.
2610559b331SSteve Price */
2620559b331SSteve Price CLIENT *
NewClient(u_int8_t * addr)263266ebcd3SWarner Losh NewClient(u_int8_t *addr)
2640559b331SSteve Price {
2650559b331SSteve Price 	CLIENT *ctmp;
2660559b331SSteve Price 
2670559b331SSteve Price 	if ((ctmp = (CLIENT *) malloc(sizeof(CLIENT))) == NULL) {
2680559b331SSteve Price 		syslog(LOG_ERR, "NewClient: out of memory (%s)",
2690559b331SSteve Price 		       GetEtherAddr(addr));
2700559b331SSteve Price 		return(NULL);
2710559b331SSteve Price 	}
2720559b331SSteve Price 
27311fe7d5eSSteve Price 	memset(ctmp, 0, sizeof(CLIENT));
27411fe7d5eSSteve Price 	memmove(&ctmp->addr[0], addr, RMP_ADDRLEN);
2750559b331SSteve Price 	return(ctmp);
2760559b331SSteve Price }
2770559b331SSteve Price 
2780559b331SSteve Price /*
2790559b331SSteve Price **  FreeClient -- free linked list of Clients.
2800559b331SSteve Price **
2810559b331SSteve Price **	Parameters:
2820559b331SSteve Price **		None.
2830559b331SSteve Price **
2840559b331SSteve Price **	Returns:
2850559b331SSteve Price **		Nothing.
2860559b331SSteve Price **
2870559b331SSteve Price **	Side Effects:
2880559b331SSteve Price **		- All malloc'd memory associated with the linked list of
2890559b331SSteve Price **		  CLIENTS will be free'd; `Clients' will be set to NULL.
2900559b331SSteve Price **
2910559b331SSteve Price **	Warnings:
2920559b331SSteve Price **		- This routine must be called with SIGHUP blocked.
2930559b331SSteve Price */
2940559b331SSteve Price void
FreeClients(void)295266ebcd3SWarner Losh FreeClients(void)
2960559b331SSteve Price {
29711fe7d5eSSteve Price 	CLIENT *ctmp;
2980559b331SSteve Price 
2990559b331SSteve Price 	while (Clients != NULL) {
3000559b331SSteve Price 		ctmp = Clients;
3010559b331SSteve Price 		Clients = Clients->next;
3020559b331SSteve Price 		FreeClient(ctmp);
3030559b331SSteve Price 	}
3040559b331SSteve Price }
3050559b331SSteve Price 
3060559b331SSteve Price /*
3070559b331SSteve Price **  NewStr -- allocate memory for a character array.
3080559b331SSteve Price **
3090559b331SSteve Price **	Parameters:
3100559b331SSteve Price **		str - null terminated character array.
3110559b331SSteve Price **
3120559b331SSteve Price **	Returns:
3130559b331SSteve Price **		Ptr to new character array or NULL if we ran out of memory.
3140559b331SSteve Price **
3150559b331SSteve Price **	Side Effects:
3160559b331SSteve Price **		- Memory will be malloc'd for the new character array.
3170559b331SSteve Price **		- If malloc() fails, a log message will be generated.
3180559b331SSteve Price */
3190559b331SSteve Price char *
NewStr(char * str)320266ebcd3SWarner Losh NewStr(char *str)
3210559b331SSteve Price {
3220559b331SSteve Price 	char *stmp;
3230559b331SSteve Price 
3240559b331SSteve Price 	if ((stmp = (char *)malloc((unsigned) (strlen(str)+1))) == NULL) {
3250559b331SSteve Price 		syslog(LOG_ERR, "NewStr: out of memory (%s)", str);
3260559b331SSteve Price 		return(NULL);
3270559b331SSteve Price 	}
3280559b331SSteve Price 
3290559b331SSteve Price 	(void) strcpy(stmp, str);
3300559b331SSteve Price 	return(stmp);
3310559b331SSteve Price }
3320559b331SSteve Price 
3330559b331SSteve Price /*
3340559b331SSteve Price **  To save time, NewConn and FreeConn maintain a cache of one RMPCONN
3350559b331SSteve Price **  in `LastFree' (defined below).
3360559b331SSteve Price */
3370559b331SSteve Price 
3380559b331SSteve Price static RMPCONN *LastFree = NULL;
3390559b331SSteve Price 
3400559b331SSteve Price /*
3410559b331SSteve Price **  NewConn -- allocate memory for a new RMPCONN connection.
3420559b331SSteve Price **
3430559b331SSteve Price **	Parameters:
3440559b331SSteve Price **		rconn - initialization template for new connection.
3450559b331SSteve Price **
3460559b331SSteve Price **	Returns:
3470559b331SSteve Price **		Ptr to new RMPCONN or NULL if we ran out of memory.
3480559b331SSteve Price **
3490559b331SSteve Price **	Side Effects:
3500559b331SSteve Price **		- Memory may be malloc'd for the new RMPCONN (if not cached).
3510559b331SSteve Price **		- If malloc() fails, a log message will be generated.
3520559b331SSteve Price */
3530559b331SSteve Price RMPCONN *
NewConn(RMPCONN * rconn)354266ebcd3SWarner Losh NewConn(RMPCONN *rconn)
3550559b331SSteve Price {
3560559b331SSteve Price 	RMPCONN *rtmp;
3570559b331SSteve Price 
3580559b331SSteve Price 	if (LastFree == NULL) {		/* nothing cached; make a new one */
3590559b331SSteve Price 		if ((rtmp = (RMPCONN *) malloc(sizeof(RMPCONN))) == NULL) {
3600559b331SSteve Price 			syslog(LOG_ERR, "NewConn: out of memory (%s)",
3610559b331SSteve Price 			       EnetStr(rconn));
3620559b331SSteve Price 			return(NULL);
3630559b331SSteve Price 		}
3640559b331SSteve Price 	} else {			/* use the cached RMPCONN */
3650559b331SSteve Price 		rtmp = LastFree;
3660559b331SSteve Price 		LastFree = NULL;
3670559b331SSteve Price 	}
3680559b331SSteve Price 
3690559b331SSteve Price 	/*
3700559b331SSteve Price 	 *  Copy template into `rtmp', init file descriptor to `-1' and
3710559b331SSteve Price 	 *  set ptr to next elem NULL.
3720559b331SSteve Price 	 */
37311fe7d5eSSteve Price 	memmove((char *)rtmp, (char *)rconn, sizeof(RMPCONN));
3740559b331SSteve Price 	rtmp->bootfd = -1;
3750559b331SSteve Price 	rtmp->next = NULL;
3760559b331SSteve Price 
3770559b331SSteve Price 	return(rtmp);
3780559b331SSteve Price }
3790559b331SSteve Price 
3800559b331SSteve Price /*
3810559b331SSteve Price **  FreeConn -- Free memory associated with an RMPCONN connection.
3820559b331SSteve Price **
3830559b331SSteve Price **	Parameters:
3840559b331SSteve Price **		rtmp - ptr to RMPCONN to be free'd.
3850559b331SSteve Price **
3860559b331SSteve Price **	Returns:
3870559b331SSteve Price **		Nothing.
3880559b331SSteve Price **
3890559b331SSteve Price **	Side Effects:
3900559b331SSteve Price **		- Memory associated with `rtmp' may be free'd (or cached).
3910559b331SSteve Price **		- File desc associated with `rtmp->bootfd' will be closed.
3920559b331SSteve Price */
3930559b331SSteve Price void
FreeConn(RMPCONN * rtmp)394266ebcd3SWarner Losh FreeConn(RMPCONN *rtmp)
3950559b331SSteve Price {
3960559b331SSteve Price 	/*
3970559b331SSteve Price 	 *  If the file descriptor is in use, close the file.
3980559b331SSteve Price 	 */
3990559b331SSteve Price 	if (rtmp->bootfd >= 0) {
4000559b331SSteve Price 		(void) close(rtmp->bootfd);
4010559b331SSteve Price 		rtmp->bootfd = -1;
4020559b331SSteve Price 	}
4030559b331SSteve Price 
4040559b331SSteve Price 	if (LastFree == NULL)		/* cache for next time */
4050559b331SSteve Price 		rtmp = LastFree;
4060559b331SSteve Price 	else				/* already one cached; free this one */
4070559b331SSteve Price 		free((char *)rtmp);
4080559b331SSteve Price }
4090559b331SSteve Price 
4100559b331SSteve Price /*
4110559b331SSteve Price **  FreeConns -- free linked list of RMPCONN connections.
4120559b331SSteve Price **
4130559b331SSteve Price **	Parameters:
4140559b331SSteve Price **		None.
4150559b331SSteve Price **
4160559b331SSteve Price **	Returns:
4170559b331SSteve Price **		Nothing.
4180559b331SSteve Price **
4190559b331SSteve Price **	Side Effects:
4200559b331SSteve Price **		- All malloc'd memory associated with the linked list of
4210559b331SSteve Price **		  connections will be free'd; `RmpConns' will be set to NULL.
4220559b331SSteve Price **		- If LastFree is != NULL, it too will be free'd & NULL'd.
4230559b331SSteve Price **
4240559b331SSteve Price **	Warnings:
4250559b331SSteve Price **		- This routine must be called with SIGHUP blocked.
4260559b331SSteve Price */
4270559b331SSteve Price void
FreeConns(void)428266ebcd3SWarner Losh FreeConns(void)
4290559b331SSteve Price {
43011fe7d5eSSteve Price 	RMPCONN *rtmp;
4310559b331SSteve Price 
4320559b331SSteve Price 	while (RmpConns != NULL) {
4330559b331SSteve Price 		rtmp = RmpConns;
4340559b331SSteve Price 		RmpConns = RmpConns->next;
4350559b331SSteve Price 		FreeConn(rtmp);
4360559b331SSteve Price 	}
4370559b331SSteve Price 
4380559b331SSteve Price 	if (LastFree != NULL) {
4390559b331SSteve Price 		free((char *)LastFree);
4400559b331SSteve Price 		LastFree = NULL;
4410559b331SSteve Price 	}
4420559b331SSteve Price }
4430559b331SSteve Price 
4440559b331SSteve Price /*
4450559b331SSteve Price **  AddConn -- Add a connection to the linked list of connections.
4460559b331SSteve Price **
4470559b331SSteve Price **	Parameters:
4480559b331SSteve Price **		rconn - connection to be added.
4490559b331SSteve Price **
4500559b331SSteve Price **	Returns:
4510559b331SSteve Price **		Nothing.
4520559b331SSteve Price **
4530559b331SSteve Price **	Side Effects:
4540559b331SSteve Price **		- RmpConn will point to new connection.
4550559b331SSteve Price **
4560559b331SSteve Price **	Warnings:
4570559b331SSteve Price **		- This routine must be called with SIGHUP blocked.
4580559b331SSteve Price */
4590559b331SSteve Price void
AddConn(RMPCONN * rconn)460266ebcd3SWarner Losh AddConn(RMPCONN *rconn)
4610559b331SSteve Price {
4620559b331SSteve Price 	if (RmpConns != NULL)
4630559b331SSteve Price 		rconn->next = RmpConns;
4640559b331SSteve Price 	RmpConns = rconn;
4650559b331SSteve Price }
4660559b331SSteve Price 
4670559b331SSteve Price /*
4680559b331SSteve Price **  FindConn -- Find a connection in the linked list of connections.
4690559b331SSteve Price **
4700559b331SSteve Price **	We use the RMP (Ethernet) address as the basis for determining
4710559b331SSteve Price **	if this is the same connection.  According to the Remote Maint
4720559b331SSteve Price **	Protocol, we can only have one connection with any machine.
4730559b331SSteve Price **
4740559b331SSteve Price **	Parameters:
4750559b331SSteve Price **		rconn - connection to be found.
4760559b331SSteve Price **
4770559b331SSteve Price **	Returns:
4780559b331SSteve Price **		Matching connection from linked list or NULL if not found.
4790559b331SSteve Price **
4800559b331SSteve Price **	Side Effects:
4810559b331SSteve Price **		None.
4820559b331SSteve Price **
4830559b331SSteve Price **	Warnings:
4840559b331SSteve Price **		- This routine must be called with SIGHUP blocked.
4850559b331SSteve Price */
4860559b331SSteve Price RMPCONN *
FindConn(RMPCONN * rconn)487266ebcd3SWarner Losh FindConn(RMPCONN *rconn)
4880559b331SSteve Price {
48911fe7d5eSSteve Price 	RMPCONN *rtmp;
4900559b331SSteve Price 
4910559b331SSteve Price 	for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
4920559b331SSteve Price 		if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
4930559b331SSteve Price 		         (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0)
4940559b331SSteve Price 			break;
4950559b331SSteve Price 
4960559b331SSteve Price 	return(rtmp);
4970559b331SSteve Price }
4980559b331SSteve Price 
4990559b331SSteve Price /*
5000559b331SSteve Price **  RemoveConn -- Remove a connection from the linked list of connections.
5010559b331SSteve Price **
5020559b331SSteve Price **	Parameters:
5030559b331SSteve Price **		rconn - connection to be removed.
5040559b331SSteve Price **
5050559b331SSteve Price **	Returns:
5060559b331SSteve Price **		Nothing.
5070559b331SSteve Price **
5080559b331SSteve Price **	Side Effects:
5090559b331SSteve Price **		- If found, an RMPCONN will cease to exist and it will
5100559b331SSteve Price **		  be removed from the linked list.
5110559b331SSteve Price **
5120559b331SSteve Price **	Warnings:
5130559b331SSteve Price **		- This routine must be called with SIGHUP blocked.
5140559b331SSteve Price */
5150559b331SSteve Price void
RemoveConn(RMPCONN * rconn)516266ebcd3SWarner Losh RemoveConn(RMPCONN *rconn)
5170559b331SSteve Price {
51811fe7d5eSSteve Price 	RMPCONN *thisrconn, *lastrconn;
5190559b331SSteve Price 
5200559b331SSteve Price 	if (RmpConns == rconn) {		/* easy case */
5210559b331SSteve Price 		RmpConns = RmpConns->next;
5220559b331SSteve Price 		FreeConn(rconn);
5230559b331SSteve Price 	} else {				/* must traverse linked list */
5240559b331SSteve Price 		lastrconn = RmpConns;			/* set back ptr */
5250559b331SSteve Price 		thisrconn = lastrconn->next;		/* set current ptr */
5260559b331SSteve Price 		while (thisrconn != NULL) {
5270559b331SSteve Price 			if (rconn == thisrconn) {		/* found it */
5280559b331SSteve Price 				lastrconn->next = thisrconn->next;
5290559b331SSteve Price 				FreeConn(thisrconn);
5300559b331SSteve Price 				break;
5310559b331SSteve Price 			}
5320559b331SSteve Price 			lastrconn = thisrconn;
5330559b331SSteve Price 			thisrconn = thisrconn->next;
5340559b331SSteve Price 		}
5350559b331SSteve Price 	}
5360559b331SSteve Price }
537