summaryrefslogtreecommitdiff
path: root/zhm/zhm.c
blob: c2e0ef587351fe57b38bcbec6cc8c6686f5f4915 (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
/* 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 <zephyr/mit-copyright.h>
#include <zephyr/zephyr.h>

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

#include <syslog.h>
#include <signal.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <hesiod.h>

#ifdef DEBUG
#define DPR(a) fprintf(stderr, a); fflush(stderr)
#define DPR2(a,b) fprintf(stderr, a, b); fflush(stderr)
#else
#define DPR(a)
#define DPR2(a,b)
#endif

#define ever (;;)
#define Zperr(e) fprintf(stderr, "Error = %d\n", e)

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

extern int errno;
extern char *malloc();

void init_hm();

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

    /* Override server argument? */
    if (argc > 1) {
	  exp_serv = 1;
	  /* who to talk to */
	  if ((hp = gethostbyname(argv[1])) == NULL) {
		DPR("gethostbyname failed\n");
		exp_serv = 0;
	  }
	  DPR2 ("Server = %s\n", argv[1]);
    }

    init_hm();

    DPR2 ("zephyr server port: %u\n", ntohs(serv_sin.sin_port));
    DPR2 ("zephyr client port: %u\n", ntohs(cli_port));
    
    /* Put in a fork here... */

    /* Sleep with wakeup call set */
    for ever {
	  DPR ("Waiting for a packet...");
	  packet = (char *) malloc(Z_MAXPKTLEN);
	  if ((ret = ZReceiveNotice(packet, Z_MAXPKTLEN, &notice,
				    NULL, &from)) != ZERR_NONE) {
		Zperr(ret);
		com_err("hm", ret, "receiving notice");
		free(packet);
	  } else {
		/* 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 {
			    fprintf(stderr, "Unknown notice type: %d\n",
				    notice.z_kind);
			    free(packet);
		      }
		}
	  }
    }
}

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

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

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

      serv_sin.sin_family = AF_INET;
      if (!exp_serv)
	find_next_server();
      else 
	bcopy(hp->h_addr, &serv_sin.sin_addr, hp->h_length);

      send_boot_notice();
}

send_boot_notice()
{
      ZNotice_t notice;
      Code_t ret;

      /* Set up server notice */
      notice.z_kind = ACKED;
      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 = "recip";
      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, 0)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "sending startup notice");
      }
}

find_next_server()
{
      struct hostent *hp;

      if (*++cur_serv == NULL)
	cur_serv = serv_list;
      DPR2 ("Server = %s\n", *cur_serv);
      hp = gethostbyname(*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) {
	    DPR2 ("Bad notice from port %u\n", notice->z_port);
	    /* Sent a notice back saying this hostmanager isn't theirs */
      } else {
	    /* This is our server, handle the notice */
	    no_server = 0;
	    switch(notice->z_kind) {
		case HMCTL:
		  hm_control(notice);
		  break;
		case SERVNAK:
		  send_nak(notice);
		  break;
		case SERVACK:
		  send_ack(notice);
		  break;
		default:
		  DPR ("Bad notice kind!?\n");
		  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)) {
	    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");
	    }
      } else
	fprintf(stderr, "Bad control message.\n");
}

send_nak(notice)
     ZNotice_t *notice;
{
      caddr_t packet;
      struct sockaddr_in repl;
      Code_t ret;

      if (!strcmp(notice->z_opcode, HM_BOOT)) {
	    /* ignore message, just a nak from boot */
      } else {
	    if (remove_notice_from_queue(notice, &packet,
					 &repl) != ZERR_NONE) {
		  DPR ("Hey! This packet isn't in my queue!\n");
	    } else {
		  /* check if client wants an ACK, and send it */
		  if (notice->z_kind == ACKED) {
			notice->z_kind = SERVNAK;
			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");
			}
		  }
		  free(packet);
	    }
      }
}

send_ack(notice)
     ZNotice_t *notice;
{
      caddr_t packet;
      struct sockaddr_in repl;
      Code_t ret;

      if (!strcmp(notice->z_opcode, HM_BOOT)) {
	    /* ignore message, just an ack from boot */
      } else {
	    if (remove_notice_from_queue(notice, &packet,
					 &repl) != ZERR_NONE) {
		  DPR ("Hey! This packet isn't in my queue!\n");
	    } else {
		  /* check if client wants an ACK, and send it */
		  if (notice->z_kind == ACKED) {
			notice->z_kind = SERVACK;
			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 ACK");
			}
		  }
		  free(packet);
	    }
      }
}

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

      gack = *notice;
      DPR2 ("Message = %s\n", gack.z_message);
      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");
      }
      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");
      }
      add_notice_to_queue(notice, packet, &gsin);
}

new_server()
{
      ZNotice_t notice;
      Code_t ret;

      no_server = 1;
      DPR ("server going down.\n");
      notice.z_kind = ACKED;
      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 = "recip";
      notice.z_message_len = 0;
      if ((ret = ZSetDestAddr(&serv_sin)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "setting destination");
      }
      if ((ret = ZSendNotice(&notice, 0)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "sending flush notice");
      }
      find_next_server();
      send_boot_notice();
      retransmit_queue();
}