/* Copyright 1985 by the Massechusetts Institute of Technology */
/* See permission and disclaimer notice in file notice.h */
#include	<notice.h>

/*
 * This file contains the code for a internet packet generator.
 */

#include	<types.h>
#include	<sys.h>
#include	<mosu.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/in.h"
#include	"../in/inext.h"

/*
 * The fake IP protocol that the packets will have.
 */

#define	INFAKE	0


/*
 * The internet address of the destination of the packets and the first hop.
 * These are to be set with DDT before starting the code.
 */

byte	dest[4] = { 022, 032, 0, 022 };
byte	fhop[4] = { 022, 032, 0, 022 };
intf	pklen = 200;		/* The length of the packets */
intf	pps = 25;		/* Number of packets per burst */
intf	pgnet = 1;		/* Net to send packets to */

/* The main loop of the packet generator */
PKGEN()
{
    void send_loop();
    
    send_pkt();
    stime(send_loop, 0, 1, 100);
}

send_loop()
{
    int i;

    for (i = 0; i < 3; i++)
      send_pkt();
    addtsk(syshnd, 200, send_loop, 0);
}

send_pkt()
{
    reg	iorb	*iob;
    int	ret_code;

    if ((iob = getbuf(bufsiz)) == NULL) {
	iflog(L_ERRU)
	  dolog("PKGEN: Couldn't alloc pkt\n");
	return;
    }
    iob->i_addr = ((byte *)iob) + pktoff;
    
    mkinpkt(iob, iniatlst[pgnet]->inia_addr, dest, INFAKE, pklen);
    if ((ret_code = (*nets[pgnet].n_send)(iob, &nets[pgnet], P_IN, fhop)) != D_OK) {
	iflog(L_ERRU)
	  dolog("PKGEN: error %o sending pkt to net %o\n",
		ret_code, pgnet);
	freebuf(bufsiz, iob);
    }
}
