#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define VXWORKS

/* new style socket address */

struct bsd44_sockaddr_in {
	unsigned char  sin_len;
	unsigned char  sin_family;
	unsigned short sin_port;
	struct	  in_addr sin_addr;
	unsigned char  sin_zero[8];
};

/*
 * To run this on VxWorks, do the following after loading the program
 * (compile it with VXWORKS defined first, i.e.  cc -c -DVXWORKS)
 *
 *  -> sp blaster,"222.222.222.222",1000,-1,1
 *
 * where 222.222.222.222 is the IP address and 1000 is the message size.
 * -1 indicates write forever.
 * 1 indicates don't print dots for each packet.
 */
void
blaster(char *addr, int len, int howmany, int dontprint)
{
	int i;
	int sock;
	struct bsd44_sockaddr_in sin;
	int ret;
	char buf[1460];
	int opt;
	int total = 0;

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) 
		exit(1);
	bzero(&sin, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(6666);
	sin.sin_addr.s_addr = inet_addr(addr);
	opt = 1024 * 64;

	if (len == 0)
		len = 100;	/* default */
	if (howmany == 0)
		howmany = 1;	/* default */
		
	if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0)
		printf("setsockopt fail\n");
	for (i = 0; howmany == -1 || i < howmany ; i++) {
		ret = sendto(sock, buf, len, 0, &sin, sizeof(sin));
		if (ret < 0) {
			printf("sendto: errno 0x%x\n",errnoGet());
			close(sock);
			exit(1);
		}
		if (!dontprint)
			printf(".");
	}
	close(sock);
	printf("blaster normal exit");
}

#ifndef VXWORKS
main(int argc, char *argv[])
{
	if (argc < 3) {
		printf("%s ipaddr len\n", argv[0]);
		exit(1);
	}
	blaster(argv[1], atoi(argv[2]));
}
#endif
