//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include <time.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------




void __fastcall TForm1::btnpingClick(TObject *Sender)
{
  lbausgabe->Items->Clear();
  int ip1calc,diff = 0,durchgang = 1,ip2calc = 0,pakete = 2,ipdelete;
  bool first = true,ipcheck = true;
  long zeitstart,zeitende;
  if (ip1_1->Text == "")
    ipcheck = false;
  if (ip1_2->Text == "" && ipcheck)
    ipcheck = false;
  if (ip1_3->Text == "" && ipcheck)
    ipcheck = false;
  if (ip1_4->Text == "" && ipcheck)
    ipcheck = false;
  if (edtpakete->Text == "")

  if (ipcheck) {
    if (StrToInt(ip1_1->Text) < 1 || StrToInt(ip1_1->Text) > 255)
      ipcheck = false;
    if (StrToInt(ip1_2->Text) < 1 || StrToInt(ip1_2->Text) > 255)
      ipcheck = false;
    if (StrToInt(ip1_3->Text) < 1 || StrToInt(ip1_3->Text) > 255)
      ipcheck = false;
    if (StrToInt(ip1_4->Text) < 1 || StrToInt(ip1_4->Text) > 255)
      ipcheck = false;
  }

  if (ipcheck) {

    zeitstart=clock();
    if (ip2_4->Text != "") {
      ip2calc = StrToInt(ip2_4->Text);
      if (StrToInt(ip2_4->Text) < 1 || StrToInt(ip1_4->Text) > 255)
        ipcheck = false;
    }
    if (ip2_4->Text == "")
      diff = 1;

    ip1calc = StrToInt(ip1_4->Text);
    
    if (ip1calc > ip2calc && diff == 1)
      diff = (ip1calc - ip2calc)+1;

    if (ip1calc < ip2calc && diff == 1)
      diff = (ip2calc - ip1calc)+1;


    while (diff > 0) {

    pakete = 1;
    AnsiString ip1 = "ping -n ";
    AnsiString host = "";
    if (edtpakete->Text != "")
      pakete = StrToInt(edtpakete->Text);
    ip1 = ip1.Insert (IntToStr(pakete),ip1.Length()+1);
    if (cbhost->Checked == true) {
      ip1 = ip1.Insert(" ",ip1.Length()+1);
      ip1 = ip1.Insert("-a ",ip1.Length()+1);
    }
    ip1 = ip1.Insert (" ",ip1.Length()+1);
    ip1 = ip1.Insert (ip1_1->Text,ip1.Length()+1);
    ip1 = ip1.Insert (".",ip1.Length()+1);
    ip1 = ip1.Insert (ip1_2->Text,ip1.Length()+1);
    ip1 = ip1.Insert (".",ip1.Length()+1);
    ip1 = ip1.Insert (ip1_3->Text,ip1.Length()+1);
    ip1 = ip1.Insert (".",ip1.Length()+1);

      if (ip2_4->Text == "")
        ip1 = ip1.Insert (ip1_4->Text,ip1.Length()+1);


      else {
        if (first) {
          ip1 = ip1.Insert (ip1_4->Text,ip1.Length()+1);
          first = false;
        }
        else { // Durchgang löschen
          int ip1_4zahl = StrToInt(ip1_4->Text) + durchgang; // ip1_4->Text => eher extra variablen
          ip1 = ip1.Insert (IntToStr(ip1_4zahl),ip1.Length()+1);
          durchgang++;
        }
      }

      ip1 = ip1.Insert (" > file.txt", ip1.Length()+1);

//      char* eingabe = new char[ip1.Length() + 1 ];
//      strcpy(eingabe, ip1.c_str());
      system(ip1.c_str());

      Memo1->Lines->Clear();
      Memo1->Lines->LoadFromFile("file.txt");
      bool antwort = false,title = true;
      AnsiString ipadresse = "";
      for (int i = 1; i <= Memo1->Lines->Text.Length(); i++) {
        if(Memo1->Lines->Text[i] == 'A' && Memo1->Lines->Text[i+1] == 'n' &&
          Memo1->Lines->Text[i+2] == 't' && Memo1->Lines->Text[i+3] == 'w')
            antwort = true;

        
        if(Memo1->Lines->Text[i] == 'P' && Memo1->Lines->Text[i+1] == 'i' &&
           Memo1->Lines->Text[i+2] == 'n' && Memo1->Lines->Text[i+3] == 'g' && title) {
          ipadresse = Memo1->Lines->Text.SubString(i,Memo1->Lines->Text.Pos(":") );
          title = false;

        }
      }
      //Ping ha-42.web.de [217.72.195.42] mit 32 Bytes Daten:
      bool hostname = false;
      if(ipadresse.Pos("[") > 0) {
        host = "";
        host = ipadresse.SubString(ipadresse.Pos("Ping")+5,ipadresse.Pos("[")-6);
        ipadresse = ipadresse.Delete(ipadresse.Pos("Ping")+5,ipadresse.Pos("[")-5);
        hostname = true;
      }
      //Ping wird ausgeführt für 192.162.20.1 mit 32 Bytes Daten:
      bool ipadressedone = false;
      for(int i = 1; i <= ipadresse.Length() && ipadressedone == false; i++)
        if (ipadresse[i] > char('0') && ipadresse[i] < char('9') ) {
          ipadresse = ipadresse.Delete(1,i-1);
          ipadressedone = true;
          for(int i = 1; i <= ipadresse.Length(); i++)
            if (ipadresse[i] == char(' ') || ipadresse[i] == char(']') )
              ipadresse = ipadresse.Delete(i,ipadresse.Length());
        }

      if (antwort) {
        lbausgabe->Items->Add("Host gefunden bei: " + ipadresse);
        if(cbhost->Checked) {
          if (hostname)
            lbausgabe->Items->Add("Name des Hosts: " + host);
          else
            lbausgabe->Items->Add("Konnte keinen Hostname rausfinden");
        }
        lbausgabe->Items->Add("");
      }

      if (antwort==false) {
        lbausgabe->Items->Add("Keinen Host gefunden bei: " + ipadresse);
        lbausgabe->Items->Add("");
      }
        
      diff--;

    }
  }
  else {
    lbausgabe->Items->Add("Eine Ihrer Eingaben ist Inkorrekt");
    lbausgabe->Items->Add("Bitte überprüfen sie ob alle Werte der IP");
    lbausgabe->Items->Add("zwischen 1 und 255 liegen!");
    lbausgabe->Items->Add("");
    lbausgabe->Items->Add("Bitte überprüfen sie auch ob die Paketanzahl");
    lbausgabe->Items->Add("zwischen 1 und 10 liegt!");
  }

  zeitende=clock();
  double tempzahl = zeitende-zeitstart;
  tempzahl = tempzahl /1000;
  tempzahl = int(tempzahl*100.0)/100.0;
  AnsiString temp = "Der Scan dauerte " + FloatToStr(tempzahl) + " Sekunden";
  edtzeit->Text = temp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ip1_1Change(TObject *Sender)
{
  ip2_1->Text = ip1_1->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ip1_2Change(TObject *Sender)
{
  ip2_2->Text = ip1_2->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ip1_3Change(TObject *Sender)
{
  ip2_3->Text = ip1_3->Text;
}
//---------------------------------------------------------------------------



void __fastcall TForm1::btnsaveClick(TObject *Sender)
{
//  SaveDialog1->Execute();
  lbausgabe->Items->SaveToFile(SaveDialog1->FileName);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::btnwebhostClick(TObject *Sender)
{
  lbausgabe->Items->Clear();
  AnsiString webhost = edtwebhost->Text;
  webhost = webhost.Insert("ping -n 2 ",1);
  webhost = webhost.Insert(" > file.txt",webhost.Length()+1);
  char* eingabe = new char[webhost.Length() + 1 ];
  strcpy(eingabe, webhost.c_str());
  system(eingabe);

  Memo1->Lines->Clear();
  Memo1->Lines->LoadFromFile("file.txt");
  
  AnsiString ipadresse = Memo1->Lines->Text;
  bool ipadressedone = false,keineantwort = true;
  for(int i = 1; i <= ipadresse.Length() && ipadressedone == false; i++) {
  if (ipadresse[i] > char('0') && ipadresse[i] < char('9') ) {
    ipadresse = ipadresse.Delete(1,i-1);
    ipadressedone = true;
      for(int i = 1; i <= ipadresse.Length(); i++) {
        if (ipadresse[i] == char(' ') || ipadresse[i] == char(']') ) {
          ipadresse = ipadresse.Delete(i,ipadresse.Length());
          keineantwort = false;
        }
      }
    }
  }
  if (keineantwort)
    lbausgabe->Items->Add("Konnte keine IP finden");
  else
    lbausgabe->Items->Add("Die IP-Adresse von " + edtwebhost->Text + " lautet " + ipadresse);

}
//---------------------------------------------------------------------------

bool showhelp = false;
void __fastcall TForm1::btnhelpClick(TObject *Sender)
{
  if (showhelp == false) {
    frmhelp->Visible = True;
    showhelp = true;
  }
  else {
    frmhelp->Visible = False;
    showhelp = false;
  }
}
//---------------------------------------------------------------------------

