Skip to content

SocketConnect

Description

Open + connect to a remote endpoint.

Parameters

Name Direction Description
out out Connected socket. Untouched on failure.
kind in SOCKET_KIND_TCP or SOCKET_KIND_UDP.
target in Address to connect to.

Success

returns true; out->fd is connected.

Failure

returns false; logs the failing syscall.

Usage example (Cross-references)

Usage examples (Cross-references)
        }
    
        if (!SocketConnect(&client, SOCKET_KIND_TCP, &connect_addr)) {
            ListenerClose(&listener);
            return false;
    // ---------------------------------------------------------------------------
    
    bool SocketConnect(Socket *out, SocketKind kind, const SocketAddr *target) {
        if (!out || !target) {
            LOG_ERROR("SocketConnect: NULL argument");
    bool SocketConnect(Socket *out, SocketKind kind, const SocketAddr *target) {
        if (!out || !target) {
            LOG_ERROR("SocketConnect: NULL argument");
            return false;
        }
        i32 proto    = sock_kind_to_protocol(kind);
        if (af == AF_UNSPEC || socktype < 0) {
            LOG_ERROR("SocketConnect: invalid family/kind combination");
            return false;
        }
        i32 fd = socket(af, socktype, proto);
        if (fd < 0) {
            LOG_SYS_ERROR("SocketConnect: socket() failed");
            return false;
        }
    
        if (connect(fd, (const struct sockaddr *)target->raw, (socklen_t)target->length) < 0) {
            LOG_SYS_ERROR("SocketConnect: connect() failed");
            close(fd);
            return false;
    
        Socket upstream;
        if (!SocketConnect(&upstream, SOCKET_KIND_TCP, upstream_addr)) {
            LOG_ERROR("failed to dial upstream for client [{}]", peer_str);
            SocketClose(client);
Last updated on