// used in iotjs_module_udp.cpp void AddressToJS(jerry_value_t obj, const sockaddr* addr) { char ip[INET6_ADDRSTRLEN]; const sockaddr_in* a4; const sockaddr_in6* a6; int port; switch (addr->sa_family) { case AF_INET6: { a6 = (const sockaddr_in6*)(addr); uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip); port = ntohs(a6->sin6_port); iotjs_jval_set_property_string_raw(obj, IOTJS_MAGIC_STRING_ADDRESS, ip); iotjs_jval_set_property_string_raw(obj, IOTJS_MAGIC_STRING_FAMILY, IOTJS_MAGIC_STRING_IPV6); iotjs_jval_set_property_number(obj, IOTJS_MAGIC_STRING_PORT, port); break; } case AF_INET: { a4 = (const sockaddr_in*)(addr); uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip); port = ntohs(a4->sin_port); iotjs_jval_set_property_string_raw(obj, IOTJS_MAGIC_STRING_ADDRESS, ip); iotjs_jval_set_property_string_raw(obj, IOTJS_MAGIC_STRING_FAMILY, IOTJS_MAGIC_STRING_IPV4); iotjs_jval_set_property_number(obj, IOTJS_MAGIC_STRING_PORT, port); break; } default: { iotjs_jval_set_property_string_raw(obj, IOTJS_MAGIC_STRING_ADDRESS, ""); break; } } }
Addrinfo::Addrinfo(const struct sockaddr* sa) : addr("undefined"), port(-1) { switch (sa->sa_family) { case AF_INET: { char ip_str[INET_ADDRSTRLEN]; struct sockaddr_in* src = (struct sockaddr_in*) sa; int ret = uv_inet_ntop(AF_INET, &src->sin_addr, ip_str, sizeof(ip_str)); if (ret) { LINEAR_LOG(LOG_ERR, "fail inet_ntop: %s", uv_strerror(ret)); } else { addr = std::string(ip_str); port = ntohs(src->sin_port); } break; } case AF_INET6: { char ip_str[INET6_ADDRSTRLEN]; struct sockaddr_in6* src = (struct sockaddr_in6*) sa; int ret = uv_inet_ntop(AF_INET6, &src->sin6_addr, ip_str, sizeof(ip_str)); if (ret) { LINEAR_LOG(LOG_ERR, "fail inet_ntop: %s", uv_strerror(ret)); } else { addr = std::string(ip_str); port = ntohs(src->sin6_port); } break; } default: break; } }
int luv_tcp_getsockname(lua_State* L) { uv_tcp_t* handle = (uv_tcp_t*)luv_checkudata(L, 1, "tcp"); int port = 0; char ip[INET6_ADDRSTRLEN]; int family; struct sockaddr_storage address; int addrlen = sizeof(address); if (uv_tcp_getsockname(handle, (struct sockaddr*)(&address), &addrlen)) { uv_err_t err = uv_last_error(luv_get_loop(L)); return luaL_error(L, "tcp_getsockname: %s", uv_strerror(err)); } family = address.ss_family; if (family == AF_INET) { struct sockaddr_in* addrin = (struct sockaddr_in*)&address; uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN); port = ntohs(addrin->sin_port); } else if (family == AF_INET6) { struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address; uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN); port = ntohs(addrin6->sin6_port); } lua_newtable(L); lua_pushnumber(L, port); lua_setfield(L, -2, "port"); lua_pushnumber(L, family); lua_setfield(L, -2, "family"); lua_pushstring(L, ip); lua_setfield(L, -2, "address"); return 1; }
int LuaIO_parse_socket_address(lua_State* L, struct sockaddr_storage* address) { char ip[INET6_ADDRSTRLEN]; struct sockaddr_in* addr_in; struct sockaddr_in6* addr_in6; int family = 0; int port = 0; switch (address->ss_family) { case AF_INET: addr_in = (struct sockaddr_in*)address; uv_inet_ntop(AF_INET, &(addr_in->sin_addr), ip, sizeof(ip)); family = 4; port = ntohs(addr_in->sin_port); break; case AF_INET6: addr_in6 = (struct sockaddr_in6*)address; uv_inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip, sizeof(ip)); family = 6; port = ntohs(addr_in6->sin6_port); break; default: lua_pushnil(L); return UV_EAI_ADDRFAMILY; } lua_createtable(L, 0, 3); lua_pushinteger(L, family); lua_setfield(L, -2, "family"); lua_pushinteger(L, port); lua_setfield(L, -2, "port"); lua_pushstring(L, ip); lua_setfield(L, -2, "address"); return 0; }
bud_error_t bud_config_format_proxyline(bud_config_t* config) { int r; char host[INET6_ADDRSTRLEN]; struct sockaddr_in* addr4; struct sockaddr_in6* addr6; addr4 = (struct sockaddr_in*) &config->frontend.addr; addr6 = (struct sockaddr_in6*) &config->frontend.addr; if (config->frontend.addr.ss_family == AF_INET) r = uv_inet_ntop(AF_INET, &addr4->sin_addr, host, sizeof(host)); else r = uv_inet_ntop(AF_INET6, &addr6->sin6_addr, host, sizeof(host)); if (r != 0) return bud_error(kBudErrNtop); r = snprintf(config->proxyline_fmt, sizeof(config->proxyline_fmt), "PROXY %%s %%s %s %%hu %hu\r\n", host, config->frontend.port); ASSERT(r < (int) sizeof(config->proxyline_fmt), "Proxyline format overflowed"); return bud_ok(); }
static std::string to_string(CassInet value) { char buffer[INET6_ADDRSTRLEN]; if (value.address_length == 4) { uv_inet_ntop(AF_INET, value.address, buffer, sizeof(buffer)); } else { uv_inet_ntop(AF_INET6, value.address, buffer, sizeof(buffer)); } return std::string(buffer); }
bud_client_error_t bud_client_fill_host(bud_client_t* client, bud_client_host_t* host) { int r; struct sockaddr_storage storage; int storage_size; struct sockaddr_in* addr; struct sockaddr_in6* addr6; storage_size = sizeof(storage); if (host == &client->remote) { r = uv_tcp_getpeername(&client->frontend.tcp, (struct sockaddr*) &storage, &storage_size); } else { r = uv_tcp_getsockname(&client->frontend.tcp, (struct sockaddr*) &storage, &storage_size); } if (r != 0) goto fatal; addr = (struct sockaddr_in*) &storage; addr6 = (struct sockaddr_in6*) &storage; host->family = storage.ss_family; if (storage.ss_family == AF_INET) { host->port = addr->sin_port; r = uv_inet_ntop(AF_INET, &addr->sin_addr, host->host, sizeof(host->host)); } else if (storage.ss_family == AF_INET6) { host->port = addr6->sin6_port; r = uv_inet_ntop(AF_INET6, &addr6->sin6_addr, host->host, sizeof(host->host)); } else { r = -1; goto fatal; } if (r != 0) goto fatal; host->host_len = strlen(host->host); return bud_client_ok(&client->backend); fatal: return bud_client_error(bud_error_num(kBudErrClientProxyline, r), &client->backend); }
static void luv_on_udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { int port; char ip[INET6_ADDRSTRLEN]; /* load the lua state and the userdata */ lua_State *L = luv_handle_get_lua(handle->data); /* perform some magic */ /* the base buffer is the offset of the slab block + sizeof(MemBlock) */ MemBlock *mb = (MemBlock *)(buf.base - sizeof(MemBlock)); printf("luv_on_read: %p pool=%p\n", mb, mb->pool); if (nread == 0) { return; } if (nread < 0) { uv_close((uv_handle_t *)handle, luv_on_close); luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_recv", NULL); luv_emit_event(L, "error", 1); return; } lua_pushlstring(L, buf.base, nread); lua_newtable(L); if (addr->sa_family == AF_INET) { uv_inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in*)addr)->sin_port); } else if (addr->sa_family == AF_INET6){ uv_inet_ntop(AF_INET6, &(((struct sockaddr_in6*)addr)->sin6_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in6*)addr)->sin6_port); } lua_pushstring(L, ip); lua_setfield(L, -2, "address"); lua_pushnumber(L, port); lua_setfield(L, -2, "port"); lua_pushboolean(L, flags == UV_UDP_PARTIAL); lua_setfield(L, -2, "partial"); lua_pushnumber(L, nread); lua_setfield(L, -2, "size"); luv_emit_event(L, "message", 2); lev_slab_decRef( mb ); /*free(buf.base);*/ }
static void luv_on_udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { int port; char ip[INET6_ADDRSTRLEN]; /* load the lua state and the userdata */ lua_State *L = luv_handle_get_lua(handle->data); if (nread == 0) { return; } if (nread < 0) { uv_close((uv_handle_t *)handle, luv_on_close); luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_recv", NULL); luv_emit_event(L, "error", 1); return; } lua_pushlstring(L, buf.base, nread); lua_newtable(L); if (addr->sa_family == AF_INET) { uv_inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in*)addr)->sin_port); } else if (addr->sa_family == AF_INET6){ uv_inet_ntop(AF_INET6, &(((struct sockaddr_in6*)addr)->sin6_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in6*)addr)->sin6_port); } lua_pushstring(L, ip); lua_setfield(L, -2, "address"); lua_pushnumber(L, port); lua_setfield(L, -2, "port"); lua_pushboolean(L, flags == UV_UDP_PARTIAL); lua_setfield(L, -2, "partial"); lua_pushnumber(L, nread); lua_setfield(L, -2, "size"); luv_emit_event(L, "message", 2); free(buf.base); buf.base = NULL; }
static PyObject * DNSResolver_servers_get(DNSResolver *self, void *closure) { int r; char ip[INET6_ADDRSTRLEN]; struct ares_addr_node *server, *servers; PyObject *server_list; PyObject *tmp; UNUSED_ARG(closure); server_list = PyList_New(0); if (!server_list) { PyErr_NoMemory(); return NULL; } r = ares_get_servers(self->channel, &servers); if (r != 0) { PyErr_SetString(PyExc_DNSError, "error getting c-ares nameservers"); return NULL; } for (server = servers; server != NULL; server = server->next) { if (server->family == AF_INET) { uv_inet_ntop(AF_INET, &(server->addr.addr4), ip, INET_ADDRSTRLEN); tmp = PyString_FromString(ip); } else { uv_inet_ntop(AF_INET6, &(server->addr.addr6), ip, INET6_ADDRSTRLEN); tmp = PyString_FromString(ip); } if (tmp == NULL) { break; } r = PyList_Append(server_list, tmp); Py_DECREF(tmp); if (r != 0) { break; } } return server_list; }
void call_iface_info_cb(iface_info_cb iface_cb, char const* iface_name, struct sockaddr_in6 const* address) { char string_address[INET6_ADDRSTRLEN]; ASSERT(0 == uv_inet_ntop(AF_INET6, &address->sin6_addr, string_address, INET6_ADDRSTRLEN)); iface_cb(string_address, iface_name, address->sin6_scope_id); }
bud_error_t bud_config_format_proxyline(bud_config_t* config) { int r; char host[INET6_ADDRSTRLEN]; struct sockaddr_in* addr4; struct sockaddr_in6* addr6; addr4 = (struct sockaddr_in*) &config->frontend.addr; addr6 = (struct sockaddr_in6*) &config->frontend.addr; if (config->frontend.addr.ss_family == AF_INET) r = uv_inet_ntop(AF_INET, &addr4->sin_addr, host, sizeof(host)); else r = uv_inet_ntop(AF_INET6, &addr6->sin6_addr, host, sizeof(host)); if (r != 0) return bud_error(kBudErrNtop); r = snprintf(config->proxyline_fmt.haproxy, sizeof(config->proxyline_fmt.haproxy), "PROXY %%s %%s %s %%hu %hu\r\n", host, config->frontend.port); ASSERT(r < (int) sizeof(config->proxyline_fmt.haproxy), "Proxyline format overflowed"); r = snprintf(config->proxyline_fmt.json, sizeof(config->proxyline_fmt.json), "BUD {\"family\":\"%%s\"," "\"bud\":{\"host\":\"%s\",\"port\":%hu}," "\"peer\":{" "\"host\":\"%%s\"," "\"port\":%%hu," "\"cn\":%%c%%s%%c," "\"dn\":%%c%%s%%c}" "}\r\n", host, config->frontend.port); ASSERT(r < (int) sizeof(config->proxyline_fmt.json), "Proxyline format overflowed"); return bud_ok(); }
static void parse_sockaddr(lua_State* L, struct sockaddr_storage* address, int addrlen) { char ip[INET6_ADDRSTRLEN]; int port = 0; lua_newtable(L); if (address->ss_family == AF_INET) { struct sockaddr_in* addrin = (struct sockaddr_in*)address; uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, addrlen); port = ntohs(addrin->sin_port); } else if (address->ss_family == AF_INET6) { struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)address; uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, addrlen); port = ntohs(addrin6->sin6_port); } lua_pushstring(L, luv_af_num_to_string(address->ss_family)); lua_setfield(L, -2, "family"); lua_pushinteger(L, port); lua_setfield(L, -2, "port"); lua_pushstring(L, ip); lua_setfield(L, -2, "ip"); }
int fill_ipaddr(struct sockaddr *addr, int port, char *ipstr, int ipstr_len, struct addrinfo *ai) { if (ai->ai_family == AF_INET) { struct sockaddr_in *addr4 = (struct sockaddr_in *)addr; memcpy(addr4, ai->ai_addr, sizeof(struct sockaddr_in)); addr4->sin_port = port; uv_inet_ntop(addr4->sin_family, &addr4->sin_addr, ipstr, ipstr_len); } else if (ai->ai_family == AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; memcpy(addr6, ai->ai_addr, sizeof(struct sockaddr_in6)); addr6->sin6_port = port; uv_inet_ntop(addr6->sin6_family, &addr6->sin6_addr, ipstr, ipstr_len); } else { LOG_W("unexpected ai_family: %d", ai->ai_family); return -1; } return 0; }
void GetAddressInfo(const struct sockaddr* addr, int* family, std::string &ip, uint16_t* port) { char _ip[INET6_ADDRSTRLEN + 1]; int err; err = uv_inet_ntop(AF_INET, &((struct sockaddr_in*)addr)->sin_addr, _ip, INET_ADDRSTRLEN); if (err) throw emu_fatalerror("uv_inet_ntop() failed: %s", uv_strerror(err)); *port = (uint16_t)ntohs(((struct sockaddr_in*)addr)->sin_port); *family = addr->sa_family; ip.assign(_ip); }
const char* melo_get_ip_port(const struct sockaddr* addr, char* ipbuf, int buflen, int* port) { switch (addr->sa_family) { case AF_INET: { const struct sockaddr_in* addrin = (const struct sockaddr_in*) addr; if (ipbuf) { uv_inet_ntop(AF_INET, &(addrin->sin_addr), ipbuf, buflen); } if (port) { *port = (int) ntohs(addrin->sin_port); } break; } case AF_INET6: { const struct sockaddr_in6* addrin = (const struct sockaddr_in6*) addr; if (ipbuf) { uv_inet_ntop(AF_INET6, &(addrin->sin6_addr), ipbuf, buflen); } if (port) { *port = (int) ntohs(addrin->sin6_port); } break; } default: if (port) { *port = 0; } return NULL; } return ipbuf ? ipbuf : NULL; }
static void address_cb(uv_getaddrinfo_t *req, int status, struct addrinfo *addrs) { struct addrinfo *ai; char addrbuf[INET6_ADDRSTRLEN + 1]; const void *addrv; union { struct sockaddr addr; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } s; if (status < 0) { printf("get address info has cancel it\n"); return; } for (ai = addrs; ai != NULL; ai = ai->ai_next) { printf("==> GET AN ADDRESS\n"); printf(" FAMILY:%s\n",ai->ai_family == AF_INET ? "IPV4" : "IPV6"); if (ai->ai_family == AF_INET) { s.addr4 = *(const struct sockaddr_in *)ai->ai_addr; addrv = &s.addr4.sin_addr; } else if (ai->ai_family == AF_INET6) { s.addr6 = *(const struct sockaddr_in6 *)ai->ai_addr; addrv = &s.addr6.sin6_addr; } else { printf("UNREACHABLE\n"); return; } if (uv_inet_ntop(s.addr.sa_family, addrv, addrbuf, sizeof(addrbuf))) { printf("UNREACHABLE\n"); return; } printf(" ADDRESS:%s\n",addrbuf); } uv_freeaddrinfo(addrs); uv_stop(uv_default_loop()); }
static void luv_pushaddrinfo(lua_State* L, struct addrinfo* res) { char ip[INET6_ADDRSTRLEN]; int port, i = 0; const char *addr; struct addrinfo* curr = res; lua_newtable(L); for (curr = res; curr; curr = curr->ai_next) { if (curr->ai_family == AF_INET || curr->ai_family == AF_INET6) { lua_newtable(L); if (curr->ai_family == AF_INET) { addr = (char*) &((struct sockaddr_in*) curr->ai_addr)->sin_addr; port = ((struct sockaddr_in*) curr->ai_addr)->sin_port; } else { addr = (char*) &((struct sockaddr_in6*) curr->ai_addr)->sin6_addr; port = ((struct sockaddr_in6*) curr->ai_addr)->sin6_port; } lua_pushstring(L, luv_af_num_to_string(curr->ai_family)); lua_setfield(L, -2, "family"); uv_inet_ntop(curr->ai_family, addr, ip, INET6_ADDRSTRLEN); lua_pushstring(L, ip); lua_setfield(L, -2, "addr"); if (ntohs(port)) { lua_pushinteger(L, ntohs(port)); lua_setfield(L, -2, "port"); } lua_pushstring(L, luv_sock_num_to_string(curr->ai_socktype)); lua_setfield(L, -2, "socktype"); lua_pushstring(L, luv_af_num_to_string(curr->ai_protocol)); lua_setfield(L, -2, "protocol"); if (curr->ai_canonname) { lua_pushstring(L, curr->ai_canonname); lua_setfield(L, -2, "canonname"); } lua_rawseti(L, -2, ++i); } } }
static void host_cb(void *arg, int status, int timeouts, struct hostent *hostent) { PyGILState_STATE gstate = PyGILState_Ensure(); char ip[INET6_ADDRSTRLEN]; char **ptr; ares_cb_data_t *data; DNSResolver *self; PyObject *callback, *dns_name, *errorno, *dns_aliases, *dns_addrlist, *dns_result, *tmp, *result; ASSERT(arg); data = (ares_cb_data_t*)arg; 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 != ARES_SUCCESS) { errorno = PyInt_FromLong((long)status); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } dns_aliases = PyList_New(0); dns_addrlist = PyList_New(0); dns_result = PyTuple_New(3); if (!(dns_aliases && dns_addrlist && dns_result)) { PyErr_NoMemory(); PyErr_WriteUnraisable(Py_None); Py_XDECREF(dns_aliases); Py_XDECREF(dns_addrlist); Py_XDECREF(dns_result); errorno = PyInt_FromLong((long)ARES_ENOMEM); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } for (ptr = hostent->h_aliases; *ptr != NULL; ptr++) { if (*ptr != hostent->h_name && strcmp(*ptr, hostent->h_name)) { tmp = PyString_FromString(*ptr); if (tmp == NULL) { break; } PyList_Append(dns_aliases, tmp); Py_DECREF(tmp); } } for (ptr = hostent->h_addr_list; *ptr != NULL; ptr++) { if (hostent->h_addrtype == AF_INET) { uv_inet_ntop(AF_INET, *ptr, ip, INET_ADDRSTRLEN); tmp = PyString_FromString(ip); } else if (hostent->h_addrtype == AF_INET6) { uv_inet_ntop(AF_INET6, *ptr, ip, INET6_ADDRSTRLEN); tmp = PyString_FromString(ip); } else { continue; } if (tmp == NULL) { break; } PyList_Append(dns_addrlist, tmp); Py_DECREF(tmp); } dns_name = PyString_FromString(hostent->h_name); PyTuple_SET_ITEM(dns_result, 0, dns_name); PyTuple_SET_ITEM(dns_result, 1, dns_aliases); PyTuple_SET_ITEM(dns_result, 2, dns_addrlist); 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); PyMem_Free(data); Py_DECREF(self); PyGILState_Release(gstate); }
void log_ipv4_and_port(void *ipv4, int port, const char *msg) { char data[INET_ADDRSTRLEN]; uv_inet_ntop(AF_INET, ipv4, data, INET_ADDRSTRLEN); LOG_V("%s: %s:%d", msg, data, port); }
int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size) { return uv_inet_ntop(AF_INET, &src->sin_addr, dst, size); }
// .. c:function:: static ch_inline void _ch_rd_handshake( ch_connection_t* conn, ch_reader_t* reader, ch_buf* buf, size_t read ) // :noindex: // // see: :c:func:`_ch_rd_handshake` // // .. code-block:: cpp // { struct sockaddr_storage addr; int addr_len; ch_connection_t* old_conn; ch_chirp_t* chirp = conn->chirp; A(chirp->_init == CH_CHIRP_MAGIC, "Not a ch_chirp_t*"); ch_chirp_int_t* ichirp = chirp->_; ch_protocol_t* protocol = &ichirp->protocol; if(read < sizeof(ch_rd_handshake_t)) { E( chirp, "Illegal handshake size -> shutdown. ch_chirp_t:%p, " "ch_connection_t:%p", (void*) chirp, (void*) conn ); ch_cn_shutdown(conn); return; } memcpy(&reader->hs, buf, sizeof(ch_rd_handshake_t)); conn->port = ntohs(reader->hs.port); conn->max_timeout = ntohs( reader->hs.max_timeout + ( (ichirp->config.RETRIES + 2) * ichirp->config.TIMEOUT ) ); memcpy( conn->remote_identity, reader->hs.identity, sizeof(conn->remote_identity) ); if(uv_tcp_getpeername( &conn->client, (struct sockaddr*) &addr, &addr_len ) != CH_SUCCESS) { E( chirp, "Could not get remote address. ch_chirp_t:%p, " "ch_connection_t:%p", (void*) chirp, (void*) conn ); ch_cn_shutdown(conn); return; }; if(addr.ss_family == AF_INET6) { struct sockaddr_in6* saddr = (struct sockaddr_in6*) &addr; conn->ip_protocol = CH_IPV6; memcpy( &conn->address, &saddr->sin6_addr, sizeof(saddr->sin6_addr) ); } else { struct sockaddr_in* saddr = (struct sockaddr_in*) &addr; conn->ip_protocol = CH_IPV4; memcpy( &conn->address, &saddr->sin_addr, sizeof(saddr->sin_addr) ); } old_conn = sglib_ch_connection_t_find_member( protocol->connections, conn ); // If there is a network race condition we replace the old connection and // leave the old one for garbage collection if(old_conn) { // Since we reuse the tree members we have to delete the connection // from the old data-structure, before adding it to the new. L( chirp, "ch_connection_t:%p replaced ch_connection_t:%p. " "ch_chirp_t:%p", (void*) conn, (void*) old_conn, (void*) chirp ); sglib_ch_connection_t_delete( &protocol->connections, old_conn ); sglib_ch_connection_set_t_add( &protocol->old_connections, old_conn ); } sglib_ch_connection_t_add( &protocol->connections, conn ); # ifndef NDEBUG { ch_text_address_t addr; char identity[33]; uv_inet_ntop( conn->ip_protocol == CH_IPV6 ? AF_INET6 : AF_INET, conn->address, addr.data, sizeof(addr) ); ch_bytes_to_hex( conn->remote_identity, sizeof(conn->remote_identity), identity, sizeof(identity) ); L( chirp, "Handshake with remote %s:%d (%s) done. ch_chirp_t:%p, " "ch_connection_t:%p", addr.data, conn->port, identity, (void*) chirp, (void*) conn ); } # endif }
void cass_inet_string(CassInet inet, char* output) { uv_inet_ntop(inet.address_length == CASS_INET_V4_LENGTH ? AF_INET : AF_INET6, inet.address, output, CASS_INET_STRING_LENGTH); }
/* Bind a server to each address that getaddrinfo() reported. */ static void do_bind(uv_getaddrinfo_t *req, int status, struct addrinfo *addrs) { char addrbuf[INET6_ADDRSTRLEN + 1]; unsigned int ipv4_naddrs; unsigned int ipv6_naddrs; server_state *state; server_config *cf; struct addrinfo *ai; const void *addrv; const char *what; uv_loop_t *loop; server_ctx *sx; unsigned int n; int err; union { struct sockaddr addr; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } s; state = CONTAINER_OF(req, server_state, getaddrinfo_req); loop = state->loop; cf = &state->config; if (status < 0) { pr_err("getaddrinfo(\"%s\"): %s", cf->bind_host, uv_strerror(status)); uv_freeaddrinfo(addrs); return; } ipv4_naddrs = 0; ipv6_naddrs = 0; for (ai = addrs; ai != NULL; ai = ai->ai_next) { if (ai->ai_family == AF_INET) { ipv4_naddrs += 1; } else if (ai->ai_family == AF_INET6) { ipv6_naddrs += 1; } } if (ipv4_naddrs == 0 && ipv6_naddrs == 0) { pr_err("%s has no IPv4/6 addresses", cf->bind_host); uv_freeaddrinfo(addrs); return; } state->servers = xmalloc((ipv4_naddrs + ipv6_naddrs) * sizeof(state->servers[0])); n = 0; for (ai = addrs; ai != NULL; ai = ai->ai_next) { if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { continue; } if (ai->ai_family == AF_INET) { s.addr4 = *(const struct sockaddr_in *) ai->ai_addr; s.addr4.sin_port = htons(cf->bind_port); addrv = &s.addr4.sin_addr; } else if (ai->ai_family == AF_INET6) { s.addr6 = *(const struct sockaddr_in6 *) ai->ai_addr; s.addr6.sin6_port = htons(cf->bind_port); addrv = &s.addr6.sin6_addr; } else { UNREACHABLE(); } if (uv_inet_ntop(s.addr.sa_family, addrv, addrbuf, sizeof(addrbuf))) { UNREACHABLE(); } sx = state->servers + n; sx->loop = loop; sx->idle_timeout = state->config.idle_timeout; CHECK(0 == uv_tcp_init(loop, &sx->tcp_handle)); what = "uv_tcp_bind"; err = uv_tcp_bind(&sx->tcp_handle, &s.addr, 0); if (err == 0) { what = "uv_listen"; err = uv_listen((uv_stream_t *) &sx->tcp_handle, 128, on_connection); } if (err != 0) { pr_err("%s(\"%s:%hu\"): %s", what, addrbuf, cf->bind_port, uv_strerror(err)); while (n > 0) { n -= 1; uv_close((uv_handle_t *) (state->servers + n), NULL); } break; } pr_info("listening on %s:%hu", addrbuf, cf->bind_port); n += 1; } uv_freeaddrinfo(addrs); }
static void request_start(struct client_context *client, char *req_buf) { struct socks5_request *req = (struct socks5_request *)req_buf; struct remote_context *remote = client->remote; assert(remote->stage == XSTAGE_FORWARD); client->cmd = req->cmd; if (req->cmd != S5_CMD_CONNECT && req->cmd != S5_CMD_UDP_ASSOCIATE) { logger_log(LOG_ERR, "unsupported cmd: 0x%02x", req->cmd); request_ack(client, S5_REP_CMD_NOT_SUPPORTED); return; } if (req->cmd == S5_CMD_UDP_ASSOCIATE) { request_ack(client, S5_REP_SUCCESSED); return; } char buf[260] = {0}; size_t buflen; uint16_t portlen = 2; /* * * xsocks request * +------+----------+----------+ * | ATYP | BND.ADDR | BND.PORT | * +------+----------+----------+ * | 1 | Variable | 2 | * +------+----------+----------+ * */ if (req->atyp == ATYP_IPV4) { size_t in_addr_len = sizeof(struct in_addr); buflen = sizeof(struct xsocks_request) + in_addr_len + portlen; buf[0] = ATYP_IPV4; memcpy(buf + 1, req->addr, in_addr_len); memcpy(buf + 1 + in_addr_len, req->addr + in_addr_len, portlen); uv_inet_ntop(AF_INET, (const void *)(req->addr), client->target_addr, INET_ADDRSTRLEN); uint16_t port = read_size((uint8_t*)(req->addr + in_addr_len)); sprintf(client->target_addr, "%s:%u", client->target_addr, port); } else if (req->atyp == ATYP_HOST) { uint8_t namelen = *(uint8_t *)(req->addr); if (namelen > 0xFF) { logger_log(LOG_ERR, "unsupported address type: 0x%02x", req->atyp); request_ack(client, S5_REP_ADDRESS_TYPE_NOT_SUPPORTED); return; } buflen = sizeof(struct xsocks_request) + 1 + namelen + portlen; buf[0] = ATYP_HOST; memcpy(buf + 1, req->addr, 1 + namelen); memcpy(buf + 1 + 1 + namelen, req->addr + 1 + namelen, portlen); memcpy(client->target_addr, req->addr + 1, namelen); uint16_t port = read_size((uint8_t*)(req->addr + 1 + namelen)); sprintf(client->target_addr, "%s:%u", client->target_addr, port); } else if (req->atyp == ATYP_IPV6) { size_t in6_addr_len = sizeof(struct in6_addr); buflen = sizeof(struct xsocks_request) + in6_addr_len + portlen; buf[0] = ATYP_IPV6; memcpy(buf + 1, req->addr, in6_addr_len); memcpy(buf + 1 + in6_addr_len, req->addr + in6_addr_len, portlen); uv_inet_ntop(AF_INET6, (const void *)(req->addr), client->target_addr, INET_ADDRSTRLEN); uint16_t port = read_size((uint8_t*)(req->addr + in6_addr_len)); sprintf(client->target_addr, "%s:%u", client->target_addr, port); } else { logger_log(LOG_ERR, "unsupported address type: 0x%02x", req->atyp); request_ack(client, S5_REP_ADDRESS_TYPE_NOT_SUPPORTED); return; } request_ack(client, S5_REP_SUCCESSED); // TODO: handle UDP ASSOCIATE if (req->cmd == S5_CMD_CONNECT) { if (verbose) { logger_log(LOG_INFO, "connect to %s", client->target_addr); } int clen = buflen + PRIMITIVE_BYTES; uint8_t *c = client->buf + HEADER_BYTES; int rc = crypto_encrypt(c, (uint8_t *)buf, buflen); if (!rc) { forward_to_remote(remote, c, clen); } } }
void log_ipv6_and_port(void *ipv6, int port, const char *msg) { char data[INET6_ADDRSTRLEN]; uv_inet_ntop(AF_INET6, ipv6, data, INET6_ADDRSTRLEN); LOG_V("%s: [%s]:%d", msg, data, port); }
static void on_addrinfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { luv_callback_t* callback = req->data; lua_State* L = callback->L; struct addrinfo* curr = res; char ip[INET6_ADDRSTRLEN]; const char *addr; int port; int i = 1; /* Get the callback and remove the reference to it. */ lua_rawgeti(L, LUA_REGISTRYINDEX, callback->ref); luaL_unref(L, LUA_REGISTRYINDEX, callback->ref); free(callback); free(req); lua_newtable(L); for (curr = res; curr; curr = curr->ai_next) { if (curr->ai_family == AF_INET || curr->ai_family == AF_INET6) { lua_newtable(L); if (curr->ai_family == AF_INET) { addr = (char*) &((struct sockaddr_in*) curr->ai_addr)->sin_addr; port = ((struct sockaddr_in*) curr->ai_addr)->sin_port; lua_pushstring(L, "IPv4"); lua_setfield(L, -2, "family"); } else { addr = (char*) &((struct sockaddr_in6*) curr->ai_addr)->sin6_addr; port = ((struct sockaddr_in6*) curr->ai_addr)->sin6_port; lua_pushstring(L, "IPv6"); lua_setfield(L, -2, "family"); } uv_inet_ntop(curr->ai_family, addr, ip, INET6_ADDRSTRLEN); lua_pushstring(L, ip); lua_setfield(L, -2, "addr"); if (ntohs(port)) { lua_pushinteger(L, ntohs(port)); lua_setfield(L, -2, "port"); } if (curr->ai_socktype == SOCK_STREAM) { lua_pushstring(L, "STREAM"); lua_setfield(L, -2, "socktype"); } else if (curr->ai_socktype == SOCK_DGRAM) { lua_pushstring(L, "DGRAM"); lua_setfield(L, -2, "socktype"); } switch (curr->ai_protocol) { #ifdef AF_UNIX case AF_UNIX: lua_pushstring(L, "UNIX"); break; #endif #ifdef AF_INET case AF_INET: lua_pushstring(L, "INET"); break; #endif #ifdef AF_INET6 case AF_INET6: lua_pushstring(L, "INET6"); break; #endif #ifdef AF_IPX case AF_IPX: lua_pushstring(L, "IPX"); break; #endif #ifdef AF_NETLINK case AF_NETLINK: lua_pushstring(L, "NETLINK"); break; #endif #ifdef AF_X25 case AF_X25: lua_pushstring(L, "X25"); break; #endif #ifdef AF_AX25 case AF_AX25: lua_pushstring(L, "AX25"); break; #endif #ifdef AF_ATMPVC case AF_ATMPVC: lua_pushstring(L, "ATMPVC"); break; #endif #ifdef AF_APPLETALK case AF_APPLETALK: lua_pushstring(L, "APPLETALK"); break; #endif #ifdef AF_PACKET case AF_PACKET: lua_pushstring(L, "PACKET"); break; #endif default: lua_pushstring(L, NULL); } lua_setfield(L, -2, "protocol"); lua_pushstring(L, curr->ai_canonname); lua_setfield(L, -2, "canonname"); lua_rawseti(L, -2, i++); } } uv_freeaddrinfo(res); luv_call(L, 1, 0); }
static void query_aaaa_cb(void *arg, int status,int timeouts, unsigned char *answer_buf, int answer_len) { PyGILState_STATE gstate = PyGILState_Ensure(); int parse_status; char ip[INET6_ADDRSTRLEN]; char **ptr; struct hostent *hostent; ares_cb_data_t *data; DNSResolver *self; PyObject *dns_result, *errorno, *tmp, *result, *callback; ASSERT(arg); data = (ares_cb_data_t*)arg; 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 != ARES_SUCCESS) { errorno = PyInt_FromLong((long)status); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } parse_status = ares_parse_aaaa_reply(answer_buf, answer_len, &hostent, NULL, NULL); if (parse_status != ARES_SUCCESS) { errorno = PyInt_FromLong((long)parse_status); 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)ARES_ENOMEM); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } for (ptr = hostent->h_addr_list; *ptr != NULL; ptr++) { uv_inet_ntop(hostent->h_addrtype, *ptr, ip, sizeof(ip)); tmp = PyString_FromString(ip); if (tmp == NULL) { break; } PyList_Append(dns_result, tmp); Py_DECREF(tmp); } ares_free_hostent(hostent); 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); PyMem_Free(data); Py_DECREF(self); PyGILState_Release(gstate); }
int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) { return uv_inet_ntop(AF_INET6, &src->sin6_addr, dst, size); }
bool Address::from_string(const std::string& ip, int port, Address* output) { char buf[sizeof(struct in6_addr)]; #if UV_VERSION_MAJOR == 0 if (uv_inet_pton(AF_INET, ip.c_str(), &buf).code == UV_OK) { #else if (uv_inet_pton(AF_INET, ip.c_str(), &buf) == 0) { #endif if (output != NULL) { struct sockaddr_in addr; #if UV_VERSION_MAJOR == 0 addr = uv_ip4_addr(ip.c_str(), port); #else uv_ip4_addr(ip.c_str(), port, &addr); #endif output->init(copy_cast<struct sockaddr_in*, struct sockaddr*>(&addr)); } return true; #if UV_VERSION_MAJOR == 0 } else if (uv_inet_pton(AF_INET6, ip.c_str(), &buf).code == UV_OK) { #else } else if (uv_inet_pton(AF_INET6, ip.c_str(), &buf) == 0) { #endif if (output != NULL) { struct sockaddr_in6 addr; #if UV_VERSION_MAJOR == 0 addr = uv_ip6_addr(ip.c_str(), port); #else uv_ip6_addr(ip.c_str(), port, &addr); #endif output->init(copy_cast<struct sockaddr_in6*, struct sockaddr*>(&addr)); } return true; } else { return false; } } void Address::from_inet(const char* data, size_t size, int port, Address* output) { assert(size == 4 || size == 16); if (size == 4) { char buf[INET_ADDRSTRLEN]; uv_inet_ntop(AF_INET, data, buf, sizeof(buf)); if (output != NULL) { struct sockaddr_in addr; #if UV_VERSION_MAJOR == 0 addr = uv_ip4_addr(buf, port); #else uv_ip4_addr(buf, port, &addr); #endif output->init(copy_cast<struct sockaddr_in*, struct sockaddr*>(&addr)); } } else { char buf[INET6_ADDRSTRLEN]; uv_inet_ntop(AF_INET6, data, buf, sizeof(buf)); if (output != NULL) { struct sockaddr_in6 addr; #if UV_VERSION_MAJOR == 0 addr = uv_ip6_addr(buf, port); #else uv_ip6_addr(buf, port, &addr); #endif output->init(copy_cast<struct sockaddr_in6*, struct sockaddr*>(&addr)); } } }