#include "dns.h"

/* Thanks to Stewart Brodie for the following code snippet
Unfortunately I had to butcher it to get it to work on my setup, sorry!*/

int dns_find_ip_address(const char *hostname, struct in_addr *ip)
{
  _kernel_oserror *e;
  _kernel_swi_regs rin, rout;
  int errno;  /* <-- we are preserving the global errno */
  struct hostent *h;

  if (inet_aton(hostname, ip)) {
    /* We were supplied with a dotted-quad address */
    return 0;
  }

  rin.r[0]=(int) hostname;

  e = _kernel_swi(Resolver_GetHost, &rin, &rout);

  errno = rout.r[0];

  h = (struct hostent *)rout.r[1];

  if (e || errno<0) return 2;
  if (errno == 36 || h == NULL) return errno;

  /* To get here, we got a valid hostent */
  *ip = **((struct in_addr **)h->h_addr_list);
  return 0;
}

