summaryrefslogtreecommitdiff
path: root/server/hostm.c
blob: 5a1abc870ce76045e3ac1c2e4ccaf3461caf35df (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains functions for communicating with the HostManager.
 *
 *	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
#ifndef SABER
static char rcsid_hostm_s_c[] = "$Header$";
#endif SABER
#endif lint

#include "zserver.h"

/*
 *
 * External functions:
 *
 * void hostm_dispatch(notice, auth, who)
 *	ZNotice_t *notice;
 *	int auth;
 *	struct sockaddr_in *who;
 *
 * void hostm_flush(host)
 *	ZHostList_t *host;
 *
 * ZHostList_t *hostm_find_host(addr)
 *	struct in_addr *addr;
 *
 * ZServerDesc_t *hostm_find_server(addr)
 *	struct in_addr *addr;
 *
 * void hostm_shutdown()
 *
 */

struct hostlist {
	ZHostList_t *host;
	ZServerDesc_t *server;
};

#define	NULLHLT		((struct hostlist *) 0)

static struct hostlist *all_hosts;

static int num_hosts;			/* number of hosts in all_hosts */


static void host_detach(), flush(), deathgram(), insert_host(), remove_host();
static Code_t host_attach();
static int cmp_hostlist();

/*
 * We received a HostManager packet.  process accordingly.
 */
void
hostm_dispatch(notice, auth, who)
ZNotice_t *notice;
int auth;
struct sockaddr_in *who;
{
	ZServerDesc_t *owner;
	ZHostList_t *host;
	char *opcode = notice->z_opcode;
	Code_t retval;


	zdbug1("hm_disp");
	owner = hostm_find_server(&who->sin_addr);
	if (!strcmp(opcode, HM_BOOT)) {
		zdbug2("boot %s",inet_ntoa(who->sin_addr));
		if (owner == &otherservers[me_server_idx]) {
			zdbug1("hm_disp flushing");
			/* I own him.  Just cancel any subscriptions */
			flush(who, me_server);
		
		} else if (owner == NULLZSDT) {
			zdbug1("acquiring");
			/* no owner.  Acquire him. */
			if ((retval = host_attach(who, me_server)) != ZERR_NONE) {
				syslog(LOG_WARNING, "hattach failed: %s",
				       error_message(retval));
				return;
			}
		} else {
			zdbug1("hostm_flush'ing");
			/* He has switched servers.  Take him, then
			   tell the owner and other hosts to flush. */
			hostm_flush(hostm_find_host(&who->sin_addr), owner);
			/* XXX tell other servers */

			if ((retval = host_attach(who, me_server)) != ZERR_NONE)
				syslog(LOG_WARNING, "hattach failed: %s",
				       error_message(retval));
		}
		ack(notice, who);
	} else if (!strcmp(opcode, HM_FLUSH)) {
		zdbug2("hm_disp flush %s", inet_ntoa(who->sin_addr));
		if (owner == NULLZSDT || (host = hostm_find_host(&who->sin_addr)) == NULLZHLT)
			return;
		hostm_flush(host, owner);
		return;
	} else {
		syslog(LOG_WARNING, "hm_disp: unknown opcode %s",opcode);
		return;
	}
	return;
}

void
hostm_flush(host, server)
ZHostList_t *host;
ZServerDesc_t *server;
{
	register ZClientList_t *clist = NULLZCLT, *clt;

	zdbug1("hostm_flush");
	if ((clist = host->zh_clients) != NULLZCLT)
		for (clt = clist->q_forw; clt != clist; clt = clist->q_forw)
			/* client_deregister frees this client & subscriptions */
			/* and remque()s the client */
			client_deregister(clt->zclt_client, host);

	uloc_hflush(&host->zh_addr);
	host_detach(&host->zh_addr.sin_addr, server);
	/* XXX tell other servers */
	return;
}

/*
 * send a shutdown to each of our hosts, then tell the other servers
 */
void
hostm_shutdown()
{
	register ZHostList_t *hosts = otherservers[me_server_idx].zs_hosts;
	register ZHostList_t *host;

	zdbug1("hostm_shutdown");
	if (hosts == NULLZHLT)
		return;

	/* kill them all */
	for (host = hosts->q_forw;
	     host != hosts;
	     host = host->q_forw)
		deathgram(&host->zh_addr);
	
	/* XXX tell other servers */

	return;
}

/*
 * Binary search on the host table to find this host.
 */

ZHostList_t *
hostm_find_host(addr)
struct in_addr *addr;
{
	register int i, rlo, rhi;

	if (all_hosts == NULLHLT)
		return(NULLZHLT);

	/* i is the current host we are checking */
	/* rlo is the lowest we will still check, rhi is the highest we will
	   still check */

	i = num_hosts >> 1;		/* start in the middle */
	rlo = 0;
	rhi = num_hosts - 1;		/* first index is 0 */

	while ((all_hosts[i].host)->zh_addr.sin_addr.s_addr != addr->s_addr) {
		if ((all_hosts[i].host)->zh_addr.sin_addr.s_addr < addr->s_addr)
			rlo = i + 1;
		else
			rhi = i - 1;
		if (rhi - rlo < 0)
			return(NULLZHLT);
		i = (rhi + rlo) >> 1; /* split the diff */
	}
	return(all_hosts[i].host);
}

ZServerDesc_t *
hostm_find_server(addr)
struct in_addr *addr;
{
	register int i, rlo, rhi;

	if (all_hosts == NULLHLT)
		return(NULLZSDT);

	/* i is the current host we are checking */
	/* rlo is the lowest we will still check, rhi is the highest we will
	   still check */

	i = num_hosts >> 1;		/* start in the middle */
	rlo = 0;
	rhi = num_hosts - 1;		/* first index is 0 */

	while ((all_hosts[i].host)->zh_addr.sin_addr.s_addr != addr->s_addr) {
		if ((all_hosts[i].host)->zh_addr.sin_addr.s_addr < addr->s_addr)
			rlo = i + 1;
		else
			rhi = i - 1;
		if (rhi - rlo < 0)
			return(NULLZSDT);
		i = (rhi + rlo) >> 1; /* split the diff */
	}
	return(all_hosts[i].server);
}

static void
insert_host(host, server)
ZHostList_t *host;
ZServerDesc_t *server;
{
	struct hostlist *oldlist;
	register int i = 0;

	zdbug2("insert_host %s",inet_ntoa(host->zh_addr.sin_addr));

	if (hostm_find_host(&host->zh_addr.sin_addr) != NULLZHLT)
		return;

	num_hosts++;
	oldlist = all_hosts;

	if (!oldlist) {			/* this is the first */
		if ((all_hosts = (struct hostlist *) xmalloc(num_hosts * sizeof(struct hostlist))) == NULLHLT) {
			syslog(LOG_CRIT, "insert_host: nomem");
			abort();
		}
		all_hosts[0].host = host;
		all_hosts[0].server = server;
		return;
	}

	if ((all_hosts = (struct hostlist *) realloc((caddr_t) oldlist, (unsigned) num_hosts * sizeof(struct hostlist))) == NULLHLT) {
		syslog(LOG_CRIT, "insert_host: nomem");
		abort();
	}

	all_hosts[num_hosts - 1].host = host;
	all_hosts[num_hosts - 1].server = server;

	/* sort it */

	qsort((caddr_t) all_hosts, num_hosts, sizeof(struct hostlist), cmp_hostlist);

#ifdef DEBUG
	if (zdebug) {
		char buf[512];
		for (i = 0; i < num_hosts; i++)
			syslog(LOG_DEBUG, "%d: %s %s",i,
			       strcpy(buf,inet_ntoa((all_hosts[i].host)->zh_addr.sin_addr)),
			       inet_ntoa((all_hosts[i].server)->zs_addr.sin_addr));
	}
#endif DEBUG
	return;
}

static void
remove_host(host)
ZHostList_t *host;
{
	struct hostlist *oldlist;
	register int i = 0;

	zdbug1("remove_host");
	if (hostm_find_host(&host->zh_addr.sin_addr) == NULLZHLT)
		return;

	if (--num_hosts == 0) {
		zdbug1("last host");
		xfree(all_hosts);
		all_hosts = NULLHLT;
		return;
	}

	oldlist = all_hosts;

	if ((all_hosts = (struct hostlist *) xmalloc(num_hosts * sizeof(struct hostlist))) == NULLHLT) {
		syslog(LOG_CRIT, "remove_host: nomem");
		abort();
	}

	/* copy old pointers */
	while (i < num_hosts && (oldlist[i].host)->zh_addr.sin_addr.s_addr < host->zh_addr.sin_addr.s_addr) {
		all_hosts[i] = oldlist[i];
		i++;
	}

	i++;				/* skip over this one */

	/* copy the rest */
	while (i <= num_hosts) {
		all_hosts[i - 1] = oldlist[i];
		i++;
	}
	xfree(oldlist);
#ifdef DEBUG
	if (zdebug) {
		char buf[512];
		for (i = 0; i < num_hosts; i++)
			syslog(LOG_DEBUG, "%d: %s %s",i,
			       strcpy(buf,inet_ntoa((all_hosts[i].host)->zh_addr.sin_addr)),
			       inet_ntoa((all_hosts[i].server)->zs_addr.sin_addr));
	}
#endif DEBUG
	return;
}

static int
cmp_hostlist(el1, el2)
struct hostlist *el1, *el2;
{
	if (el1->host->zh_addr.sin_addr.s_addr <
	    el2->host->zh_addr.sin_addr.s_addr) return (-1);
	else if (el1->host->zh_addr.sin_addr.s_addr ==
		 el2->host->zh_addr.sin_addr.s_addr) return (0);
	else return(1);
}
/*
 * Flush the info for this host, but maintain ownership.
 */

static void
flush(who, server)
struct sockaddr_in *who;
ZServerDesc_t *server;
{
	register ZHostList_t *hlp = server->zs_hosts;
	register ZHostList_t *hlp2;
	Code_t retval;

	zdbug2("flush %s",inet_ntoa(who->sin_addr));

	for (hlp2 = hlp->q_forw; hlp2 != hlp; hlp2 = hlp2->q_forw) {
		if (hlp2->zh_addr.sin_addr.s_addr == who->sin_addr.s_addr)
			/* already here */
			break;
	}
	if (hlp2 == hlp) {		/* not here */
		syslog(LOG_WARNING, "(h)flush: wrong server");
		return;
	}
	hostm_flush(hlp2, server);
	if ((retval = host_attach(who, server)) != ZERR_NONE)
		syslog(LOG_ERR, "flush h_attach: %s",
		       error_message(retval));
}

/*
 * attach the host with return address in who to the server.
 */

static Code_t
host_attach(who, server)
struct sockaddr_in *who;
ZServerDesc_t *server;
{
	register ZHostList_t *hlist;
	register ZClientList_t *clist;

	/* allocate a header */
	if ((hlist = (ZHostList_t *)xmalloc(sizeof(ZHostList_t))) == NULLZHLT) {
		syslog(LOG_WARNING, "hm_attach malloc");
		return(ENOMEM);
	}
	/* set up */
	if ((clist = (ZClientList_t *)xmalloc(sizeof(ZClientList_t))) == NULLZCLT) {
		xfree(hlist);
		return(ENOMEM);
	}
	clist->q_forw = clist->q_back = clist;

	hlist->zh_clients = clist;
	hlist->zh_addr = *who;
	hlist->q_forw = hlist->q_back = hlist;

	/* chain in */
	insert_host(hlist, server);
	xinsque(hlist, server->zs_hosts);
	return(ZERR_NONE);
}

static void
host_detach(addr, server)
struct in_addr *addr;
ZServerDesc_t *server;
{
	/* undo what we did in host_attach */
	register ZHostList_t *hlist;

	for (hlist = server->zs_hosts->q_forw;
	     hlist != server->zs_hosts;
	     hlist = hlist->q_forw)
		if (hlist->zh_addr.sin_addr.s_addr == addr->s_addr)
			/* found him */
			break;
	if (hlist == server->zs_hosts) {
		syslog(LOG_WARNING, "host_detach: wrong server");
		return;
	}

	xfree(hlist->zh_clients);
	xremque(hlist);
	remove_host(hlist);
	xfree(hlist);
	return;
}

static void
deathgram(sin)
struct sockaddr_in *sin;
{
	Code_t retval;
	int shutlen;
	ZNotice_t shutnotice;
	ZPacket_t shutpack;

	zdbug2("deathgram %s",inet_ntoa(sin->sin_addr));

	/* fill in the shutdown notice */

	shutnotice.z_kind = HMCTL;
	shutnotice.z_port = sock_sin.sin_port;
	shutnotice.z_class = HM_CTL_CLASS;
	shutnotice.z_class_inst = HM_CTL_SERVER;
	shutnotice.z_opcode = SERVER_SHUTDOWN;
	shutnotice.z_sender = HM_CTL_SERVER;
	shutnotice.z_recipient = "foo";
	shutnotice.z_message = NULL;
	shutnotice.z_message_len = 0;
	
	shutlen = sizeof(shutpack);
	if ((retval = ZFormatNotice(&shutnotice, shutpack, shutlen, &shutlen, 0)) != ZERR_NONE) {
		syslog(LOG_ERR, "hm_shut format: %s",error_message(retval));
		return;
	}
	if ((retval = ZSetDestAddr(sin)) != ZERR_NONE) {
		syslog(LOG_WARNING, "hm_shut set addr: %s",
		       error_message(retval));
		return;
	}
	if ((retval = ZSendPacket(shutpack, shutlen)) != ZERR_NONE) {
		syslog(LOG_WARNING, "hm_shut xmit: %s", error_message(retval));
		return;
	}
	return;
}