]>
git.saurik.com Git - apple/network_cmds.git/blob - rbootd.tproj/utils.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1992 The University of Utah and the Center
26 * for Software Science (CSS).
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * the Center for Software Science of the University of Utah Computer
32 * Science Department. CSS requests users of this software to return
33 * to css-dist@cs.utah.edu any improvements that they make and grant
34 * CSS redistribution rights.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)utils.c 8.2 (Berkeley) 2/22/94
66 * Utah $Hdr: utils.c 3.1 92/07/06$
67 * Author: Jeff Forys, University of Utah CSS
71 static char sccsid
[] = "@(#)utils.c 8.2 (Berkeley) 2/22/94";
74 #include <sys/param.h>
88 ** DispPkt -- Display the contents of an RMPCONN packet.
91 ** rconn - packet to be displayed.
92 ** direct - direction packet is going (DIR_*).
101 DispPkt(rconn
, direct
)
105 static char BootFmt
[] = "\t\tRetCode:%u SeqNo:%lx SessID:%x Vers:%u";
106 static char ReadFmt
[] = "\t\tRetCode:%u Offset:%lx SessID:%x\n";
109 register struct rmp_packet
*rmp
;
114 * Since we will be working with RmpConns as well as DbgFp, we
115 * must block signals that can affect either.
117 omask
= sigblock(sigmask(SIGHUP
)|sigmask(SIGUSR1
)|sigmask(SIGUSR2
));
119 if (DbgFp
== NULL
) { /* sanity */
120 (void) sigsetmask(omask
);
124 /* display direction packet is going using '>>>' or '<<<' */
125 fputs((direct
==DIR_RCVD
)?"<<< ":(direct
==DIR_SENT
)?">>> ":"", DbgFp
);
127 /* display packet timestamp */
128 tmp
= localtime((time_t *)&rconn
->tstamp
.tv_sec
);
129 fprintf(DbgFp
, "%02d:%02d:%02d.%06ld ", tmp
->tm_hour
, tmp
->tm_min
,
130 tmp
->tm_sec
, rconn
->tstamp
.tv_usec
);
132 /* display src or dst addr and information about network interface */
133 fprintf(DbgFp
, "Addr: %s Intf: %s\n", EnetStr(rconn
), IntfName
);
137 /* display IEEE 802.2 Logical Link Control header */
138 (void) fprintf(DbgFp
, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n",
139 rmp
->hp_llc
.dsap
, rmp
->hp_llc
.ssap
, rmp
->hp_llc
.cntrl
);
141 /* display HP extensions to 802.2 Logical Link Control header */
142 (void) fprintf(DbgFp
, "\tHP Ext: DXSAP:%x SXSAP:%x\n",
143 rmp
->hp_llc
.dxsap
, rmp
->hp_llc
.sxsap
);
146 * Display information about RMP packet using type field to
147 * determine what kind of packet this is.
149 switch(rmp
->r_type
) {
150 case RMP_BOOT_REQ
: /* boot request */
151 (void) fprintf(DbgFp
, "\tBoot Request:");
152 GETWORD(rmp
->r_brq
.rmp_seqno
, t
);
153 if (rmp
->r_brq
.rmp_session
== RMP_PROBESID
) {
154 if (WORDZE(rmp
->r_brq
.rmp_seqno
))
155 fputs(" (Send Server ID)", DbgFp
);
157 fprintf(DbgFp
," (Send Filename #%u)",t
);
159 (void) fputc('\n', DbgFp
);
160 (void) fprintf(DbgFp
, BootFmt
, rmp
->r_brq
.rmp_retcode
,
161 t
, rmp
->r_brq
.rmp_session
,
162 rmp
->r_brq
.rmp_version
);
163 (void) fprintf(DbgFp
, "\n\t\tMachine Type: ");
164 for (i
= 0; i
< RMP_MACHLEN
; i
++)
165 (void) fputc(rmp
->r_brq
.rmp_machtype
[i
], DbgFp
);
166 DspFlnm(rmp
->r_brq
.rmp_flnmsize
, &rmp
->r_brq
.rmp_flnm
);
168 case RMP_BOOT_REPL
: /* boot reply */
169 fprintf(DbgFp
, "\tBoot Reply:\n");
170 GETWORD(rmp
->r_brpl
.rmp_seqno
, t
);
171 (void) fprintf(DbgFp
, BootFmt
, rmp
->r_brpl
.rmp_retcode
,
172 t
, rmp
->r_brpl
.rmp_session
,
173 rmp
->r_brpl
.rmp_version
);
174 DspFlnm(rmp
->r_brpl
.rmp_flnmsize
,&rmp
->r_brpl
.rmp_flnm
);
176 case RMP_READ_REQ
: /* read request */
177 (void) fprintf(DbgFp
, "\tRead Request:\n");
178 GETWORD(rmp
->r_rrq
.rmp_offset
, t
);
179 (void) fprintf(DbgFp
, ReadFmt
, rmp
->r_rrq
.rmp_retcode
,
180 t
, rmp
->r_rrq
.rmp_session
);
181 (void) fprintf(DbgFp
, "\t\tNoOfBytes: %u\n",
182 rmp
->r_rrq
.rmp_size
);
184 case RMP_READ_REPL
: /* read reply */
185 (void) fprintf(DbgFp
, "\tRead Reply:\n");
186 GETWORD(rmp
->r_rrpl
.rmp_offset
, t
);
187 (void) fprintf(DbgFp
, ReadFmt
, rmp
->r_rrpl
.rmp_retcode
,
188 t
, rmp
->r_rrpl
.rmp_session
);
189 (void) fprintf(DbgFp
, "\t\tNoOfBytesSent: %d\n",
190 rconn
->rmplen
- RMPREADSIZE(0));
192 case RMP_BOOT_DONE
: /* boot complete */
193 (void) fprintf(DbgFp
, "\tBoot Complete:\n");
194 (void) fprintf(DbgFp
, "\t\tRetCode:%u SessID:%x\n",
195 rmp
->r_done
.rmp_retcode
,
196 rmp
->r_done
.rmp_session
);
199 (void) fprintf(DbgFp
, "\tUnknown Type:(%d)\n",
202 (void) fputc('\n', DbgFp
);
203 (void) fflush(DbgFp
);
205 (void) sigsetmask(omask
); /* reset old signal mask */
210 ** GetEtherAddr -- convert an RMP (Ethernet) address into a string.
212 ** An RMP BOOT packet has been received. Look at the type field
213 ** and process Boot Requests, Read Requests, and Boot Complete
214 ** packets. Any other type will be dropped with a warning msg.
217 ** addr - array of RMP_ADDRLEN bytes.
220 ** Pointer to static string representation of `addr'.
226 ** - The return value points to a static buffer; it must
227 ** be copied if it's to be saved.
228 ** - For speed, we assume a u_char consists of 8 bits.
234 static char Hex
[] = "0123456789abcdef";
235 static char etherstr
[RMP_ADDRLEN
*3];
237 register char *cp1
, *cp2
;
240 * For each byte in `addr', convert it to "<hexchar><hexchar>:".
241 * The last byte does not get a trailing `:' appended.
247 *cp2
++ = Hex
[*cp1
>> 4 & 0xf];
248 *cp2
++ = Hex
[*cp1
++ & 0xf];
249 if (++i
== RMP_ADDRLEN
)
260 ** DispFlnm -- Print a string of bytes to DbgFp (often, a file name).
263 ** size - number of bytes to print.
264 ** flnm - address of first byte.
270 ** - Characters are sent to `DbgFp'.
279 (void) fprintf(DbgFp
, "\n\t\tFile Name (%d): <", size
);
280 for (i
= 0; i
< size
; i
++)
281 (void) fputc(*flnm
++, DbgFp
);
282 (void) fputs(">\n", DbgFp
);
287 ** NewClient -- allocate memory for a new CLIENT.
290 ** addr - RMP (Ethernet) address of new client.
293 ** Ptr to new CLIENT or NULL if we ran out of memory.
296 ** - Memory will be malloc'd for the new CLIENT.
297 ** - If malloc() fails, a log message will be generated.
305 if ((ctmp
= (CLIENT
*) malloc(sizeof(CLIENT
))) == NULL
) {
306 syslog(LOG_ERR
, "NewClient: out of memory (%s)",
311 bzero(ctmp
, sizeof(CLIENT
));
312 bcopy(addr
, &ctmp
->addr
[0], RMP_ADDRLEN
);
317 ** FreeClient -- free linked list of Clients.
326 ** - All malloc'd memory associated with the linked list of
327 ** CLIENTS will be free'd; `Clients' will be set to NULL.
330 ** - This routine must be called with SIGHUP blocked.
335 register CLIENT
*ctmp
;
337 while (Clients
!= NULL
) {
339 Clients
= Clients
->next
;
345 ** NewStr -- allocate memory for a character array.
348 ** str - null terminated character array.
351 ** Ptr to new character array or NULL if we ran out of memory.
354 ** - Memory will be malloc'd for the new character array.
355 ** - If malloc() fails, a log message will be generated.
363 if ((stmp
= (char *)malloc((unsigned) (strlen(str
)+1))) == NULL
) {
364 syslog(LOG_ERR
, "NewStr: out of memory (%s)", str
);
368 (void) strcpy(stmp
, str
);
373 ** To save time, NewConn and FreeConn maintain a cache of one RMPCONN
374 ** in `LastFree' (defined below).
377 static RMPCONN
*LastFree
= NULL
;
380 ** NewConn -- allocate memory for a new RMPCONN connection.
383 ** rconn - initialization template for new connection.
386 ** Ptr to new RMPCONN or NULL if we ran out of memory.
389 ** - Memory may be malloc'd for the new RMPCONN (if not cached).
390 ** - If malloc() fails, a log message will be generated.
398 if (LastFree
== NULL
) { /* nothing cached; make a new one */
399 if ((rtmp
= (RMPCONN
*) malloc(sizeof(RMPCONN
))) == NULL
) {
400 syslog(LOG_ERR
, "NewConn: out of memory (%s)",
404 } else { /* use the cached RMPCONN */
410 * Copy template into `rtmp', init file descriptor to `-1' and
411 * set ptr to next elem NULL.
413 bcopy((char *)rconn
, (char *)rtmp
, sizeof(RMPCONN
));
421 ** FreeConn -- Free memory associated with an RMPCONN connection.
424 ** rtmp - ptr to RMPCONN to be free'd.
430 ** - Memory associated with `rtmp' may be free'd (or cached).
431 ** - File desc associated with `rtmp->bootfd' will be closed.
435 register RMPCONN
*rtmp
;
438 * If the file descriptor is in use, close the file.
440 if (rtmp
->bootfd
>= 0) {
441 (void) close(rtmp
->bootfd
);
445 if (LastFree
== NULL
) /* cache for next time */
447 else /* already one cached; free this one */
452 ** FreeConns -- free linked list of RMPCONN connections.
461 ** - All malloc'd memory associated with the linked list of
462 ** connections will be free'd; `RmpConns' will be set to NULL.
463 ** - If LastFree is != NULL, it too will be free'd & NULL'd.
466 ** - This routine must be called with SIGHUP blocked.
471 register RMPCONN
*rtmp
;
473 while (RmpConns
!= NULL
) {
475 RmpConns
= RmpConns
->next
;
479 if (LastFree
!= NULL
) {
480 free((char *)LastFree
);
486 ** AddConn -- Add a connection to the linked list of connections.
489 ** rconn - connection to be added.
495 ** - RmpConn will point to new connection.
498 ** - This routine must be called with SIGHUP blocked.
502 register RMPCONN
*rconn
;
504 if (RmpConns
!= NULL
)
505 rconn
->next
= RmpConns
;
510 ** FindConn -- Find a connection in the linked list of connections.
512 ** We use the RMP (Ethernet) address as the basis for determining
513 ** if this is the same connection. According to the Remote Maint
514 ** Protocol, we can only have one connection with any machine.
517 ** rconn - connection to be found.
520 ** Matching connection from linked list or NULL if not found.
526 ** - This routine must be called with SIGHUP blocked.
530 register RMPCONN
*rconn
;
532 register RMPCONN
*rtmp
;
534 for (rtmp
= RmpConns
; rtmp
!= NULL
; rtmp
= rtmp
->next
)
535 if (bcmp((char *)&rconn
->rmp
.hp_hdr
.saddr
[0],
536 (char *)&rtmp
->rmp
.hp_hdr
.saddr
[0], RMP_ADDRLEN
) == 0)
543 ** RemoveConn -- Remove a connection from the linked list of connections.
546 ** rconn - connection to be removed.
552 ** - If found, an RMPCONN will cease to exist and it will
553 ** be removed from the linked list.
556 ** - This routine must be called with SIGHUP blocked.
560 register RMPCONN
*rconn
;
562 register RMPCONN
*thisrconn
, *lastrconn
;
564 if (RmpConns
== rconn
) { /* easy case */
565 RmpConns
= RmpConns
->next
;
567 } else { /* must traverse linked list */
568 lastrconn
= RmpConns
; /* set back ptr */
569 thisrconn
= lastrconn
->next
; /* set current ptr */
570 while (thisrconn
!= NULL
) {
571 if (rconn
== thisrconn
) { /* found it */
572 lastrconn
->next
= thisrconn
->next
;
576 lastrconn
= thisrconn
;
577 thisrconn
= thisrconn
->next
;