/* Copyright 1986 by the Massachusetts Institute of Technology */

#include	<types.h>
#include	<sys.h>
#include	"../src/const.h"
#include	"../src/param.h"
#include	"../src/defs.h"
#include	"../src/macs.h"
#include	"../src/net.h"
#include	"../src/ext.h"
#include	"in.h"
#include	"inext.h"

#include	"inudp.h"
#include	"inlog.h"

ext byte inlog_host[4];
static unss log_curid;

iorb *get_log_pkt()
{
    reg iorb *iob;

    if ((iob = getbuf()) == NULL)
      return iob;
    iob->i_addr = ((byte *)iob) + pktoff + sizeof(inpkt) + sizeof(udppkt);
    iob->i_addr += sizeof(logpkt);
    return iob;
}

send_log(iob)
reg iorb *iob;
{
    reg udppkt *upkt;
    reg logpkt *lpkt;
    ext char name[];
    
    iob->i_breq += sizeof(logpkt);
    iob->i_addr -= sizeof(logpkt);
    lpkt = mklogpkt(iob->i_addr);
    lpkt->l_type = LGT_LOG;
    lpkt->l_stype = LGST_LOGNOACK;
    snprintf(3, lpkt->l_id, "%02d", log_curid++);
    if (log_curid > 99) log_curid = 1;
    copy(name, lpkt->l_name, 8);
    copy(name, lpkt->l_sender, 8);
    copy("   \n", lpkt->l_stat, 4);

    iob->i_breq += sizeof(udppkt);
    iob->i_addr -= sizeof(udppkt);
    upkt = mkudp(iob->i_addr);
    upkt->u_src = swab(UDPLOG);
    upkt->u_dst = swab(UDPLOG);
    upkt->u_len = swab(iob->i_breq);
    upkt->u_chk = 0;

    mkinpkt(iob, iniatlst[nets[0].n_net]->inia_addr, inlog_host,
	    INUDP, iob->i_breq);
    upkt->u_chk = udp_cksum(iob->i_addr);
    inxaddq(iob);
}

/* Fill in a packet with statistics and return it to the requester. */
in_send_stat(iob)
reg iorb *iob;
{
    inpkt *ipkt;
    inaddr src, dst;
    udppkt *upkt;
    int len;

    ipkt = mkin(iob->i_addr);
    src.i_long = ipkt->i_src.i_long;
    dst.i_long = ipkt->i_dst.i_long;

    iob->i_addr = (byte *)iob + pktoff + sizeof(inpkt) + sizeof(udppkt);
    len = stat_fill(iob->i_addr);
    len += sizeof(udppkt);
    iob->i_addr -= sizeof(udppkt);
    upkt = mkudp(iob->i_addr);
    upkt->u_src = s2ip(GWSTATSOCK);
    upkt->u_dst = s2ip(GWSTATSOCK);
    upkt->u_len = s2ip(len);
    upkt->u_chk = 0;

    mkinpkt(iob, &dst, &src, INUDP, len); /* Note src and dst swapped */
    upkt->u_chk = udp_cksum(iob->i_addr);
    inxaddq(iob);
}
