1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988, 1992 The University of Utah and the Center
5 *	for Software Science (CSS).
6 * Copyright (c) 1992, 1993
7 *	The Regents of the University of California.  All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Center for Software Science of the University of Utah Computer
11 * Science Department.  CSS requests users of this software to return
12 * to css-dist@cs.utah.edu any improvements that they make and grant
13 * CSS redistribution rights.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * From: Utah Hdr: defs.h 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
41 */
42
43#include "rmp.h"
44#include "rmp_var.h"
45
46/*
47**  Common #define's and external variables.  All other files should
48**  include this.
49*/
50
51/*
52 *  This may be defined in <sys/param.h>, if not, it's defined here.
53 */
54#ifndef	MAXHOSTNAMELEN
55#define	MAXHOSTNAMELEN 256
56#endif
57
58/*
59 *  SIGUSR1 and SIGUSR2 are defined in <signal.h> for 4.3BSD systems.
60 */
61#ifndef SIGUSR1
62#define	SIGUSR1 SIGEMT
63#endif
64#ifndef SIGUSR2
65#define	SIGUSR2 SIGFPE
66#endif
67
68/*
69 *  These can be faster & more efficient than strcmp()/strncmp()...
70 */
71#define	STREQN(s1,s2)		((*s1 == *s2) && (strcmp(s1,s2) == 0))
72#define	STRNEQN(s1,s2,n)	((*s1 == *s2) && (strncmp(s1,s2,n) == 0))
73
74/*
75 *  Configuration file limitations.
76 */
77#define	C_MAXFILE	10		/* max number of boot-able files */
78#define	C_LINELEN	1024		/* max length of line */
79
80/*
81 *  Direction of packet (used as argument to DispPkt).
82 */
83#define	DIR_RCVD	0
84#define	DIR_SENT	1
85#define	DIR_NONE	2
86
87/*
88 *  These need not be functions, so...
89 */
90#define	FreeStr(str)	free(str)
91#define	FreeClient(cli)	free(cli)
92#define	GenSessID()	(++SessionID ? SessionID: ++SessionID)
93
94/*
95 *  Converting an Ethernet address to a string is done in many routines.
96 *  Using `rmp.hp_hdr.saddr' works because this field is *never* changed;
97 *  it will *always* contain the source address of the packet.
98 */
99#define	EnetStr(rptr)	GetEtherAddr(&(rptr)->rmp.hp_hdr.saddr[0])
100
101/*
102 *  Every machine we can boot will have one of these allocated for it
103 *  (unless there are no restrictions on who we can boot).
104 */
105typedef struct client_s {
106	u_int8_t		addr[RMP_ADDRLEN];	/* addr of machine */
107	char			*files[C_MAXFILE];	/* boot-able files */
108	struct client_s		*next;			/* ptr to next */
109} CLIENT;
110
111/*
112 *  Every active connection has one of these allocated for it.
113 */
114typedef struct rmpconn_s {
115	struct rmp_packet	rmp;			/* RMP packet */
116	int			rmplen;			/* length of packet */
117	struct timeval		tstamp;			/* last time active */
118	int			bootfd;			/* open boot file */
119	struct rmpconn_s	*next;			/* ptr to next */
120} RMPCONN;
121
122/*
123 *  All these variables are defined in "conf.c".
124 */
125extern	char	MyHost[];		/* this hosts' name */
126extern	pid_t	MyPid;			/* this processes' ID */
127extern	int	DebugFlg;		/* set true if debugging */
128extern	int	BootAny;		/* set true if we can boot anyone */
129
130extern	char	*ConfigFile;		/* configuration file */
131extern	char	*DfltConfig;		/* default configuration file */
132extern	char	*DbgFile;		/* debug output file */
133extern	char	*PidFile;		/* file containing pid of server */
134extern	char	*BootDir;		/* directory w/boot files */
135
136extern	FILE	*DbgFp;			/* debug file pointer */
137extern	char	*IntfName;		/* interface we are attached to */
138
139extern	u_int16_t SessionID;		/* generated session ID */
140
141extern	char	*BootFiles[];		/* list of boot files */
142
143extern	CLIENT	*Clients;		/* list of addrs we'll accept */
144extern	RMPCONN	*RmpConns;		/* list of active connections */
145
146extern	u_int8_t RmpMcastAddr[];	/* RMP multicast address */
147
148void	 AddConn(RMPCONN *);
149int	 BootDone(RMPCONN *);
150void	 BpfClose(void);
151char	*BpfGetIntfName(char **);
152int	 BpfOpen(void);
153int	 BpfRead(RMPCONN *, int);
154int	 BpfWrite(RMPCONN *);
155void	 DebugOff(int);
156void	 DebugOn(int);
157void	 DispPkt(RMPCONN *, int);
158void	 DoTimeout(void);
159void	 DspFlnm(u_int, char *);
160void	 Exit(int);
161CLIENT	*FindClient(RMPCONN *);
162RMPCONN	*FindConn(RMPCONN *);
163void	 FreeClients(void);
164void	 FreeConn(RMPCONN *);
165void	 FreeConns(void);
166int	 GetBootFiles(void);
167char	*GetEtherAddr(u_int8_t *);
168CLIENT	*NewClient(u_int8_t *);
169RMPCONN	*NewConn(RMPCONN *);
170char	*NewStr(char *);
171u_int8_t *ParseAddr(char *);
172int	 ParseConfig(void);
173void	 ProcessPacket(RMPCONN *, CLIENT *);
174void	 ReConfig(int);
175void	 RemoveConn(RMPCONN *);
176int	 SendBootRepl(struct rmp_packet *, RMPCONN *, char *[]);
177int	 SendFileNo(struct rmp_packet *, RMPCONN *, char *[]);
178int	 SendPacket(RMPCONN *);
179int	 SendReadRepl(RMPCONN *);
180int	 SendServerID(RMPCONN *);
181