
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in_systm.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<fcntl.h>
#include<arpa/inet.h>

#include<sys/stat.h>
#include<errno.h>

#include"sitar.h"
#include"sitar_scan.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"

extern global_data_t gd;


// #define HOST_BUG


/*----------------- SCAN HOST --------------------*/
char * scan_host(char * ip_data, char * ip_addr, char * backdata, int timeout, int * stat)
{
int resolv_ret, bcount, co, dmc;
unsigned long addr;
unsigned char inip_addr[4];
char ch[256];
char * ch_addr = ch;
char ci[256];
char * chip = ci;
char di[256];
char * dnsip = di;
char sw[512];
char * swap = sw;

memset(ch_addr, 0x00, 256);
memset(chip, 0x00, 256);
memset(dnsip, 0x00, 256);
memset(swap, 0x00, 512);

resolv_ret = 0;
addr = 0;
bcount = 0;
* stat = 1;

#ifdef HOST_BUG
printf("ipdata = %s\n", ip_data);
#endif

self_memcpy(dnsip, gd.di.dns_name, 256);
bcount = char_count(ip_data, 'D');

if(1 == bcount){
   if(-1 < self_delim_get(ip_data, chip, 'D', 0, 256)){
      if(-1 < self_delim_get(ip_data, dnsip, 'D', 1, 256)){
         #ifdef HOST_BUG
         printf("HOST: scan_host(00): chip = %s  dnsip = %s\n", chip, dnsip);
         #endif
         }
      else{
         backdata = safe_sprintf(backdata, BUF_MAX, "The following addresses are abnormal. %s\n", ip_data);
         #ifdef HOST_BUG
         printf("HOST: scan_host(01): RESOLV NAME %s\n", backdata);
         #endif
         * stat = 1;
         return backdata;
         }
      }
   else{
      backdata = safe_sprintf(backdata, BUF_MAX, "The following addresses are abnormal. %s\n", ip_data);
      #ifdef HOST_BUG
      printf("HOST: scan_host(02): RESOLV NAME %s\n", backdata);
      #endif
      * stat = 1;
      return backdata;
      }
   }
else if(1 < bcount){
   backdata = safe_sprintf(backdata, BUF_MAX, "The following addresses are abnormal. %s\n", ip_data);
   #ifdef HOST_BUG
   printf("HOST: scan_host(03): RESOLV NAME %s\n", backdata);
   #endif
   * stat = 1;
   return backdata;
   }
else{
   self_memcpy(chip, ip_data, 256);
   }

backdata = resolvename_sk(backdata, ip_addr, timeout, &addr, dnsip, &resolv_ret, '4');
self_memcpy(swap, backdata, 512);
backdata = null_free(backdata);

if(-1 == resolv_ret){
   backdata = safe_sprintf(backdata, BUF_MAX, "Following DNS is illegal or it doesn't exist. %s %s\n",dnsip, swap);
   #ifdef HOST_BUG
   printf("HOST: scan_host(04): RESOLV NAME %s\n", backdata);
   #endif
   * stat = 1;
   return backdata;
   }




memcpy(inip_addr, &addr, sizeof(inip_addr));

sprintf(ch_addr,"%d.%d.%d.%d",
  inip_addr[0],
  inip_addr[1],
  inip_addr[2],
  inip_addr[3]
  );

if(0 == (int)addr){
   backdata = safe_sprintf(backdata, BUF_MAX, "%s %s %s\n", SOCK_2, ip_addr, ch_addr);
   #ifdef HOST_BUG
   printf("HOST: scan_host(1): NOT RESOLV NAME %s\n", backdata);
   #endif
   * stat = -1;
   return backdata;
   }
else{
   memset(swap, 0x00, 512);
   dmc = char_count(chip, 'P');
   for(co = 0; co <= dmc; co++){
      if(-1 < self_delim_get(chip, swap, 'P', co, 256)){
         if(0 == self_memcmp(ch_addr, swap)){
            backdata = safe_sprintf(backdata, BUF_MAX, "%s %s Answer%s == %s data%s dns%s\n",
              SOCK_8, ip_addr, ch_addr, swap, chip, dnsip);
            #ifdef HOST_BUG
            printf("HOST: scan_host(2): RESOLV NAME %s\n", backdata);
            #endif
            * stat = 1;
            return backdata;
            }
         else{
            backdata = safe_sprintf(backdata, BUF_MAX, "%s %s Answer%s != data%s dns%s\n",
               SOCK_9, ip_addr, ch_addr, chip, dnsip);
            #ifdef HOST_BUG
            printf("HOST: scan_host(3): RESOLV NAME %s\n", backdata);
            #endif
            * stat = -1;
            }
         memset(swap, 0x00, 512);
         }
      else{
         backdata = safe_sprintf(backdata, BUF_MAX, "The following addresses are illegal. %s\n", chip);
         #ifdef HOST_BUG
         printf("HOST: scan_host(4): RESOLV NAME %s\n", backdata);
         #endif
         * stat = -1;
         return backdata;
         }
      }
   }

// * stat = 1;
return backdata;
}






