/*	ArpFlush Client
	Copyright (C) 2009 Mark Weaver <mark@npsl.co.uk>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

static struct option long_options[] =
{
	{ "address",  required_argument, 0, 'a' },
	{ "port",     required_argument, 0, 'p' },
	{ "password", required_argument, 0, 'P' },
	{ "count",    required_argument, 0, 'c' },
	{ "delay",    required_argument, 0, 'd' },
	{ "help",     no_argument, 0, 'h' },
	{ 0, 0, 0, 0 }
};

static void usage()
{
	printf("Usage: arpflush -p password [options]\n");
	printf("Options:\n");
	printf("  -P, --password    The password to send\n");
	printf("  -a, --address     The address to bind to\n");
	printf("  -p, --port        The port to send the flush request to\n");
	printf("  -c, --count       The number of flush requests to send\n");
	printf("  -d, --delay       The delay between flush requests in milliseconds\n");
	printf("  -h, --help        Display this help text\n");
}

int main(int argc, char *argv[])
{
	int broadcast = 1, fd, i, count = 5, delay = 1000, long_index, c;
	const char *password = 0;
	struct sockaddr_in addr;

	/* set up destination address */
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
	addr.sin_port = htons(6666);

	while ( (c = getopt_long(argc, argv, ":a:p:P:c:d:h", long_options, &long_index)) != -1 )
	{
		switch (c)
		{
		case 'a':
		{
			struct in_addr iaddr;
			if (!inet_aton(optarg, &iaddr))
			{
				fprintf(stderr, "Invalid broadcast address: %s\n", optarg);
				return 1;
			}
			addr.sin_addr = iaddr;
			break;
		}
	
		case 'p':
		{
			int port = atoi(optarg);
			if (port <= 0 || port > 65535)
			{
				fprintf(stderr, "Invalid port: %s\n", optarg);
				return 1;
			}
			addr.sin_port = htons(port);
			break;
		}

		case 'P':
			password = optarg;
			break;

		case 'c':
		{
			count = atoi(optarg);
			if (count <= 0)
			{
				fprintf(stderr, "Invalid count: %s\n", optarg);
				return 1;
			}
			break;
		}

		case 'd':
		{
			delay = atoi(optarg);
			if (delay <= 0)
			{
				fprintf(stderr, "Invalid delay: %s\n", optarg);
				return 1;
			}
			break;
		}

		case ':':
			fprintf(stderr, "Option '-%c' requires an argument\n", optopt);
			return 1;

		case '?':
			fprintf(stderr, "Unknown option '-%c'\n", optopt);
			return 1;

		case 'h':
			usage();
			return 0;
		}
	}

	if (!password) 
	{
		fprintf(stderr, "Password must be specified\n");
		return 1;
	}

	/* connect a broadcast capable socket */
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	{
		perror("socket");
		return 2;
	}
	if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0)
	{
		perror("setsockopt");
		return 2;
	}
	
	/* send 5 pings */
	for (i = 0; i < count; ++i)
	{
		if (sendto(fd, password, strlen(password), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		{
			perror("sendto");
			return 2;
		}
		usleep(delay * 1000);
	}
}
