Exemplo n.º 1
0
void
client_accept_cb(uv_stream_t *server, int status) {
    struct client_context *client = new_client();
    struct remote_context *remote = new_remote(idle_timeout);

    client->remote = remote;
    remote->client = client;

    uv_timer_init(server->loop, remote->timer);

    uv_tcp_init(server->loop, &client->handle.tcp);
    uv_tcp_init(server->loop, &remote->handle.tcp);


    int rc = uv_accept(server, &client->handle.stream);
    if (rc == 0) {
        int namelen = sizeof client->addr;
        uv_tcp_getpeername(&client->handle.tcp, &client->addr, &namelen);
        reset_timer(remote); // start timer
        connect_to_remote(remote);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_client(client);
        close_remote(remote);
    }
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
static void
do_accept(uv_stream_t* server, int status) {
    CHECK(status);
    GETDATA(ircd_t, ircd, server);

    struct irc_session *new_session = (struct irc_session *)malloc(sizeof(struct irc_session));
    uv_tcp_init(uv_default_loop(),&new_session->handle);
    new_session->handle.data = new_session;

    new_session->buffer = NULL;
    new_session->ircd = ircd;
    new_session->mode = INITIALIZING;

    if (uv_accept((uv_stream_t*)&ircd->handle, (uv_stream_t*)&new_session->handle) < 0) {
        free(new_session);
        perror("accept");
    } else {
        struct sockaddr_storage addr;
        int addrlen = sizeof(addr);
        uv_tcp_getpeername(&new_session->handle,(struct sockaddr*)&addr,&addrlen);

        printf("accepted connection from %s\n", sprint_addrport((struct sockaddr *)&addr));

        new_session->inbuf_used = 0;
        new_session->next = ircd->session_list;
        if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr, new_session->ip, INET6_ADDRSTRLEN)) {
            perror("inet_ntop");
        }
        ircd->session_list = new_session;

        uv_read_start((uv_stream_t*)&new_session->handle,alloc_buffer,on_read);
    }
}
Exemplo n.º 4
0
Address HttpRequest::clientAddress() {
  Address address;

  if (_handle.isTcp) {
    struct sockaddr_in addr = {0};
    int len = sizeof(sockaddr_in);
    int r = uv_tcp_getpeername(&_handle.tcp, (struct sockaddr*)&addr, &len);
    if (r) {
      // TODO: warn?
      return address;
    }

    if (addr.sin_family != AF_INET) {
      // TODO: warn
      return address;
    }

    // addrstr is a pointer to static buffer, no need to free
    char* addrstr = inet_ntoa(addr.sin_addr);
    if (addrstr)
      address.host = std::string(addrstr);
    else {
      // TODO: warn?
    }
    address.port = ntohs(addr.sin_port);
  }

  return address;
}
Exemplo n.º 5
0
int luv_tcp_getpeername(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_getpeername(handle, (struct sockaddr*)(&address), &addrlen)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "tcp_getpeername: %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;
}
Exemplo n.º 6
0
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);
}
Exemplo n.º 7
0
extern "C" int
rust_uv_tcp_getpeername
(uv_tcp_t* handle, sockaddr_storage* name) {
    // sockaddr_storage is big enough to hold either
    // sockaddr_in or sockaddr_in6
    int namelen = sizeof(sockaddr_in);
    return uv_tcp_getpeername(handle, (sockaddr*)name, &namelen);
}
Exemplo n.º 8
0
PeerAddress UvTcpServerStream::GetPeerAddress()
{
	sockaddr_storage addr;
	int len = sizeof(addr);

	uv_tcp_getpeername(m_client.get(), reinterpret_cast<sockaddr*>(&addr), &len);

	return PeerAddress(reinterpret_cast<sockaddr*>(&addr), static_cast<socklen_t>(len));
}
Exemplo n.º 9
0
Arquivo: tcp.c Projeto: senlinms/luv
static int luv_tcp_getpeername(lua_State* L) {
  uv_tcp_t* handle = luv_check_tcp(L, 1);
  struct sockaddr_storage address;
  int addrlen = sizeof(address);
  int ret = uv_tcp_getpeername(handle, (struct sockaddr*)&address, &addrlen);
  if (ret < 0) return luv_error(L, ret);
  parse_sockaddr(L, &address, addrlen);
  return 1;
}
Exemplo n.º 10
0
static int lluv_tcp_getpeername(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int sa_len = sizeof(sa);
  int err = uv_tcp_getpeername(LLUV_H(handle, uv_tcp_t), (struct sockaddr*)&sa, &sa_len);
  lua_settop(L, 1);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return lluv_push_addr(L, &sa);
}
Exemplo n.º 11
0
static int get_nameinfo(lcb_io_opt_t iobase,
                        lcb_sockdata_t *sockbase,
                        struct lcb_nameinfo_st *ni)
{
    my_sockdata_t *sock = (my_sockdata_t *)sockbase;
    my_iops_t *io = (my_iops_t *)iobase;
    uv_tcp_getpeername(&sock->tcp.t, ni->remote.name, ni->remote.len);
    uv_tcp_getsockname(&sock->tcp.t, ni->local.name, ni->local.len);

    (void)io;
    return 0;
}
Exemplo n.º 12
0
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);
}
Exemplo n.º 13
0
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);
}
Exemplo n.º 14
0
static void
remote_timer_expire(uv_timer_t *handle) {
    struct remote_context *remote = handle->data;
    struct client_context *client = remote->client;
    if (verbose) {
        struct sockaddr peername;
        int namelen = sizeof peername;
        uv_tcp_getpeername(&client->handle.tcp, &peername, &namelen);
        char addr[INET6_ADDRSTRLEN + 1];
        int port = ip_name(&peername, addr, sizeof addr);
        logger_log(LOG_WARNING, "%s:%d <-> %s connection timeout", addr, port, dest_addr_buf);
    }
    close_client(client);
    close_remote(remote);
}
Exemplo n.º 15
0
Arquivo: melo.c Projeto: susemm/melo
const char * melo_get_tcp_ip_port(uv_tcp_t* uvclient, char* ipbuf, int buflen, int* port) 
{
    struct sockaddr addr;
    int len = sizeof(addr);
    int r = uv_tcp_getpeername(uvclient, &addr, &len);
    if (r == 0) 
    {
        return melo_get_ip_port(&addr, ipbuf, buflen, port);
    } 
    else 
    {
        printf("\n!!! [uvx] get client ip fails: %s\n", uv_strerror(r));
        return NULL;
    }
}
Exemplo n.º 16
0
int SocketGetPeerInfo(SocketRef const socket, char *const out, size_t const max) {
	assert(max > 0);
	assert(out);
	if(!socket) return UV_EINVAL;
	struct sockaddr_storage peer[1];
	int len = sizeof(*peer);
	int rc = uv_tcp_getpeername(socket->stream, (struct sockaddr *)peer, &len);
	if(rc < 0) return rc;

	uv_getnameinfo_t req[1];
	rc = async_getnameinfo(req, (struct sockaddr *)peer, NI_NUMERICSERV);
	if(rc < 0) return rc;
	strlcpy(out, req->host, max);
	return 0;
}
Exemplo n.º 17
0
int check_ip(const char * ipstr, http_connection_t * conn)
{
    struct sockaddr_in ip_addr, compare_addr;
    size_t namelen = sizeof(struct sockaddr_in);

    uv_tcp_getpeername(&conn->stream, &ip_addr, &namelen);
    uv_ip4_addr(ipstr, 0, &compare_addr);
    
    if (compare_addr.sin_family == ip_addr.sin_family) {
        return memcmp(&ip_addr.sin_addr,
            &compare_addr.sin_addr,
            sizeof compare_addr.sin_addr) == 0;
    }

    return 0;
}
Exemplo n.º 18
0
static void
remote_timer_expire(uv_timer_t *handle) {
    struct remote_context *remote = handle->data;
    struct client_context *client = remote->client;
    if (verbose) {
        struct sockaddr peername;
        int namelen = sizeof peername;
        uv_tcp_getpeername(&client->handle.tcp, &peername, &namelen);
        char addr1[INET6_ADDRSTRLEN + 1];
        char addr2[INET6_ADDRSTRLEN + 1];
        int p1 = ip_name(&peername, addr1, sizeof addr2);
        int p2 = ip_name(&client->target_addr, addr2, sizeof(addr2));
        logger_log(LOG_WARNING, "%s:%d <-> %s:%d timeout", addr1, p1, addr2, p2);
    }
    close_client(client);
    close_remote(remote);
}
Exemplo n.º 19
0
bool tcp_connection::set_peer_address()
{
	int err;
	int len = sizeof(m_peer_addr);

	err = uv_tcp_getpeername(m_uv_handle, (struct sockaddr*)&m_peer_addr, &len);
	if (err)
	{
		osd_printf_error("uv_tcp_getpeername() failed: %s\n", uv_strerror(err));

		return false;
	}

	int family;
	GetAddressInfo((const struct sockaddr*)&m_peer_addr, &family, m_peer_ip, &m_peer_port);

	return true;
}
Exemplo n.º 20
0
static void on_connect(uv_stream_t *server, int status) {
  grpc_tcp_listener *sp = (grpc_tcp_listener *)server->data;
  uv_tcp_t *client;
  grpc_endpoint *ep = NULL;
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_resolved_address peer_name;
  char *peer_name_string;
  int err;

  if (status < 0) {
    gpr_log(GPR_INFO, "Skipping on_accept due to error: %s",
            uv_strerror(status));
    return;
  }

  client = gpr_malloc(sizeof(uv_tcp_t));
  uv_tcp_init(uv_default_loop(), client);
  // UV documentation says this is guaranteed to succeed
  uv_accept((uv_stream_t *)server, (uv_stream_t *)client);
  // If the server has not been started, we discard incoming connections
  if (sp->server->on_accept_cb == NULL) {
    uv_close((uv_handle_t *)client, accepted_connection_close_cb);
  } else {
    peer_name_string = NULL;
    memset(&peer_name, 0, sizeof(grpc_resolved_address));
    peer_name.len = sizeof(struct sockaddr_storage);
    err = uv_tcp_getpeername(client, (struct sockaddr *)&peer_name.addr,
                             (int *)&peer_name.len);
    if (err == 0) {
      peer_name_string = grpc_sockaddr_to_uri(&peer_name);
    } else {
      gpr_log(GPR_INFO, "uv_tcp_getpeername error: %s", uv_strerror(status));
    }
    ep = grpc_tcp_create(client, sp->server->resource_quota, peer_name_string);
    // Create acceptor.
    grpc_tcp_server_acceptor *acceptor = gpr_malloc(sizeof(*acceptor));
    acceptor->from_server = sp->server;
    acceptor->port_index = sp->port_index;
    acceptor->fd_index = 0;
    sp->server->on_accept_cb(&exec_ctx, sp->server->on_accept_cb_arg, ep, NULL,
                             acceptor);
    grpc_exec_ctx_finish(&exec_ctx);
  }
}
Exemplo n.º 21
0
Arquivo: tcp.c Projeto: idobatter/pyuv
static PyObject *
TCP_func_getpeername(TCP *self)
{
    int err, namelen;
    struct sockaddr_storage peername;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    namelen = sizeof(peername);

    err = uv_tcp_getpeername(&self->tcp_h, (struct sockaddr *)&peername, &namelen);
    if (err < 0) {
        RAISE_UV_EXCEPTION(err, PyExc_TCPError);
        return NULL;
    }

    return makesockaddr((struct sockaddr *)&peername);
}
Exemplo n.º 22
0
net::Address TCPSocket::peerAddress() const
{
    //TraceLS(this) << "Get peer address: " << closed() << endl;
    if (!active())
        return net::Address();
        //throw std::runtime_error("Invalid TCP socket: No peer address");

    struct sockaddr_storage address;
    int addrlen = sizeof(address);
    int r = uv_tcp_getpeername(ptr<uv_tcp_t>(),
                                reinterpret_cast<sockaddr*>(&address),
                                &addrlen);

    if (r)
        return net::Address();
        //throwLastError("Invalid TCP socket: No peer address");

    return net::Address(reinterpret_cast<const sockaddr*>(&address), addrlen);
}
Exemplo n.º 23
0
Arquivo: tcp.c Projeto: eagles125/pyuv
static PyObject *
TCP_func_getpeername(TCP *self)
{
    int r, namelen;
    struct sockaddr peername;

    namelen = sizeof(peername);

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    r = uv_tcp_getpeername(&self->tcp_h, &peername, &namelen);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError);
        return NULL;
    }

    return makesockaddr(&peername, namelen);
}
Exemplo n.º 24
0
bool TcpConnection::SetPeerAddress()
{
	MS_TRACE();

	int err;
	int len = sizeof(this->peerAddr);

	err = uv_tcp_getpeername(this->uvHandle, (struct sockaddr*)&this->peerAddr, &len);
	if (err)
	{
		MS_ERROR("uv_tcp_getpeername() failed: %s", uv_strerror(err));

		return false;
	}

	int family;
	Utils::IP::GetAddressInfo((const struct sockaddr*)&this->peerAddr, &family, this->peerIP, &this->peerPort);

	return true;
}
Exemplo n.º 25
0
static void on_connect(uv_connect_t* req, int status) {
  struct sockaddr sockname, peername;
  int r, namelen;

  ASSERT(status == 0);

  namelen = sizeof sockname;
  r = uv_tcp_getsockname((uv_tcp_t*) req->handle, &sockname, &namelen);
  ASSERT(r == 0);
  check_sockname(&sockname, "127.0.0.1", 0, "connected socket");
  getsocknamecount++;

  namelen = sizeof peername;
  r = uv_tcp_getpeername((uv_tcp_t*) req->handle, &peername, &namelen);
  ASSERT(r == 0);
  check_sockname(&peername, "127.0.0.1", server_port, "connected socket peer");
  getpeernamecount++;

  uv_close((uv_handle_t*)&tcp, NULL);
}
Exemplo n.º 26
0
static int tcp_get_peerport(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;

    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");
    }
    
    lua_pushboolean(l, 1);
    lua_pushinteger(l, ntohs(sin->sin_port));
    return 2;
}
Exemplo n.º 27
0
int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) {
  WSAPROTOCOL_INFOW protocol_info;
  int opt_len;
  int err;
  struct sockaddr_storage saddr;
  int saddr_len;

  /* Detect the address family of the socket. */
  opt_len = (int) sizeof protocol_info;
  if (getsockopt(sock,
                 SOL_SOCKET,
                 SO_PROTOCOL_INFOW,
                 (char*) &protocol_info,
                 &opt_len) == SOCKET_ERROR) {
    return uv_translate_sys_error(GetLastError());
  }

  err = uv_tcp_set_socket(handle->loop,
                          handle,
                          sock,
                          protocol_info.iAddressFamily,
                          1);
  if (err) {
    return uv_translate_sys_error(err);
  }

  /* Support already active socket. */
  saddr_len = sizeof(saddr);
  if (!uv_tcp_getsockname(handle, (struct sockaddr*) &saddr, &saddr_len)) {
    /* Socket is already bound. */
    handle->flags |= UV_HANDLE_BOUND;
    saddr_len = sizeof(saddr);
    if (!uv_tcp_getpeername(handle, (struct sockaddr*) &saddr, &saddr_len)) {
      /* Socket is already connected. */
      uv_connection_init((uv_stream_t*) handle);
      handle->flags |= UV_HANDLE_READABLE | UV_HANDLE_WRITABLE;
    }
  }

  return 0;
}
Exemplo n.º 28
0
TStr TSockSys::GetPeerIpNum(const uint64& SockId) {
	// make sure it's a valid socket
	IAssert(IsSock(SockId));
	uv_tcp_t* SockHnd = SockIdToHndH.GetDat(SockId);
	// get peer IP
	struct sockaddr PeerName;
	int NameLen = sizeof(PeerName);
	const int ResCd = uv_tcp_getpeername(SockHnd, &PeerName, &NameLen);
	EAssertR(ResCd == 0, "SockSys.GetLocalIpNum: " + SockSys.GetLastErr());
	// decode IP
	char PeerIpNum[17];
	if (PeerName.sa_family == AF_INET) {
		uv_ip4_name((sockaddr_in*)&PeerName, PeerIpNum, sizeof(PeerIpNum));
	} else if (PeerName.sa_family == AF_INET6) {
		uv_ip6_name((sockaddr_in6*)&PeerName, PeerIpNum, sizeof(PeerIpNum));
	} else {
		throw TExcept::New("SockSys.GetLocalIpNum: unkown address family");
	}
	// return
	return TStr(PeerIpNum);
}
Exemplo n.º 29
0
static int tcp_listener(void) {
  struct sockaddr_in addr;
  struct sockaddr sockname, peername;
  int namelen;
  int r;

  ASSERT(0 == uv_ip4_addr("0.0.0.0", server_port, &addr));

  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);
  namelen = sizeof sockname;
  r = uv_tcp_getsockname(&tcpServer, &sockname, &namelen);
  ASSERT(r == 0);
  check_sockname(&sockname, "0.0.0.0", server_port, "server socket");
  getsocknamecount++;

  namelen = sizeof sockname;
  r = uv_tcp_getpeername(&tcpServer, &peername, &namelen);
  ASSERT(r == UV_ENOTCONN);
  getpeernamecount++;

  return 0;
}
Exemplo n.º 30
0
static void on_connection(uv_stream_t* server, int status) {	//当有客户端连接时触发
	int r;
	Request* request;
	struct sockaddr_in sockaddr;	//定义套接字地址
	socklen_t addrlen;
	uv_stream_t* handle;
	dprint("on connection.");
	if (status != 0) {				//状态判断, 状态必须为0
	fprintf(stderr, "Connect error %d\n",
		uv_last_error(loop).code);
	}
	ASSERT(status == 0);

	handle = malloc(sizeof(uv_tcp_t));
	GIL_LOCK(0);
	request = Request_init();
	GIL_UNLOCK(0);

	r = uv_tcp_init(loop, (uv_tcp_t*)handle);//初始化连接流结构体
	ASSERT(r == 0);

	r = uv_accept(server, handle);
	ASSERT(r == 0);

	//-----------------得到客户端信息
	addrlen = sizeof(struct sockaddr_in);
	r = uv_tcp_getpeername((uv_tcp_t *)handle, (struct sockaddr *)&sockaddr, &addrlen);
	ASSERT(r == 0);
	
	request->client_addr = PyString_FromString(inet_ntoa(sockaddr.sin_addr));
	request->client_fd = 1;//request->ev_watcher.socket;
	request->ev_watcher = handle;
	handle->data = request;
	DBG_REQ(request, "Accepted client %s:%d on fd %d",
		  inet_ntoa(sockaddr.sin_addr), ntohs(sockaddr.sin_port), (int)(GET_HANDLE_FD(handle)));
	//@@@@@@@@@@@@@@@@@free(&sockaddr);
	//-----------------读事件循环
	r = uv_read_start(handle, on_alloc, on_read);
	ASSERT(r == 0);
}