示例#1
0
CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
                                const char *local_ip6)
{
#if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
  unsigned char a6[INET6_ADDRSTRLEN];

  if((!local_ip6) || (local_ip6[0] == 0)) {
    /* disabled: do not bind to a specific address */
    memset(a6, 0, sizeof(a6));
  }
  else {
    if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
      return CURLE_BAD_FUNCTION_ARGUMENT;
    }
  }

  ares_set_local_ip6((ares_channel)data->state.resolver, a6);

  return CURLE_OK;
#else /* c-ares version too old! */
  (void)data;
  (void)local_ip6;
  return CURLE_NOT_BUILT_IN;
#endif
}
static PyObject *
Channel_func_set_local_ip(Channel *self, PyObject *args)
{
    char *ip;
    struct in_addr addr4;
    struct in6_addr addr6;

    CHECK_CHANNEL(self);

    if (!PyArg_ParseTuple(args, "s:set_local_ip", &ip)) {
        return NULL;
    }

    if (ares_inet_pton(AF_INET, ip, &addr4) == 1) {
        ares_set_local_ip4(self->channel, ntohl(addr4.s_addr));
    } else if (ares_inet_pton(AF_INET6, ip, &addr6) == 1) {
        ares_set_local_ip6(self->channel, addr6.s6_addr);
    } else {
        PyErr_SetString(PyExc_ValueError, "invalid IP address");
        return NULL;
    }

    Py_RETURN_NONE;
}