static PyObject * conn_getattr(connobject *self, char *name) { PyObject *res; res = Py_FindMethod(connobjectmethods, (PyObject *)self, name); if (res != NULL) return res; PyErr_Clear(); if (strcmp(name, "base_server") == 0) { /* base_server serverobject is created as needed */ if (self->base_server == NULL) { if (self->conn->base_server == NULL) { Py_INCREF(Py_None); return Py_None; } else { self->base_server = MpServer_FromServer(self->conn->base_server); Py_INCREF(self->base_server); return self->base_server; } } else { Py_INCREF(self->base_server); return self->base_server; } } else if (strcmp(name, "aborted") == 0) { return PyInt_FromLong(self->conn->aborted); } else if (strcmp(name, "keepalive") == 0) { return PyInt_FromLong(self->conn->keepalive); } else if (strcmp(name, "double_reverse") == 0) { return PyInt_FromLong(self->conn->double_reverse); } else if (strcmp(name, "local_addr") == 0) { return makesockaddr(self->conn->local_addr); } #if AP_MODULE_MAGIC_AT_LEAST(20111130,0) else if (strcmp(name, "client_addr") == 0) { return makesockaddr(self->conn->client_addr); } else if (strcmp(name, "remote_addr") == 0) { ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, self->conn, "%s", "mod_python: conn.remote_addr deprecated in 2.4, " "use req.useragent_addr or conn.client_addr"); return makesockaddr(self->conn->client_addr); #else else if (strcmp(name, "remote_addr") == 0) {
static void on_udp_read(uv_udp_t* handle, int nread, const uv_buf_t* buf, struct sockaddr* addr, unsigned flags) { PyGILState_STATE gstate = PyGILState_Ensure(); Loop *loop; UDP *self; PyObject *result, *address_tuple, *data, *py_errorno; ASSERT(handle); ASSERT(flags == 0); self = PYUV_CONTAINER_OF(handle, UDP, udp_h); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (nread == 0) { goto done; } if (nread > 0) { ASSERT(addr); address_tuple = makesockaddr(addr, sizeof(*addr)); data = PyBytes_FromStringAndSize(buf->base, nread); py_errorno = Py_None; Py_INCREF(Py_None); } else { address_tuple = Py_None; Py_INCREF(Py_None); data = Py_None; Py_INCREF(Py_None); py_errorno = PyInt_FromLong((long)nread); } result = PyObject_CallFunctionObjArgs(self->on_read_cb, self, address_tuple, PyInt_FromLong((long)flags), data, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(address_tuple); Py_DECREF(data); Py_DECREF(py_errorno); done: /* data has been read, unlock the buffer */ loop = handle->loop->data; ASSERT(loop); loop->buffer.in_use = False; Py_DECREF(self); PyGILState_Release(gstate); }
static PyObject * TCP_func_getpeername(TCP *self) { int r, namelen; struct sockaddr peername; namelen = sizeof(peername); RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); r = uv_tcp_getpeername(&self->tcp_h, &peername, &namelen); if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError); return NULL; } return makesockaddr(&peername, namelen); }
static PyObject * UDP_func_getsockname(UDP *self) { int err, namelen; struct sockaddr_storage sockname; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); namelen = sizeof(sockname); err = uv_udp_getsockname(&self->udp_h, (struct sockaddr *)&sockname, &namelen); if (err < 0) { RAISE_UV_EXCEPTION(err, PyExc_UDPError); return NULL; } return makesockaddr((struct sockaddr *)&sockname, namelen); }
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); }
static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { PyGILState_STATE gstate = PyGILState_Ensure(); struct addrinfo *ptr; uv_err_t err; Loop *loop; PyObject *callback, *addr, *item, *errorno, *dns_result, *result; ASSERT(req); callback = (PyObject *)req->data; loop = (Loop *)req->loop->data; if (status != 0) { err = uv_last_error(loop->uv_loop); errorno = PyInt_FromLong((long)err.code); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } dns_result = PyList_New(0); if (!dns_result) { 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_Clear(); break; } item = PyStructSequence_New(&AddrinfoResultType); if (!item) { PyErr_Clear(); break; } PyStructSequence_SET_ITEM(item, 0, PyInt_FromLong((long)ptr->ai_family)); PyStructSequence_SET_ITEM(item, 1, PyInt_FromLong((long)ptr->ai_socktype)); PyStructSequence_SET_ITEM(item, 2, PyInt_FromLong((long)ptr->ai_protocol)); PyStructSequence_SET_ITEM(item, 3, Py_BuildValue("s", ptr->ai_canonname ? ptr->ai_canonname : "")); PyStructSequence_SET_ITEM(item, 4, addr); PyList_Append(dns_result, item); Py_DECREF(item); } errorno = Py_None; Py_INCREF(Py_None); callback: result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL); if (result == NULL) { handle_uncaught_exception(loop); } Py_XDECREF(result); Py_DECREF(dns_result); Py_DECREF(errorno); Py_DECREF(loop); Py_DECREF(callback); uv_freeaddrinfo(res); PyMem_Free(req); PyGILState_Release(gstate); }