/*====================================================================*
 *
 *   signed anynic (char buffer [], size_t length);
 *
 *   ether.h
 *
 *   find an available host Ethernet interface and copy the name
 *   into the designated buffer; truncate the name if necessary;
 *   return 0 on success or -1 on error;
 *
 *   if the only interface is loopback then -1 is returned since 
 *   lo is of no use;
 *
 *   this function is only available for Linux at this time and
 *   is only used by the CGI device manager;
 *
 *   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 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *      Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#ifndef ANYNIC_SOURCE
#define ANYNIC_SOURCE

#include <sys/ioctl.h>
#include <memory.h>
#include <errno.h>

#include "../ether/ether.h"
#include "../tools/error.h"

signed anynic (char buffer [], size_t length) 

{
	char memory [1024];
	struct ifconf ifc;
	struct ifreq *ifr;
	signed fd;
	memset (buffer, 0, length);
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) 
	{
		error (1, errno, "Can't open socket");
	}
	ifc.ifc_len = sizeof (memory);
	ifc.ifc_buf = memory;
	if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) 
	{
		error (1, errno, "Can't read configuration");
	}
	ifr = ifc.ifc_req;
	if ((length) && (ifc.ifc_len > sizeof (struct ifreq))) 
	{
		memcpy (buffer, (++ifr)->ifr_name, --length);
		return (0);
	}
	return (-1);
}

#endif