#include "dod.h"
//#include "logstuff.h"

void DialOnDemand::ensureonline()
   {
   // DOD stuff
/*   DOD_On = 0;

// Future stuff
  DOD_Retries = 0;

  // If no DOD files, don't bother
   if(!(access(StartDOD, R_OK) || access(StopDOD, R_OK)))
      {
      DOD_Sleeptime = 60;

    if(!access(DODCfg, R_OK))      // read DOD config file if there is one
    {
#ifdef VERBOSE_OPEN
      sprintf(logstr,"Reading %s for DOD parameters...\n", DODCfg);
      LogScreen(logstr);
#endif

      FILE *cfg;
      cfg = fopen(DODCfg, "r+t");

      fgets(netstring, 25, cfg);     // Use netstring as temp buffer
      DOD_Sleeptime = atoi(netstring);   // Get sleep time

      fgets(netstring, 25, cfg);     // Get the real netstring

      nl = strlen(netstring) - 1;    // make sure there's no \n
      if(netstring[nl] == '\n')
        netstring[nl] = '\0';


      fgets(netstring, 25, cfg);     // Get max retries
      if(atoi(netstring) > 0)
        DOD_Retries = atoi(netstring);

      fclose(cfg);
    }
    else
      strcpy(netstring, "default");  // default for english OS/2

    if(!rweonline())
    {
#ifdef VERBOSE_OPEN
      sprintf(logstr, "Not Connected, Starting StartDOD.cmd...\n"
          "Waiting for %d seconds to be connected.\n", DOD_Sleeptime);
      LogScreen(logstr);
#endif
      do
      {
        spawnl( P_NOWAIT, StartDOD, (char *)NULL );
        sleep( DOD_Sleeptime );
        if(!rweonline())   // Still not connected.
           {
#ifdef VERBOSE_OPEN
           sprintf(logstr,"Could not connect, hanging up and trying again...\n");
           LogScreen(logstr);
#endif
           spawnl( P_NOWAIT, StopDOD, (char *)NULL );
           sleep( 10 );     // Give it time to run whatever program that hangs up
           }
        else
           DOD_On = 1;
      } while(!rweonline() || SignalTriggered);
    }
  }
*/
  }

//////////////////////////////////////////////////////////////////////////////

void DialOnDemand::checkoffline()
   {
/*   if(DOD_On)
      {
#ifdef VERBOSE_OPEN
      sprintf(logstr,"Dialed in with DOD, disconnecting...\n");
      LogScreen(logstr);
      flushall();
#endif
      spawnl( P_NOWAIT, StopDOD, (char *)NULL );
      }
*/   }

//////////////////////////////////////////////////////////////////////////////

int DialOnDemand::rweonline(void)
   {
/* Old online detection
   int online = 0, len;

   const s32 index = 68;     // index to check if connected
   s32  s, rc, tmpmode;
   char netstat[128];

   netstat[index] = 0;       // clear the bit
   // Get routing data
   s = socket(PF_INET, SOCK_DGRAM, 0);
   rc = ioctl(s, SIOSTATRT, netstat, 128);
   soclose(s);

   if(rc != 0)
      {
      LogScreenf("Unable to get routing information\n");
      online = 0;      // not online
      }

   if(!netstat[index])  // no connection
      online = 0;
   else
      online = 1;

      return online;
*/

   int s, rc, i, j;
   struct ifmib MyIFMib = {0};
   struct ifact MyIFNet = {0};

   s = socket(PF_INET, SOCK_STREAM, 0);
   rc = ioctl(s, SIOSTATAT, (char *)&MyIFNet, sizeof(MyIFNet));             /* get active interfaces list */
   rc = ioctl(s, SIOSTATIF, (char *)&MyIFMib, sizeof(MyIFMib));

   for (i = 0; i < MyIFNet.ifNumber; i++)
      {
      j = MyIFNet.iftable[i].ifIndex;      /* j is now the index into the stats table for this i/f */
      if (MyIFMib.iftable[j].ifType != HT_ETHER)   /* i/f is not ethernet */
         {
         if (MyIFMib.iftable[j].ifType != HT_PPP)  /* i/f is not loopback (yes I know it says PPP) */
            {
            if (MyIFNet.iftable[i].ifa_addr != 0x0100007f)  /* same thing for TCPIP < 4.1 */
               {
               struct ifreq MyIFReq = {0};
               if (j < 9)
                  {
                  sprintf(MyIFReq.ifr_name, "lan%d", j);
                  }
               else
                  {
                  if (j > 9)
                     {
                     sprintf(MyIFReq.ifr_name, "ppp%d", j-10);
                     }
                  else
                     {
                     strcpy(MyIFReq.ifr_name, "lo");
                     }
                  }
               rc = ioctl(s, SIOCGIFFLAGS, (char*)&MyIFReq, sizeof(MyIFReq));
               if (!rc)
                  {
                  if ((MyIFReq.ifr_flags & IFF_UP) != 0)
                     {
                     soclose(s);
                     return 0;      // Report online if SLIP or PPP detected
                     }
                  }
               }
            }
         }
      }
   soclose(s);
   return 0;
   }

