/*====================================================================*
 *
 *   arpc1.nif - Atheros Sniffer Indication Logger;
 *
 *   this application is special purpose and not intended for general
 *   use;
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *	C. Maier, charles.maier@atheros.com
 *
 *--------------------------------------------------------------------*/

#define _GETOPT_H

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include "../tools/getoptv.h"
#include "../tools/putoptv.h"
#include "../tools/memory.h"
#include "../tools/number.h"
#include "../tools/types.h"
#include "../tools/flags.h"
#include "../tools/files.h"
#include "../tools/error.h"
#include "../int6k/intellon.h"
#include "../int6k/homeplug.h"
#include "../int6k/message.h"
#include "../int6k/problem.h"
#include "../ether/channel.h"
#include "../mme/mme.h"

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/hexdump.c"
#include "../tools/hexwrite.c"
#include "../tools/uintspec.c"
#include "../tools/todigit.c"
#include "../tools/error.c"
#endif

#ifndef MAKEFILE
#include "../ether/channel.c"
#include "../ether/openchannel.c"
#include "../ether/closechannel.c"
#include "../ether/sendpacket.c"
#include "../ether/readpacket.c"
#endif

/*====================================================================*
 *    program constants; 
 *--------------------------------------------------------------------*/

#define PLCDEVICE "PLC"
#define ARPC_ASCII_EACH '2'
#define ARPC_ASCII_SOME '1'
#define ARPC_ASCII_EXIT '0'

/*====================================================================*
 *
 *   void monitor (void const * memory, size_t extent);
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

static void monitor (void const * memory, size_t extent) 

{

#ifndef __GNUC__
#pragma pack (push,1)
#endif

	struct __packed vs_sniffer_data 
	{
		uint8_t DIRECTION;
		uint64_t SYSTIME;
		uint32_t BEACONTIME;
		uint8_t FRAMECTRL [16];
		uint8_t BEACONBODY [165];
	}
	* data = (struct vs_sniffer_data *)(memory);

#ifndef __GNUC__
#pragma pack (pop)
#endif

	hexdump (data, 0, sizeof (struct vs_sniffer_data), stdout);
	return;
}

/*====================================================================*
 *   
 *   void function (struct channel * channel, unsigned period, signed fo);
 *   
 *   capture and record VS_ARPC messages from any Atheros powerline 
 *   device; devices must be programmed to send these messages;
 *   the first payload character determines message disposition;
 *
 *   this function has no practical use under operation conditions;
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

static void function (struct channel * channel) 

{
	struct message message;

#ifndef __GNUC__
#pragma pack (push,1)
#endif

	struct __packed vs_arpc_ind 
	{
		struct header_eth ethernet;
		struct header_int intellon;
		uint8_t SNIFFTYPE;
		uint8_t SNIFFDATA [165];
	}
	* indicate = (struct vs_arpc_ind *)(&message);

#ifndef __GNUC__
#pragma pack (pop)
#endif

	signed length;
	error (0, 0, "Listening");
	while ((length = readpacket (channel, &message, sizeof (message))) >= 0) 
	{
		if (!length) 
		{
			continue;
		}
		if (message.ethernet.MTYPE != htons (HOMEPLUG_MTYPE)) 
		{
			continue;
		}
		if (message.intellon.MMV != INTELLON_MMV) 
		{
			continue;
		}
		if (message.intellon.MMTYPE != HTOLE16 (VS_SNIFFER | MMTYPE_IND)) 
		{
			continue;
		}
		monitor (indicate->SNIFFDATA, sizeof (indicate->SNIFFDATA));
	}
	return;
}

/*====================================================================*
 *   
 *   int main (int argc, char * argv[]);
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

int main (int argc, const char * argv []) 

{
	extern struct channel channel;
	static const char *optv [] = 
	{
		"i:qt:v",
		PUTOPTV_S_DIVINE,
		"Atheros Powerline Packet Monitor",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i s\tuse host interface s [" CHANNEL_ETHDEVICE "]",

#else

		"i n\tuse host interface n [2]",

#endif

		"q\tsuppress output on stderr",
		"t n\ttimeout is (n) milliseconds [50]",
		"v\tverbose messages on stdout",
		(const char *) (0) 
	};
	signed c;
	if (getenv (PLCDEVICE)) 
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.id = atoi (getenv (PLCDEVICE));

#else

		channel.name = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	opterr = 1;
	optmin = 0;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch ((char) (c)) 
		{
		case 't':
			channel.timeout = (unsigned)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.id = atoi (optarg);

#else

			channel.name = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;

#ifndef WIN32

/*
 *      raw packets require root priviledges on linux; one does not have to be
 *      root when this program is installed setuid using 'chown root:root' and 
 *      'chmod 4555';
 */

	if (geteuid ()) 
	{
		error (1, EPERM, INT6K_NEEDROOT);
	}

#endif

	openchannel (&channel);
	function (&channel);
	closechannel (&channel);
	return (0);
}