summaryrefslogtreecommitdiff
path: root/zhm/zhm_server.c
blob: 3496be40b55b85f90f1bb60ad1bd2f0b789c66ce (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
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the hostmanager <--> server interaction routines.
 *
 *      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 "zhm.h"

#ifndef lint
#ifndef SABER
static char rcsid_hm_server_c[] = "$Header$";
#endif /* SABER */
#endif /* lint */

int serv_loop = 0;
extern u_short cli_port;
extern struct sockaddr_in serv_sin, from;
extern int timeout_type, hmdebug, nservchang, booting, nserv, no_server;
extern int deactivated, rebootflag;
extern char **serv_list, **cur_serv_list;
extern char cur_serv[], prim_serv[];
extern void die_gracefully();
extern char *strcpy();
extern unsigned long inet_addr();

/* Argument is whether we are actually booting, or just attaching
 * after a server switch */
send_boot_notice(op)
char *op;
{
     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 = op;
     notice.z_sender = "HM";
     notice.z_recipient = "";
     notice.z_default_format = "";
     notice.z_num_other_fields = 0;
     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);
}

/* Argument is whether we are detaching or really going down */
send_flush_notice(op)
char *op;
{
     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 = op;
     notice.z_sender = "HM";
     notice.z_recipient = "";
     notice.z_default_format = "";
     notice.z_num_other_fields = 0;
     notice.z_message_len = 0;

     /* Tell server to lose us */
     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");
     }
}

find_next_server(sugg_serv)
char *sugg_serv;
{
     struct hostent *hp;
     int done = 0;
     char **parse = serv_list;
  
     if (sugg_serv) {
	  do {
	       if (!strcmp(*parse, sugg_serv))
		    done = 1;
	  } while ((done == 0) && (*++parse != NULL));
     }
     if (done) {
	  if ((hp = gethostbyname(sugg_serv)) != NULL) {
	       DPR2 ("Server = %s\n", sugg_serv);	
	       (void)strcpy(cur_serv, sugg_serv);
	       if (hmdebug)
		    syslog(LOG_DEBUG, "Suggested server: %s\n", sugg_serv);
	  } else {
	       done = 0; 
	       sleep(1);
	  }
     }
     if (!done)
	  do {		  
	       if ((++serv_loop > 3) && (strcmp(cur_serv, prim_serv))) {
		    serv_loop = 0;
		    if ((hp = gethostbyname(prim_serv)) != NULL) {
			 DPR2 ("Server = %s\n", prim_serv);
			 (void)strcpy(cur_serv, prim_serv);
			 done = 1;
		    } else
			 sleep(1);
	       } else {
		    if (*++cur_serv_list == NULL) {
			 /* Exit if we are rebooting, */
			 /* and cannot find a server. */
			 if (rebootflag)
			      die_gracefully();
			 cur_serv_list = serv_list;
			 if (!cur_serv_list[1]) {
			     /* server list has only one entry, use it */
			     if ((hp = gethostbyname(*cur_serv_list))
				 != NULL) {
				 DPR2 ("Server = %s\n", *cur_serv_list);
				 (void)strcpy(cur_serv, *cur_serv_list);
				 done = 1;
			     } else
				 sleep(1);
			 }
		    }
		    if (strcmp(*cur_serv_list, cur_serv)) {
			 if ((hp = gethostbyname(*cur_serv_list)) != NULL){
			      DPR2 ("Server = %s\n", *cur_serv_list);
			      (void)strcpy(cur_serv, *cur_serv_list);
			      done = 1;
			 } else
			      sleep(1);
		    }
	       }
	  } while (done == 0);
     bcopy(hp->h_addr, (char *)&serv_sin.sin_addr, hp->h_length);
     nservchang++;
}

server_manager(notice)
ZNotice_t *notice;
{
     if ((bcmp((char *)&serv_sin.sin_addr, (char *)&from.sin_addr, 4) != 0) ||
	 (serv_sin.sin_port != from.sin_port)) {
	  syslog (LOG_INFO, "Bad notice from port %u.", notice->z_port);
     } else {
	  /* This is our server, handle the notice */
	  booting = 0;
	  DPR ("A notice came in from the server.\n");
	  nserv++;
	  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;
     struct hostent *hp;
     char suggested_server[64];
     unsigned long addr;
     
     DPR("Control message!\n");
     if (!strcmp(notice->z_opcode, SERVER_SHUTDOWN)) {
	  if (notice->z_message_len) {
	       addr = inet_addr(notice->z_message);
	       if ((hp = gethostbyaddr((char *)&addr, sizeof(addr),
				       AF_INET)) != NULL) {
		    (void)strcpy(suggested_server, hp->h_name);
		    new_server(suggested_server);
	       } else
		    new_server((char *)NULL);
	  }
	  else
	       new_server((char *)NULL);
     } 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 = send_outgoing(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) ||
	 !strcmp(notice->z_opcode, HM_ATTACH)) {
	  /* ignore message, just an ack from boot, but exit if we
	   * are rebooting.
	   */
	  if (rebootflag)
	       die_gracefully();
     } 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 = send_outgoing(notice)) != ZERR_NONE) {
			 Zperr(ret);
			 com_err("hm", ret, "sending ACK");
		    }
	       }
	  }
     }
     if (no_server) {
	  no_server = 0;
	  retransmit_queue(&serv_sin);
     }
}

new_server(sugg_serv)
char *sugg_serv;
{
     no_server = 1;
     syslog (LOG_INFO, "Server went down, finding new server.");
     send_flush_notice(HM_DETACH);
     find_next_server(sugg_serv);
     if (booting) {
	  send_boot_notice(HM_BOOT);
	  deactivated = 0;
     } else
	  send_boot_notice(HM_ATTACH);
}