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.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)utils.c 8.1 (Berkeley) 6/4/93
39 * From: Utah Hdr: utils.c 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
45 static const char sccsid[] = "@(#)utils.c 8.1 (Berkeley) 6/4/93";
47 static const char rcsid[] =
51 #include <sys/param.h>
53 #include <netinet/in.h>
66 ** DispPkt -- Display the contents of an RMPCONN packet.
69 ** rconn - packet to be displayed.
70 ** direct - direction packet is going (DIR_*).
79 DispPkt(RMPCONN *rconn, int direct)
81 static const char BootFmt[] = "\t\tRetCode:%u SeqNo:%x SessID:%x Vers:%u";
82 static const char ReadFmt[] = "\t\tRetCode:%u Offset:%x SessID:%x\n";
85 struct rmp_packet *rmp;
90 * Since we will be working with RmpConns as well as DbgFp, we
91 * must block signals that can affect either.
93 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2));
95 if (DbgFp == NULL) { /* sanity */
96 (void) sigsetmask(omask);
100 /* display direction packet is going using '>>>' or '<<<' */
101 fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp);
103 /* display packet timestamp */
104 tmp = localtime((time_t *)&rconn->tstamp.tv_sec);
105 fprintf(DbgFp, "%02d:%02d:%02d.%06ld ", tmp->tm_hour, tmp->tm_min,
106 tmp->tm_sec, rconn->tstamp.tv_usec);
108 /* display src or dst addr and information about network interface */
109 fprintf(DbgFp, "Addr: %s Intf: %s\n", EnetStr(rconn), IntfName);
113 /* display IEEE 802.2 Logical Link Control header */
114 (void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n",
115 rmp->hp_llc.dsap, rmp->hp_llc.ssap, ntohs(rmp->hp_llc.cntrl));
117 /* display HP extensions to 802.2 Logical Link Control header */
118 (void) fprintf(DbgFp, "\tHP Ext: DXSAP:%x SXSAP:%x\n",
119 ntohs(rmp->hp_llc.dxsap), ntohs(rmp->hp_llc.sxsap));
122 * Display information about RMP packet using type field to
123 * determine what kind of packet this is.
125 switch(rmp->r_type) {
126 case RMP_BOOT_REQ: /* boot request */
127 (void) fprintf(DbgFp, "\tBoot Request:");
128 GETWORD(rmp->r_brq.rmp_seqno, t);
129 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
130 if (WORDZE(rmp->r_brq.rmp_seqno))
131 fputs(" (Send Server ID)", DbgFp);
133 fprintf(DbgFp," (Send Filename #%u)",t);
135 (void) fputc('\n', DbgFp);
136 (void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode,
137 t, ntohs(rmp->r_brq.rmp_session),
138 ntohs(rmp->r_brq.rmp_version));
139 (void) fprintf(DbgFp, "\n\t\tMachine Type: ");
140 for (i = 0; i < RMP_MACHLEN; i++)
141 (void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp);
142 DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm);
144 case RMP_BOOT_REPL: /* boot reply */
145 fprintf(DbgFp, "\tBoot Reply:\n");
146 GETWORD(rmp->r_brpl.rmp_seqno, t);
147 (void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode,
148 t, ntohs(rmp->r_brpl.rmp_session),
149 ntohs(rmp->r_brpl.rmp_version));
150 DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm);
152 case RMP_READ_REQ: /* read request */
153 (void) fprintf(DbgFp, "\tRead Request:\n");
154 GETWORD(rmp->r_rrq.rmp_offset, t);
155 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode,
156 t, ntohs(rmp->r_rrq.rmp_session));
157 (void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n",
158 ntohs(rmp->r_rrq.rmp_size));
160 case RMP_READ_REPL: /* read reply */
161 (void) fprintf(DbgFp, "\tRead Reply:\n");
162 GETWORD(rmp->r_rrpl.rmp_offset, t);
163 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode,
164 t, ntohs(rmp->r_rrpl.rmp_session));
165 (void) fprintf(DbgFp, "\t\tNoOfBytesSent: %zu\n",
166 rconn->rmplen - RMPREADSIZE(0));
168 case RMP_BOOT_DONE: /* boot complete */
169 (void) fprintf(DbgFp, "\tBoot Complete:\n");
170 (void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n",
171 rmp->r_done.rmp_retcode,
172 ntohs(rmp->r_done.rmp_session));
175 (void) fprintf(DbgFp, "\tUnknown Type:(%d)\n",
178 (void) fputc('\n', DbgFp);
179 (void) fflush(DbgFp);
181 (void) sigsetmask(omask); /* reset old signal mask */
186 ** GetEtherAddr -- convert an RMP (Ethernet) address into a string.
188 ** An RMP BOOT packet has been received. Look at the type field
189 ** and process Boot Requests, Read Requests, and Boot Complete
190 ** packets. Any other type will be dropped with a warning msg.
193 ** addr - array of RMP_ADDRLEN bytes.
196 ** Pointer to static string representation of `addr'.
202 ** - The return value points to a static buffer; it must
203 ** be copied if it's to be saved.
206 GetEtherAddr(u_int8_t *addr)
208 static char Hex[] = "0123456789abcdef";
209 static char etherstr[RMP_ADDRLEN*3];
214 * For each byte in `addr', convert it to "<hexchar><hexchar>:".
215 * The last byte does not get a trailing `:' appended.
220 *cp++ = Hex[*addr >> 4 & 0xf];
221 *cp++ = Hex[*addr++ & 0xf];
222 if (++i == RMP_ADDRLEN)
233 ** DispFlnm -- Print a string of bytes to DbgFp (often, a file name).
236 ** size - number of bytes to print.
237 ** flnm - address of first byte.
243 ** - Characters are sent to `DbgFp'.
246 DspFlnm(u_int size, char *flnm)
250 (void) fprintf(DbgFp, "\n\t\tFile Name (%u): <", size);
251 for (i = 0; i < size; i++)
252 (void) fputc(*flnm++, DbgFp);
253 (void) fputs(">\n", DbgFp);
258 ** NewClient -- allocate memory for a new CLIENT.
261 ** addr - RMP (Ethernet) address of new client.
264 ** Ptr to new CLIENT or NULL if we ran out of memory.
267 ** - Memory will be malloc'd for the new CLIENT.
268 ** - If malloc() fails, a log message will be generated.
271 NewClient(u_int8_t *addr)
275 if ((ctmp = (CLIENT *) malloc(sizeof(CLIENT))) == NULL) {
276 syslog(LOG_ERR, "NewClient: out of memory (%s)",
281 memset(ctmp, 0, sizeof(CLIENT));
282 memmove(&ctmp->addr[0], addr, RMP_ADDRLEN);
287 ** FreeClient -- free linked list of Clients.
296 ** - All malloc'd memory associated with the linked list of
297 ** CLIENTS will be free'd; `Clients' will be set to NULL.
300 ** - This routine must be called with SIGHUP blocked.
307 while (Clients != NULL) {
309 Clients = Clients->next;
315 ** NewStr -- allocate memory for a character array.
318 ** str - null terminated character array.
321 ** Ptr to new character array or NULL if we ran out of memory.
324 ** - Memory will be malloc'd for the new character array.
325 ** - If malloc() fails, a log message will be generated.
332 if ((stmp = (char *)malloc((unsigned) (strlen(str)+1))) == NULL) {
333 syslog(LOG_ERR, "NewStr: out of memory (%s)", str);
337 (void) strcpy(stmp, str);
342 ** To save time, NewConn and FreeConn maintain a cache of one RMPCONN
343 ** in `LastFree' (defined below).
346 static RMPCONN *LastFree = NULL;
349 ** NewConn -- allocate memory for a new RMPCONN connection.
352 ** rconn - initialization template for new connection.
355 ** Ptr to new RMPCONN or NULL if we ran out of memory.
358 ** - Memory may be malloc'd for the new RMPCONN (if not cached).
359 ** - If malloc() fails, a log message will be generated.
362 NewConn(RMPCONN *rconn)
366 if (LastFree == NULL) { /* nothing cached; make a new one */
367 if ((rtmp = (RMPCONN *) malloc(sizeof(RMPCONN))) == NULL) {
368 syslog(LOG_ERR, "NewConn: out of memory (%s)",
372 } else { /* use the cached RMPCONN */
378 * Copy template into `rtmp', init file descriptor to `-1' and
379 * set ptr to next elem NULL.
381 memmove((char *)rtmp, (char *)rconn, sizeof(RMPCONN));
389 ** FreeConn -- Free memory associated with an RMPCONN connection.
392 ** rtmp - ptr to RMPCONN to be free'd.
398 ** - Memory associated with `rtmp' may be free'd (or cached).
399 ** - File desc associated with `rtmp->bootfd' will be closed.
402 FreeConn(RMPCONN *rtmp)
405 * If the file descriptor is in use, close the file.
407 if (rtmp->bootfd >= 0) {
408 (void) close(rtmp->bootfd);
412 if (LastFree == NULL) /* cache for next time */
414 else /* already one cached; free this one */
419 ** FreeConns -- free linked list of RMPCONN connections.
428 ** - All malloc'd memory associated with the linked list of
429 ** connections will be free'd; `RmpConns' will be set to NULL.
430 ** - If LastFree is != NULL, it too will be free'd & NULL'd.
433 ** - This routine must be called with SIGHUP blocked.
440 while (RmpConns != NULL) {
442 RmpConns = RmpConns->next;
446 if (LastFree != NULL) {
447 free((char *)LastFree);
453 ** AddConn -- Add a connection to the linked list of connections.
456 ** rconn - connection to be added.
462 ** - RmpConn will point to new connection.
465 ** - This routine must be called with SIGHUP blocked.
468 AddConn(RMPCONN *rconn)
470 if (RmpConns != NULL)
471 rconn->next = RmpConns;
476 ** FindConn -- Find a connection in the linked list of connections.
478 ** We use the RMP (Ethernet) address as the basis for determining
479 ** if this is the same connection. According to the Remote Maint
480 ** Protocol, we can only have one connection with any machine.
483 ** rconn - connection to be found.
486 ** Matching connection from linked list or NULL if not found.
492 ** - This routine must be called with SIGHUP blocked.
495 FindConn(RMPCONN *rconn)
499 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
500 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
501 (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0)
508 ** RemoveConn -- Remove a connection from the linked list of connections.
511 ** rconn - connection to be removed.
517 ** - If found, an RMPCONN will cease to exist and it will
518 ** be removed from the linked list.
521 ** - This routine must be called with SIGHUP blocked.
524 RemoveConn(RMPCONN *rconn)
526 RMPCONN *thisrconn, *lastrconn;
528 if (RmpConns == rconn) { /* easy case */
529 RmpConns = RmpConns->next;
531 } else { /* must traverse linked list */
532 lastrconn = RmpConns; /* set back ptr */
533 thisrconn = lastrconn->next; /* set current ptr */
534 while (thisrconn != NULL) {
535 if (rconn == thisrconn) { /* found it */
536 lastrconn->next = thisrconn->next;
540 lastrconn = thisrconn;
541 thisrconn = thisrconn->next;