/*====================================================================*
 *   
 *   signed Sniffer (struct int6k * int6k);
 *   
 *   int6k.h
 *
 *   This software and documentation is the property of Atheros 
 *   Corporation, Ocala, Florida. It is provided 'as is' without 
 *   expressed or implied warranty of any kind to anyone for any 
 *   reason. Atheros assumes no responsibility or liability for 
 *   errors or omissions in the software or documentation and 
 *   reserves the right to make changes without notification. 
 *   
 *   Atheros customers may modify and distribute the software 
 *   without obligation to Atheros. Since use of this software 
 *   is optional, users shall bear sole responsibility and 
 *   liability for any consequences of it's use. 
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV; 
 *:  Published 2007 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *      Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#ifndef SNIFFER_SOURCE
#define SNIFFER_SOURCE

#include <memory.h>
#include <errno.h>

#include "../int6k/int6k.h"
#include "../int6k/message.h"
#include "../int6k/message.h"
#include "../int6k/intellon.h"
#include "../int6k/homeplug.h"
#include "../int6k/problem.h"
#include "../ether/channel.h"
#include "../tools/memory.h"
#include "../tools/error.h"
#include "../tools/flags.h"

signed Sniffer (struct int6k * int6k) 

{
	struct channel * channel = (struct channel *)(int6k->channel);
	struct message * message = (struct message *)(int6k->message);

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

	struct __packed vs_sniffer_request 
	{
		struct header_eth ethernet;
		struct header_int intellon;
		uint8_t SNIFFCTRL;
		uint32_t RESERVED;
	}
	* request = (struct vs_sniffer_request *) (&message);
	struct __packed vs_sniffer_confirm 
	{
		struct header_eth ethernet;
		struct header_int intellon;
		uint8_t MSTATUS;
		uint8_t SNIFSTATE;
		uint8_t SNIFFER_DA [ETHER_ADDR_LEN];
	}
	* confirm = (struct vs_sniffer_confirm *) (&message);
	char string [ETHER_ADDR_LEN * 3];

#ifndef __GNUC__
#pragma pack (pop)
#endif

	memset (message, 0, sizeof (struct message));
	EthernetHeader (&message->ethernet, channel->peer, channel->host);
	IntellonHeader (&message->intellon, (VS_SNIFFER | MMTYPE_REQ));
	request->SNIFFCTRL = int6k->hostaction;
	if (sendpacket (channel, message, ETHER_MIN_LEN) <= 0) 
	{
		error (1, ECANCELED, INT6K_CANTSEND);
	}
	while (readpacket (channel, message, sizeof (struct message)) > 0) 
	{
		if (UnwantedPacket (message, (VS_SNIFFER | MMTYPE_REQ))) 
		{
			continue;
		}
		Display (int6k, "%d %s", confirm->SNIFSTATE, hexstring (string, sizeof (string), confirm->SNIFFER_DA, sizeof (confirm->SNIFFER_DA)));
	}
	return (0);
}

#endif