summaryrefslogtreecommitdiff
path: root/zhm/zhm.c
blob: ee3c9b13517ae904ef63f6193ab6856572ed4a3a (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 the hostmanager client program.
 *
 *      Created by:     David C. Jedlinsky
 *
 *      $Source$
 *      $Author$
 *
 *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h". 
 */

#include "hm.h"

#ifndef lint
#ifndef SABER
static char rcsid_hm_c[] = "$Header$";
#endif SABER
#endif lint

#include <ctype.h>
#include <signal.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <hesiod.h>
#include <sys/ioctl.h>
#include <sys/file.h>

int serv_sock, no_server = 1, timeout_type = 0, serv_loop = 0;
struct sockaddr_in cli_sin, serv_sin, from;
struct hostent *hp;
char **serv_list, **cur_serv, **clust_info;
u_short cli_port;
char hostname[MAXHOSTNAMELEN], prim_serv[MAXHOSTNAMELEN], loopback[4];

extern int errno;
extern char *index();

void init_hm(), detach(), handle_timeout(), resend_notices(), die_gracefully();
char *upcase();

main(argc, argv)
char *argv[];
{
    caddr_t packet;
    ZNotice_t notice;
    Code_t ret;

    /* Override server argument? */
    (void)gethostname(hostname, MAXHOSTNAMELEN);
    (void)strcpy(prim_serv, "");
    if (argc > 1) 
      (void)strcpy(prim_serv, argv[1]);
    else {
	  if ((clust_info = hes_resolve(hostname, "CLUSTER")) == NULL) {
		printf("No hesiod information available.\n");
		exit(ZERR_SERVNAK);
	  }
	  for ( ; *clust_info; clust_info++)
	    if (!strncmp("ZEPHYR", upcase(*clust_info), 6)) {
		  (void)strcpy(prim_serv, index(*clust_info, ' ')+1);
		  break;
	    }
    }
    
    init_hm();

    DPR2 ("zephyr server port: %u\n", ntohs(serv_sin.sin_port));
    DPR2 ("zephyr client port: %u\n", ntohs(cli_port));
    
    /* Main loop */
    for ever {
	  DPR ("Waiting for a packet...");
	  packet = (char *) malloc(Z_MAXPKTLEN);
	  ret = ZReceiveNotice(packet, Z_MAXPKTLEN, &notice, NULL, &from);
	  if ((ret != ZERR_NONE) && (ret != EINTR)){
		Zperr(ret);
		com_err("hm", ret, "receiving notice");
		free(packet);
	  } else if (ret != EINTR) {
		/* Where did it come from? */
		DPR ("Got a packet.\n");
		DPR ("notice:\n");
		DPR2("\tz_kind: %d\n", notice.z_kind);
		DPR2("\tz_port: %u\n", ntohs(notice.z_port));
		DPR2("\tz_class: %s\n", notice.z_class);
		DPR2("\tz_class_inst: %s\n", notice.z_class_inst);
		DPR2("\tz_opcode: %s\n", notice.z_opcode);
		DPR2("\tz_sender: %s\n", notice.z_sender);
		DPR2("\tz_recip: %s\n", notice.z_recipient);
		if ((notice.z_kind == SERVACK) ||
		    (notice.z_kind == SERVNAK) ||
		    (notice.z_kind == HMCTL)) {
		      server_manager(&notice);
		      free(packet);
		} else {
		      if (bcmp(loopback, &from.sin_addr, 4) == 0) {
			    /* Client program... */
			    transmission_tower(&notice, packet);
			    DPR2 ("Pending = %d\n", ZPending());
		      } else {
			    syslog(LOG_INFO, "Unknown notice type: %d",
				   notice.z_kind);
			    free(packet);
		      }
		}
	  }
    }
}

void init_hm()
{
      struct servent *sp;
      Code_t ret;

      openlog("hm", LOG_PID, LOG_DAEMON);

#ifndef DEBUG
      detach();
#endif DEBUG

      (void)ZInitialize();
      (void)ZSetServerState(1);  /* Aargh!!! */
      init_queue();
      if ((serv_list = hes_resolve("zephyr", "sloc")) == (char **)NULL) {
	    syslog(LOG_ERR, "No servers or no hesiod");
	    serv_list = (char **)malloc(sizeof(char *));
	    serv_list[0] = (char *)malloc(MAXHOSTNAMELEN);
	    strcpy(serv_list[0], prim_serv);
      }
      cur_serv = serv_list;
      if (!strcmp(prim_serv, ""))
	(void)strcpy(prim_serv, *cur_serv);
      
      loopback[0] = 127;
      loopback[1] = 0;
      loopback[2] = 0;
      loopback[3] = 1;
      
      /* Open client socket, for receiving client and server notices */
      
      if ((sp = getservbyname("zephyr-hm", "udp")) == NULL) {
	    printf("No zephyr-hm entry in /etc/services.\n");
	    exit(1);
      }
      cli_port = sp->s_port;
      
      if ((ret = ZOpenPort(&cli_port)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "opening port");
	    exit(ret);
      }
      cli_sin = ZGetDestAddr();
      cli_sin.sin_port = sp->s_port;
      
      /* Open the server socket */
      
      if ((sp = getservbyname("zephyr-clt", "udp")) == NULL) {
	    printf("No zephyr-clt entry in /etc/services.\n");
	    exit(1);
      }
      bzero(&serv_sin, sizeof(struct sockaddr_in));
      serv_sin.sin_port = sp->s_port;
      
      if ((serv_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
	    syslog(LOG_ERR, "Server socket failed.");
	    exit(1);
      }

      /* Set up communications with server */
      /* target is "zephyr-clt" port on server machine */

      serv_sin.sin_family = AF_INET;

      /* who to talk to */
      if ((hp = gethostbyname(prim_serv)) == NULL) {
	    DPR("gethostbyname failed\n");
	    find_next_server();
      } else {
	    DPR2 ("Server = %s\n", prim_serv);
	    bcopy(hp->h_addr, &serv_sin.sin_addr, hp->h_length);
      }

      send_boot_notice();

      (void)signal (SIGALRM, handle_timeout);
      (void)signal (SIGTERM, die_gracefully);
}

char *upcase(s)
     register char *s;
{
      char *r = s;

      for (; *s; s++)
	if (islower(*s)) *s = toupper(*s);
      return(r);
}

send_boot_notice()
{
      ZNotice_t notice;
      Code_t ret;

      /* Set up server notice */
      notice.z_kind = HMCTL;
      notice.z_port = cli_port;
      notice.z_class = ZEPHYR_CTL_CLASS;
      notice.z_class_inst = ZEPHYR_CTL_HM;
      notice.z_opcode = HM_BOOT;
      notice.z_sender = "sender";
      notice.z_recipient = "";
      notice.z_message_len = 0;
      
      /* Notify server that this host is here */
      if ((ret = ZSetDestAddr(&serv_sin)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "setting destination");
      }
      if ((ret = ZSendNotice(&notice, ZNOAUTH)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "sending startup notice");
      }
      timeout_type = BOOTING;
      (void)alarm(SERV_TIMEOUT);
}

send_flush_notice()
{
      ZNotice_t notice;
      Code_t ret;

      /* Set up server notice */
      notice.z_kind = HMCTL;
      notice.z_port = cli_port;
      notice.z_class = ZEPHYR_CTL_CLASS;
      notice.z_class_inst = ZEPHYR_CTL_HM;
      notice.z_opcode = HM_FLUSH;
      notice.z_sender = "sender";
      notice.z_recipient = "";
      notice.z_message_len = 0;

      /* Tell server to look the other way */
      if ((ret = ZSetDestAddr(&serv_sin)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "setting destination");
      }
      if ((ret = ZSendNotice(&notice, ZNOAUTH)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "sending flush notice");
      }
}

static void
detach()
{
        /* detach from terminal and fork. */
        register int i, size = getdtablesize();

        if (i = fork()) {
                if (i < 0)
                        perror("fork");
                exit(0);
        }

        for (i = 0; i < size; i++) {
                (void) close(i);
        }
        i = open("/dev/tty", O_RDWR, 666);
        (void) ioctl(i, TIOCNOTTY, (caddr_t) 0);
        (void) close(i);
}

find_next_server()
{
      struct hostent *hp;

      if (++serv_loop > 3) {
	    serv_loop = 0;
	    hp = gethostbyname(prim_serv);
	    DPR2 ("Server = %s\n", prim_serv);
      } else {
	    if (*++cur_serv == NULL)
	      cur_serv = serv_list;
	    hp = gethostbyname(*cur_serv);
	    DPR2 ("Server = %s\n", *cur_serv);
      }
      bcopy(hp->h_addr, &serv_sin.sin_addr, hp->h_length);
}

server_manager(notice)
     ZNotice_t *notice;
{
      DPR ("A notice came in from the server.\n");
      if (bcmp(&serv_sin.sin_addr, &from.sin_addr, 4) != 0) {
	    syslog (LOG_INFO, "Bad notice from port %u.", notice->z_port);
	    /* Sent a notice back saying this hostmanager isn't theirs */
      } else {
	    /* This is our server, handle the notice */
	    switch(notice->z_kind) {
		case HMCTL:
		  hm_control(notice);
		  break;
		case SERVNAK:
		case SERVACK:
		  send_back(notice);
		  break;
		default:
		  syslog (LOG_INFO, "Bad notice kind!?");
		  break;
	    }
      }
}

hm_control(notice)
     ZNotice_t *notice;
{
      Code_t ret;

      DPR("Control message!\n");
      if (!strcmp(notice->z_opcode, SERVER_SHUTDOWN))
	    new_server();
      else if (!strcmp(notice->z_opcode, SERVER_PING)) {
	    if (no_server)
	      (void)alarm(0);
	    notice->z_kind = HMACK;
	    if ((ret = ZSetDestAddr(&serv_sin)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "setting destination");
	    }
	    if ((ret = ZSendRawNotice(notice)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "sending ACK");
	    }
	    if (no_server) {
		  no_server = 0;
		  retransmit_queue(&serv_sin);
	    }
      } else
	syslog (LOG_INFO, "Bad control message.");
}

send_back(notice)
     ZNotice_t *notice;
{
      ZNotice_Kind_t kind;
      struct sockaddr_in repl;
      Code_t ret;

      if (no_server)
	(void)alarm(0);
      if (!strcmp(notice->z_opcode, HM_BOOT)) {
	    /* ignore message, just a nak from boot */
      } else {
	    if (remove_notice_from_queue(notice, &kind,
					 &repl) != ZERR_NONE) {
		  syslog (LOG_INFO, "Hey! This packet isn't in my queue!");
	    } else {
		  /* check if client wants an ACK, and send it */
		  if (kind == ACKED) {
			DPR2 ("Client ACK port: %u\n", ntohs(repl.sin_port));
			if ((ret = ZSetDestAddr(&repl)) != ZERR_NONE) {
			      Zperr(ret);
			      com_err("hm", ret, "setting destination");
			}
			if ((ret = ZSendRawNotice(notice)) != ZERR_NONE) {
			      Zperr(ret);
			      com_err("hm", ret, "sending NAK");
			}
		  }
	    }
      }
      if (no_server) {
	    no_server = 0;
	    retransmit_queue(&serv_sin);
      }
}

transmission_tower(notice, packet)
     ZNotice_t *notice;
     caddr_t packet;
{
      ZNotice_t gack;
      Code_t ret;
      struct sockaddr_in gsin;
      int tleft;

      if (notice->z_kind != UNSAFE) {
	    gack = *notice;
	    gack.z_kind = HMACK;
	    gack.z_message_len = 0;
	    gsin = cli_sin;
	    gsin.sin_port = from.sin_port;
	    if (gack.z_port == 0)
	      gack.z_port = from.sin_port;
	    DPR2 ("Client Port = %u\n", ntohs(gack.z_port));
	    notice->z_port = gack.z_port;
	    if ((ret = ZSetDestAddr(&gsin)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "setting destination");
	    }
	    /* Bounce ACK to library */
	    if ((ret = ZSendRawNotice(&gack)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "sending raw notice");
	    }
      }
      if (!no_server) {
	    DPR2 ("Server Port = %u\n", ntohs(serv_sin.sin_port));
	    if ((ret = ZSetDestAddr(&serv_sin)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "setting destination");
	    }
	    if ((ret = ZSendRawNotice(notice)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "while sending raw notice");
	    }
	    if ((tleft = alarm(0)) > 0)
	      (void)alarm(tleft);
	    else {
		  timeout_type = NOTICES;
		  (void)alarm(NOTICE_TIMEOUT);
	    }
      }
      (void)add_notice_to_queue(notice, packet, &gsin);
}

new_server()
{
      ZNotice_t notice;
      Code_t ret;

      no_server = 1;
      syslog (LOG_INFO, "Server went down, finding new server.");
      send_flush_notice();
      find_next_server();
      send_boot_notice();
}

void handle_timeout()
{
      switch(timeout_type) {
	  case BOOTING:
	    new_server();
	    break;
	  case NOTICES:
	    DPR ("Notice timeout\n");
	    resend_notices(&serv_sin);
	    break;
	  default:
	    syslog (LOG_ERR, "Unknown timeout type: %d\n", timeout_type);
	    break;
      }
}

void die_gracefully()
{
      syslog(LOG_INFO, "Terminate signal caught...");
      send_flush_notice();
      closelog();
      exit(0);
}