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