コード例 #1
0
ファイル: udp.c プロジェクト: jppommet/pyuv
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;
}
コード例 #2
0
ファイル: luv_udp.c プロジェクト: AndrewTsao/luvit
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;
}
コード例 #3
0
ファイル: udp.cpp プロジェクト: luojunqiang/libuv-java
/*
 * Class:     com_oracle_libuv_handles_UDPHandle
 * Method:    _bind6
 * Signature: (JILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_oracle_libuv_handles_UDPHandle__1bind6
  (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_in6 addr = uv_ip6_addr(h, port);
  unsigned flags = 0;
  int r = uv_udp_bind6(handle, addr, flags);
  if (r) {
    ThrowException(env, handle->loop, "uv_udp_bind6", h);
  }
  env->ReleaseStringUTFChars(host, h);
  return r;
}
コード例 #4
0
ファイル: udp.c プロジェクト: Ankso/node
int uv_udp_send6(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
    int bufcnt, struct sockaddr_in6 addr, uv_udp_send_cb cb) {

  if (!(handle->flags & UV_HANDLE_BOUND) &&
      uv_udp_bind6(handle, uv_addr_ip6_any_, 0) < 0) {
    return -1;
  }

  return uv__udp_send(req,
                      handle,
                      bufs,
                      bufcnt,
                      (struct sockaddr*) &addr,
                      sizeof addr,
                      cb);
}
コード例 #5
0
ファイル: udp.c プロジェクト: DrPizza/node
int uv_udp_connect6(uv_connect_t* req, uv_udp_t* handle,
    struct sockaddr_in6 address, uv_connect_cb cb) {
  uv_loop_t* loop = handle->loop;
  int addrsize = sizeof(struct sockaddr_in6);
  BOOL success;
  DWORD bytes;

  if (!uv_allow_ipv6) {
    uv_new_sys_error(WSAEAFNOSUPPORT);
    return -1;
  }

  if (address.sin6_family != AF_INET6) {
    uv_set_sys_error(loop, WSAEFAULT);
    return -1;
  }

  if (!(handle->flags & UV_HANDLE_BOUND) &&
      uv_udp_bind6(handle, uv_addr_ip6_any_, 0) < 0)
    return -1;

  uv_req_init(loop, (uv_req_t*) req);
  req->type = UV_CONNECT;
  req->handle = (uv_stream_t*) handle;
  req->cb = cb;
  memset(&req->overlapped, 0, sizeof(req->overlapped));

  success = pConnectEx6(handle->socket,
                       (struct sockaddr*) &address,
                       addrsize,
                       NULL,
                       0,
                       &bytes,
                       &req->overlapped);

  if (UV_SUCCEEDED_WITHOUT_IOCP(success)) {
    handle->reqs_pending++;
    uv_insert_pending_req(loop, (uv_req_t*)req);
  } else if (UV_SUCCEEDED_WITH_IOCP(success)) {
    handle->reqs_pending++;
  } else {
    uv_set_sys_error(loop, WSAGetLastError());
    return -1;
  }

  return 0;
}
コード例 #6
0
ファイル: test-udp-ipv6.c プロジェクト: Jan357/node
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_bind6(&server, &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, &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();
}
コード例 #7
0
ファイル: uv_udp_handle.cpp プロジェクト: deway/node-lua
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);
	}
}
コード例 #8
0
ファイル: rust_uv.cpp プロジェクト: amishra-1/rust
extern "C" int
rust_uv_udp_bind6(uv_udp_t* server, sockaddr_in6* addr_ptr, unsigned flags) {
    return uv_udp_bind6(server, *addr_ptr, flags);
}