/*
 *(C) 2004 by Sebastian Ortwein <kron@animeland.de>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <string.h>

#include "setup.h"
#include "readwriteconfs.h"

cNetworkSetup NetworkSetup;

// --- cNetworkSetup ---------------------------------------------------------------

cNetworkSetup::cNetworkSetup(void)
{
  Init();
}

void cNetworkSetup::Init(void)
{
 if (netGetNameserver(nameserver) == true);
 {
 	ipadresse(nameserver, 0, nameservera);
 	ipadresse(nameserver, 1, nameserverb);
 	ipadresse(nameserver, 2, nameserverc);
 	ipadresse(nameserver, 3, nameserverd);
 }
 
  networkcard = 0;
  if (getInetAttributes("eth0", automatic_eth0, method_eth0, address_eth0, netmask_eth0, broadcast_eth0, gateway_eth0) == true )
  {

	if (automatic_eth0 == true)
		automatic_start_eth0 = 1;
	else
		automatic_start_eth0 = 0;

	if (method_eth0 == "dhcp")
	{
		eth0 = 1;
		modi_eth0 = 0;
		addressa_eth0 = 0;
		addressb_eth0 = 0;
		addressc_eth0 = 0;
		addressd_eth0 = 0;
		netmaska_eth0 = 0;
		netmaskb_eth0 = 0;
		netmaskc_eth0 = 0;
		netmaskd_eth0 = 0;
		broadcasta_eth0 = 0;
		broadcastb_eth0 = 0;
		broadcastc_eth0 = 0;
		broadcastd_eth0 = 0;
		gatewaya_eth0 = 0;
		gatewayb_eth0 = 0;
		gatewayc_eth0 = 0;
		gatewayd_eth0 = 0;
	}
	else if (method_eth0 == "static")
	{
		eth0 = 1;
		modi_eth0 = 1;
		ipadresse(address_eth0, 0, addressa_eth0);
		ipadresse(address_eth0, 1, addressb_eth0);
		ipadresse(address_eth0, 2, addressc_eth0);
		ipadresse(address_eth0, 3, addressd_eth0);
		ipadresse(netmask_eth0, 0, netmaska_eth0);
		ipadresse(netmask_eth0, 1, netmaskb_eth0);
		ipadresse(netmask_eth0, 2, netmaskc_eth0);
		ipadresse(netmask_eth0, 3, netmaskd_eth0);
		ipadresse(broadcast_eth0, 0, broadcasta_eth0);
		ipadresse(broadcast_eth0, 1, broadcastb_eth0);
		ipadresse(broadcast_eth0, 2, broadcastc_eth0);
		ipadresse(broadcast_eth0, 3, broadcastd_eth0);
		ipadresse(gateway_eth0, 0, gatewaya_eth0);
		ipadresse(gateway_eth0, 1, gatewayb_eth0);
		ipadresse(gateway_eth0, 2, gatewayc_eth0);
		ipadresse(gateway_eth0, 3, gatewayd_eth0);
	}
	else
		eth0 = 0;
  }
  else
	  eth0 = 0;


    if (getInetAttributes("eth1", automatic_eth1, method_eth1, address_eth1, netmask_eth1, broadcast_eth1, gateway_eth1) == true )
  {
	if (automatic_eth1 == true)
		automatic_start_eth1 = 1;
	else
		automatic_start_eth1 = 0;

	if (method_eth1 == "dhcp")
	{
		eth1 = 1;
		modi_eth1 = 0;
		addressa_eth1 = 0;
		addressb_eth1 = 0;
		addressc_eth1 = 0;
		addressd_eth1 = 0;
		netmaska_eth1 = 0;
		netmaskb_eth1 = 0;
		netmaskc_eth1 = 0;
		netmaskd_eth1 = 0;
		broadcasta_eth1 = 0;
		broadcastb_eth1 = 0;
		broadcastc_eth1 = 0;
		broadcastd_eth1 = 0;
		gatewaya_eth1 = 0;
		gatewayb_eth1 = 0;
		gatewayc_eth1 = 0;
		gatewayd_eth1 = 0;
	}
	else if (method_eth1 == "static")
	{
		eth1 = 1;
		modi_eth1 = 1;
		ipadresse(address_eth1, 0, addressa_eth1);
		ipadresse(address_eth1, 1, addressb_eth1);
		ipadresse(address_eth1, 2, addressc_eth1);
		ipadresse(address_eth1, 3, addressd_eth1);
		ipadresse(netmask_eth1, 0, netmaska_eth1);
		ipadresse(netmask_eth1, 1, netmaskb_eth1);
		ipadresse(netmask_eth1, 2, netmaskc_eth1);
		ipadresse(netmask_eth1, 3, netmaskd_eth1);
		ipadresse(broadcast_eth1, 0, broadcasta_eth1);
		ipadresse(broadcast_eth1, 1, broadcastb_eth1);
		ipadresse(broadcast_eth1, 2, broadcastc_eth1);
		ipadresse(broadcast_eth1, 3, broadcastd_eth1);
		ipadresse(gateway_eth1, 0, gatewaya_eth1);
		ipadresse(gateway_eth1, 1, gatewayb_eth1);
		ipadresse(gateway_eth1, 2, gatewayc_eth1);
		ipadresse(gateway_eth1, 3, gatewayd_eth1);
	}
  }
  else
	  eth1 = 0;


    if (getInetAttributes("eth2", automatic_eth2, method_eth2, address_eth2, netmask_eth2, broadcast_eth2, gateway_eth2) == true )
  {
	if (automatic_eth2 == true)
		automatic_start_eth2 = 1;
	else
		automatic_start_eth2 = 0;

	if (method_eth2 == "dhcp")
	{
		eth2 = 1;
		modi_eth2 = 0;
		addressa_eth2 = 0;
		addressb_eth2 = 0;
		addressc_eth2 = 0;
		addressd_eth2 = 0;
		netmaska_eth2 = 0;
		netmaskb_eth2 = 0;
		netmaskc_eth2 = 0;
		netmaskd_eth2 = 0;
		broadcasta_eth2 = 0;
		broadcastb_eth2 = 0;
		broadcastc_eth2 = 0;
		broadcastd_eth2 = 0;
		gatewaya_eth2 = 0;
		gatewayb_eth2 = 0;
		gatewayc_eth2 = 0;
		gatewayd_eth2 = 0;
	}
	else if (method_eth2 == "static")
	{
		eth2 = 1;
		modi_eth2 = 1;
		ipadresse(address_eth2, 0, addressa_eth2);
		ipadresse(address_eth2, 1, addressb_eth2);
		ipadresse(address_eth2, 2, addressc_eth2);
		ipadresse(address_eth2, 3, addressd_eth2);
		ipadresse(netmask_eth2, 0, netmaska_eth2);
		ipadresse(netmask_eth2, 1, netmaskb_eth2);
		ipadresse(netmask_eth2, 2, netmaskc_eth2);
		ipadresse(netmask_eth2, 3, netmaskd_eth2);
		ipadresse(broadcast_eth2, 0, broadcasta_eth2);
		ipadresse(broadcast_eth2, 1, broadcastb_eth2);
		ipadresse(broadcast_eth2, 2, broadcastc_eth2);
		ipadresse(broadcast_eth2, 3, broadcastd_eth2);
		ipadresse(gateway_eth2, 0, gatewaya_eth2);
		ipadresse(gateway_eth2, 1, gatewayb_eth2);
		ipadresse(gateway_eth2, 2, gatewayc_eth2);
		ipadresse(gateway_eth2, 3, gatewayd_eth2);
	}
  }
  else
	  eth2 = 0;


    if (getInetAttributes("eth3", automatic_eth3, method_eth3, address_eth3, netmask_eth3, broadcast_eth3, gateway_eth3) == true )
  {
	if (automatic_eth3 == true)
		automatic_start_eth3 = 1;
	else
		automatic_start_eth3 = 0;
	if (method_eth3 == "dhcp")
	{
		eth3 = 1;
		modi_eth3 = 0;
		addressa_eth3 = 0;
		addressb_eth3 = 0;
		addressc_eth3 = 0;
		addressd_eth3 = 0;
		netmaska_eth3 = 0;
		netmaskb_eth3 = 0;
		netmaskc_eth3 = 0;
		netmaskd_eth3 = 0;
		broadcasta_eth3 = 0;
		broadcastb_eth3 = 0;
		broadcastc_eth3 = 0;
		broadcastd_eth3 = 0;
		gatewaya_eth3 = 0;
		gatewayb_eth3 = 0;
		gatewayc_eth3 = 0;
		gatewayd_eth3 = 0;
	}
	else if (method_eth3 == "static")
	{
		eth3 = 1;
		modi_eth3 = 1;
		ipadresse(address_eth3, 0, addressa_eth3);
		ipadresse(address_eth3, 1, addressb_eth3);
		ipadresse(address_eth3, 2, addressc_eth3);
		ipadresse(address_eth3, 3, addressd_eth3);
		ipadresse(netmask_eth3, 0, netmaska_eth3);
		ipadresse(netmask_eth3, 1, netmaskb_eth3);
		ipadresse(netmask_eth3, 2, netmaskc_eth3);
		ipadresse(netmask_eth3, 3, netmaskd_eth3);
		ipadresse(broadcast_eth3, 0, broadcasta_eth3);
		ipadresse(broadcast_eth3, 1, broadcastb_eth3);
		ipadresse(broadcast_eth3, 2, broadcastc_eth3);
		ipadresse(broadcast_eth3, 3, broadcastd_eth3);
		ipadresse(gateway_eth3, 0, gatewaya_eth3);
		ipadresse(gateway_eth3, 1, gatewayb_eth3);
		ipadresse(gateway_eth3, 2, gatewayc_eth3);
		ipadresse(gateway_eth3, 3, gatewayd_eth3);
	}
  }
  else
	  eth3 = 0;
}

bool cNetworkSetup::ipadresse(string zahl, int stelle, int &zahlint)
{
	int l = zahl.length();
	char *sz;
	sz = new char[l + 1];
	strcpy(sz, zahl.c_str());
	int i=0;
	int j=0;
	int k=0;
	char *str;
	str = new char[l];

	while (i != stelle && j <= l)
	{
	j++;
		if (sz[j] == '.')
		{
			j++;
			i++;
		}
	}
	
	while (sz[j] != '.' && j <= l)
	{
		str[k] = sz[j];
		k++;
		j++;
	}
	
	str[k] = '\0';
	zahlint = atoi ( str );
	delete [] sz;
	delete [] str;
	return true;
}

bool switchautomatic(int schalter)
{
	if (schalter == 1)
		return true;
	else
		return false;
}
