1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains functions for communication with other servers.
4 * Created by: John T. Kohl
6 * $Source: /afs/dev.mit.edu/source/repository/athena/lib/zephyr/server/server.c,v $
9 * Copyright (c) 1987, 1991 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
14 #include <zephyr/mit-copyright.h>
16 #include <sys/socket.h>
20 static const char rcsid_server_c[] = "$Id: server.c,v 1.66 2001/02/27 04:51:41 zacheiss Exp $";
24 #define SRV_NACKTAB_HASHSIZE 1023
25 #define SRV_NACKTAB_HASHVAL(which, uid) (((which) ^ (uid).zuid_addr.s_addr ^ \
26 (uid).tv.tv_sec ^ (uid).tv.tv_usec) \
27 % SRV_NACKTAB_HASHSIZE)
29 * Server manager. Deal with traffic to and from other servers.
33 * void server_shutdown()
35 * void server_timo(which)
38 * void server_dispatch(notice, auth, who)
41 * struct sockaddr_in *who;
43 * void server_recover(client)
46 * void server_adispatch(notice, auth, who, server)
49 * struct sockaddr_in *who;
52 * void server_forward(notice, auth, who)
55 * struct sockaddr_in *who;
57 * Server *server_which_server(who)
58 * struct sockaddr_in *who;
60 * void server_kill_clt(client);
63 * void server_dump_servers(fp);
66 * void server_reset();
69 static void server_flush __P((Server *));
70 static void hello_respond __P((struct sockaddr_in *, int, int));
71 static void srv_responded __P((struct sockaddr_in *));
72 static void send_msg __P((struct sockaddr_in *, char *, int));
73 static void send_msg_list __P((struct sockaddr_in *, char *, char **, int,
75 static void srv_nack_cancel __P((ZNotice_t *, struct sockaddr_in *));
76 static void srv_nack_release __P((Server *));
77 static void srv_nack_renumber __P((int *));
78 static void send_stats __P((struct sockaddr_in *));
79 static void server_queue __P((Server *, int, void *, int,
80 struct sockaddr_in *));
81 static void server_hello __P((Server *, int));
82 static void setup_server __P((Server *, struct in_addr *));
83 static void srv_rexmit __P((void *));
84 static void server_forw_reliable __P((Server *, caddr_t, int, ZNotice_t *));
85 static Code_t admin_dispatch __P((ZNotice_t *, int, struct sockaddr_in *,
87 static Code_t kill_clt __P((ZNotice_t *, Server *));
88 static Code_t extract_addr __P((ZNotice_t *, struct sockaddr_in *));
91 static Code_t server_register();
94 static struct in_addr *get_server_addrs __P((int *number));
95 static char **get_server_list __P((char *file));
96 static void free_server_list __P((char **list));
98 static Unacked *srv_nacktab[SRV_NACKTAB_HASHSIZE];
99 Server *otherservers; /* points to an array of the known
101 int nservers; /* number of other servers */
102 int me_server_idx; /* # of my entry in the array */
104 #define ADJUST (1) /* adjust timeout on hello input */
105 #define DONT_ADJUST (0) /* don't adjust timeout */
107 /* parameters controlling the transitions of the FSM's--patchable with adb */
108 long timo_up = TIMO_UP;
109 long timo_tardy = TIMO_TARDY;
110 long timo_dead = TIMO_DEAD;
112 /* counters to measure old protocol use */
114 int old_compat_count_uloc = 0;
115 int old_compat_count_ulocate = 0;
116 int old_compat_count_subscr = 0;
117 #endif /* OLD_COMPAT */
119 int new_compat_count_uloc = 0;
120 int new_compat_count_subscr = 0;
121 #endif /* NEW_COMPAT */
127 * Initialize the array of servers. The `limbo' server goes in the first
128 * slot (otherservers[0]).
129 * Contact Hesiod to find all the other servers, allocate space for the
130 * structure, initialize them all to SERV_DEAD with expired timeouts.
131 * Set up a list header for server_forward retransmits.
138 struct in_addr *serv_addr, *server_addrs, limbo_addr;
140 /* we don't need to mask SIGFPE here since when we are called,
141 the signal handler isn't set up yet. */
143 /* talk to hesiod here, set nservers */
144 server_addrs = get_server_addrs(&nservers);
146 syslog(LOG_ERR, "No servers?!?");
155 /* increment servers to make room for 'limbo' */
158 otherservers = (Server *) malloc(nservers * sizeof(Server));
162 limbo_addr.s_addr = 0;
163 setup_server(otherservers, &limbo_addr);
164 timer_reset(otherservers[0].timer);
165 otherservers[0].timer = NULL;
166 otherservers[0].queue = NULL;
167 otherservers[0].dumping = 0;
169 for (serv_addr = server_addrs, i = 1; i < nservers; serv_addr++, i++) {
170 setup_server(&otherservers[i], serv_addr);
172 if (serv_addr->s_addr == my_addr.s_addr) {
174 otherservers[i].state = SERV_UP;
175 timer_reset(otherservers[i].timer);
176 otherservers[i].timer = NULL;
177 otherservers[i].queue = NULL;
178 otherservers[i].dumping = 0;
180 zdbug((LOG_DEBUG,"found myself"));
185 /* free up the addresses */
188 if (me_server_idx == -1) {
189 syslog(LOG_WARNING, "I'm a renegade server!");
190 otherservers = (Server *) realloc(otherservers,
191 ++nservers * sizeof(Server));
193 syslog(LOG_CRIT, "renegade realloc");
196 setup_server(&otherservers[nservers - 1], &my_addr);
198 otherservers[nservers - 1].state = SERV_UP;
200 /* I don't send hello's to myself--cancel the timer */
201 timer_reset(otherservers[nservers - 1].timer);
202 otherservers[nservers - 1].timer = NULL;
204 /* cancel and reschedule all the timers--pointers need
206 /* don't reschedule limbo's timer, so start i=1 */
207 for (i = 1; i < nservers - 1; i++) {
208 timer_reset(otherservers[i].timer);
209 /* all the HELLO's are due now */
210 otherservers[i].timer = timer_set_rel(0L, server_timo,
213 me_server_idx = nservers - 1;
219 * server_reset: re-initializes otherservers array by refreshing from Hesiod
222 * If any server is no longer named in the new list, and that server is in
223 * state SERV_DEAD, it is dropped from the server list.
224 * All other currently-known servers are retained.
225 * Any additional servers not previously known are added to the table.
227 * WARNING: Don't call this routine if any of the ancestor procedures have a
228 * handle on a particular server other than by indexing on otherservers[].
234 struct in_addr *server_addrs;
235 struct in_addr *serv_addr;
238 int *ok_list_new, *ok_list_old;
242 zdbug((LOG_DEBUG, "server_reset"));
246 syslog(LOG_INFO, "server_reset while alone, punt");
251 /* Find out what servers are supposed to be known. */
252 server_addrs = get_server_addrs(&num_servers);
254 syslog(LOG_ERR, "server_reset no servers. nothing done.");
257 ok_list_new = (int *) malloc(num_servers * sizeof(int));
259 syslog(LOG_ERR, "server_reset no mem new");
262 ok_list_old = (int *) malloc(nservers * sizeof(int));
264 syslog(LOG_ERR, "server_reset no mem old");
269 memset(ok_list_old, 0, nservers * sizeof(int));
270 memset(ok_list_new, 0, num_servers * sizeof(int));
272 /* reset timers--pointers will move */
273 for (j = 1; j < nservers; j++) { /* skip limbo */
274 if (j == me_server_idx)
276 timer_reset(otherservers[j].timer);
277 otherservers[j].timer = NULL;
280 /* check off entries on new list which are on old list.
281 check off entries on old list which are on new list. */
283 /* count limbo as "OK" */
285 ok_list_old[0] = 1; /* limbo is OK */
287 for (serv_addr = server_addrs, i = 0; i < num_servers; serv_addr++, i++) {
288 for (j = 1; j < nservers; j++) { /* j = 1 since we skip limbo */
289 if (otherservers[j].addr.sin_addr.s_addr == serv_addr->s_addr) {
290 /* if server is on both lists, mark */
294 break; /* for j loop */
299 /* remove any dead servers on old list not on new list. */
300 if (num_ok < nservers) {
303 new_num = 1; /* limbo */
304 /* count number of servers to keep */
305 for (j = 1; j < nservers; j++) {
306 /* since we are never SERV_DEAD, the following
307 test prevents removing ourself from the list */
308 if (ok_list_old[j] || (otherservers[j].state != SERV_DEAD)) {
309 syslog(LOG_INFO, "keeping server %s",
310 otherservers[j].addr_str);
314 if (new_num < nservers) {
315 servers = (Server *) malloc(new_num * sizeof(Server));
317 syslog(LOG_CRIT, "server_reset server malloc");
321 servers[0] = otherservers[0]; /* copy limbo */
323 srv = (int *) malloc(nservers * sizeof(int));
324 memset(srv, 0, nservers * sizeof(int));
326 /* copy the kept servers */
327 for (j = 1; j < nservers; j++) { /* skip limbo */
328 if (ok_list_old[j] ||
329 otherservers[j].state != SERV_DEAD) {
330 servers[i] = otherservers[j];
334 syslog(LOG_INFO, "flushing server %s",
335 otherservers[j].addr_str);
336 server_flush(&otherservers[j]);
341 srv_nack_renumber(srv);
345 otherservers = servers;
350 /* add any new servers on new list not on old list. */
352 for (i = 0; i < num_servers; i++) {
357 /* new_num is number of extras. */
359 otherservers = (Server *) realloc(otherservers, nservers * sizeof(Server));
361 syslog(LOG_CRIT, "server_reset realloc");
366 for (j = 1; j < nservers - new_num; j++) {
367 if (otherservers[j].addr.sin_addr.s_addr == my_addr.s_addr) {
372 if (!me_server_idx) {
373 syslog(LOG_CRIT, "can't find myself");
377 /* fill in otherservers with the new servers */
378 for (i = 0; i < num_servers; i++) {
379 if (!ok_list_new[i]) {
380 setup_server(&otherservers[nservers - (new_num--)],
382 syslog(LOG_INFO, "adding server %s", inet_ntoa(server_addrs[i]));
387 /* reset timers, to go off now.
388 We can't get a time-left indication (bleagh!)
389 so we expire them all now. This will generally
390 be non-destructive. We assume that when this code is
391 entered via a SIGHUP trigger that a system wizard
392 is watching the goings-on to make sure things straighten
395 for (i = 1; i < nservers; i++) { /* skip limbo */
396 if (i != me_server_idx && !otherservers[i].timer) {
397 otherservers[i].timer =
398 timer_set_rel(0L, server_timo, &otherservers[i]);
400 zdbug((LOG_DEBUG, "reset timer for %s",
401 otherservers[i].addr_str));
409 zdbug((LOG_DEBUG, "server_reset: %d servers now", nservers));
413 /* note: these must match the order given in zserver.h */
430 * A server timout has expired. If enough hello's have been unanswered,
431 * change state and act accordingly. Send a "hello" and reset the timer,
432 * incrementing the number of hello's sent.
434 * See the FSM in the Zephyr document for a better picture of what's
442 Server *which = (Server *) arg;
446 zdbug((LOG_DEBUG,"srv_timo: %s", which->addr_str));
448 /* change state and reset if appropriate */
449 switch(which->state) {
450 case SERV_DEAD: /* leave him dead */
454 case SERV_UP: /* he's now tardy */
455 which->state = SERV_TARDY;
456 which->num_hello_sent = 0;
457 which->timeout = timo_tardy;
462 if (which->num_hello_sent >= ((which->state == SERV_TARDY) ?
465 /* he hasn't answered, assume DEAD */
466 which->state = SERV_DEAD;
467 which->num_hello_sent = 0;
468 which->timeout = timo_dead;
469 srv_nack_release(which);
474 syslog(LOG_ERR,"Bad server state, server 0x%x\n",which);
477 /* now he's either TARDY, STARTING, or DEAD
478 We send a "hello," which increments the counter */
480 zdbug((LOG_DEBUG, "srv %s is %s", which->addr_str,
481 srv_states[which->state]));
483 server_hello(which, auth);
484 /* reschedule the timer */
485 which->timer = timer_set_rel(which->timeout, server_timo, which);
489 * Dispatch a notice from some other server
494 server_dispatch(notice, auth, who)
497 struct sockaddr_in *who;
500 struct sockaddr_in newwho;
502 String *notice_class;
505 zdbug((LOG_DEBUG, "server_dispatch"));
508 if (notice->z_kind == SERVACK) {
509 srv_nack_cancel(notice, who);
513 /* set up a who for the real origin */
514 memset(&newwho, 0, sizeof(newwho));
515 newwho.sin_family = AF_INET;
516 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
517 newwho.sin_port = notice->z_port;
519 server = server_which_server(who);
521 /* we can dispatch to routines safely here, since they will
522 return ZSRV_REQUEUE if appropriate. We bounce this back
523 to the caller, and the caller will re-queue the message
524 for us to process later. */
526 notice_class = make_string(notice->z_class, 1);
528 if (realm_which_realm(&newwho))
529 status = realm_dispatch(notice, auth, &newwho, server);
530 else if (class_is_admin(notice_class)) {
531 /* admins don't get acked, else we get a packet loop */
532 /* will return requeue if bdump request and dumping */
534 return admin_dispatch(notice, auth, who, server);
535 } else if (class_is_control(notice_class)) {
536 status = control_dispatch(notice, auth, &newwho, server);
538 } else if (class_is_ulogin(notice_class)) {
539 status = ulogin_dispatch(notice, auth, &newwho, server);
541 } else if (class_is_ulocate(notice_class)) {
542 status = ulocate_dispatch(notice, auth, &newwho, server);
545 /* shouldn't come from another server */
546 syslog(LOG_WARNING, "srv_disp: pkt cls %s", notice->z_class);
547 status = ZERR_NONE; /* XXX */
549 if (status != ZSRV_REQUEUE)
550 ack(notice, who); /* acknowledge it if processed */
551 free_string(notice_class);
557 * Register a new server (one not in our list). This MUST be authenticated.
562 server_register(notice, auth, who)
565 struct sockaddr_in *who;
571 if (who->sin_port != srv_addr.sin_port) {
573 zdbug((LOG_DEBUG, "srv_wrong port %d", ntohs(who->sin_port)));
577 /* Not yet... talk to ken about authenticators */
581 zdbug((LOG_DEBUG, "srv_unauth"));
586 /* OK, go ahead and set him up. */
587 temp = (Server *) malloc((nservers + 1) * sizeof(Server));
589 syslog(LOG_CRIT, "srv_reg malloc");
593 memcpy(temp, otherservers, nservers * sizeof(Server));
596 /* don't reschedule limbo's timer, so start i=1 */
597 for (i = 1; i < nservers; i++) {
598 if (i == me_server_idx) /* don't reset myself */
600 /* reschedule the timers--we moved otherservers */
601 timerval = timer_when(otherservers[i].timer);
602 timer_reset(otherservers[i].timer);
603 otherservers[i].timer = timer_set_abs(timerval, server_timo,
606 setup_server(&otherservers[nservers], &who->sin_addr);
607 otherservers[nservers].state = SERV_STARTING;
608 otherservers[nservers].timeout = timo_tardy;
609 otherservers[nservers].update_queue = NULL;
610 otherservers[nservers].dumping = 0;
614 zdbug((LOG_DEBUG, "srv %s is %s", otherservers[nservers].addr_str,
615 srv_states[otherservers[nservers].state]));
623 * Tell the other servers that this client died.
627 server_kill_clt(client)
631 char buf[512], *lyst[2];
633 ZNotice_t *pnotice; /* speed hack */
638 lyst[0] = inet_ntoa(client->addr.sin_addr),
639 sprintf(buf, "%d", ntohs(client->addr.sin_port));
643 zdbug((LOG_DEBUG, "server kill clt %s/%s", lyst[0], lyst[1]));
648 memset (¬ice, 0, sizeof(notice));
650 pnotice->z_kind = ACKED;
652 pnotice->z_port = srv_addr.sin_port;
653 pnotice->z_class = ZEPHYR_ADMIN_CLASS;
654 pnotice->z_class_inst = "";
655 pnotice->z_opcode = ADMIN_KILL_CLT;
656 pnotice->z_sender = myname; /* myname is the hostname */
657 pnotice->z_recipient = "";
658 pnotice->z_default_format = "";
659 pnotice->z_num_other_fields = 0;
664 /* don't tell limbo to flush, start at 1*/
665 for (i = 1; i < nservers; i++) {
666 if (i == me_server_idx) /* don't xmit to myself */
668 if (otherservers[i].state == SERV_DEAD)
671 retval = ZFormatNoticeList(pnotice, lyst, 2, &pack, &packlen,
672 auth ? ZAUTH : ZNOAUTH);
673 if (retval != ZERR_NONE) {
674 syslog(LOG_WARNING, "kill_clt format: %s", error_message(retval));
677 server_forw_reliable(&otherservers[i], pack, packlen, pnotice);
682 * A client has died. remove it
686 kill_clt(notice, server)
690 struct sockaddr_in who;
694 zdbug((LOG_DEBUG, "kill_clt"));
696 if (extract_addr(notice, &who) != ZERR_NONE)
697 return ZERR_NONE; /* XXX */
698 client = client_find(&who.sin_addr, notice->z_port);
700 syslog(LOG_NOTICE, "kill_clt: no such client (%s/%d) from %s",
701 inet_ntoa(who.sin_addr), ntohs(who.sin_port),
703 return ZERR_NONE; /* XXX */
707 syslog(LOG_DEBUG, "kill_clt clt_dereg %s/%d from %s",
708 inet_ntoa(who.sin_addr), ntohs(who.sin_port), server->addr_str);
712 /* remove the locations, too */
713 client_deregister(client, 1);
718 * extract a sockaddr_in from a message body
722 extract_addr(notice, who)
724 struct sockaddr_in *who;
726 char *cp = notice->z_message;
728 if (!notice->z_message_len) {
729 syslog(LOG_WARNING, "bad addr pkt");
732 who->sin_addr.s_addr = inet_addr(notice->z_message);
734 cp += strlen(cp) + 1;
735 if (cp >= notice->z_message + notice->z_message_len) {
736 syslog(LOG_WARNING, "short addr pkt");
739 who->sin_port = notice->z_port = htons((u_short) atoi(cp));
740 who->sin_family = AF_INET;
742 zdbug((LOG_DEBUG,"ext %s/%d", inet_ntoa(who->sin_addr),
743 ntohs(who->sin_port)));
749 * Flush all data associated with the server which
758 syslog(LOG_DEBUG, "server_flush %s", which->addr_str);
760 srv_nack_release(which);
764 * send a hello to which, updating the count of hello's sent
765 * Authenticate if auth is set.
769 server_hello(which, auth)
773 send_msg(&which->addr, ADMIN_HELLO, auth);
774 which->num_hello_sent++;
778 * Handle an ADMIN message from a server
783 admin_dispatch(notice, auth, who, server)
786 struct sockaddr_in *who;
789 char *opcode = notice->z_opcode;
790 Code_t status = ZERR_NONE;
793 zdbug((LOG_DEBUG, "ADMIN received"));
796 if (strcmp(opcode, ADMIN_HELLO) == 0) {
797 hello_respond(who, ADJUST, auth);
798 } else if (strcmp(opcode, ADMIN_IMHERE) == 0) {
800 } else if (strcmp(opcode, ADMIN_SHUTDOWN) == 0) {
802 zdbug((LOG_DEBUG, "server shutdown"));
805 srv_nack_release(server);
806 server->state = SERV_DEAD;
807 server->timeout = timo_dead;
808 /* don't worry about the timer, it will
809 be set appropriately on the next send */
811 zdbug((LOG_DEBUG, "srv %s is %s", server->addr_str,
812 srv_states[server->state]));
815 } else if (strcmp(opcode, ADMIN_BDUMP) == 0) {
816 /* Ignore a brain dump request if this is a brain dump packet
817 * or a packet being processed concurrently during a brain
819 if (bdumping || bdump_concurrent)
821 bdump_get(notice, auth, who, server);
822 } else if (strcmp(opcode, ADMIN_KILL_CLT) == 0) {
823 status = kill_clt(notice, server);
824 if (status == ZERR_NONE)
827 syslog(LOG_WARNING, "ADMIN unknown opcode %s",opcode);
834 * Handle an ADMIN message from some random client.
835 * For now, assume it's a registration-type message from some other
836 * previously unknown server
841 server_adispatch(notice, auth, who, server)
844 struct sockaddr_in *who;
848 /* this had better be a HELLO message--start of acquisition
849 protocol, OR a status req packet */
851 if (strcmp(notice->z_opcode, ADMIN_STATUS) == 0) {
858 syslog(LOG_INFO, "disp: new server?");
859 if (server_register(notice, auth, who) != ZERR_NONE) {
860 syslog(LOG_INFO, "new server failed");
862 syslog(LOG_INFO, "new server %s, %d", inet_ntoa(who->sin_addr),
863 ntohs(who->sin_port));
864 hello_respond(who, DONT_ADJUST, auth);
867 syslog(LOG_INFO, "srv_adisp: server attempt from %s",
868 inet_ntoa(who->sin_addr));
876 struct sockaddr_in *who;
882 char *vers, *pkts, *upt;
886 #define NUM_FIXED 3 /* 3 fixed fields, plus server info */
887 /* well, not really...but for
888 backward compatibility, we gotta
890 vers = get_version();
892 sprintf(buf, "%d pkts", npackets);
894 sprintf(buf, "%d seconds operational",NOW - uptime);
898 if (old_compat_count_uloc)
900 if (old_compat_count_ulocate)
902 if (old_compat_count_subscr)
904 #endif /* OLD_COMPAT */
906 if (new_compat_count_uloc)
908 if (new_compat_count_subscr)
910 #endif /* NEW_COMPAT */
911 extrafields += nrealms;
912 responses = (char **) malloc((NUM_FIXED + nservers + extrafields) *
918 num_resp = NUM_FIXED;
919 /* start at 1 and ignore limbo */
920 for (i = 1; i < nservers ; i++) {
921 sprintf(buf, "%s/%s%s", otherservers[i].addr_str,
922 srv_states[(int) otherservers[i].state],
923 otherservers[i].dumping ? " (DUMPING)" : "");
924 responses[num_resp++] = strsave(buf);
927 if (old_compat_count_uloc) {
928 sprintf(buf, "%d old old location requests", old_compat_count_uloc);
929 responses[num_resp++] = strsave(buf);
931 if (old_compat_count_ulocate) {
932 sprintf(buf, "%d old old loc lookup requests",
933 old_compat_count_ulocate);
934 responses[num_resp++] = strsave(buf);
936 if (old_compat_count_subscr) {
937 sprintf(buf, "%d old old subscr requests", old_compat_count_subscr);
938 responses[num_resp++] = strsave(buf);
940 #endif /* OLD_COMPAT */
942 if (new_compat_count_uloc) {
943 sprintf(buf, "%d new old location requests", new_compat_count_uloc);
944 responses[num_resp++] = strsave(buf);
946 if (new_compat_count_subscr) {
947 sprintf(buf, "%d new old subscr requests", new_compat_count_subscr);
948 responses[num_resp++] = strsave(buf);
950 #endif /* NEW_COMPAT */
951 for (realm = otherrealms, i = 0; i < nrealms ; i++, realm++) {
952 sprintf(buf, "%s(%s)/%s", realm->name,
953 inet_ntoa((realm->addrs[realm->idx]).sin_addr),
954 rlm_states[(int) realm->state]);
955 responses[num_resp++] = strsave(buf);
958 send_msg_list(who, ADMIN_STATUS, responses, num_resp, 0);
960 /* Start at one; don't try to free static version string */
961 for (i = 1; i < num_resp; i++)
967 * Get a list of server addresses.
969 * This list is retrieved from Hesiod.
971 * This list is read from a file.
973 * Return a pointer to an array of allocated storage. This storage is
974 * freed by the caller.
977 static struct in_addr *
978 get_server_addrs(number)
979 int *number; /* RETURN */
982 char **server_hosts = NULL;
983 char **server_hosts_free = NULL;
985 struct in_addr *addrs;
986 struct in_addr *addr;
989 server_hosts = get_server_list(list_file);
990 server_hosts_free = server_hosts;
993 server_hosts = hes_resolve("zephyr","sloc");
999 for (cpp = server_hosts; *cpp; cpp++)
1002 addrs = (struct in_addr *) malloc(i * sizeof(struct in_addr));
1004 /* Convert to in_addr's */
1005 for (cpp = server_hosts, addr = addrs, i = 0; *cpp; cpp++) {
1006 hp = gethostbyname(*cpp);
1008 memcpy(addr, hp->h_addr, sizeof(struct in_addr));
1011 syslog(LOG_WARNING, "hostname failed, %s", *cpp);
1015 if (server_hosts_free)
1016 free_server_list(server_hosts_free);
1020 static int nhosts = 0;
1023 * read "file" to get a list of names of hosts to peer with.
1024 * The file should contain a list of host names, one per line.
1028 get_server_list(file)
1032 char buf[MAXHOSTNAMELEN];
1037 /* start with 16, realloc if necessary */
1039 ret_list = (char **) malloc(nhosts * sizeof(char *));
1041 fp = fopen(file, "r");
1043 while (fgets(buf, MAXHOSTNAMELEN, fp)) {
1044 /* nuke the newline, being careful not to overrun
1045 the buffer searching for it with strlen() */
1046 buf[MAXHOSTNAMELEN - 1] = '\0';
1047 newline = strchr(buf, '\n');
1051 if (nused + 1 >= nhosts) {
1052 /* get more pointer space if necessary */
1053 /* +1 to leave room for null pointer */
1054 ret_list = (char **) realloc(ret_list, nhosts * 2);
1055 nhosts = nhosts * 2;
1057 ret_list[nused++] = strsave(buf);
1061 if (gethostname(buf, sizeof(buf)) < 0) {
1065 ret_list[nused++] = strsave(buf);
1067 ret_list[nused] = NULL;
1072 * free storage allocated by get_server_list
1075 free_server_list(list)
1078 char **orig_list = list;
1080 if (!nhosts) /* nothing allocated */
1082 for (; *list; list++)
1089 * initialize the server structure for address addr, and set a timer
1090 * to go off immediately to send hello's to other servers.
1094 setup_server(server, addr)
1096 struct in_addr *addr;
1098 server->state = SERV_DEAD;
1099 server->timeout = timo_dead;
1100 server->num_hello_sent = 0;
1101 server->addr.sin_family = AF_INET;
1102 /* he listens to the same port we do */
1103 server->addr.sin_port = srv_addr.sin_port;
1104 server->addr.sin_addr = *addr;
1105 strcpy(server->addr_str, inet_ntoa(*addr));
1106 server->timer = timer_set_rel(0L, server_timo, server);
1107 server->queue = NULL;
1108 server->dumping = 0;
1112 * Someone sent us a hello message, respond to them.
1116 hello_respond(who, adj, auth)
1117 struct sockaddr_in *who;
1124 zdbug((LOG_DEBUG, "hello from %s", inet_ntoa(who->sin_addr)));
1127 send_msg(who, ADMIN_IMHERE, auth);
1131 /* If we think he's down, schedule an immediate HELLO. */
1133 which = server_which_server(who);
1137 switch (which->state) {
1139 /* he said hello, we thought he was dead.
1140 reschedule his hello for now. */
1141 timer_reset(which->timer);
1142 which->timer = timer_set_rel(0L, server_timo, which);
1153 * return the server descriptor for server at who
1157 server_which_server(who)
1158 struct sockaddr_in *who;
1163 if (who->sin_port != srv_addr.sin_port)
1166 /* don't check limbo */
1167 for (server = &otherservers[1], i = 1; i < nservers; i++, server++) {
1168 if (server->addr.sin_addr.s_addr == who->sin_addr.s_addr)
1175 * We received a response to a hello packet or an ack. Adjust server state
1180 struct sockaddr_in *who;
1182 Server *which = server_which_server(who);
1185 zdbug((LOG_DEBUG, "srv_responded %s", inet_ntoa(who->sin_addr)));
1189 syslog(LOG_ERR, "hello input from non-server?!");
1193 switch (which->state) {
1195 /* he responded, we thought he was dead. mark as starting
1197 which->state = SERV_STARTING;
1198 which->timeout = timo_tardy;
1199 timer_reset(which->timer);
1200 which->timer = timer_set_rel(0L, server_timo, which);
1203 /* here we negotiate and set up a braindump */
1204 if (bdump_socket < 0)
1209 which->state = SERV_UP;
1213 /* reset the timer and counts */
1214 which->num_hello_sent = 0;
1215 which->timeout = timo_up;
1216 timer_reset(which->timer);
1217 which->timer = timer_set_rel(which->timeout, server_timo, which);
1221 zdbug((LOG_DEBUG, "srv %s is %s", which->addr_str,
1222 srv_states[which->state]));
1227 * Send each of the other servers a shutdown message.
1235 /* don't tell limbo to go away, start at 1*/
1236 for (i = 1; i < nservers; i++)
1237 send_msg(&otherservers[i].addr, ADMIN_SHUTDOWN, 1);
1241 * send a message to who with admin class and opcode and clinst as specified.
1242 * auth is set if we want to send authenticated
1246 send_msg(who, opcode, auth)
1247 struct sockaddr_in *who;
1252 ZNotice_t *pnotice; /* speed hack */
1259 memset (¬ice, 0, sizeof(notice));
1261 pnotice->z_kind = ACKED;
1263 pnotice->z_port = srv_addr.sin_port;
1264 pnotice->z_class = ZEPHYR_ADMIN_CLASS;
1265 pnotice->z_class_inst = "";
1266 pnotice->z_opcode = opcode;
1267 pnotice->z_sender = myname; /* myname is the hostname */
1268 pnotice->z_recipient = "";
1269 pnotice->z_default_format = "";
1270 pnotice->z_message = NULL;
1271 pnotice->z_message_len = 0;
1272 pnotice->z_num_other_fields = 0;
1274 /* XXX for now, we don't do authentication */
1277 retval = ZFormatNotice(pnotice, &pack, &packlen, auth ? ZAUTH : ZNOAUTH);
1278 if (retval != ZERR_NONE) {
1279 syslog(LOG_WARNING, "snd_msg format: %s", error_message(retval));
1282 retval = ZSetDestAddr(who);
1283 if (retval != ZERR_NONE) {
1284 syslog(LOG_WARNING, "snd_msg set addr: %s", error_message(retval));
1288 /* don't wait for ack */
1289 retval = ZSendPacket(pack, packlen, 0);
1290 if (retval != ZERR_NONE)
1291 syslog(LOG_WARNING, "snd_msg xmit: %s", error_message(retval));
1296 * send a notice with a message to who with admin class and opcode and
1297 * message body as specified.
1298 * auth is set if we want to send authenticated
1299 * server_idx is -1 if we are sending to a client, or the server index
1300 * if we are sending to a server.
1304 send_msg_list(who, opcode, lyst, num, auth)
1305 struct sockaddr_in *who;
1317 memset (¬ice, 0, sizeof(notice));
1319 notice.z_kind = UNSAFE;
1320 notice.z_port = srv_addr.sin_port;
1321 notice.z_class = ZEPHYR_ADMIN_CLASS;
1322 notice.z_class_inst = "";
1323 notice.z_opcode = opcode;
1324 notice.z_sender = myname; /* myname is the hostname */
1325 notice.z_recipient = "";
1326 notice.z_default_format = "";
1327 notice.z_message = NULL;
1328 notice.z_message_len = 0;
1329 notice.z_num_other_fields = 0;
1331 /* XXX for now, we don't do authentication */
1334 retval = ZFormatNoticeList(¬ice, lyst, num, &pack, &packlen,
1335 auth ? ZAUTH : ZNOAUTH);
1336 if (retval != ZERR_NONE) {
1337 syslog(LOG_WARNING, "snd_msg_lst format: %s", error_message(retval));
1340 retval = ZSetDestAddr(who);
1341 if (retval != ZERR_NONE) {
1342 syslog(LOG_WARNING, "snd_msg_lst set addr: %s", error_message(retval));
1346 xmit_frag(¬ice, pack, packlen, 0);
1351 * Forward the notice to the other servers
1355 server_forward(notice, auth, who)
1358 struct sockaddr_in *who;
1366 zdbug((LOG_DEBUG, "srv_forw"));
1368 /* don't send to limbo */
1369 for (i = 1; i < nservers; i++) {
1370 if (i == me_server_idx) /* don't xmit to myself */
1372 if (otherservers[i].state == SERV_DEAD &&
1373 otherservers[i].dumping == 0) {
1374 /* if we are dumping to him, we want to
1375 queue it, even if he's dead */
1379 pack = malloc(sizeof(ZPacket_t));
1381 syslog(LOG_CRIT, "srv_fwd malloc");
1384 retval = ZFormatSmallRawNotice(notice, pack, &packlen);
1385 if (retval != ZERR_NONE) {
1386 syslog(LOG_WARNING, "srv_fwd format: %s", error_message(retval));
1389 if (otherservers[i].dumping) {
1390 server_queue(&otherservers[i], packlen, pack, auth, who);
1393 server_forw_reliable(&otherservers[i], pack, packlen, notice);
1398 server_forw_reliable(server, pack, packlen, notice)
1408 retval = ZSetDestAddr(&server->addr);
1409 if (retval != ZERR_NONE) {
1410 syslog(LOG_WARNING, "srv_fwd_rel set addr: %s", error_message(retval));
1414 retval = ZSendPacket(pack, packlen, 0);
1415 if (retval != ZERR_NONE) {
1416 syslog(LOG_WARNING, "srv_fwd xmit: %s", error_message(retval));
1420 /* now we've sent it, mark it as not ack'ed */
1422 nacked = (Unacked *) malloc(sizeof(Unacked));
1424 /* no space: just punt */
1425 syslog(LOG_ERR, "srv_forw_rel nack malloc");
1430 nacked->client = NULL;
1431 nacked->rexmits = 0;
1432 nacked->packet = pack;
1433 nacked->dest.srv_idx = server - otherservers;
1434 nacked->packsz = packlen;
1435 nacked->uid = notice->z_uid;
1436 nacked->timer = timer_set_rel(rexmit_times[0], srv_rexmit, nacked);
1437 hashval = SRV_NACKTAB_HASHVAL(nacked->dest.srv_idx, nacked->uid);
1438 LIST_INSERT(&srv_nacktab[hashval], nacked);
1442 * send the queued message for the server.
1446 server_send_queue(server)
1453 while (server->queue) {
1454 pending = server_dequeue(server);
1455 status = ZParseNotice(pending->packet, pending->len, ¬ice);
1456 if (status != ZERR_NONE) {
1457 syslog(LOG_ERR, "ssq bad notice parse (%s): %s",
1458 inet_ntoa(pending->who.sin_addr), error_message(status));
1460 server_forw_reliable(server, pending->packet, pending->len,
1463 /* ACK handling routines will free the packet */
1469 * a server has acknowledged a message we sent to him; remove it from
1470 * server unacked queue
1474 srv_nack_cancel(notice, who)
1476 struct sockaddr_in *who;
1478 Server *server = server_which_server(who);
1483 syslog(LOG_ERR, "non-server ack?");
1486 hashval = SRV_NACKTAB_HASHVAL(server - otherservers, notice->z_uid);
1487 for (nacked = srv_nacktab[hashval]; nacked; nacked = nacked->next) {
1488 if (nacked->dest.srv_idx == server - otherservers
1489 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
1490 timer_reset(nacked->timer);
1491 free(nacked->packet);
1492 LIST_DELETE(nacked);
1498 zdbug((LOG_DEBUG, "srv_nack not found"));
1503 * retransmit a message to another server
1510 Unacked *packet = (Unacked *) arg;
1512 /* retransmit the packet */
1515 zdbug((LOG_DEBUG,"srv_rexmit to %s/%d",
1516 otherservers[packet->dest.srv_idx].addr_str,
1517 ntohs(otherservers[packet->dest.srv_idx].addr.sin_port)));
1519 if (otherservers[packet->dest.srv_idx].state == SERV_DEAD) {
1521 zdbug((LOG_DEBUG, "cancelling send to dead server"));
1523 LIST_DELETE(packet);
1524 free(packet->packet);
1525 srv_nack_release(&otherservers[packet->dest.srv_idx]);
1529 retval = ZSetDestAddr(&otherservers[packet->dest.srv_idx].addr);
1530 if (retval != ZERR_NONE) {
1531 syslog(LOG_WARNING, "srv_rexmit set addr: %s", error_message(retval));
1533 retval = ZSendPacket(packet->packet, packet->packsz, 0);
1534 if (retval != ZERR_NONE)
1535 syslog(LOG_WARNING, "srv_rexmit xmit: %s",
1536 error_message(retval));
1539 /* reset the timer */
1540 if (rexmit_times[packet->rexmits + 1] != -1)
1542 packet->timer = timer_set_rel(rexmit_times[packet->rexmits], srv_rexmit,
1547 * Clean up the not-yet-acked queue and release anything destined
1552 srv_nack_release(server)
1556 Unacked *nacked, *next;
1558 for (i = 0; i < SRV_NACKTAB_HASHSIZE; i++) {
1559 for (nacked = srv_nacktab[i]; nacked; nacked = next) {
1560 next = nacked->next;
1561 if (nacked->dest.srv_idx == server - otherservers) {
1562 timer_reset(nacked->timer);
1563 LIST_DELETE(nacked);
1564 free(nacked->packet);
1572 * Adjust indices of not-yet-acked packets sent to other servers to
1573 * continue to refer to the correct server.
1577 srv_nack_renumber (new_idx)
1580 /* XXX release any private queue for this server */
1584 /* search the not-yet-acked list for anything destined to 'from', and
1585 change the index to 'to'. */
1586 for (i = 0; i < SRV_NACKTAB_HASHSIZE; i++) {
1587 for (nacked = srv_nacktab[i]; nacked; nacked = nacked->next) {
1588 idx = new_idx[nacked->dest.srv_idx];
1590 syslog(LOG_ERR, "srv_nack_renumber error: [%d]=%d",
1591 nacked->dest.srv_idx, idx);
1594 nacked->dest.srv_idx = idx;
1600 * Queue this notice to be transmitted to the server when it is ready.
1603 server_queue(server, len, pack, auth, who)
1608 struct sockaddr_in *who;
1612 pending = (Pending *) malloc(sizeof(Pending));
1614 syslog(LOG_CRIT, "update_queue malloc");
1617 pending->packet = pack;
1619 pending->auth = auth;
1620 pending->who = *who;
1621 pending->next = NULL;
1623 /* put it on the end of the list */
1625 server->queue_last->next = pending;
1627 server->queue = server->queue_last = pending;
1631 * Pull a notice off the hold queue.
1635 server_dequeue(server)
1642 pending = server->queue;
1643 server->queue = pending->next;
1648 * free storage used by a pending queue entry.
1652 server_pending_free(pending)
1655 free(pending->packet);
1661 * Queue something to be handled later by this server.
1665 server_self_queue(notice, auth, who)
1668 struct sockaddr_in * who;
1674 retval = ZFormatRawNotice(notice, &pack, &packlen);
1675 if (retval != ZERR_NONE) {
1676 syslog(LOG_CRIT, "srv_self_queue format: %s", error_message(retval));
1679 server_queue(me_server, packlen, pack, auth, who);
1683 * dump info about servers onto the fp.
1684 * assumed to be called with SIGFPE blocked
1685 * (true if called from signal handler)
1688 server_dump_servers(fp)
1693 for (i = 0; i < nservers ; i++) {
1694 fprintf(fp, "%d:%s/%s%s\n", i, otherservers[i].addr_str,
1695 srv_states[otherservers[i].state],
1696 otherservers[i].dumping ? " (DUMPING)" : "");