/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the hostmanager <--> client interaction routines.
 *
 *      Created by:     David C. Jedlinsky
 *
 *      $Source: /afs/media-lab.mit.edu/user/warlord/C/zephyr/zhm/RCS/zhm_client.c,v $
 *      $Author: warlord $
 *
 *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h". 
 */

#include "zhm.h"
#include "newqueue.h"
#include "ZRemoteRealms.h"

#ifndef lint
#ifndef SABER
static char rcsid_hm_client_c[] = "$Header: /afs/media-lab.mit.edu/user/warlord/C/zephyr/zhm/RCS/zhm_client.c,v 1.2 92/03/12 16:14:24 warlord Exp $";
#endif /* SABER */
#endif /* lint */

extern int timeout_type, nclt;
extern struct sockaddr_in cli_sin, from;
extern RealmEntry *PrimRealm;

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

      nclt++;
      if (notice->z_kind == HMCTL) {
	    if (!strcmp(notice->z_opcode, CLIENT_FLUSH)) {
		  flush_all_servers();
	  }
	    else if (!strcmp(notice->z_opcode, CLIENT_NEW_SERVER))
		    new_server(PrimRealm, (char *)NULL);
	    else
		    syslog (LOG_INFO, "Bad control notice from client.");
	    return;
    } else
	if (notice->z_kind != UNSAFE) {
	      gack = *notice;
	      gack.z_kind = HMACK;
	      gack.z_message_len = 0;
	      gack.z_multinotice = "";
	      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 = send_outgoing(&gack)) != ZERR_NONE) {
		    Zperr(ret);
		    com_err("hm", ret, "sending raw notice");
	      }
	}
      ZStripDestRealm(notice);

      if (!Realm->no_server) {
	    DPR2 ("Server Port = %u\n", ntohs(Realm->host_sin.sin_port));
	    if ((ret = ZSetDestAddr(Realm->hp)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "setting destination");
	    }
	    if ((ret = send_outgoing(notice)) != ZERR_NONE) {
		  Zperr(ret);
		  com_err("hm", ret, "while sending raw notice");
	    }
	    set_alarm();
      }
      (void)add_notice_to_queue(Realm->queue, notice, packet, &gsin, pak_len);
}

Code_t
send_outgoing(notice)
ZNotice_t *notice;
{
	Code_t retval;
	char *packet;
	int length;

	if (!(packet = (char *) malloc((unsigned)sizeof(ZPacket_t))))
		return(ENOMEM);

	if ((retval = ZFormatSmallRawNotice(notice, packet, &length))
	    != ZERR_NONE) {
		free(packet);
		return(retval);
	}
	retval = ZSendPacket(packet, length, 0);
	free(packet);
	return(retval);
}
