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 2157 2008-01-21 20:06:42Z kcr $";
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 *));
90 static struct in_addr *get_server_addrs __P((int *number));
91 static char **get_server_list __P((char *file));
92 static char **get_single_server __P((void));
93 static void free_server_list __P((char **list));
95 static Unacked *srv_nacktab[SRV_NACKTAB_HASHSIZE];
96 Server *otherservers; /* points to an array of the known
98 int nservers; /* number of other servers */
99 int me_server_idx; /* # of my entry in the array */
101 #define ADJUST (1) /* adjust timeout on hello input */
102 #define DONT_ADJUST (0) /* don't adjust timeout */
104 /* parameters controlling the transitions of the FSM's--patchable with adb */
105 long timo_up = TIMO_UP;
106 long timo_tardy = TIMO_TARDY;
107 long timo_dead = TIMO_DEAD;
109 /* counters to measure old protocol use */
111 int old_compat_count_uloc = 0;
112 int old_compat_count_ulocate = 0;
113 int old_compat_count_subscr = 0;
114 #endif /* OLD_COMPAT */
116 int new_compat_count_uloc = 0;
117 int new_compat_count_subscr = 0;
118 #endif /* NEW_COMPAT */
124 * Initialize the array of servers. The `limbo' server goes in the first
125 * slot (otherservers[0]).
126 * Contact Hesiod to find all the other servers, allocate space for the
127 * structure, initialize them all to SERV_DEAD with expired timeouts.
128 * Set up a list header for server_forward retransmits.
135 struct in_addr *serv_addr, *server_addrs, limbo_addr;
137 /* we don't need to mask SIGFPE here since when we are called,
138 the signal handler isn't set up yet. */
140 /* talk to hesiod here, set nservers */
141 server_addrs = get_server_addrs(&nservers);
143 syslog(LOG_ERR, "No servers?!?");
152 /* increment servers to make room for 'limbo' */
155 otherservers = (Server *) malloc(nservers * sizeof(Server));
159 limbo_addr.s_addr = 0;
160 setup_server(otherservers, &limbo_addr);
161 timer_reset(otherservers[0].timer);
162 otherservers[0].timer = NULL;
163 otherservers[0].queue = NULL;
164 otherservers[0].dumping = 0;
166 for (serv_addr = server_addrs, i = 1; i < nservers; serv_addr++, i++) {
167 setup_server(&otherservers[i], serv_addr);
169 if (serv_addr->s_addr == my_addr.s_addr) {
171 otherservers[i].state = SERV_UP;
172 timer_reset(otherservers[i].timer);
173 otherservers[i].timer = NULL;
174 otherservers[i].queue = NULL;
175 otherservers[i].dumping = 0;
179 /* free up the addresses */
182 if (me_server_idx == -1) {
183 syslog(LOG_WARNING, "I'm a renegade server!");
184 otherservers = (Server *) realloc(otherservers,
185 ++nservers * sizeof(Server));
187 syslog(LOG_CRIT, "renegade realloc");
190 setup_server(&otherservers[nservers - 1], &my_addr);
192 otherservers[nservers - 1].state = SERV_UP;
194 /* I don't send hello's to myself--cancel the timer */
195 timer_reset(otherservers[nservers - 1].timer);
196 otherservers[nservers - 1].timer = NULL;
198 /* cancel and reschedule all the timers--pointers need
200 /* don't reschedule limbo's timer, so start i=1 */
201 for (i = 1; i < nservers - 1; i++) {
202 timer_reset(otherservers[i].timer);
203 /* all the HELLO's are due now */
204 otherservers[i].timer = timer_set_rel(0L, server_timo,
207 me_server_idx = nservers - 1;
213 * server_reset: re-initializes otherservers array by refreshing from Hesiod
216 * If any server is no longer named in the new list, and that server is in
217 * state SERV_DEAD, it is dropped from the server list.
218 * All other currently-known servers are retained.
219 * Any additional servers not previously known are added to the table.
221 * WARNING: Don't call this routine if any of the ancestor procedures have a
222 * handle on a particular server other than by indexing on otherservers[].
228 struct in_addr *server_addrs;
229 struct in_addr *serv_addr;
232 int *ok_list_new, *ok_list_old;
237 syslog(LOG_INFO, "server_reset while alone, punt");
242 /* Find out what servers are supposed to be known. */
243 server_addrs = get_server_addrs(&num_servers);
245 syslog(LOG_ERR, "server_reset no servers. nothing done.");
248 ok_list_new = (int *) malloc(num_servers * sizeof(int));
250 syslog(LOG_ERR, "server_reset no mem new");
253 ok_list_old = (int *) malloc(nservers * sizeof(int));
255 syslog(LOG_ERR, "server_reset no mem old");
260 memset(ok_list_old, 0, nservers * sizeof(int));
261 memset(ok_list_new, 0, num_servers * sizeof(int));
263 /* reset timers--pointers will move */
264 for (j = 1; j < nservers; j++) { /* skip limbo */
265 if (j == me_server_idx)
267 timer_reset(otherservers[j].timer);
268 otherservers[j].timer = NULL;
271 /* check off entries on new list which are on old list.
272 check off entries on old list which are on new list. */
274 /* count limbo as "OK" */
276 ok_list_old[0] = 1; /* limbo is OK */
278 for (serv_addr = server_addrs, i = 0; i < num_servers; serv_addr++, i++) {
279 for (j = 1; j < nservers; j++) { /* j = 1 since we skip limbo */
280 if (otherservers[j].addr.sin_addr.s_addr == serv_addr->s_addr) {
281 /* if server is on both lists, mark */
285 break; /* for j loop */
290 /* remove any dead servers on old list not on new list. */
291 if (num_ok < nservers) {
294 new_num = 1; /* limbo */
295 /* count number of servers to keep */
296 for (j = 1; j < nservers; j++) {
297 /* since we are never SERV_DEAD, the following
298 test prevents removing ourself from the list */
299 if (ok_list_old[j] || (otherservers[j].state != SERV_DEAD)) {
300 syslog(LOG_INFO, "keeping server %s",
301 otherservers[j].addr_str);
305 if (new_num < nservers) {
306 servers = (Server *) malloc(new_num * sizeof(Server));
308 syslog(LOG_CRIT, "server_reset server malloc");
312 servers[0] = otherservers[0]; /* copy limbo */
314 srv = (int *) malloc(nservers * sizeof(int));
315 memset(srv, 0, nservers * sizeof(int));
317 /* copy the kept servers */
318 for (j = 1; j < nservers; j++) { /* skip limbo */
319 if (ok_list_old[j] ||
320 otherservers[j].state != SERV_DEAD) {
321 servers[i] = otherservers[j];
325 syslog(LOG_INFO, "flushing server %s",
326 otherservers[j].addr_str);
327 server_flush(&otherservers[j]);
332 srv_nack_renumber(srv);
336 otherservers = servers;
341 /* add any new servers on new list not on old list. */
343 for (i = 0; i < num_servers; i++) {
348 /* new_num is number of extras. */
350 otherservers = (Server *) realloc(otherservers, nservers * sizeof(Server));
352 syslog(LOG_CRIT, "server_reset realloc");
357 for (j = 1; j < nservers - new_num; j++) {
358 if (otherservers[j].addr.sin_addr.s_addr == my_addr.s_addr) {
363 if (!me_server_idx) {
364 syslog(LOG_CRIT, "can't find myself");
368 /* fill in otherservers with the new servers */
369 for (i = 0; i < num_servers; i++) {
370 if (!ok_list_new[i]) {
371 setup_server(&otherservers[nservers - (new_num--)],
373 syslog(LOG_INFO, "adding server %s", inet_ntoa(server_addrs[i]));
378 /* reset timers, to go off now.
379 We can't get a time-left indication (bleagh!)
380 so we expire them all now. This will generally
381 be non-destructive. We assume that when this code is
382 entered via a SIGHUP trigger that a system wizard
383 is watching the goings-on to make sure things straighten
386 for (i = 1; i < nservers; i++) { /* skip limbo */
387 if (i != me_server_idx && !otherservers[i].timer) {
388 otherservers[i].timer =
389 timer_set_rel(0L, server_timo, &otherservers[i]);
397 /* note: these must match the order given in zserver.h */
414 * A server timout has expired. If enough hello's have been unanswered,
415 * change state and act accordingly. Send a "hello" and reset the timer,
416 * incrementing the number of hello's sent.
418 * See the FSM in the Zephyr document for a better picture of what's
423 server_timo(void *arg)
425 Server *which = (Server *) arg;
428 /* change state and reset if appropriate */
429 switch(which->state) {
430 case SERV_DEAD: /* leave him dead */
434 case SERV_UP: /* he's now tardy */
435 which->state = SERV_TARDY;
436 which->num_hello_sent = 0;
437 which->timeout = timo_tardy;
442 if (which->num_hello_sent >= ((which->state == SERV_TARDY) ?
445 /* he hasn't answered, assume DEAD */
446 which->state = SERV_DEAD;
447 which->num_hello_sent = 0;
448 which->timeout = timo_dead;
449 srv_nack_release(which);
454 syslog(LOG_ERR,"Bad server state, server 0x%x\n", (int)which);
457 /* now he's either TARDY, STARTING, or DEAD
458 We send a "hello," which increments the counter */
459 server_hello(which, auth);
460 /* reschedule the timer */
461 which->timer = timer_set_rel(which->timeout, server_timo, which);
465 * Dispatch a notice from some other server
470 server_dispatch(ZNotice_t *notice,
472 struct sockaddr_in *who)
475 struct sockaddr_in newwho;
477 String *notice_class;
480 if (notice->z_kind == SERVACK) {
481 srv_nack_cancel(notice, who);
485 /* set up a who for the real origin */
486 memset(&newwho, 0, sizeof(newwho));
487 newwho.sin_family = AF_INET;
488 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
489 newwho.sin_port = notice->z_port;
491 server = server_which_server(who);
493 /* we can dispatch to routines safely here, since they will
494 return ZSRV_REQUEUE if appropriate. We bounce this back
495 to the caller, and the caller will re-queue the message
496 for us to process later. */
498 notice_class = make_string(notice->z_class, 1);
500 if (realm_which_realm(&newwho))
501 status = realm_dispatch(notice, auth, &newwho, server);
502 else if (class_is_admin(notice_class)) {
503 /* admins don't get acked, else we get a packet loop */
504 /* will return requeue if bdump request and dumping */
506 return admin_dispatch(notice, auth, who, server);
507 } else if (class_is_control(notice_class)) {
508 status = control_dispatch(notice, auth, &newwho, server);
510 } else if (class_is_ulogin(notice_class)) {
511 status = ulogin_dispatch(notice, auth, &newwho, server);
513 } else if (class_is_ulocate(notice_class)) {
514 status = ulocate_dispatch(notice, auth, &newwho, server);
517 /* shouldn't come from another server */
518 syslog(LOG_WARNING, "srv_disp: pkt cls %s", notice->z_class);
519 status = ZERR_NONE; /* XXX */
521 if (status != ZSRV_REQUEUE)
522 ack(notice, who); /* acknowledge it if processed */
523 free_string(notice_class);
528 * Tell the other servers that this client died.
532 server_kill_clt(Client *client)
535 char buf[512], *lyst[2];
537 ZNotice_t *pnotice; /* speed hack */
542 lyst[0] = inet_ntoa(client->addr.sin_addr),
543 sprintf(buf, "%d", ntohs(client->addr.sin_port));
548 memset (¬ice, 0, sizeof(notice));
550 pnotice->z_kind = ACKED;
552 pnotice->z_port = srv_addr.sin_port;
553 pnotice->z_class = ZEPHYR_ADMIN_CLASS;
554 pnotice->z_class_inst = "";
555 pnotice->z_opcode = ADMIN_KILL_CLT;
556 pnotice->z_sender = myname; /* myname is the hostname */
557 pnotice->z_recipient = "";
558 pnotice->z_default_format = "";
559 pnotice->z_num_other_fields = 0;
564 /* don't tell limbo to flush, start at 1*/
565 for (i = 1; i < nservers; i++) {
566 if (i == me_server_idx) /* don't xmit to myself */
568 if (otherservers[i].state == SERV_DEAD)
571 retval = ZFormatNoticeList(pnotice, lyst, 2, &pack, &packlen,
572 auth ? ZAUTH : ZNOAUTH);
573 if (retval != ZERR_NONE) {
574 syslog(LOG_WARNING, "kill_clt format: %s", error_message(retval));
577 server_forw_reliable(&otherservers[i], pack, packlen, pnotice);
582 * A client has died. remove it
586 kill_clt(ZNotice_t *notice,
589 struct sockaddr_in who;
592 if (extract_addr(notice, &who) != ZERR_NONE)
593 return ZERR_NONE; /* XXX */
594 client = client_find(&who.sin_addr, notice->z_port);
596 syslog(LOG_NOTICE, "kill_clt: no such client (%s/%d) from %s",
597 inet_ntoa(who.sin_addr), ntohs(who.sin_port),
599 return ZERR_NONE; /* XXX */
603 syslog(LOG_DEBUG, "kill_clt clt_dereg %s/%d from %s",
604 inet_ntoa(who.sin_addr), ntohs(who.sin_port), server->addr_str);
607 /* remove the locations, too */
608 client_deregister(client, 1);
613 * extract a sockaddr_in from a message body
617 extract_addr(ZNotice_t *notice,
618 struct sockaddr_in *who)
620 char *cp = notice->z_message;
622 if (!notice->z_message_len) {
623 syslog(LOG_WARNING, "bad addr pkt");
626 who->sin_addr.s_addr = inet_addr(notice->z_message);
628 cp += strlen(cp) + 1;
629 if (cp >= notice->z_message + notice->z_message_len) {
630 syslog(LOG_WARNING, "short addr pkt");
633 who->sin_port = notice->z_port = htons((u_short) atoi(cp));
634 who->sin_family = AF_INET;
639 * Flush all data associated with the server which
643 server_flush(Server *which)
645 srv_nack_release(which);
649 * send a hello to which, updating the count of hello's sent
650 * Authenticate if auth is set.
654 server_hello(Server *which,
657 send_msg(&which->addr, ADMIN_HELLO, auth);
658 which->num_hello_sent++;
662 * Handle an ADMIN message from a server
667 admin_dispatch(ZNotice_t *notice,
669 struct sockaddr_in *who,
672 char *opcode = notice->z_opcode;
673 Code_t status = ZERR_NONE;
675 if (strcmp(opcode, ADMIN_HELLO) == 0) {
676 hello_respond(who, ADJUST, auth);
677 } else if (strcmp(opcode, ADMIN_IMHERE) == 0) {
679 } else if (strcmp(opcode, ADMIN_SHUTDOWN) == 0) {
681 srv_nack_release(server);
682 server->state = SERV_DEAD;
683 server->timeout = timo_dead;
684 /* don't worry about the timer, it will
685 be set appropriately on the next send */
687 } else if (strcmp(opcode, ADMIN_BDUMP) == 0) {
688 /* Ignore a brain dump request if this is a brain dump packet
689 * or a packet being processed concurrently during a brain
691 if (bdumping || bdump_concurrent)
693 bdump_get(notice, auth, who, server);
694 } else if (strcmp(opcode, ADMIN_KILL_CLT) == 0) {
695 status = kill_clt(notice, server);
696 if (status == ZERR_NONE)
699 syslog(LOG_WARNING, "ADMIN unknown opcode %s",opcode);
706 * Handle an ADMIN message from some random client.
707 * For now, assume it's a registration-type message from some other
708 * previously unknown server
713 server_adispatch(ZNotice_t *notice,
715 struct sockaddr_in *who,
719 /* this had better be a HELLO message--start of acquisition
720 protocol, OR a status req packet */
722 if (strcmp(notice->z_opcode, ADMIN_STATUS) == 0) {
728 syslog(LOG_INFO, "srv_adisp: server attempt from %s",
729 inet_ntoa(who->sin_addr));
735 send_stats(struct sockaddr_in *who)
741 char *vers, *pkts, *upt;
745 #define NUM_FIXED 3 /* 3 fixed fields, plus server info */
746 /* well, not really...but for
747 backward compatibility, we gotta
749 vers = get_version();
751 sprintf(buf, "%lu pkts", npackets);
753 sprintf(buf, "%ld seconds operational",NOW - uptime);
757 if (old_compat_count_uloc)
759 if (old_compat_count_ulocate)
761 if (old_compat_count_subscr)
763 #endif /* OLD_COMPAT */
765 if (new_compat_count_uloc)
767 if (new_compat_count_subscr)
769 #endif /* NEW_COMPAT */
770 extrafields += nrealms;
771 responses = (char **) malloc((NUM_FIXED + nservers + extrafields) *
777 num_resp = NUM_FIXED;
778 /* start at 1 and ignore limbo */
779 for (i = 1; i < nservers ; i++) {
780 sprintf(buf, "%s/%s%s", otherservers[i].addr_str,
781 srv_states[(int) otherservers[i].state],
782 otherservers[i].dumping ? " (DUMPING)" : "");
783 responses[num_resp++] = strsave(buf);
786 if (old_compat_count_uloc) {
787 sprintf(buf, "%d old old location requests", old_compat_count_uloc);
788 responses[num_resp++] = strsave(buf);
790 if (old_compat_count_ulocate) {
791 sprintf(buf, "%d old old loc lookup requests",
792 old_compat_count_ulocate);
793 responses[num_resp++] = strsave(buf);
795 if (old_compat_count_subscr) {
796 sprintf(buf, "%d old old subscr requests", old_compat_count_subscr);
797 responses[num_resp++] = strsave(buf);
799 #endif /* OLD_COMPAT */
801 if (new_compat_count_uloc) {
802 sprintf(buf, "%d new old location requests", new_compat_count_uloc);
803 responses[num_resp++] = strsave(buf);
805 if (new_compat_count_subscr) {
806 sprintf(buf, "%d new old subscr requests", new_compat_count_subscr);
807 responses[num_resp++] = strsave(buf);
809 #endif /* NEW_COMPAT */
810 for (realm = otherrealms, i = 0; i < nrealms ; i++, realm++) {
811 sprintf(buf, "%s(%s)/%s", realm->name,
812 inet_ntoa((realm->addrs[realm->idx]).sin_addr),
813 rlm_states[(int) realm->state]);
814 responses[num_resp++] = strsave(buf);
817 send_msg_list(who, ADMIN_STATUS, responses, num_resp, 0);
819 /* Start at one; don't try to free static version string */
820 for (i = 1; i < num_resp; i++)
826 * Get a list of server addresses.
828 * This list is retrieved from Hesiod.
830 * This list is read from a file.
832 * Return a pointer to an array of allocated storage. This storage is
833 * freed by the caller.
836 static struct in_addr *
837 get_server_addrs(int *number)
840 char **server_hosts = NULL;
841 char **server_hosts_free = NULL;
843 struct in_addr *addrs;
844 struct in_addr *addr;
847 server_hosts = get_server_list(list_file);
848 server_hosts_free = server_hosts;
851 server_hosts = hes_resolve("zephyr","sloc");
854 server_hosts = get_single_server();
855 server_hosts_free = server_hosts;
861 for (cpp = server_hosts; *cpp; cpp++)
864 addrs = (struct in_addr *) malloc(i * sizeof(struct in_addr));
866 /* Convert to in_addr's */
867 for (cpp = server_hosts, addr = addrs, i = 0; *cpp; cpp++) {
868 hp = gethostbyname(*cpp);
870 memcpy(addr, hp->h_addr, sizeof(struct in_addr));
873 syslog(LOG_WARNING, "hostname failed, %s", *cpp);
877 if (server_hosts_free)
878 free_server_list(server_hosts_free);
882 static int nhosts = 0;
885 * read "file" to get a list of names of hosts to peer with.
886 * The file should contain a list of host names, one per line.
890 get_server_list(char *file)
893 char buf[MAXHOSTNAMELEN];
898 fp = fopen(file, "r");
901 /* start with 16, realloc if necessary */
903 ret_list = (char **) malloc(nhosts * sizeof(char *));
907 while (fgets(buf, MAXHOSTNAMELEN, fp)) {
908 /* nuke the newline, being careful not to overrun
909 the buffer searching for it with strlen() */
910 buf[MAXHOSTNAMELEN - 1] = '\0';
911 newline = strchr(buf, '\n');
915 if (nused + 1 >= nhosts) {
916 /* get more pointer space if necessary */
917 /* +1 to leave room for null pointer */
918 ret_list = (char **) realloc(ret_list, nhosts * 2);
921 ret_list[nused++] = strsave(buf);
928 ret_list[nused] = NULL;
933 get_single_server(void)
935 char buf[MAXHOSTNAMELEN];
939 ret_list = (char **) malloc(nhosts * sizeof(char *));
942 if (gethostname(buf, sizeof(buf)) < 0) {
946 ret_list[nused++] = strsave(buf);
947 ret_list[nused] = NULL;
952 * free storage allocated by get_server_list
955 free_server_list(char **list)
957 char **orig_list = list;
959 if (!nhosts) /* nothing allocated */
961 for (; *list; list++)
968 * initialize the server structure for address addr, and set a timer
969 * to go off immediately to send hello's to other servers.
973 setup_server(Server *server,
974 struct in_addr *addr)
976 server->state = SERV_DEAD;
977 server->timeout = timo_dead;
978 server->num_hello_sent = 0;
979 server->addr.sin_family = AF_INET;
980 /* he listens to the same port we do */
981 server->addr.sin_port = srv_addr.sin_port;
982 server->addr.sin_addr = *addr;
983 strcpy(server->addr_str, inet_ntoa(*addr));
984 server->timer = timer_set_rel(0L, server_timo, server);
985 server->queue = NULL;
990 * Someone sent us a hello message, respond to them.
994 hello_respond(struct sockaddr_in *who,
1000 send_msg(who, ADMIN_IMHERE, auth);
1004 /* If we think he's down, schedule an immediate HELLO. */
1006 which = server_which_server(who);
1010 switch (which->state) {
1012 /* he said hello, we thought he was dead.
1013 reschedule his hello for now. */
1014 timer_reset(which->timer);
1015 which->timer = timer_set_rel(0L, server_timo, which);
1026 * return the server descriptor for server at who
1030 server_which_server(struct sockaddr_in *who)
1035 if (who->sin_port != srv_addr.sin_port)
1038 /* don't check limbo */
1039 for (server = &otherservers[1], i = 1; i < nservers; i++, server++) {
1040 if (server->addr.sin_addr.s_addr == who->sin_addr.s_addr)
1047 * We received a response to a hello packet or an ack. Adjust server state
1051 srv_responded(struct sockaddr_in *who)
1053 Server *which = server_which_server(who);
1056 syslog(LOG_ERR, "hello input from non-server?!");
1060 switch (which->state) {
1062 /* he responded, we thought he was dead. mark as starting
1064 which->state = SERV_STARTING;
1065 which->timeout = timo_tardy;
1066 timer_reset(which->timer);
1067 which->timer = timer_set_rel(0L, server_timo, which);
1070 /* here we negotiate and set up a braindump */
1071 if (bdump_socket < 0)
1076 which->state = SERV_UP;
1080 /* reset the timer and counts */
1081 which->num_hello_sent = 0;
1082 which->timeout = timo_up;
1083 timer_reset(which->timer);
1084 which->timer = timer_set_rel(which->timeout, server_timo, which);
1090 * Send each of the other servers a shutdown message.
1094 server_shutdown(void)
1098 /* don't tell limbo to go away, start at 1*/
1099 for (i = 1; i < nservers; i++)
1100 send_msg(&otherservers[i].addr, ADMIN_SHUTDOWN, 1);
1104 * send a message to who with admin class and opcode and clinst as specified.
1105 * auth is set if we want to send authenticated
1109 send_msg(struct sockaddr_in *who,
1114 ZNotice_t *pnotice; /* speed hack */
1121 memset (¬ice, 0, sizeof(notice));
1123 pnotice->z_kind = ACKED;
1125 pnotice->z_port = srv_addr.sin_port;
1126 pnotice->z_class = ZEPHYR_ADMIN_CLASS;
1127 pnotice->z_class_inst = "";
1128 pnotice->z_opcode = opcode;
1129 pnotice->z_sender = myname; /* myname is the hostname */
1130 pnotice->z_recipient = "";
1131 pnotice->z_default_format = "";
1132 pnotice->z_message = NULL;
1133 pnotice->z_message_len = 0;
1134 pnotice->z_num_other_fields = 0;
1136 /* XXX for now, we don't do authentication */
1139 retval = ZFormatNotice(pnotice, &pack, &packlen, auth ? ZAUTH : ZNOAUTH);
1140 if (retval != ZERR_NONE) {
1141 syslog(LOG_WARNING, "snd_msg format: %s", error_message(retval));
1144 retval = ZSetDestAddr(who);
1145 if (retval != ZERR_NONE) {
1146 syslog(LOG_WARNING, "snd_msg set addr: %s", error_message(retval));
1150 /* don't wait for ack */
1151 retval = ZSendPacket(pack, packlen, 0);
1152 if (retval != ZERR_NONE)
1153 syslog(LOG_WARNING, "snd_msg xmit: %s", error_message(retval));
1158 * send a notice with a message to who with admin class and opcode and
1159 * message body as specified.
1160 * auth is set if we want to send authenticated
1161 * server_idx is -1 if we are sending to a client, or the server index
1162 * if we are sending to a server.
1166 send_msg_list(struct sockaddr_in *who,
1177 memset (¬ice, 0, sizeof(notice));
1179 notice.z_kind = UNSAFE;
1180 notice.z_port = srv_addr.sin_port;
1181 notice.z_class = ZEPHYR_ADMIN_CLASS;
1182 notice.z_class_inst = "";
1183 notice.z_opcode = opcode;
1184 notice.z_sender = myname; /* myname is the hostname */
1185 notice.z_recipient = "";
1186 notice.z_default_format = "";
1187 notice.z_message = NULL;
1188 notice.z_message_len = 0;
1189 notice.z_num_other_fields = 0;
1191 /* XXX for now, we don't do authentication */
1194 retval = ZFormatNoticeList(¬ice, lyst, num, &pack, &packlen,
1195 auth ? ZAUTH : ZNOAUTH);
1196 if (retval != ZERR_NONE) {
1197 syslog(LOG_WARNING, "snd_msg_lst format: %s", error_message(retval));
1200 retval = ZSetDestAddr(who);
1201 if (retval != ZERR_NONE) {
1202 syslog(LOG_WARNING, "snd_msg_lst set addr: %s", error_message(retval));
1206 xmit_frag(¬ice, pack, packlen, 0);
1211 * Forward the notice to the other servers
1215 server_forward(ZNotice_t *notice,
1217 struct sockaddr_in *who)
1224 /* don't send to limbo */
1225 for (i = 1; i < nservers; i++) {
1226 if (i == me_server_idx) /* don't xmit to myself */
1228 if (otherservers[i].state == SERV_DEAD &&
1229 otherservers[i].dumping == 0) {
1230 /* if we are dumping to him, we want to
1231 queue it, even if he's dead */
1235 pack = malloc(sizeof(ZPacket_t));
1237 syslog(LOG_CRIT, "srv_fwd malloc");
1240 retval = ZNewFormatSmallRawNotice(notice, pack, &packlen);
1241 if (retval != ZERR_NONE) {
1242 syslog(LOG_WARNING, "srv_fwd format: %s", error_message(retval));
1245 if (otherservers[i].dumping) {
1246 server_queue(&otherservers[i], packlen, pack, auth, who);
1249 server_forw_reliable(&otherservers[i], pack, packlen, notice);
1254 server_forw_reliable(Server *server,
1263 retval = ZSetDestAddr(&server->addr);
1264 if (retval != ZERR_NONE) {
1265 syslog(LOG_WARNING, "srv_fwd_rel set addr: %s", error_message(retval));
1269 retval = ZSendPacket(pack, packlen, 0);
1270 if (retval != ZERR_NONE) {
1271 syslog(LOG_WARNING, "srv_fwd xmit: %s", error_message(retval));
1275 /* now we've sent it, mark it as not ack'ed */
1277 nacked = (Unacked *) malloc(sizeof(Unacked));
1279 /* no space: just punt */
1280 syslog(LOG_ERR, "srv_forw_rel nack malloc");
1285 nacked->client = NULL;
1286 nacked->rexmits = 0;
1287 nacked->packet = pack;
1288 nacked->dest.srv_idx = server - otherservers;
1289 nacked->packsz = packlen;
1290 nacked->uid = notice->z_uid;
1291 nacked->timer = timer_set_rel(rexmit_times[0], srv_rexmit, nacked);
1292 hashval = SRV_NACKTAB_HASHVAL(nacked->dest.srv_idx, nacked->uid);
1293 Unacked_insert(&srv_nacktab[hashval], nacked);
1297 * send the queued message for the server.
1301 server_send_queue(Server *server)
1307 while (server->queue) {
1308 pending = server_dequeue(server);
1309 status = ZParseNotice(pending->packet, pending->len, ¬ice);
1310 if (status != ZERR_NONE) {
1311 syslog(LOG_ERR, "ssq bad notice parse (%s): %s",
1312 inet_ntoa(pending->who.sin_addr), error_message(status));
1314 server_forw_reliable(server, pending->packet, pending->len,
1317 /* ACK handling routines will free the packet */
1323 * a server has acknowledged a message we sent to him; remove it from
1324 * server unacked queue
1328 srv_nack_cancel(ZNotice_t *notice,
1329 struct sockaddr_in *who)
1331 Server *server = server_which_server(who);
1336 syslog(LOG_ERR, "non-server ack?");
1339 hashval = SRV_NACKTAB_HASHVAL(server - otherservers, notice->z_uid);
1340 for (nacked = srv_nacktab[hashval]; nacked; nacked = nacked->next) {
1341 if (nacked->dest.srv_idx == server - otherservers
1342 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
1343 timer_reset(nacked->timer);
1344 free(nacked->packet);
1345 Unacked_delete(nacked);
1353 * retransmit a message to another server
1357 srv_rexmit(void *arg)
1359 Unacked *packet = (Unacked *) arg;
1361 /* retransmit the packet */
1363 if (otherservers[packet->dest.srv_idx].state == SERV_DEAD) {
1364 Unacked_delete(packet);
1365 free(packet->packet);
1366 srv_nack_release(&otherservers[packet->dest.srv_idx]);
1370 retval = ZSetDestAddr(&otherservers[packet->dest.srv_idx].addr);
1371 if (retval != ZERR_NONE) {
1372 syslog(LOG_WARNING, "srv_rexmit set addr: %s", error_message(retval));
1374 retval = ZSendPacket(packet->packet, packet->packsz, 0);
1375 if (retval != ZERR_NONE)
1376 syslog(LOG_WARNING, "srv_rexmit xmit: %s",
1377 error_message(retval));
1380 /* reset the timer */
1381 if (rexmit_times[packet->rexmits + 1] != -1)
1383 packet->timer = timer_set_rel(rexmit_times[packet->rexmits], srv_rexmit,
1388 * Clean up the not-yet-acked queue and release anything destined
1393 srv_nack_release(Server *server)
1396 Unacked *nacked, *next;
1398 for (i = 0; i < SRV_NACKTAB_HASHSIZE; i++) {
1399 for (nacked = srv_nacktab[i]; nacked; nacked = next) {
1400 next = nacked->next;
1401 if (nacked->dest.srv_idx == server - otherservers) {
1402 timer_reset(nacked->timer);
1403 Unacked_delete(nacked);
1404 free(nacked->packet);
1412 * Adjust indices of not-yet-acked packets sent to other servers to
1413 * continue to refer to the correct server.
1417 srv_nack_renumber (int *new_idx)
1419 /* XXX release any private queue for this server */
1423 /* search the not-yet-acked list for anything destined to 'from', and
1424 change the index to 'to'. */
1425 for (i = 0; i < SRV_NACKTAB_HASHSIZE; i++) {
1426 for (nacked = srv_nacktab[i]; nacked; nacked = nacked->next) {
1427 idx = new_idx[nacked->dest.srv_idx];
1429 syslog(LOG_ERR, "srv_nack_renumber error: [%d]=%d",
1430 nacked->dest.srv_idx, idx);
1433 nacked->dest.srv_idx = idx;
1439 * Queue this notice to be transmitted to the server when it is ready.
1442 server_queue(Server *server,
1446 struct sockaddr_in *who)
1450 pending = (Pending *) malloc(sizeof(Pending));
1452 syslog(LOG_CRIT, "update_queue malloc");
1455 pending->packet = pack;
1457 pending->auth = auth;
1458 pending->who = *who;
1459 pending->next = NULL;
1461 /* put it on the end of the list */
1463 server->queue_last->next = pending;
1465 server->queue = server->queue_last = pending;
1469 * Pull a notice off the hold queue.
1473 server_dequeue(Server *server)
1479 pending = server->queue;
1480 server->queue = pending->next;
1485 * free storage used by a pending queue entry.
1489 server_pending_free(Pending *pending)
1491 free(pending->packet);
1497 * Queue something to be handled later by this server.
1501 server_self_queue(ZNotice_t* notice,
1503 struct sockaddr_in * who)
1509 retval = ZFormatRawNotice(notice, &pack, &packlen);
1510 if (retval != ZERR_NONE) {
1511 syslog(LOG_CRIT, "srv_self_queue format: %s", error_message(retval));
1514 server_queue(me_server, packlen, pack, auth, who);
1518 * dump info about servers onto the fp.
1519 * assumed to be called with SIGFPE blocked
1520 * (true if called from signal handler)
1523 server_dump_servers(FILE *fp)
1527 for (i = 0; i < nservers ; i++) {
1528 fprintf(fp, "%d:%s/%s%s\n", i, otherservers[i].addr_str,
1529 srv_states[otherservers[i].state],
1530 otherservers[i].dumping ? " (DUMPING)" : "");