Exemplo n.º 1
0
EAPI int
ecore_con_info_get(Ecore_Con_Server *svr,
                   Ecore_Con_Info_Cb done_cb,
                   void             *data,
                   struct addrinfo  *hints)
{
   Ecore_Con_CAres *cares;
   int ai_family = AF_INET6;

   cares = calloc(1, sizeof(Ecore_Con_CAres));
   if (!cares)
     return 0;

   cares->svr = svr;
   cares->done_cb = done_cb;
   cares->data = data;

   if (hints)
     {
        ai_family = hints->ai_family;
        memcpy(&cares->hints, hints, sizeof(struct addrinfo));
     }

   if (inet_pton(AF_INET, svr->name, &cares->addr.v4) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_FALSE;
        ares_gethostbyaddr(info_channel, &cares->addr.v4,
                           sizeof(cares->addr.v4),
                           AF_INET,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
   else if (inet_pton(AF_INET6, svr->name, &cares->addr.v6) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_TRUE;
        ares_gethostbyaddr(info_channel, &cares->addr.v6,
                           sizeof(cares->addr.v6),
                           AF_INET6,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
   else
     {
        cares->byaddr = EINA_FALSE;
        ares_gethostbyname(info_channel, svr->name, ai_family,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }

   svr->infos = eina_list_append(svr->infos, cares);
   return 1;
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
	WSADATA wsa;
	subnet sub;
	int ret, x;
	WSAStartup(0x22, &wsa);
	fprintf(stderr, "CIDR Reverse DNS - William Welna ([email protected])\n\n");
	if(argc != 3) {
		fprintf(stderr, "No Range Specified and/or no output file or too many arguments\n");
		fprintf(stderr, "Usage: %s 127.0.0.0/24 localrdns.txt\n", argv[0]);
		exit(0);
	}
	if(ret=ares_library_init(ARES_LIB_INIT_ALL)!=ARES_SUCCESS) {
		fprintf(stderr, "ares_library_init() failed %i\n", ret);
		exit(0);
	}
	if(ret=ares_init(&ares_chan)!=ARES_SUCCESS) {
		fprintf(stderr, "ares_init() failed %i\n", ret);
		exit(0);
	}
	calculate_subnet(argv[1], &sub);
	build_ips(sub.addr_s, sub.addr_e);
	global_total = sub.total_ips;
	for(x=0; x < 60 && IPS_P != NULL; ++x, IPS_P=IPS_P->n)
		ares_gethostbyaddr(ares_chan, &IPS_P->ip, 4, AF_INET, rdns_callback, IPS_P);
	fprintf(stderr, "Resolving %s to %s (%i)\n", sub.ip_s, sub.ip_e, sub.total_ips);
	tehloop();
	dump_results(argv[2]);
	return 0;
}
Exemplo n.º 3
0
void rdns_callback(void *arg, int status, int timeouts, struct hostent *host) {
	list_struct *p = (list_struct *)arg;
	if(status == ARES_SUCCESS) {
		strcpy(p->hostname, host->h_name);
	} else {
		p->hostname[0] = '.';
	}
	++global_counter;
	if(IPS_P) { // when one is finished add another to the channel to be resolved
		ares_gethostbyaddr(ares_chan, &IPS_P->ip, 4, AF_INET, rdns_callback, IPS_P);
		IPS_P=IPS_P->n;
	}
}
Exemplo n.º 4
0
static PyObject *
DNSResolver_func_gethostbyaddr(DNSResolver *self, PyObject *args, PyObject *kwargs)
{
    char *name;
    int family, length;
    void *address;
    struct in_addr addr4;
    struct in6_addr addr6;
    ares_cb_data_t *cb_data;
    PyObject *callback;

    if (!PyArg_ParseTuple(args, "sO:gethostbyaddr", &name, &callback)) {
        return NULL;
    }

    if (!PyCallable_Check(callback)) {
        PyErr_SetString(PyExc_TypeError, "a callable is required");
        return NULL;
    }

    if (uv_inet_pton(AF_INET, name, &addr4) == 1) {
        family = AF_INET;
        length = sizeof(struct in_addr);
        address = (void *)&addr4;
    } else if (uv_inet_pton(AF_INET6, name, &addr6) == 1) {
        family = AF_INET6;
        length = sizeof(struct in6_addr);
        address = (void *)&addr6;
    } else {
        PyErr_SetString(PyExc_ValueError, "invalid IP address");
        return NULL;
    }

    cb_data = (ares_cb_data_t*) PyMem_Malloc(sizeof *cb_data);
    if (!cb_data) {
        return PyErr_NoMemory();
    }

    Py_INCREF(callback);
    cb_data->resolver = self;
    cb_data->cb = callback;

    ares_gethostbyaddr(self->channel, address, length, family, &host_cb, (void *)cb_data);

    Py_RETURN_NONE;
}
static PyObject *
Channel_func_gethostbyaddr(Channel *self, PyObject *args)
{
    char *name;
    int family, length;
    void *address;
    struct in_addr addr4;
    struct in6_addr addr6;
    PyObject *callback;

    CHECK_CHANNEL(self);

    if (!PyArg_ParseTuple(args, "sO:gethostbyaddr", &name, &callback)) {
        return NULL;
    }

    if (!PyCallable_Check(callback)) {
        PyErr_SetString(PyExc_TypeError, "a callable is required");
        return NULL;
    }

    if (ares_inet_pton(AF_INET, name, &addr4) == 1) {
        length = sizeof(struct in_addr);
        address = (void *)&addr4;
        family = AF_INET;
    } else if (ares_inet_pton(AF_INET6, name, &addr6) == 1) {
        length = sizeof(struct in6_addr);
        address = (void *)&addr6;
        family = AF_INET6;
    } else {
        PyErr_SetString(PyExc_ValueError, "invalid IP address");
        return NULL;
    }

    Py_INCREF(callback);
    ares_gethostbyaddr(self->channel, address, length, family, &host_cb, (void *)callback);

    Py_RETURN_NONE;
}
Exemplo n.º 6
0
    int gethostbyaddr_with_ares(const void *addr, int addrlen, int af,
            struct hostent *ret_h, char *buf, size_t &buflen,
            struct hostent **result, int *h_errnop)
    {
        int err = 0;
        if (!h_errnop) h_errnop = &err;

        cares_async_context ctx;
        ctx.called = false;
        ctx.errnop = h_errnop;
        ctx.ret = 0;
        ctx.result = result;
        ctx.buf = buf;
        ctx.buflen = &buflen;

        ares_channel c;
        ares_init(&c);
        ares_gethostbyaddr(c, addr, addrlen, af,
                [](void * arg, int status, int timeouts, struct hostent *hostent)
                {
                    cares_async_context *ctx = (cares_async_context *)arg;
                    ctx->called = true;
                    if (status != ARES_SUCCESS) {
                        *ctx->result = nullptr;

                        switch (status) {
                        case ARES_ENODATA:
                            *ctx->errnop = NO_DATA;
                            break;

                        case ARES_EBADNAME:
                            *ctx->errnop = NO_RECOVERY;
                            break;

                        case ARES_ENOTFOUND:
                        default:
                            *ctx->errnop = HOST_NOT_FOUND;
                            break;
                        }

                        ctx->ret = -1;
                        return ;
                    }

                    if (!hostent_dup(hostent, *ctx->result, ctx->buf, *ctx->buflen)) {
                        *ctx->result = nullptr;
                        *ctx->errnop = ENOEXEC;    // 奇怪的错误码.
                        ctx->ret = -1;
                        return ;
                    }

                    *ctx->errnop = 0;
                    ctx->ret = 0;
                }, &ctx);

        fd_set readers, writers;
        FD_ZERO(&readers);
        FD_ZERO(&writers);

        int nfds = ares_fds(c, &readers, &writers);
        if (nfds) {
            struct timeval tv, *tvp;
            tvp = ares_timeout(c, NULL, &tv);
            int count = select(nfds, &readers, &writers, NULL, tvp);
            if (count > 0) {
                ares_process(c, &readers, &writers);
            }
        }

        if (ctx.called)
            return ctx.ret;

        // No yet invoke callback.
        *result = nullptr;
        *h_errnop = HOST_NOT_FOUND; // TODO
        return -1;
    }
Exemplo n.º 7
0
void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa,
                      ares_socklen_t salen,
                      int flags, ares_nameinfo_callback callback, void *arg)
{
  struct sockaddr_in *addr = NULL;
  struct sockaddr_in6 *addr6 = NULL;
  struct nameinfo_query *niquery;
  unsigned int port = 0;

  /* Verify the buffer size */
  if (salen == sizeof(struct sockaddr_in))
    {
      addr = (struct sockaddr_in *)sa;
      port = addr->sin_port;
    }
  else if (salen == sizeof(struct sockaddr_in6))
    {
      addr6 = (struct sockaddr_in6 *)sa;
      port = addr6->sin6_port;
    }
  else
    {
      callback(arg, ARES_ENOTIMP, 0, NULL, NULL);
      return;
    }

  /* If neither, assume they want a host */
  if (!(flags & ARES_NI_LOOKUPSERVICE) && !(flags & ARES_NI_LOOKUPHOST))
    flags |= ARES_NI_LOOKUPHOST;

  /* All they want is a service, no need for DNS */
  if ((flags & ARES_NI_LOOKUPSERVICE) && !(flags & ARES_NI_LOOKUPHOST))
    {
      char buf[33], *service;

      service = lookup_service((unsigned short)(port & 0xffff),
                               flags, buf, sizeof(buf));
      callback(arg, ARES_SUCCESS, 0, NULL, service);
      return;
    }

  /* They want a host lookup */
  if ((flags & ARES_NI_LOOKUPHOST))
    {
     /* A numeric host can be handled without DNS */
     if ((flags & ARES_NI_NUMERICHOST))
      {
        char ipbuf[IPBUFSIZ];
        char srvbuf[33];
        char *service = NULL;
        ipbuf[0] = 0;

        /* Specifying not to lookup a host, but then saying a host
         * is required has to be illegal.
         */
        if (flags & ARES_NI_NAMEREQD)
          {
            callback(arg, ARES_EBADFLAGS, 0, NULL, NULL);
            return;
          }
        if (salen == sizeof(struct sockaddr_in6))
          {
            ares_inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, IPBUFSIZ);
            /* If the system supports scope IDs, use it */
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
            append_scopeid(addr6, flags, ipbuf, sizeof(ipbuf));
#endif
          }
        else
          {
            ares_inet_ntop(AF_INET, &addr->sin_addr, ipbuf, IPBUFSIZ);
          }
        /* They also want a service */
        if (flags & ARES_NI_LOOKUPSERVICE)
          service = lookup_service((unsigned short)(port & 0xffff),
                                   flags, srvbuf, sizeof(srvbuf));
        callback(arg, ARES_SUCCESS, 0, ipbuf, service);
        return;
      }
    /* This is where a DNS lookup becomes necessary */
    else
      {
        niquery = malloc(sizeof(struct nameinfo_query));
        if (!niquery)
          {
            callback(arg, ARES_ENOMEM, 0, NULL, NULL);
            return;
          }
        niquery->callback = callback;
        niquery->arg = arg;
        niquery->flags = flags;
        niquery->timeouts = 0;
        if (sa->sa_family == AF_INET)
          {
            niquery->family = AF_INET;
            memcpy(&niquery->addr.addr4, addr, sizeof(addr));
            ares_gethostbyaddr(channel, &addr->sin_addr, sizeof(struct in_addr), AF_INET,
                               nameinfo_callback, niquery);
          }
        else
          {
            niquery->family = AF_INET6;
            memcpy(&niquery->addr.addr6, addr6, sizeof(addr6));
            ares_gethostbyaddr(channel, &addr6->sin6_addr, sizeof(struct in6_addr), AF_INET6,
                               nameinfo_callback, niquery);
          }
      }
    }
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
  struct ares_options options;
  int optmask = 0;
  ares_channel channel;
  int status, nfds, c, addr_family = AF_INET;
  fd_set read_fds, write_fds;
  struct timeval *tvp, tv;
  struct in_addr addr4;
  struct ares_in6_addr addr6;

#ifdef USE_WINSOCK
  WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
  WSADATA wsaData;
  WSAStartup(wVersionRequested, &wsaData);
#endif

  memset(&options, 0, sizeof(options));

  status = ares_library_init(ARES_LIB_INIT_ALL);
  if (status != ARES_SUCCESS)
    {
      fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
      return 1;
    }

  while ((c = ares_getopt(argc,argv,"dt:hs:")) != -1)
    {
      switch (c)
        {
        case 'd':
#ifdef WATT32
          dbug_init();
#endif
          break;
        case 's':
          optmask |= ARES_OPT_DOMAINS;
          options.ndomains++;
          options.domains = realloc(options.domains,
                                    options.ndomains * sizeof(char *));
          options.domains[options.ndomains - 1] = strdup(optarg);
          break;
        case 't':
          if (!strcasecmp(optarg,"a"))
            addr_family = AF_INET;
          else if (!strcasecmp(optarg,"aaaa"))
            addr_family = AF_INET6;
          else if (!strcasecmp(optarg,"u"))
            addr_family = AF_UNSPEC;
          else
            usage();
          break;
        case 'h':
        default:
          usage();
          break;
        }
    }

  argc -= optind;
  argv += optind;
  if (argc < 1)
    usage();

  status = ares_init_options(&channel, &options, optmask);
  if (status != ARES_SUCCESS)
    {
      fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
      return 1;
    }

  /* Initiate the queries, one per command-line argument. */
  for ( ; *argv; argv++)
    {
      if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
        {
          ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
                             *argv);
        }
      else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
        {
          ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
                             *argv);
        }
      else
        {
          ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
        }
    }

  /* Wait for all queries to complete. */
  for (;;)
    {
      int res;
      FD_ZERO(&read_fds);
      FD_ZERO(&write_fds);
      nfds = ares_fds(channel, &read_fds, &write_fds);
      if (nfds == 0)
        break;
      tvp = ares_timeout(channel, NULL, &tv);
      res = select(nfds, &read_fds, &write_fds, NULL, tvp);
      if (-1 == res)
        break;
      ares_process(channel, &read_fds, &write_fds);
    }

  ares_destroy(channel);

  ares_library_cleanup();

#ifdef USE_WINSOCK
  WSACleanup();
#endif

  return 0;
}
Exemplo n.º 9
0
void p_dns_gethostbyaddr(const void *address, int addrlen, int family, dns_host_callback callback, void *arg)
{
    pcontext;
    ares_gethostbyaddr(resolver, address, addrlen, family, (ares_host_callback)callback, arg);
}
Exemplo n.º 10
0
EAPI int
ecore_con_info_get(Ecore_Con_Server *obj,
                   Ecore_Con_Info_Cb done_cb,
                   void *data,
                   struct addrinfo *hints)
{
   Ecore_Con_Server_Data *svr = eo_data_scope_get(obj, ECORE_CON_SERVER_CLASS);
   Ecore_Con_CAres *cares;
#ifdef HAVE_IPV6
   int ai_family = AF_INET6;
#else
   int ai_family = AF_INET;
#endif

   cares = calloc(1, sizeof(Ecore_Con_CAres));
   if (!cares)
     return 0;

   cares->svr = obj;
   cares->done_cb = done_cb;
   cares->data = data;

   if (hints)
     {
        ai_family = hints->ai_family;
        memcpy(&cares->hints, hints, sizeof(struct addrinfo));
     }

   if (inet_pton(AF_INET, svr->ecs ? svr->ecs->ip : svr->name, &cares->addr.v4) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_FALSE;
        ares_gethostbyaddr(info_channel, &cares->addr.v4,
                           sizeof(cares->addr.v4),
                           AF_INET,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
#ifdef HAVE_IPV6
   else if (inet_pton(AF_INET6, svr->ecs ? svr->ecs->ip : svr->name, &cares->addr.v6) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_TRUE;
        ares_gethostbyaddr(info_channel, &cares->addr.v6,
                           sizeof(cares->addr.v6),
                           AF_INET6,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
#endif
   else
     {
        cares->byaddr = EINA_FALSE;
        ares_gethostbyname(info_channel, svr->ecs ? svr->ecs->ip : svr->name, ai_family,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }

   svr->infos = eina_list_append(svr->infos, cares);
   return 1;
}