Exemplo n.º 1
0
int uv_run(uv_loop_t* loop) {
  if (pGetQueuedCompletionStatusEx) {
    UV_LOOP(loop, uv_poll_ex);
  } else {
    UV_LOOP(loop, uv_poll);
  }

  assert(!UV_LOOP_ALIVE((loop)));
  return 0;
}
Exemplo n.º 2
0
int uv_run(uv_loop_t* loop) {
  if (pGetQueuedCompletionStatusEx) {
    UV_LOOP(loop, uv_poll_ex);
  } else {
    UV_LOOP(loop, uv_poll);
  }

  assert(loop->refs == 0);
  return 0;
}
Exemplo n.º 3
0
int uv_run() {
  if (pGetQueuedCompletionStatusEx) {
    UV_LOOP(uv_poll_ex);
  } else {
    UV_LOOP(uv_poll);
  }

  assert(LOOP->refs == 0);
  return 0;
}
Exemplo n.º 4
0
static void
DNSResolver_tp_dealloc(DNSResolver *self)
{
    if (self->channel) {
        uv_ares_destroy(UV_LOOP(self), self->channel);
        self->channel = NULL;
    }
    DNSResolver_tp_clear(self);
    Py_TYPE(self)->tp_free((PyObject *)self);
}
Exemplo n.º 5
0
static int
DNSResolver_tp_init(DNSResolver *self, PyObject *args, PyObject *kwargs)
{
    int r, optmask;
    struct ares_options options;
    Loop *loop;
    PyObject *servers = NULL;

    static char *kwlist[] = {"loop", "servers", NULL};

    if (self->channel) {
        PyErr_SetString(PyExc_DNSError, "Object already initialized");
        return -1;
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O:__init__", kwlist, &LoopType, &loop, &servers)) {
        return -1;
    }

    Py_INCREF(loop);
    self->loop = loop;

    r = ares_library_init(ARES_LIB_INIT_ALL);
    if (r != 0) {
        PyErr_SetString(PyExc_DNSError, "error initializing c-ares library");
        return -1;
    }

    optmask = ARES_OPT_FLAGS;
    options.flags = ARES_FLAG_USEVC;

    r = uv_ares_init_options(UV_LOOP(self), &self->channel, &options, optmask);
    if (r) {
        PyErr_SetString(PyExc_DNSError, "error c-ares library options");
        return -1;
    }

    if (servers) {
        return set_dns_servers(self, servers);
    }

    return 0;
}
Exemplo n.º 6
0
static PyObject *
DNSResolver_func_getaddrinfo(DNSResolver *self, PyObject *args, PyObject *kwargs)
{
    char *name;
    char port_str[6];
    int port, family, socktype, protocol, flags, r;
    struct addrinfo hints;
    ares_cb_data_t *cb_data = NULL;
    uv_getaddrinfo_t* handle = NULL;
    PyObject *callback;

    static char *kwlist[] = {"callback", "name", "port", "family", "socktype", "protocol", "flags", NULL};

    port = socktype = protocol = flags = 0;
    family = AF_UNSPEC;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|iiiii:getaddrinfo", kwlist, &name, &callback, &port, &family, &socktype, &protocol, &flags)) {
        return NULL;
    }

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

    if (port < 0 || port > 65536) {
        PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65536");
        return NULL;
    }
    snprintf(port_str, sizeof(port_str), "%d", port);

    handle = PyMem_Malloc(sizeof(uv_getaddrinfo_t));
    if (!handle) {
        PyErr_NoMemory();
        goto error;
    }

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

    Py_INCREF(callback);
    cb_data->resolver = self;
    cb_data->cb = callback;
    handle->data = (void *)cb_data;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = family;
    hints.ai_socktype = socktype;
    hints.ai_protocol = protocol;
    hints.ai_flags = flags;

    r = uv_getaddrinfo(UV_LOOP(self), handle, &getaddrinfo_cb, name, port_str, &hints);
    if (r != 0) {
        raise_uv_exception(self->loop, PyExc_DNSError);
        goto error;
    }

    Py_RETURN_NONE;

error:
    if (handle) {
        PyMem_Free(handle);
    }
    if (cb_data) {
        Py_DECREF(callback);
        PyMem_Free(cb_data);
    }
    return NULL;
}
Exemplo n.º 7
0
static void
getaddrinfo_cb(uv_getaddrinfo_t* handle, int status, struct addrinfo* res)
{
    PyGILState_STATE gstate = PyGILState_Ensure();
    struct addrinfo *ptr;
    ares_cb_data_t *data;
    uv_err_t err;
    DNSResolver *self;
    PyObject *callback, *addr, *item, *errorno, *dns_result, *result;

    ASSERT(handle);

    data = (ares_cb_data_t*)(handle->data);
    self = data->resolver;
    callback = data->cb;

    ASSERT(self);
    /* Object could go out of scope in the callback, increase refcount to avoid it */
    Py_INCREF(self);

    if (status != UV_OK) {
        err = uv_last_error(UV_LOOP(self));
        errorno = PyInt_FromLong((long)err.code);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    dns_result = PyList_New(0);
    if (!dns_result) {
        PyErr_NoMemory();
        PyErr_WriteUnraisable(Py_None);
        errorno = PyInt_FromLong((long)UV_ENOMEM);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    for (ptr = res; ptr; ptr = ptr->ai_next) {
        addr = makesockaddr(ptr->ai_addr, ptr->ai_addrlen);
        if (!addr) {
            PyErr_NoMemory();
            PyErr_WriteUnraisable(callback);
            break;
        }

        item = Py_BuildValue("iiisO", ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol, ptr->ai_canonname ? ptr->ai_canonname : "", addr);
        Py_DECREF(addr);
        if (!item) {
            PyErr_NoMemory();
            PyErr_WriteUnraisable(callback);
            break;
        }

        PyList_Append(dns_result, item);
        Py_DECREF(item);
    }
    errorno = Py_None;
    Py_INCREF(Py_None);

callback:
    result = PyObject_CallFunctionObjArgs(callback, self, dns_result, errorno, NULL);
    if (result == NULL) {
        PyErr_WriteUnraisable(callback);
    }
    Py_XDECREF(result);

    Py_DECREF(callback);
    uv_freeaddrinfo(res);
    PyMem_Free(handle);
    PyMem_Free(data);

    Py_DECREF(self);
    PyGILState_Release(gstate);
}