summaryrefslogtreecommitdiff
path: root/server/main.c
blob: f891246f5be88a3cc4dfb854bad95c0c6b421082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the main loop of the Zephyr server
 *
 *	Created by:	John T. Kohl
 *
 *	$Source$
 *	$Author$
 *
 *	Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>

#ifndef lint
static char rcsid_main_c[] = "$Header$";
static char copyright[] = "Copyright (c) 1987 Massachusetts Institute of Technology.  Portions Copyright (c) 1986 Student Information Processing Board, Massachusetts Institute of Technology\n";
#endif lint

/*
 * Server loop for Zephyr.
 */

#include "zserver.h"			/* which includes
					   zephyr/zephyr.h
					   	<errno.h>
						<sys/types.h>
						<netinet/in.h>
						<sys/time.h>
						<stdio.h>
					   <syslog.h>
					   timer.h
					 */

#include <netdb.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/ioctl.h>

#define	EVER		(;;)		/* don't stop looping */
#define	max(a,b)	((a) > (b) ? (a) : (b))

static int do_net_setup(), initialize();
static struct in_addr *get_server_addrs();
static void usage();
struct in_addr *get_server_addrs();
char *rindex();

int srv_socket;				/* dgram socket for clients
					   and other servers */
struct sockaddr_in sock_sin;
struct in_addr my_addr;
struct timeval nexthost_tv = {0, 0};	/* time till next host keepalive
					   initialize to zero so select doesn't
					   timeout */

int nservers;			/* number of other servers */
ZServerDesc_t *otherservers;		/* points to an array of the known
					   servers */
ZServerDesc_t *me_server;		/* pointer to my entry in the array */
ZNotAcked_t *nacklist;			/* list of packets waiting for ack's */

char *programname;			/* set to the last element of argv[0] */
char myname[MAXHOSTNAMELEN];		/* my host name */

int zdebug = 0;

main(argc,argv)
int argc;
char **argv;
{
	int nfound;			/* #fildes ready on select */
	int nfildes;			/* number to look at in select() */
	int packetsize;			/* size of packet received */
	int authentic;			/* authentic flag for ZParseNotice */
	Code_t status;
	ZPacket_t new_packet;		/* from the network */
	ZNotice_t new_notice;		/* parsed from new_packet */
	fd_set readable, interesting;
	struct timeval *tvp;
	struct sockaddr_in whoisit;	/* for holding peer's address */

	int optchar;			/* option processing */
	extern char *optarg;
	extern int optind;

	/* set name */
	if (programname = rindex(argv[0],'/'))
		programname++;
	else programname = argv[0];

	/* process arguments */
	
	argv++, argc--;

	while ((optchar = getopt(argc, argv, "d")) != EOF) {
		switch(optchar) {
		case 'd':
			zdebug = 1;
		case '?':
		default:
			usage();
			/*NOTREACHED*/
		}
	}

	/* open log */
	/* XXX eventually make this LOG_DAEMON */
	openlog(programname, LOG_PID, LOG_LOCAL6);

	/* set up sockets & my_addr and myname, 
	   find other servers and set up server table, initialize queues
	   for retransmits, initialize error tables,
	   set up restricted classes */
	if (initialize())
		exit(1);

	FD_ZERO(&interesting);
	FD_SET(srv_socket, &interesting);

	nfildes = srv_socket + 1;


	/* GO! */
	syslog(LOG_INFO, "Ready for action");

	for EVER {
		tvp = &nexthost_tv;
		if (nexttimo != 0L) {
			nexthost_tv.tv_sec = nexttimo - NOW;
			nexthost_tv.tv_usec = 0;
			if (nexthost_tv.tv_sec < 0) { /* timeout has passed! */
				/* so we process one timeout, then pop to
				   select, polling for input.  This way we get
				   work done even if swamped with many
				   timeouts */
				/* this will reset nexttimo */
				(void) timer_process();
				nexthost_tv.tv_sec = 0;
			}
		} else {			/* no timeouts to process */
			tvp = (struct timeval *) NULL;
		}
		readable = interesting;
		nfound = select(nfildes, &readable, (fd_set *) NULL,
				(fd_set *) NULL, tvp);
		if (nfound < 0) {
			syslog(LOG_WARNING, "select error: %m");
			continue;
		}
		if (nfound == 0)
			/* either we timed out for keepalive or we were just
			   polling for input.  Either way we want to continue
			   the loop, and process the next timeout */
			continue;
		else {
			if (FD_ISSET(srv_socket, &readable)) {
				/* handle traffic */
				
				if (status = ZReceiveNotice(new_packet,
							    sizeof(new_packet),
							    &new_notice,
							    &authentic,
							    &whoisit)) {
					syslog(LOG_ERR,
					       "bad notice receive: %s",
					       error_message(status));
					continue;
				}
				dispatch(&new_notice, authentic, &whoisit);
			} else
				syslog(LOG_ERR, "select weird?!?!");
		}
	}
}

/* Initialize net stuff.
   Contact Hesiod to find all the other servers, allocate space for the
   structure, initialize them all to SERV_DEAD with expired timeouts.
   Initialize the packet ack queues to be empty.
   Initialize the error tables.
   */

static int
initialize()
{
	register int i;
	struct in_addr *serv_addr, *hes_addrs;

	if (do_net_setup())
		return(1);

	/* talk to hesiod here, set nservers */
	if ((hes_addrs = get_server_addrs(&nservers)) ==
	    (struct in_addr *) NULL) {
		    syslog(LOG_ERR, "No servers?!?");
		    exit(1);
	    }

	otherservers = (ZServerDesc_t *) malloc(nservers *
						sizeof(ZServerDesc_t));
	for (serv_addr = hes_addrs, i = 0; serv_addr; serv_addr++, i++) {
		if (!bcmp(serv_addr, &my_addr, sizeof(struct sockaddr_in)))
			me_server = &otherservers[i];
		otherservers[i].zs_state = SERV_DEAD;
		otherservers[i].zs_timeout = 0;	/* he's due NOW */
		otherservers[i].zs_numsent = 0;
		otherservers[i].zs_addr.sin_family = AF_INET;
		/* he listens to the same port we do */
		otherservers[i].zs_addr.sin_port = sock_sin.sin_port;
		otherservers[i].zs_addr.sin_addr = *serv_addr;

		/* set up a timer for this server */
		otherservers[i].zs_timer =
			timer_set_rel(0L,
				      server_timo,
				      (caddr_t) &otherservers[i]);
		if ((otherservers[i].zs_hosts =
		     (ZHostList_t *) malloc(sizeof(ZHostList_t))) == NULLZHLT)
		{
			/* unrecoverable */
			syslog(LOG_CRIT, "zs_host malloc");
			abort();
		}
	}
	free(hes_addrs);
	if ((nacklist = (ZNotAcked_t *) malloc(sizeof(ZNotAcked_t))) ==
		(ZNotAcked_t *) NULL)
	{
		/* unrecoverable */
		syslog(LOG_CRIT, "nacklist malloc");
		abort();
	}
	nacklist->q_forw = nacklist->q_back = NULL;

	nexttimo = 1L;			/* trigger the timers when we hit
					   the FOR loop */

	ZInitialize();			/* set up the library */
	init_zsrv_err_tbl();		/* set up err table */

	ZSetFD(srv_socket);		/* set up the socket as the
					   input fildes */

	return;
}

/* 
 * Set up the server and client sockets, and initialize my_addr and myname
 */

static int
do_net_setup()
{
	struct servent *sp;
	struct hostent *hp;
	char hostname[MAXHOSTNAMELEN+1];
	int on = 1;

	if (gethostname(hostname, MAXHOSTNAMELEN+1)) {
		syslog(LOG_ERR, "no hostname: %m");
		return(1);
	}
	if ((hp = gethostbyname(hostname)) == (struct hostent *) NULL) {
		syslog(LOG_ERR, "no gethostbyname repsonse");
		strncpy(myname, hostname, MAXHOSTNAMELEN);
		return(1);
	}
	strncpy(myname, hp->h_name, MAXHOSTNAMELEN);
	bcopy(hp->h_addr, &my_addr, sizeof(hp->h_addr));
	
	/* note that getservbyname may actually ask hesiod and not
	   /etc/services */
	(void) setservent(1);		/* keep file/connection open */
	
	if ((sp = getservbyname("zephyr-clt", "udp")) ==
	    (struct servent *) NULL) {
		syslog(LOG_ERR, "zephyr-clt/udp unknown");
		return(1);
	}
	bzero(sock_sin, sizeof(sock_sin));
	bcopy(&sock_sin.sin_port, sp->s_port, sizeof(sp->s_port));
	
	(void) endservent();
	
	if ((srv_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		syslog(LOG_ERR, "client_sock failed: %m");
		return(1);
	}
	if (bind (srv_socket, (struct sockaddr *) &sock_sin,
		  sizeof(sock_sin)) < 0) {
		syslog(LOG_ERR, "client bind failed: %m");
		return(1);
	}

	/* set not-blocking */
	(void) ioctl(srv_socket, FIONBIO, &on);

	return(0);
}    


/* get a list of server addresses, from Hesiod.  Return a pointer to an
   array of allocated storage.  This storage is freed by the caller.
   */
static struct in_addr *
get_server_addrs(number)
int *number;				/* RETURN */
{
	register int i;
	char **hes_resolve();
	char **server_hosts;
	register char **cpp;
	struct in_addr *addrs;
	register struct in_addr *addr;
	register struct hostent *hp;

	/* get the names from Hesiod */
	if ((server_hosts = hes_resolve("*","ZEPHYR-SERVER")) == (char **)NULL)
		return((struct in_addr *)NULL);

	/* count up */
	for (cpp = server_hosts, i = 0; *cpp; cpp++, i++);
	
	addrs = (struct in_addr *) malloc(i * sizeof(struct in_addr));

	/* Convert to in_addr's */
	for (cpp = server_hosts, addr = addrs, i = 0; *cpp; cpp++) {
		hp = gethostbyname(*cpp);
		if (hp) {
			bcopy(hp->h_addr, addr, sizeof(struct in_addr));
			addr++, i++;
		} else
			syslog(LOG_WARNING, "hostname failed, %s",*cpp);
	}
	*number = i;
	return(addrs);
}

static void
usage()
{
	fprintf(stderr,"Usage: %s [-d]\n",programname);
	exit(2);
}