static PyObject * UDP_func_getsockname(UDP *self) { int r, namelen; char ip[INET6_ADDRSTRLEN]; struct sockaddr sockname; struct sockaddr_in *addr4; struct sockaddr_in6 *addr6; namelen = sizeof(sockname); RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); r = uv_udp_getsockname((uv_udp_t *)UV_HANDLE(self), &sockname, &namelen); if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); return NULL; } if (sockname.sa_family == AF_INET) { addr4 = (struct sockaddr_in*)&sockname; uv_ip4_name(addr4, ip, INET_ADDRSTRLEN); return Py_BuildValue("si", ip, ntohs(addr4->sin_port)); } else if (sockname.sa_family == AF_INET6) { addr6 = (struct sockaddr_in6*)&sockname; uv_ip6_name(addr6, ip, INET6_ADDRSTRLEN); return Py_BuildValue("si", ip, ntohs(addr6->sin6_port)); } else { PyErr_SetString(PyExc_UDPError, "unknown address type detected"); return NULL; } }
void UvTcpSocket::on_connection1(uv_stream_t* stream, int status) { uv_stream_t* client = NULL; int conditon = SOCKET_READABLE; do { m_newConnection = NULL; ASSERT(status == 0); if (status) { print_info("connection error\r\n"); break; } client = (uv_stream_t*)malloc(sizeof uv_tcp_t); int ret = uv_tcp_init((uv_loop_t*)m_env->TaskScheduler()->loopHandle(), (uv_tcp_t*)client); ASSERT(ret == 0); if (ret) { LOG_ERROR("tcp error \r\n"); break; } ret = uv_accept(stream,(uv_stream_t*)client); ASSERT(ret == 0); if(ret) { LOG_ERROR("accept error\r\n"); break; } struct sockaddr_in addrIn ; int addrSize = sizeof(sockaddr); uv_tcp_getsockname((uv_tcp_t*)client, (sockaddr*)&addrIn, &addrSize); struct sockaddr_in addrInPeer ; uv_tcp_getpeername((uv_tcp_t*)client, (sockaddr*)&addrInPeer, &addrSize); char serverIp[128],client[128]; uv_ip4_name(&addrInPeer,client,128); uv_ip4_name(&addrIn,serverIp,128); LOG_DEBUG("%s:%d accept connection from %s:%d",serverIp, htons(addrIn.sin_port),client,htons(addrInPeer.sin_port)); m_newConnection = new UvTcpSocket(m_env,(uv_tcp_t*)client); if(m_ioHandlerProc) m_ioHandlerProc(m_ClientData, conditon); return ; } while (0); conditon |= SOCKET_EXCEPTION; if(m_ioHandlerProc) m_ioHandlerProc(m_ClientData, conditon); if(client) free(client); }
static int luv_interface_addresses(lua_State* L) { uv_interface_address_t* interfaces; int count, i; char ip[INET6_ADDRSTRLEN]; char netmask[INET6_ADDRSTRLEN]; uv_interface_addresses(&interfaces, &count); lua_newtable(L); for (i = 0; i < count; i++) { lua_getfield(L, -1, interfaces[i].name); if (!lua_istable(L, -1)) { lua_pop(L, 1); lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, -3, interfaces[i].name); } lua_newtable(L); lua_pushboolean(L, interfaces[i].is_internal); lua_setfield(L, -2, "internal"); lua_pushlstring(L, interfaces[i].phys_addr, sizeof(interfaces[i].phys_addr)); lua_setfield(L, -2, "mac"); if (interfaces[i].address.address4.sin_family == AF_INET) { uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip)); uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask)); } else if (interfaces[i].address.address4.sin_family == AF_INET6) { uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip)); uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask)); } else { strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN); strncpy(netmask, "<unknown sa family>", INET6_ADDRSTRLEN); } lua_pushstring(L, ip); lua_setfield(L, -2, "ip"); lua_pushstring(L, netmask); lua_setfield(L, -2, "netmask"); lua_pushstring(L, luv_af_num_to_string(interfaces[i].address.address4.sin_family)); lua_setfield(L, -2, "family"); lua_rawseti(L, -2, lua_rawlen (L, -2) + 1); lua_pop(L, 1); } uv_free_interface_addresses(interfaces, count); return 1; }
void UDPClient::on_read(uv_udp_t *req, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { UDPClient *serverPtr = (UDPClient *) req->data; if (nread < 0) { if (nread == -4095) { logger.log("UDPClient", "Socket closed.", LogLevel::warning); //std::cerr << "socket closed"; serverPtr->disconnect(true); } fprintf(stderr, "Read error %s\n", uv_err_name(nread)); uv_close((uv_handle_t *) req, NULL); return; } else if (nread == 0) { //nothing } else { char sender[17] = {0}; uv_ip4_name((const struct sockaddr_in *) addr, sender, 16); if (serverPtr->_onMessage != NULL) { serverPtr->_onMessage(std::string(buf->base, nread), std::string(sender)); } if (serverPtr->onMessageBin != NULL) { serverPtr->onMessageBin(buf->base, nread, std::string(sender)); } } free(buf->base); }
extern bool sockaddr_host(struct sockaddr* addr, char* host, uint32_t host_len, bool* ipv6, uint16_t* port) { uint16_t family = addr->sa_family; if (family == AF_INET) { if (host != NULL) { uv_ip4_name((sockaddr_in*)addr, host, host_len); } if (ipv6 != NULL) { *ipv6 = false; } if (port != NULL) { *port = ntohs(((sockaddr_in*)addr)->sin_port); } return true; } if (family == AF_INET6) { if (host != NULL) { uv_ip6_name((sockaddr_in6*)addr, host, host_len); } if (ipv6 != NULL) { *ipv6 = true; } if (port != NULL) { *port = ntohs(((sockaddr_in6*)addr)->sin6_port); } return true; } return false; }
/* Modified from Python Modules/socketmodule.c */ static PyObject * makesockaddr(struct sockaddr *addr, int addrlen) { struct sockaddr_in *addr4; struct sockaddr_in6 *addr6; char ip[INET6_ADDRSTRLEN]; if (addrlen == 0) { /* No address */ Py_RETURN_NONE; } switch (addr->sa_family) { case AF_INET: { addr4 = (struct sockaddr_in*)addr; uv_ip4_name(addr4, ip, INET_ADDRSTRLEN); return Py_BuildValue("si", ip, ntohs(addr4->sin_port)); } case AF_INET6: { addr6 = (struct sockaddr_in6*)addr; uv_ip6_name(addr6, ip, INET6_ADDRSTRLEN); return Py_BuildValue("siii", ip, ntohs(addr6->sin6_port), addr6->sin6_flowinfo, addr6->sin6_scope_id); } default: /* If we don't know the address family, don't raise an exception -- return it as a tuple. */ return Py_BuildValue("is#", addr->sa_family, addr->sa_data, sizeof(addr->sa_data)); } }
void on_resolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) { if (status != 0) { printf("on resolved failed: %s\n", uv_strerror(status)); exit(-1); } uv_loop_t *loop = req->loop; task *task = (struct task*)req->data; http_request *request = task->head_request; task->addrinfo = res; char addr[17] = { 0 }; uv_ip4_name((struct sockaddr_in*)res->ai_addr, addr, 16); printf("resolver ip %s\n", addr); struct sockaddr_in dest; uv_ip4_addr(addr, http_url_get_port(task->url), &dest); uv_tcp_init(loop, request->stream); request->stream->data = task; uv_tcp_connect(request->connect, request->stream, (const struct sockaddr*)&dest, send_head_request); free(req); }
static void udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { struct sockaddr sockname; char ip[20]; int namelen; int r; ASSERT(nread >= 0); if (nread == 0) { free(buf.base); return; } namelen = sizeof(sockname); r = uv_getsockname((uv_handle_t*)&udp, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (connector) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); printf("sockname = %s\n", ip); getsocknamecount++; uv_close((uv_handle_t*) &udp, NULL); uv_close((uv_handle_t*) handle, NULL); }
static int tcp_get_peerip(lua_State *l) { tcp_udata_t *udata = connection_udata(l); ls_tcp_t *handle = udata->handle; struct sockaddr sockname; int namelen = sizeof(sockname); struct sockaddr_in *sin = (struct sockaddr_in*)&sockname; char ip[17]; if (handle == NULL) return ls_error_return(l, LS_ERRCODE_EOF, "tcp connection closed"); if (uv_tcp_getpeername(&handle->handle, &sockname, &namelen)) { return ls_error_return(l, LS_ERRCODE_ERROR, "get sock name error"); } if (uv_ip4_name(sin, ip, sizeof ip)) { return ls_error_return(l, LS_ERRCODE_ERROR, "invalid ipv4."); } lua_pushboolean(l, 1); lua_pushstring(l, ip); return 2; }
std::string host() const { char dest[16]; if (uv_ip4_name(const_cast<sockaddr_in*>(&_addr), dest, 16) != 0) throw std::runtime_error("Cannot parse IPv4 hostname"); return dest; }
static void check_sockname(struct sockaddr* addr, const char* compare_ip, int compare_port, const char* context) { struct sockaddr_in check_addr = *(struct sockaddr_in*) addr; struct sockaddr_in compare_addr; char check_ip[17]; int r; ASSERT(0 == uv_ip4_addr(compare_ip, compare_port, &compare_addr)); /* Both addresses should be ipv4 */ ASSERT(check_addr.sin_family == AF_INET); ASSERT(compare_addr.sin_family == AF_INET); /* Check if the ip matches */ ASSERT(memcmp(&check_addr.sin_addr, &compare_addr.sin_addr, sizeof compare_addr.sin_addr) == 0); /* Check if the port matches. If port == 0 anything goes. */ ASSERT(compare_port == 0 || check_addr.sin_port == compare_addr.sin_port); r = uv_ip4_name(&check_addr, (char*) check_ip, sizeof check_ip); ASSERT(r == 0); printf("%s: %s:%d\n", context, check_ip, ntohs(check_addr.sin_port)); }
static void on_udp_read(uv_udp_t* handle, int nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { PyGILState_STATE gstate = PyGILState_Ensure(); char ip[INET6_ADDRSTRLEN]; struct sockaddr_in addr4; struct sockaddr_in6 addr6; uv_err_t err; UDP *self; PyObject *result, *address_tuple, *data, *py_errorno; ASSERT(handle); ASSERT(flags == 0); self = (UDP *)handle->data; ASSERT(self); /* 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); if (addr->sa_family == AF_INET) { addr4 = *(struct sockaddr_in*)addr; uv_ip4_name(&addr4, ip, INET_ADDRSTRLEN); address_tuple = Py_BuildValue("(si)", ip, ntohs(addr4.sin_port)); } else { addr6 = *(struct sockaddr_in6*)addr; uv_ip6_name(&addr6, ip, INET6_ADDRSTRLEN); address_tuple = Py_BuildValue("(si)", ip, ntohs(addr6.sin6_port)); } 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); err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong((long)err.code); } 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: Py_DECREF(self); PyGILState_Release(gstate); }
std::string UVTCPClient::IP4Addr() const { std::string s; char addr[17] = {'\0'}; uv_ip4_name((struct sockaddr_in*) address, addr, 16); s.assign(addr, strlen(addr)); return s; }
void TSockSys::OnGetHost(uv_getaddrinfo_t* RequestHnd, int Status, struct addrinfo* AddrInfo) { // get SockHost PSockHost SockHost; if (SockSys.HndToSockHostH.IsKey((uint64)RequestHnd)) { SockHost = SockSys.HndToSockHostH.GetDat((uint64)RequestHnd); SockSys.HndToSockHostH.DelKey((uint64)RequestHnd); } else { free(RequestHnd); uv_freeaddrinfo(AddrInfo); SaveToErrLog("SockSys.OnGetHost: unkown RequestId"); return; } // get SockEvent PSockEvent SockEvent; if (SockHost->IsSockEvent()) { SockEvent = SockHost->GetSockEvent(); } else { free(RequestHnd); uv_freeaddrinfo(AddrInfo); SaveToErrLog("SockSys.OnGetHost: SockHost without SockEvent"); return; } // parse results if (Status == 0) { SockHost->Status = shsOk; // temporary buffer for storing IPs char _addr[64] = {'\0'}; // iterate over all the resolved IPs struct addrinfo* AddrInfoIter = AddrInfo; while (AddrInfoIter != NULL) { //if (AddrInfoIter->ai_family //AF_INET6 // get IP as string if (AddrInfoIter->ai_family == AF_INET) { uv_ip4_name((struct sockaddr_in*)AddrInfoIter->ai_addr, _addr, sizeof(_addr)); } else if (AddrInfoIter->ai_family == AF_INET6) { uv_ip6_name((struct sockaddr_in6*)AddrInfoIter->ai_addr, _addr, sizeof(_addr)); } TStr IpNum(_addr); // add to SockHost SockHost->AddIpNum(IpNum); // go to the next IP on the list AddrInfoIter = AddrInfoIter->ai_next; } } else if (Status == -1) { // something went wrong SockHost->Status = shsError; SockHost->ErrMsg = "SockSys.OnGetHost: " + SockSys.GetLastErr(); } else { // unkown status SockHost->Status = shsError; SockHost->ErrMsg = TStr::Fmt("SockSys.OnGetHost: unkown status %d", Status); } // clean up free(RequestHnd); uv_freeaddrinfo(AddrInfo); // callback SockEvent->OnGetHost(SockHost); }
int main(int argc, char *argv[]) { char** hosts; char* hostname; char* user; char* name; int i, c = 0; uv_interface_address_t* addresses; uv_err_t err; srand(time(NULL)); atexit(forza__kill); signal(SIGTERM, forza__on_sigterm); loop = uv_default_loop(); #ifdef FORZA_VERSION_HASH printf("forza "FORZA_VERSION_HASH"\n"); #else printf("forza\n"); #endif opt = saneopt_init(argc - 1, argv + 1); saneopt_alias(opt, "host", "h"); hosts = saneopt_get_all(opt, "host"); hostname = saneopt_get(opt, "hostname"); user = saneopt_get(opt, "app-user"); name = saneopt_get(opt, "app-name"); arguments = saneopt_arguments(opt); if (hostname == NULL) { hostname = malloc(256 * sizeof(*hostname)); err = uv_interface_addresses(&addresses, &c); if (err.code != UV_OK) { fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop))); return 1; } for (i = 0; i < c; i++) { /* For now, only grab the first non-internal, non 0.0.0.0 interface. * TODO: Make this smarter. */ if (addresses[i].is_internal) continue; uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname)); if (strcmp(hostname, "0.0.0.0") != 0) break; } uv_free_interface_addresses(addresses, c); } forza_connect(hosts, hostname, user, name, on_connect); uv_run(loop, UV_RUN_DEFAULT); free(hosts); return 0; }
char * sg_etp_session_get_client_addr(sg_etp_session_t * session) { char * addr = NULL; addr = malloc(256); uv_ip4_name((const struct sockaddr_in*)&(session->addr), addr, 256); return addr; }
inline bool from_ip4_addr(ip4_addr* src, std::string& ip, int& port) { char dest[16]; if(uv_ip4_name(src, dest, 16) == 0) { ip = dest; port = static_cast<int>(ntohs(src->sin_port)); return true; } return false; }
int ip_name(const struct sockaddr *ip, char *name, size_t size) { int port = -1; if (ip->sa_family == AF_INET) { uv_ip4_name((const struct sockaddr_in*)ip, name, size); port = ntohs(((const struct sockaddr_in*)ip)->sin_port); } else if (ip->sa_family == AF_INET6) { uv_ip6_name((const struct sockaddr_in6*)ip, name, size); port = ntohs(((const struct sockaddr_in6*)ip)->sin6_port); } return port; }
/** List available interfaces. */ static int net_interfaces(lua_State *L) { /* Retrieve interface list */ int count = 0; char buf[INET6_ADDRSTRLEN]; /* http://tools.ietf.org/html/rfc4291 */ uv_interface_address_t *info = NULL; uv_interface_addresses(&info, &count); lua_newtable(L); for (int i = 0; i < count; ++i) { uv_interface_address_t iface = info[i]; lua_getfield(L, -1, iface.name); if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_newtable(L); } /* Address */ lua_getfield(L, -1, "addr"); if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_newtable(L); } if (iface.address.address4.sin_family == AF_INET) { uv_ip4_name(&iface.address.address4, buf, sizeof(buf)); } else if (iface.address.address4.sin_family == AF_INET6) { uv_ip6_name(&iface.address.address6, buf, sizeof(buf)); } else { buf[0] = '\0'; } lua_pushstring(L, buf); lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); lua_setfield(L, -2, "addr"); /* Hardware address. */ char *p = buf; memset(buf, 0, sizeof(buf)); for (unsigned k = 0; k < sizeof(iface.phys_addr); ++k) { sprintf(p, "%.2x:", iface.phys_addr[k] & 0xff); p += 3; } *(p - 1) = '\0'; lua_pushstring(L, buf); lua_setfield(L, -2, "mac"); /* Push table */ lua_setfield(L, -2, iface.name); } uv_free_interface_addresses(info, count); return 1; }
static int tcp_tostring(lua_State *l) { tcp_udata_t *udata = connection_udata(l); ls_tcp_t *handle = udata->handle; struct sockaddr lsockname, rsockname; int lnamelen = sizeof(lsockname); int rnamelen = sizeof(rsockname); struct sockaddr_in *lsin = (struct sockaddr_in*)&lsockname; struct sockaddr_in *rsin = (struct sockaddr_in*)&rsockname; char lip[17], rip[17]; if (handle == NULL) lua_pushliteral(l, "tcp connection (closed)"); else if (!uv_tcp_getsockname(&handle->handle, &lsockname, &lnamelen) && !uv_ip4_name(lsin, lip, sizeof lip) && !uv_tcp_getpeername(&handle->handle, &rsockname, &rnamelen) && !uv_ip4_name(rsin, rip, sizeof rip)) lua_pushfstring(l, "tcp connection (local: %s:%d, remote: %s:%d)", lip, ntohs(lsin->sin_port), rip, ntohs(rsin->sin_port)); else lua_pushliteral(l, "tcp connection (invalid)"); return 1; }
void rig_pb_stream_accept_tcp_connection(rig_pb_stream_t *stream, uv_tcp_t *server) { uv_loop_t *loop = rut_uv_shell_get_loop(stream->shell); struct sockaddr name; int namelen; int err; c_return_if_fail(stream->type == STREAM_TYPE_DISCONNECTED); c_return_if_fail(stream->hostname == NULL); c_return_if_fail(stream->port == NULL); c_return_if_fail(stream->resolving == false); uv_tcp_init(loop, &stream->tcp.socket); stream->tcp.socket.data = stream; err = uv_accept((uv_stream_t *)server, (uv_stream_t *)&stream->tcp.socket); if (err != 0) { c_warning("Failed to accept tcp connection: %s", uv_strerror(err)); uv_close((uv_handle_t *)&stream->tcp.socket, NULL); return; } err = uv_tcp_getpeername(&stream->tcp.socket, &name, &namelen); if (err != 0) { c_warning("Failed to query peer address of tcp socket: %s", uv_strerror(err)); stream->hostname = c_strdup("unknown"); stream->port = c_strdup("0"); } else if (name.sa_family != AF_INET) { c_warning("Accepted connection isn't ipv4"); stream->hostname = c_strdup("unknown"); stream->port = c_strdup("0"); } else { struct sockaddr_in *addr = (struct sockaddr_in *)&name; char ip_address[17] = {'\0'}; uv_ip4_name(addr, ip_address, 16); stream->hostname = c_strdup(ip_address); stream->port = c_strdup_printf("%u", ntohs(addr->sin_port)); } stream->type = STREAM_TYPE_TCP; set_connected(stream); }
static void on_address_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *result) { rig_pb_stream_t *stream = rut_container_of(resolver, stream, resolver); uv_loop_t *loop = rut_uv_shell_get_loop(stream->shell); char ip_address[17] = {'\0'}; c_return_if_fail(stream->resolving); if (status < 0) { c_warning("Failed to resolve slave address \"%s\": %s", stream->hostname, uv_strerror(status)); rut_closure_list_invoke(&stream->on_error_closures, rig_pb_stream_callback_t, stream); /* NB: we were at least keeping the stream alive while * waiting for the resolve request to finish... */ stream->resolving = false; rut_object_unref(stream); return; } uv_ip4_name((struct sockaddr_in*)result->ai_addr, ip_address, 16); c_message("stream: Resolved address of \"%s\" = %s", stream->hostname, ip_address); uv_tcp_init(loop, &stream->tcp.socket); stream->tcp.socket.data = stream; /* NB: we took a reference to keep the stream alive while * resolving the address, so conceptually we are now handing * the reference over to keep the stream alive while waiting * for it to connect... */ stream->resolving = false; stream->connecting = true; stream->connection_request.data = stream; uv_tcp_connect(&stream->connection_request, &stream->tcp.socket, result->ai_addr, on_connect); uv_freeaddrinfo(result); }
void on_resolved_callback(uv_getaddrinfo_t* resolver, int status, struct addrinfo * res) { Client* c = static_cast<Client*>(resolver->data); if(status == -1) { printf("ERROR: getaddrinfo callback error: \n");//, uv_err_name(uv_last_error(c->loop))); ::exit(0); } char addr[17] = {'\0'}; uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16); printf("Found host: %s\n", addr); uv_tcp_init(c->loop, &c->socket); uv_tcp_connect(&c->connect_req, &c->socket, (struct sockaddr *)res->ai_addr, on_connect_callback); uv_freeaddrinfo(res); }
inline error read_addr_info(int status, addrinfo* res, char* buf, bool &is_ip4) { if(status == 0){ if(res->ai_family == AF_INET){ uv_ip4_name(reinterpret_cast<struct sockaddr_in*>(res->ai_addr), buf, res->ai_addrlen); is_ip4 = true; } else if(res->ai_family == AF_INET6) { uv_ip6_name(reinterpret_cast<struct sockaddr_in6*>(res->ai_addr), buf, res->ai_addrlen); is_ip4 = false; } else { is_ip4 = false; return error(EAI_ADDRFAMILY); } } return error(status); }
void Client::connect(const std::vector<addrinfo*> &ipv4, const std::vector<addrinfo*> &ipv6) { addrinfo *addr = nullptr; m_ipv6 = ipv4.empty() && !ipv6.empty(); if (m_ipv6) { addr = ipv6[ipv6.size() == 1 ? 0 : rand() % ipv6.size()]; uv_ip6_name(reinterpret_cast<sockaddr_in6*>(addr->ai_addr), m_ip, 45); } else { addr = ipv4[ipv4.size() == 1 ? 0 : rand() % ipv4.size()]; uv_ip4_name(reinterpret_cast<sockaddr_in*>(addr->ai_addr), m_ip, 16); } connect(addr->ai_addr); }
static int rava_system_interfaces(lua_State* L) { int size, i; char buf[INET6_ADDRSTRLEN]; uv_interface_address_t* info; int r = uv_interface_addresses(&info, &size); lua_settop(L, 0); if (r < 0) { lua_pushboolean(L, 0); luaL_error(L, uv_strerror(r)); return 2; } lua_newtable(L); for (i = 0; i < size; i++) { uv_interface_address_t addr = info[i]; lua_newtable(L); lua_pushstring(L, addr.name); lua_setfield(L, -2, "name"); lua_pushboolean(L, addr.is_internal); lua_setfield(L, -2, "is_internal"); if (addr.address.address4.sin_family == PF_INET) { uv_ip4_name(&addr.address.address4, buf, sizeof(buf)); } else if (addr.address.address4.sin_family == PF_INET6) { uv_ip6_name(&addr.address.address6, buf, sizeof(buf)); } lua_pushstring(L, buf); lua_setfield(L, -2, "address"); lua_rawseti(L, -2, i + 1); } uv_free_interface_addresses(info, size); return 1; }
static PyObject * Util_func_interface_addresses(PyObject *obj) { int i, count; char ip[INET6_ADDRSTRLEN]; uv_interface_address_t* interfaces; uv_err_t err; PyObject *result, *item, *exc_data; UNUSED_ARG(obj); err = uv_interface_addresses(&interfaces, &count); if (err.code == UV_OK) { result = PyList_New(0); if (!result) { uv_free_interface_addresses(interfaces, count); PyErr_NoMemory(); return NULL; } for (i = 0; i < count; i++) { item = PyDict_New(); if (!item) continue; PyDict_SetItemString(item, "name", PyString_FromString(interfaces[i].name)); PyDict_SetItemString(item, "is_internal", PyBool_FromLong((long)interfaces[i].is_internal)); if (interfaces[i].address.address4.sin_family == AF_INET) { uv_ip4_name(&interfaces[i].address.address4, ip, INET_ADDRSTRLEN); PyDict_SetItemString(item, "address", PyString_FromString(ip)); } else if (interfaces[i].address.address4.sin_family == AF_INET6) { uv_ip6_name(&interfaces[i].address.address6, ip, INET6_ADDRSTRLEN); PyDict_SetItemString(item, "address", PyString_FromString(ip)); } if (PyList_Append(result, item)) continue; Py_DECREF(item); } uv_free_interface_addresses(interfaces, count); return result; } else { exc_data = Py_BuildValue("(is)", err.code, uv_strerror(err)); if (exc_data != NULL) { PyErr_SetObject(PyExc_UVError, exc_data); Py_DECREF(exc_data); } return NULL; } }
void httpconnection_on_resolved(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { HTTPConnection* c = static_cast<HTTPConnection*>(req->data); if(status == -1) { RX_ERROR("> cannot revolve host: %s", c->host.c_str()); return; } char ip[17] = {0}; uv_ip4_name((struct sockaddr_in*)res->ai_addr, ip, 16); int r = uv_tcp_connect(&c->connect_req, c->sock, *(struct sockaddr_in*)res->ai_addr, httpconnection_on_connect); uv_freeaddrinfo(res); }
static PyObject * Util_func_interface_addresses(PyObject *obj) { int i, count; char ip[INET6_ADDRSTRLEN]; uv_interface_address_t* interfaces; uv_err_t err; PyObject *result, *item, *exc_data; UNUSED_ARG(obj); err = uv_interface_addresses(&interfaces, &count); if (err.code == UV_OK) { result = PyList_New(count); if (!result) { uv_free_interface_addresses(interfaces, count); return NULL; } for (i = 0; i < count; i++) { item = PyStructSequence_New(&InterfaceAddressesResultType); if (!item) { Py_DECREF(result); uv_free_interface_addresses(interfaces, count); return NULL; } PyStructSequence_SET_ITEM(item, 0, Py_BuildValue("s", interfaces[i].name)); PyStructSequence_SET_ITEM(item, 1, PyBool_FromLong((long)interfaces[i].is_internal)); if (interfaces[i].address.address4.sin_family == AF_INET) { uv_ip4_name(&interfaces[i].address.address4, ip, INET_ADDRSTRLEN); } else if (interfaces[i].address.address4.sin_family == AF_INET6) { uv_ip6_name(&interfaces[i].address.address6, ip, INET6_ADDRSTRLEN); } PyStructSequence_SET_ITEM(item, 2, Py_BuildValue("s", ip)); PyList_SET_ITEM(result, i, item); } uv_free_interface_addresses(interfaces, count); return result; } else { exc_data = Py_BuildValue("(is)", err.code, uv_strerror(err)); if (exc_data != NULL) { PyErr_SetObject(PyExc_UVError, exc_data); Py_DECREF(exc_data); } return NULL; } }
static int tcp_listener(int port) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port); struct sockaddr sockname; int namelen = sizeof(sockname); char ip[20]; int r; r = uv_tcp_init(loop, &tcpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_tcp_bind(&tcpServer, addr); if (r) { fprintf(stderr, "Bind error\n"); return 1; } r = uv_listen((uv_stream_t*)&tcpServer, 128, on_connection); if (r) { fprintf(stderr, "Listen error\n"); return 1; } memset(&sockname, -1, sizeof sockname); r = uv_getsockname((uv_handle_t*)&tcpServer, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (listening) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); ASSERT(ip[0] == '0'); ASSERT(ip[1] == '.'); ASSERT(ip[2] == '0'); printf("sockname = %s\n", ip); getsocknamecount++; return 0; }