Cooler_

ntop

Apr 14th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. //function from my Brother I4K the master of wizards
  2. //from Orion-Socket API
  3. int orion_getHostByName(const char* name, char* buffer)
  4. {
  5.     struct addrinfo hints, * res, * res0 = NULL;
  6.     struct sockaddr_in * target = NULL;
  7.     int error;
  8.     const char *tmp = NULL;
  9.    
  10.     memset(&hints, 0, sizeof(struct addrinfo));
  11.    
  12.     hints.ai_family = PF_UNSPEC;
  13.     hints.ai_socktype = 0;
  14.     error = getaddrinfo(name, "http", &hints, &res0);
  15.    
  16.     if(error)
  17.     {
  18.         if (res0)
  19.             freeaddrinfo(res0);
  20.         return 1;
  21.     }
  22.    
  23.     for (res = res0; res; res = res->ai_next)
  24.     {
  25.         target = (struct sockaddr_in *) res->ai_addr;
  26.         if (target)
  27.         {
  28.       //      tmp = inet_ntoa(target->sin_addr);
  29.           tmp=(char *)inet_ntop(AF_INET,&(target->sin_addr),name,INET_ADDRSTRLEN);
  30.             if (tmp && strlen(tmp))
  31.             {
  32.                 strncpy(buffer, tmp, strlen(tmp));
  33.                 buffer[strlen(tmp)] = '\0';
  34.                 if (res0)
  35.                     freeaddrinfo(res0);
  36.                 return 1;
  37.             }
  38.         }
  39.     }
  40.    
  41.     freeaddrinfo(res0);
  42.    
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment