/***************************************************************************
                             winsockx.cpp
                             -------------------
    Begin                : Sun Oct 13, 2001
    Objective            : Miscallaneous winsock functions
    Author               : Drew Hall
    Email                : dhall@Zero-Soft.com
 ***************************************************************************/

#include "stdafx.h"


struct IpInfo
{
	unsigned char	pIpAddress[4];
};
typedef struct IpInfo IPINFO;


bool StartupWinsock()
{
   DWORD nRet;
   WSADATA wsaData;

   OutputDebugString(TEXT("Loading winsock...\r\n"));
   nRet = WSAStartup(MAKEWORD(2,0), &wsaData);
   if(nRet == 0)
   {
      OutputDebugString(TEXT("Loaded Winsock 2.0!\r\n"));
      return true;
   }

   nRet = WSAStartup(MAKEWORD(1,1), &wsaData);
   if(nRet == 0)
   {
      OutputDebugString(TEXT("Loaded Winsock 1.1!\r\n"));
      return true;
   }

   nRet = WSAStartup(MAKEWORD(1,0), &wsaData);
   if(nRet == 0)
   {
      OutputDebugString(TEXT("Loaded Winsock 1.0!\r\n"));
      return true;
   }


   OutputDebugString(TEXT("Unable to load winsock!\r\n"));
   return false;
}


bool ShutdownWinsock()
{
   WSACleanup();

   return true;
}


IPINFO** GetIPList()
{
   DWORD nRet, nNumAddr;
   char szHostName[SIZE_HOSTNAME +1];
   IPINFO** pIPInfo;
   HOSTENT* pHostEnt;


   nRet = gethostname(szHostName, sizeof(szHostName));
   if(nRet != 0 || strlen(szHostName) <= 0)
   {
      DebugText(TEXT("GetHostName() Failed.\r\n"));
      return NULL;
   }

   pHostEnt = gethostbyname(szHostName);
   if(pHostEnt == NULL)
   {
      DebugText(TEXT("GetHostByName() Failed.\r\n"));
      return NULL;
   }

   nNumAddr = GetNumAddr(pHostEnt->h_addr_list);
   if(nNumAddr < 0)
   {
      DebugText(TEXT("GetNumAddr() Failed.\r\n"));
      return NULL;
   }

   pIPInfo = new IPINFO * [nNumAddr+1];
   if(pIPInfo == NULL)
   {
      DebugText(TEXT("Unable to allocate memory for IP Block.\r\n"));
      return NULL;
   }

   for(DWORD i=0; i<nNumAddr; i++)
   {
      pIPInfo[i] = new IPINFO;
      if(pIPInfo[i] == NULL)
      {
         DebugText(TEXT("Unable to allocate space for IP Block items.\r\n"));
         return pIPInfo;
      }

      pIPInfo[i]->pIpAddress[0] = pHostEnt->h_addr_list[i][0] & 0x00FF;
      pIPInfo[i]->pIpAddress[1] = pHostEnt->h_addr_list[i][1] & 0x00FF;
      pIPInfo[i]->pIpAddress[2] = pHostEnt->h_addr_list[i][2] & 0x00FF;
      pIPInfo[i]->pIpAddress[3] = pHostEnt->h_addr_list[i][3] & 0x00FF;
   }
	
   pIPInfo[i] = NULL;

	return pIPInfo;
}


int GetNumAddr(char **pzAddrList)
{
   if(pzAddrList == NULL)
      return -1;

   for(int i=0; pzAddrList[i] != NULL; i++);

   return i;
}

bool ValidateIP(IPINFO* pIPInfo)
{
   switch(pIPInfo->pIpAddress[0])
   {
      case 127:
         return false;

      case 10:
         return false;

      case 172:
         if(pIPInfo->pIpAddress[1] >= 16 && pIPInfo->pIpAddress[1] <= 31)
            return false;
         else
            return true;

      case 192:
         if(pIPInfo->pIpAddress[1] == 168)
            return false;
         else
            return true;

      default:
         return true;
   }
}

int FindIP(IPINFO* pIPInfoSearch,IPINFO ** pIPInfoBlock)
{
   for(int i=0; pIPInfoBlock[i] != NULL; i++)
   {
      if(0 == strncmp((char*)pIPInfoSearch->pIpAddress, (char*)pIPInfoBlock[i]->pIpAddress,4))
         return i;
   }

   return -1;
}


bool IsIP(char *szHostName)
{
   char *character;
   character = szHostName;

   while(*character != '\0')
   {
      if(*character != '.' && !isdigit(*character))
         return false;

      character++;
   }

   return true;
}