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; }
static int luv_udp_recv_stop(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); int ret = uv_udp_recv_stop(handle); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_send(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); uv_udp_send_t* req; uv_buf_t buf; int ret, port, ref; const char* host; struct sockaddr_storage addr; luv_check_buf(L, 2, &buf); host = luaL_checkstring(L, 3); port = luaL_checkinteger(L, 4); 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); } ref = luv_check_continuation(L, 5); req = (uv_udp_send_t*)lua_newuserdata(L, sizeof(*req)); req->data = luv_setup_req(L, ref); ret = uv_udp_send(req, handle, &buf, 1, (struct sockaddr*)&addr, luv_udp_send_cb); if (ret < 0) { luv_cleanup_req(L, (luv_req_t*)req->data); lua_pop(L, 1); return luv_error(L, ret); } return 1; }
static int luv_udp_open(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); uv_os_sock_t sock = luaL_checkinteger(L, 2); int ret = uv_udp_open(handle, sock); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_set_multicast_interface(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); const char* interface_addr = luaL_checkstring(L, 2); int ret = uv_udp_set_multicast_interface(handle, interface_addr); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_getsockname(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); struct sockaddr_storage address; int addrlen = sizeof(address); int ret = uv_udp_getsockname(handle, (struct sockaddr*)&address, &addrlen); if (ret < 0) return luv_error(L, ret); parse_sockaddr(L, &address, addrlen); return 1; }
static int luv_udp_recv_start(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); int ret; luv_check_callback(L, (luv_handle_t*)handle->data, LUV_RECV, 2); ret = uv_udp_recv_start(handle, luv_alloc_cb, luv_udp_recv_cb); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_set_ttl(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); int ttl, ret; ttl = luaL_checknumber(L, 2); ret = uv_udp_set_ttl(handle, ttl); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_set_membership(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); const char* multicast_addr = luaL_checkstring(L, 2); const char* interface_addr = luaL_checkstring(L, 3); uv_membership membership = (uv_membership)luaL_checkoption(L, 4, NULL, luv_membership_opts); int ret = uv_udp_set_membership(handle, multicast_addr, interface_addr, membership); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_set_broadcast(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); int on, ret; luaL_checktype(L, 2, LUA_TBOOLEAN); on = lua_toboolean(L, 2); ret =uv_udp_set_broadcast(handle, on); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_try_send(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); uv_buf_t buf; int ret, port; const char* host; struct sockaddr_storage addr; luv_check_buf(L, 2, &buf); host = luaL_checkstring(L, 3); port = luaL_checkinteger(L, 4); 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); } ret = uv_udp_try_send(handle, &buf, 1, (struct sockaddr*)&addr); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_udp_get_send_queue_count(lua_State* L) { uv_udp_t* handle = luv_check_udp(L, 1); lua_pushinteger(L, handle->send_queue_count); return 1; }