int init_socket(unsigned int loc_idx) { struct sockaddr_in v4addr; int status; status = uv_udp_init(loop,&udp_servers[loc_idx]); #if UV_VERSION_MAJOR == 0 v4addr = uv_ip4_addr(profile_socket[loc_idx].host, atoi(profile_socket[loc_idx].port)); #else status = uv_ip4_addr(profile_socket[loc_idx].host, atoi(profile_socket[loc_idx].port), &v4addr); #endif #if UV_VERSION_MAJOR == 0 status = uv_udp_bind(&udp_servers[loc_idx], v4addr,0); #else status = uv_udp_bind(&udp_servers[loc_idx], (struct sockaddr*)&v4addr, UV_UDP_REUSEADDR); #endif if(status < 0) { LERR( "capture: bind error"); return 2; } udp_servers[loc_idx].data = (void *) &loc_idx; status = uv_udp_recv_start(&udp_servers[loc_idx], on_alloc, on_recv); return 0; }
int udprelay_start(uv_loop_t *loop, struct server_context *server) { int rc, yes = 1; if (setsockopt(server->udp_fd, SOL_IP, IP_TRANSPARENT, &yes, sizeof(int))) { logger_stderr("setsockopt IP_TRANSPARENT error: %s", strerror(errno)); } if (setsockopt(server->udp_fd, IPPROTO_IP, IP_RECVORIGDSTADDR, &yes, sizeof(int))) { logger_stderr("setsockopt IP_RECVORIGDSTADDR error: %s", strerror(errno)); } uv_udp_init(loop, &server->udp); if ((rc = uv_udp_open(&server->udp, server->udp_fd))) { logger_stderr("udp open error: %s", uv_strerror(rc)); return 1; } if ((rc = uv_udp_bind(&server->udp, server->local_addr, UV_UDP_REUSEADDR))) { logger_stderr("udp bind error: %s", uv_strerror(rc)); return 1; } uv_poll_init_socket(loop, &server->watcher, server->udp_fd); uv_poll_start(&server->watcher, UV_READABLE, poll_cb); return 0; }
static int luv_udp_bind(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); const char* host = luaL_checkstring(L, 2); int port = luaL_checkinteger(L, 3); unsigned int flags = 0; struct sockaddr_storage addr; int ret; if (uv_ip4_addr(host, port, (struct sockaddr_in*)&addr) && uv_ip6_addr(host, port, (struct sockaddr_in6*)&addr)) { return luaL_error(L, "Invalid IP address or port [%s:%d]", host, port); } if (lua_type(L, 4) == LUA_TTABLE) { luaL_checktype(L, 4, LUA_TTABLE); lua_getfield(L, 4, "reuseaddr"); if (lua_toboolean(L, -1)) flags |= UV_UDP_REUSEADDR; lua_pop(L, 1); lua_getfield(L, 4, "ipv6only"); if (lua_toboolean(L, -1)) flags |= UV_UDP_IPV6ONLY; lua_pop(L, 1); } ret = uv_udp_bind(handle, (struct sockaddr*)&addr, flags); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
int xPLController::discoverxPLPort() { int portTCP; uv_udp_t sock; struct sockaddr_in addr; int r; r = uv_udp_init(uv_default_loop(), &sock); if(r!=0) return XPL_DEFAULT_PORT; portTCP = XPL_DEFAULT_PORT; do { uv_ip4_addr("0.0.0.0", portTCP, &addr); r = uv_udp_bind(&sock, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR); if(r==0) break; if(portTCP == XPL_DEFAULT_PORT) portTCP = XPL_PORT_LOWER_BOUND-1; portTCP++; } while(portTCP != XPL_PORT_UPPER_BOUND); uv_udp_recv_stop(&sock); if(r!=0) return XPL_DEFAULT_PORT; return portTCP; }
static PyObject * UDP_func_bind(UDP *self, PyObject *args) { int r, bind_port, address_type; char *bind_ip; RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "(si):bind", &bind_ip, &bind_port)) { return NULL; } if (bind_port < 0 || bind_port > 65535) { PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65535"); return NULL; } if (pyuv_guess_ip_family(bind_ip, &address_type)) { PyErr_SetString(PyExc_ValueError, "invalid IP address"); return NULL; } if (address_type == AF_INET) { r = uv_udp_bind((uv_udp_t *)UV_HANDLE(self), uv_ip4_addr(bind_ip, bind_port), 0); } else { r = uv_udp_bind6((uv_udp_t *)UV_HANDLE(self), uv_ip6_addr(bind_ip, bind_port), UV_UDP_IPV6ONLY); } if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); return NULL; } Py_RETURN_NONE; }
static int udp_listener() { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", server_port); struct sockaddr sockname; int namelen; int r; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_udp_bind(&udpServer, addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; } memset(&sockname, -1, sizeof sockname); namelen = sizeof sockname; r = uv_udp_getsockname(&udpServer, &sockname, &namelen); ASSERT(r == 0); check_sockname(&sockname, "0.0.0.0", server_port, "udp listener socket"); getsocknamecount++; r = uv_udp_recv_start(&udpServer, alloc, udp_recv); ASSERT(r == 0); return 0; }
int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb) { uv_loop_t* loop = handle->loop; if (handle->flags & UV_HANDLE_READING) { uv__set_sys_error(loop, WSAEALREADY); return -1; } if (!(handle->flags & UV_HANDLE_BOUND) && uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { return -1; } handle->flags |= UV_HANDLE_READING; INCREASE_ACTIVE_COUNT(loop, handle); loop->active_udp_streams++; handle->recv_cb = recv_cb; handle->alloc_cb = alloc_cb; /* If reading was stopped and then started again, there could still be a */ /* recv request pending. */ if (!(handle->flags & UV_HANDLE_READ_PENDING)) uv_udp_queue_recv(loop, handle); return 0; }
static int luv__udp_bind(lua_State *L, int family) { uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp"); const char* host = luaL_checkstring(L, 2); int port = luaL_checkint(L, 3); int flags = 0; int rc = 0; switch (family) { case AF_INET: rc = uv_udp_bind(handle, uv_ip4_addr(host, port), flags); break; case AF_INET6: rc = uv_udp_bind6(handle, uv_ip6_addr(host, port), flags); break; default: assert(0 && "unexpected family type"); abort(); } if (rc) { uv_err_t err = uv_last_error(luv_get_loop(L)); return luaL_error(L, "udp_bind: %s", uv_strerror(err)); } return 0; }
UvUdpSocket* UvUdpSocket::createUvUdpSocket(LibuvUsageEnvironment* env, string ip, int port) { uv_loop_t* uv_loop = (uv_loop_t*)env->TaskScheduler()->loopHandle(); int ret = 0; do { ASSERT(uv_loop); if(!uv_loop){ LOG_ERROR("create loop error\r\n"); ret = -1; break; } uv_udp_t* uv_udp = (uv_udp_t*)malloc(sizeof uv_udp_t); ret = uv_udp_init(uv_loop, uv_udp); struct sockaddr_in addrIn; ret = uv_ip4_addr(ip.c_str(),port, &addrIn); if(ret != 0) { break; } ret = uv_udp_bind(uv_udp, (const sockaddr*)&addrIn, 0); if(ret != 0) { break; } UvUdpSocket* udp = new UvUdpSocket(env,uv_udp); return udp; } while (0); return NULL; }
int udprelay_start(uv_loop_t *loop, struct server_context *server) { int rc; if (server->dest_addr->sa_family == AF_INET6) { addrlen = IPV6_HEADER_LEN; } uv_udp_init(loop, &server->udp); if ((rc = uv_udp_open(&server->udp, server->udp_fd))) { logger_stderr("udp open error: %s", uv_strerror(rc)); return 1; } rc = uv_udp_bind(&server->udp, server->local_addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("bind error: %s", uv_strerror(rc)); return 1; } uv_udp_recv_start(&server->udp, client_alloc_cb, client_recv_cb); return 0; }
int Server_listen( Server *server ) { struct sockaddr_storage address; checkFunction( Server_AddressInfo( server, &address ) ); server->ipFamily = address.ss_family; unsigned int flags = 0; if ( address.ss_family == AF_INET6 ) flags |= IPV6_V6ONLY; switch ( server->protocol ) { case ServerProtocol_TCP: { checkFunction( uv_tcp_bind( server->handle.tcpHandle, (struct sockaddr*)&address, flags ) ); break; } case ServerProtocol_UDP: { checkFunction( uv_udp_bind( server->handle.udpHandle, (struct sockaddr*)&address, flags ) ); log_err( "UDP actually isn't supported yet." ); return 1; // break; } default: { log_err( "An unknown server protocol happened." ); return 1; } } // e = uv_udp_recv_start( server->handle.udpHandle, allocCB, readCB ); checkFunction( uv_listen( server->handle.stream, 128, Server_newTCPConnection ) ); char namebuf[INET6_ADDRSTRLEN]; checkFunction( getnameinfo( (struct sockaddr *)&address, sizeof(address), namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST ) ); dbg_info( "Listening on [%s]:%d.", namebuf, ntohs(((struct sockaddr_in*)&address)->sin_port) ); return 0; }
int udp_start(struct tundev_context *ctx, uv_loop_t *loop) { int rc; ctx->network_buffer = malloc(ctx->tun->mtu + PRIMITIVE_BYTES); uv_udp_init(loop, &ctx->inet_udp); ctx->inet_udp_fd = create_socket(SOCK_DGRAM, mode == xTUN_SERVER ? 1 : 0); if ((rc = uv_udp_open(&ctx->inet_udp, ctx->inet_udp_fd))) { logger_log(LOG_ERR, "udp open error: %s", uv_strerror(rc)); exit(1); } #ifdef ANDROID rc = protect_socket(ctx->inet_udp_fd); logger_log(rc ? LOG_INFO : LOG_ERR, "Protect socket %s", rc ? "successful" : "failed"); #endif if (mode == xTUN_SERVER) { rc = uv_udp_bind(&ctx->inet_udp, &ctx->tun->addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("udp bind error: %s", uv_strerror(rc)); exit(1); } } return uv_udp_recv_start(&ctx->inet_udp, inet_alloc_cb, inet_recv_cb); }
static PyObject * UDP_func_bind(UDP *self, PyObject *args) { int err, flags; struct sockaddr_storage ss; PyObject *addr; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); flags = 0; if (!PyArg_ParseTuple(args, "O|i:bind", &addr, &flags)) { return NULL; } if (pyuv_parse_addr_tuple(addr, &ss) < 0) { /* Error is set by the function itself */ return NULL; } err = uv_udp_bind(&self->udp_h, (struct sockaddr *)&ss, flags); if (err < 0) { RAISE_UV_EXCEPTION(err, PyExc_UDPError); return NULL; } Py_RETURN_NONE; }
DLLEXPORT int jl_udp_bind6(uv_udp_t *handle, uint16_t port, void *host, uint32_t flags) { struct sockaddr_in6 addr; memset(&addr, 0, sizeof(struct sockaddr_in6)); addr.sin6_port = port; memcpy(&addr.sin6_addr, host, 16); addr.sin6_family = AF_INET6; return uv_udp_bind(handle, (struct sockaddr*)&addr, flags); }
DLLEXPORT int jl_udp_bind(uv_udp_t *handle, uint16_t port, uint32_t host, uint32_t flags) { struct sockaddr_in addr; memset(&addr, 0, sizeof(struct sockaddr_in)); addr.sin_port = port; addr.sin_addr.s_addr = host; addr.sin_family = AF_INET; return uv_udp_bind(handle, (struct sockaddr*)&addr, flags); }
void UdpSocketBaton::bind(unsigned int port) { IpAddress addr("0.0.0.0", port); int r = uv_udp_bind(&handle, addr.c_obj(), 0); if (r != 0) { throw std::runtime_error(uv_strerror(r)); } start_recive(); }
int udp_bind(uv_udp_t *handle, struct sockaddr *addr) { unsigned flags = UV_UDP_REUSEADDR; if (addr->sa_family == AF_INET6) { flags |= UV_UDP_IPV6ONLY; } int ret = uv_udp_bind(handle, addr, flags); if (ret != 0) { return ret; } return udp_bind_finalize((uv_handle_t *)handle); }
Status event_init (Server *server) { Event *event = &server->event; int rc = 0; event->reusable_base = NULL; event->base_alloc = false; debug ("event map init"); event_map = string_map_new (EventKey, node, key); if (!event_map) return G_ERR; size_t i = sizeof (event_keys) / sizeof (EventKey); while (i--) if (map_add (event_map, &event_keys[i], event_keys[i].key_size) != MAP_OK) goto error; debug ("json parser init"); event->json = json_parser_new (); if (!event->json) goto error; debug ("udp init"); rc = uv_udp_init (server->loop, (uv_udp_t *) event); if (rc < 0) goto error; /* Init sockaddr used for UDP. TODO: should parse either a multiaddr or * "ip:port" string ("[ip]:port" for ipv6). */ debug ("udp host init: %s:%d", server->host_ip, server->host_port); struct sockaddr_storage host; rc = uv_ip4_addr (server->host_ip, server->host_port, (struct sockaddr_in *) &host); if (rc < 0) goto error; debug ("udp bind: %s:%d", server->host_ip, server->host_port); rc = uv_udp_bind ((uv_udp_t *) event, (const struct sockaddr *) &host, UV_UDP_REUSEADDR); if (rc < 0) goto error; return G_OK; error: if (rc) debug ("error: %s", uv_strerror (rc)); return G_ERR; }
void uv_udp_handle_t::open(request_udp_open_t& request) { uv_udp_t* server = (uv_udp_t*)m_handle; if ((!request.m_ipv6 ? uv_udp_bind(server, uv_ip4_addr(REQUEST_SPARE_PTR(request), request.m_port), 0) == 0 : uv_udp_bind6(server, uv_ip6_addr(REQUEST_SPARE_PTR(request), request.m_port), 0) == 0)) { m_udp_sock = uv_udp_fd((uv_udp_t*)server); if (!singleton_ref(node_lua_t).context_send(request.m_source, 0, request.m_session, RESPONSE_UDP_OPEN, (void*)this)) { uv_close((uv_handle_t*)server, on_closed); } } else { singleton_ref(node_lua_t).context_send(request.m_source, 0, request.m_session, RESPONSE_UDP_OPEN, singleton_ref(network_t).last_error()); uv_close((uv_handle_t*)server, on_closed); } }
int udp_bind(uv_udp_t *handle, struct sockaddr *addr) { unsigned flags = UV_UDP_REUSEADDR; if (addr->sa_family == AF_INET6) { flags |= UV_UDP_IPV6ONLY; } int ret = uv_udp_bind(handle, addr, flags); if (ret != 0) { return ret; } handle->data = NULL; check_bufsize((uv_handle_t *)handle); return io_start_read((uv_handle_t *)handle); }
int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr, const char* interface_addr, uv_membership membership) { int optname; struct ip_mreq mreq; /* If the socket is unbound, bind to inaddr_any. */ if (!(handle->flags & UV_HANDLE_BOUND) && uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { return -1; } if (handle->flags & UV_HANDLE_IPV6) { uv__set_artificial_error(handle->loop, UV_ENOSYS); return -1; } memset(&mreq, 0, sizeof mreq); if (interface_addr) { mreq.imr_interface.s_addr = inet_addr(interface_addr); } else { mreq.imr_interface.s_addr = htonl(INADDR_ANY); } mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr); switch (membership) { case UV_JOIN_GROUP: optname = IP_ADD_MEMBERSHIP; break; case UV_LEAVE_GROUP: optname = IP_DROP_MEMBERSHIP; break; default: uv__set_artificial_error(handle->loop, UV_EFAULT); return -1; } if (setsockopt(handle->socket, IPPROTO_IP, optname, (char*) &mreq, sizeof mreq) == SOCKET_ERROR) { uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; } return 0; }
/* * Class: com_oracle_libuv_handles_UDPHandle * Method: _bind * Signature: (JILjava/lang/String;)I */ JNIEXPORT jint JNICALL Java_com_oracle_libuv_handles_UDPHandle__1bind (JNIEnv *env, jobject that, jlong udp, jint port, jstring host) { assert(udp); uv_udp_t* handle = reinterpret_cast<uv_udp_t*>(udp); const char* h = env->GetStringUTFChars(host, 0); sockaddr_in addr = uv_ip4_addr(h, port); unsigned flags = 0; int r = uv_udp_bind(handle, addr, flags); if (r) { ThrowException(env, handle->loop, "uv_udp_bind", h); } env->ReleaseStringUTFChars(host, h); return r; }
static int64_t HHVM_METHOD(UVUdp, bind, const String &host, int64_t port) { int64_t ret; struct sockaddr_in addr; auto* data = Native::data<UVUdpData>(this_); if((ret = uv_ip4_addr(host.c_str(), port&0xffff, &addr)) != 0){ return ret; } if((ret = uv_udp_bind(data->udp_handle, (const struct sockaddr*) &addr, 0)) != 0){ return ret; } data->udp_handle->flag |= UV_UDP_HANDLE_START; return ret; }
CAMLprim value uwt_udp_bind_na(value o_udp, value o_sock, value o_flags){ struct sockaddr_storage addr; if ( !uwt__get_sockaddr(o_sock, (struct sockaddr *) &addr)){ return VAL_UWT_INT_RESULT_UNKNOWN; } HANDLE_INIT_NA(t, o_udp); const unsigned int flags = SAFE_CONVERT_FLAG_LIST(o_flags,udp_bin_flag_table); const int ret = uv_udp_bind((uv_udp_t *)t->handle, (struct sockaddr *)&addr, flags); if ( ret >= 0 ){ t->initialized = 1; } return (VAL_UWT_UNIT_RESULT(ret)); }
int uv_udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[], int bufcnt, struct sockaddr_in addr, uv_udp_send_cb cb) { if (!(handle->flags & UV_HANDLE_BOUND) && uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { return -1; } return uv__udp_send(req, handle, bufs, bufcnt, (struct sockaddr*) &addr, sizeof addr, cb); }
static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) { struct sockaddr_in6 addr6; struct sockaddr_in addr; uv_buf_t buf; int r; ASSERT(0 == uv_ip6_addr("::0", TEST_PORT, &addr6)); r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); r = uv_udp_bind(&server, (const struct sockaddr*) &addr6, bind_flags); ASSERT(r == 0); r = uv_udp_recv_start(&server, alloc_cb, recv_cb); ASSERT(r == 0); r = uv_udp_init(uv_default_loop(), &client); ASSERT(r == 0); buf = uv_buf_init("PING", 4); ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); r = uv_udp_send(&req_, &client, &buf, 1, (const struct sockaddr*) &addr, send_cb); ASSERT(r == 0); r = uv_timer_init(uv_default_loop(), &timeout); ASSERT(r == 0); r = uv_timer_start(&timeout, timeout_cb, 500, 0); ASSERT(r == 0); ASSERT(close_cb_called == 0); ASSERT(send_cb_called == 0); ASSERT(recv_cb_called == 0); uv_run(uv_default_loop(), UV_RUN_DEFAULT); ASSERT(close_cb_called == 3); MAKE_VALGRIND_HAPPY(); }
int melo_udp_start(melo_udp_t * mudp, uv_loop_t* loop, melo_udp_config_t config) { assert(mudp && loop); mudp->uvloop = loop; memcpy(&mudp->config, &config, sizeof(melo_udp_config_t)); // init udp uv_udp_init(loop, &mudp->uvudp); mudp->uvudp.data = mudp; if (mudp->config.ip) { int r; melo_sockaddr_t sockaddr; melo_get_sock_addr(mudp->config.ip, mudp->config.port, &sockaddr); r = uv_udp_bind(&mudp->uvudp, (const struct sockaddr *)&sockaddr, 0); if(r >= 0) { char timestr[32]; time_t t; time(&t); strftime(timestr, sizeof(timestr), "[%Y-%m-%d %X]", localtime(&t)); // C99 only: %F = %Y-%m-%d MELO_INFO(config.log_out, "[uvx-udp] %s %s bind on %s:%d ...\n", timestr, mudp->config.name, mudp->config.ip, mudp->config.port); } else { MELO_ERR(config.log_err, "\n!!! [uvx-udp] %s bind on %s:%d failed: %s\n", mudp->config.name, mudp->config.ip, mudp->config.port, uv_strerror(r)); uv_close((uv_handle_t*) &mudp->uvudp, NULL); return ERROR; } } else { // if ip == NULL, default bind to local random port number (for UDP client) MELO_INFO(config.log_out, "[uvx-udp] %s bind on local random port ...\n", mudp->config.name); } uv_udp_recv_start(&mudp->uvudp, melo_alloc_buf, _cb_on_udp_recv); return OK; }
// Called after address resolution completes static void uvOnResolve(uv_getaddrinfo_t *handle, int status, struct addrinfo *response) { if (status == -1) { OnDisconnect(DISCO_RESOLVE); } else { _server_addr = response->ai_addr[0]; CAT_ENFORCE(!uv_udp_bind(&_uv_udp, &_server_addr, 0)); SendHello(); CAT_ENFORCE(!uv_timer_init(&_uv_loop, &_uv_hello)); CAT_ENFORCE(!uv_timer_start(&_uv_hello, callback, 200, 200)); } uv_freeaddrinfo(response); }
int uv_udp_set_broadcast(uv_udp_t* handle, int value) { BOOL optval = (BOOL) value; /* If the socket is unbound, bind to inaddr_any. */ if (!(handle->flags & UV_HANDLE_BOUND) && uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { return -1; } if (setsockopt(handle->socket, SOL_SOCKET, SO_BROADCAST, (char*) &optval, sizeof optval)) { uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; } return 0; }
static int udp_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_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_udp_bind(&udpServer, addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; } memset(&sockname, -1, sizeof sockname); r = uv_getsockname((uv_handle_t*)&udpServer, &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++; r = uv_udp_recv_start(&udpServer, alloc, udp_recv); ASSERT(r == 0); return 0; }