

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

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

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

// #define RESOLV_DEBUG

char * resolvename_th(char * backdata, char *name, int timeout, unsigned long * ret)
{
char * recdata;

if((recdata = (char *)calloc(BUF_LEN, sizeof(char)))==NULL){ exit(1);}

recdata = th_resolv(name, recdata, timeout, 1);
swap_mem(recdata, '\n', 0x00);

#ifdef RESOLV_DEBUG
printf("RESOLV_DEBUG: resolvname(2): resolvname %s\n", recdata);
#endif

if(0 == memcmp(recdata, COM_1, 20)){
   backdata = safe_memcpy(backdata, SOCK_10, BUF_MAX);
   #ifdef RESOLV_DEBUG
   fprintf(stderr,"RESOLV_DEBUG: resolvname(3): %s %s\n", SOCK_10, name);
   #endif
   * ret = 0;
   }
else if(0 == memcmp(recdata, "0.0.0.0", 7)){
   backdata = safe_memcpy(backdata, SOCK_2, BUF_MAX);
   #ifdef RESOLV_DEBUG
   fprintf(stderr,"RESOLV_DEBUG: resolvname(4): %s\n", SOCK_2);
   #endif
   * ret = 0;
   }
else{
   //  ちゃんと引けた場合
   backdata = safe_memcpy(backdata, SOCK_11, BUF_MAX);
   #ifdef RESOLV_DEBUG
   fprintf(stderr,"RESOLV_DEBUG: resolvname(5): %s %s\n", SOCK_11, name);
   #endif
// * ret = (unsigned long)inet_addr(recdata);
   inet_pton(AF_INET, recdata, ret);
   }

free(recdata);

#ifdef RESOLV_DEBUG
fprintf(stderr,"RESOLV_DEBUG: resolvname(6): name = %s, backdata = %s\n", name, backdata);
#endif


return(backdata);
}



