src/sock_tcp.c
author Tero Marttila <terom@fixme.fi>
Tue, 28 Apr 2009 23:10:30 +0300
branchnew-transport
changeset 159 d3e253d7281a
parent 157 1e5674d0eec4
permissions -rw-r--r--
implement heirarchial type-checking for transport_check
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     2
#include "sock_tcp_internal.h"
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     3
#include "sock_internal.h"
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     4
#include "log.h"
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <stdlib.h>
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <sys/types.h>
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include <sys/socket.h>
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include <string.h>
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    11
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    12
 * Start connecting to the given address in a non-blocking fashion. Returns any errors that immediately crop up,
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    13
 * otherwise eventually calls sock_tcp_connect_done().
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    14
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    15
static err_t sock_tcp_connect_addr (struct sock_tcp *sock, struct addrinfo *addr, error_t *err);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    16
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    17
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    18
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    19
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    20
 * Our transport_methods
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    21
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    22
static void _sock_tcp_destroy (transport_t *transport)
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 27
diff changeset
    23
{
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    24
    struct sock_tcp *sock = transport_check(transport, &sock_tcp_type);
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 27
diff changeset
    25
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    26
    // proxy
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    27
    sock_tcp_destroy(sock);
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 27
diff changeset
    28
}
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 27
diff changeset
    29
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
/*
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    31
 * Our transport_type
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
 */
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    33
struct transport_type sock_tcp_type = {
159
d3e253d7281a implement heirarchial type-checking for transport_check
Tero Marttila <terom@fixme.fi>
parents: 157
diff changeset
    34
    .parent                 = &transport_fd_type,
27
e6639132bead add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    35
    .methods                = {
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    36
        .read               = transport_fd_methods_read,
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    37
        .write              = transport_fd_methods_write,
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
    38
        .events             = transport_fd_methods_events,
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    39
        .destroy            = _sock_tcp_destroy,
27
e6639132bead add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    40
    },
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
};
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    43
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    44
 * Create a new socket() using the given addr's family/socktype/protocol, and update our transport_fd state.
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    45
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    46
static err_t sock_tcp_create_socket (struct sock_tcp *sock, struct addrinfo *addr, error_t *err)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    47
{
117
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    48
    int fd;
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    49
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    50
    // call socket()
117
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    51
    if ((fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol)) < 0)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    52
        RETURN_SET_ERROR_ERRNO(err, ERR_SOCKET);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    53
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    54
    // ok, update transport_fd
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    55
    transport_fd_set(SOCK_TCP_FD(sock), fd);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    56
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    57
12
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
    58
    return SUCCESS;
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
    59
}
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
    60
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    61
/**
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    62
 * Read the socket's error code, if any.
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    63
 *
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    64
 * The read error number is stored in err->extra on SUCCESS, unless reading the error fails, in which case
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    65
 * err contains the normal error info.
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    66
 *
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    67
 * @return error code on failure
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    68
 */
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    69
static err_t sock_tcp_read_error (struct sock_tcp *sock, error_t *err)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    70
{
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    71
    int optval;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    72
    socklen_t optlen;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    73
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    74
    RESET_ERROR(err);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    75
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 85
diff changeset
    76
    // init params
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 85
diff changeset
    77
    optval = 0;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 85
diff changeset
    78
    optlen = sizeof(optval);
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 85
diff changeset
    79
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    80
    // read error code
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    81
    if (getsockopt(SOCK_TCP_FD(sock)->fd, SOL_SOCKET, SO_ERROR, &optval, &optlen))
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    82
        RETURN_SET_ERROR_ERRNO(err, ERR_GETSOCKOPT);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    83
    
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    84
    // sanity-check optlen... not sure if this is sensible
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
    if (optlen != sizeof(optval))
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    86
        RETURN_SET_ERROR_EXTRA(err, ERR_GETSOCKOPT, EINVAL);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    88
    // then store the system error code
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    89
    ERROR_EXTRA(err) = optval;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    90
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    91
    // ok
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    92
    return SUCCESS;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    93
}
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    94
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    95
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    96
 * Attempt to connect to the given addrinfo, or the next one, if that fails, etc.
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    97
 *
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    98
 * This does not call transport_connected().
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    99
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   100
static err_t sock_tcp_connect_continue (struct sock_tcp *sock, struct addrinfo *addr, struct error_info *err)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   101
{
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   102
    if (!addr)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   103
        // no more addresses left to try
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   104
        return SET_ERROR(err, ERR_GETADDRINFO_EMPTY);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   105
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   106
    // try and connect to each one until we find one that works
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   107
    do {
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   108
        // attempt to start connect
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   109
        if (sock_tcp_connect_addr(sock, addr, err) == SUCCESS)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   110
            break;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   111
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   112
        // log a warning on the failed connect
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   113
        log_warn("sock_tcp_connect_addr(%s): %s", addr->ai_canonname, error_msg(err));
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   114
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   115
    } while ((addr = addr->ai_next));
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   116
    
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   117
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   118
    if (addr)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   119
        // we succesfully did a sock_tcp_connect_addr on valid address
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   120
        return SUCCESS;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   121
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   122
    else
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   123
        // all of the connect_async_addr's failed, return the last error
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   124
        return ERROR_CODE(err);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   125
    
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   126
}
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   127
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   128
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   129
 * Cleanup our resolver state and any connect callbacks after a connect
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   130
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   131
static void sock_tcp_connect_cleanup (struct sock_tcp *sock)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   132
{
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   133
    // free the addrinfo
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   134
    freeaddrinfo(sock->async_res); 
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   135
    sock->async_res = sock->async_cur = NULL;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   136
    
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   137
    // remove our event handler
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   138
    transport_fd_clear(SOCK_TCP_FD(sock));
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   139
}
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   140
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   141
/**
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   142
 * Our async connect operation has completed, clean up, set up state for event-based operation with user callbacks, and
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   143
 * invoke transport_connected().
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   144
 *
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   145
 * The given \a err should be NULL for successful completion, or the error for failures.
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   146
 */
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   147
static void sock_tcp_connect_done (struct sock_tcp *sock, struct error_info *conn_err)
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   148
{
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   149
    error_t err;
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   150
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   151
    // cleanup
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   152
    sock_tcp_connect_cleanup(sock);
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   153
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   154
    if (conn_err)
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   155
        // passthrough errors
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   156
        JUMP_SET_ERROR_INFO(&err, conn_err);
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   157
    
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   158
    // set up for default transport event-based operation
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   159
    if ((ERROR_CODE(&err) = transport_fd_defaults(SOCK_TCP_FD(sock))))
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   160
        goto error;
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   161
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   162
    // ok, no error
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   163
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   164
error:                
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   165
    // pass on to transport
157
1e5674d0eec4 fixed fifo
Tero Marttila <terom@fixme.fi>
parents: 156
diff changeset
   166
    transport_connected(SOCK_TCP_TRANSPORT(sock), IS_ERROR(&err) ? &err : NULL, false);
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   167
}
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   168
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   169
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   170
 * Our async connect callback
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   171
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   172
static void sock_tcp_on_connect (struct transport_fd *fd, short what, void *arg)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   173
{
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   174
    struct sock_tcp *sock = arg;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   175
    struct error_info err;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   176
    err_t tmp;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   177
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   178
    // XXX: timeouts
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   179
    (void) what;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   180
    
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   181
    // read socket error code
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   182
    if (sock_tcp_read_error(sock, &err))
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   183
        goto error;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   184
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   185
    // did the connect fail?
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   186
    if (ERROR_EXTRA(&err))
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   187
        JUMP_SET_ERROR(&err, ERR_CONNECT);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   188
    
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   189
    // done, success
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   190
    return sock_tcp_connect_done(sock, NULL);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   191
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   192
error:
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   193
    // close the socket
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   194
    if ((tmp = transport_fd_close(fd)))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   195
        log_warn("error closing socket after connect error: %s", error_name(tmp));
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   196
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   197
    // log a warning
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   198
    log_warn("connect to '%s' failed: %s", sock->async_cur->ai_canonname, error_msg(&err));
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   199
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   200
    // try the next one or fail completely
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   201
    if (sock_tcp_connect_continue(sock, sock->async_cur->ai_next, &err))
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   202
        sock_tcp_connect_done(sock, &err);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   203
}
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   204
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   205
static err_t sock_tcp_connect_addr (struct sock_tcp *sock, struct addrinfo *addr, error_t *err)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   206
{
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   207
    int ret;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   208
    err_t tmp;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   209
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   210
    // first, create the socket
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   211
    if (sock_tcp_create_socket(sock, addr, err))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   212
        return ERROR_CODE(err);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   213
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   214
    // then, set it up as nonblocking
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   215
    if ((ERROR_CODE(err) = transport_fd_nonblock(SOCK_TCP_FD(sock), true)))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   216
        goto error;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   217
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   218
    // then, initiate the connect
117
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   219
    if ((ret = connect(SOCK_TCP_FD(sock)->fd, addr->ai_addr, addr->ai_addrlen)) < 0 && errno != EINPROGRESS) 
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   220
        JUMP_SET_ERROR_ERRNO(err, ERR_CONNECT);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   221
    
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   222
    if (ret < 0) {
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   223
        // set the "current" address in case it fails and we need to try the next one
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   224
        sock->async_cur = addr;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   225
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   226
        // ok, connect started, setup our completion callback
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   227
        if ((ERROR_CODE(err) = transport_fd_setup(SOCK_TCP_FD(sock), sock_tcp_on_connect, sock)))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   228
            goto error;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   229
    
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   230
        // enable for write
156
6534a4ac957b add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
   231
        if ((ERROR_CODE(err) = transport_fd_enable(SOCK_TCP_FD(sock), TRANSPORT_WRITE)))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   232
            goto error;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   233
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   234
    } else {
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   235
        // oops... blocking connect - fail to avoid confusion
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   236
        // XXX: come up with a better error name to use
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   237
        // XXX: support non-async connects as well
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   238
        JUMP_SET_ERROR_EXTRA(err, ERR_CONNECT, EINPROGRESS);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   239
    }
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   240
    
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   241
    // ok
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   242
    return SUCCESS;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   243
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   244
error:
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   245
    // close the stuff we did open
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   246
    if ((tmp = transport_fd_close(SOCK_TCP_FD(sock))))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   247
        log_warn("error closing socket after connect error: %s", error_name(tmp));
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   248
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   249
    return ERROR_CODE(err);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   250
}
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   251
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   252
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   253
 * External interface
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   254
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   255
void sock_tcp_init (struct sock_tcp *sock)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   256
{
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   257
    struct event_base *ev_base = _sock_stream_ctx.ev_base;
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   258
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   259
    // init without any fd
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   260
    transport_fd_init(SOCK_TCP_FD(sock), ev_base, TRANSPORT_FD_INVALID);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   261
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   262
}
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   263
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   264
err_t sock_tcp_connect_async (struct sock_tcp *sock, const char *hostname, const char *service, error_t *err)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   265
{
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   266
    struct addrinfo hints;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   267
    int ret;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   268
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   269
    // build hints
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   270
    memset(&hints, 0, sizeof(hints));
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   271
    hints.ai_family = AF_UNSPEC;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   272
    hints.ai_socktype = SOCK_STREAM;
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   273
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   274
    // resolve (blocking)
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   275
    if ((ret = getaddrinfo(hostname, service, &hints, &sock->async_res)))
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   276
        RETURN_SET_ERROR_EXTRA(err, ERR_GETADDRINFO, ret);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   277
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   278
    // start connecting on the first result
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   279
    if (sock_tcp_connect_continue(sock, sock->async_res, err))
117
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   280
        goto error;
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   281
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   282
    // ok
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   283
    return SUCCESS;
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   284
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   285
error:
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   286
    // cleanup
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   287
    if (sock->async_res)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   288
        sock_tcp_connect_cleanup(sock);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   289
    
117
9cb405164250 move irc_log.c to modules/irc_log.c, and restructure sock_* to split the basic fd-level stuff out of sock_tcp and into sock_fd
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   290
    return ERROR_CODE(err);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   291
}
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   292
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   293
void sock_tcp_destroy (struct sock_tcp *sock)
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
{
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   295
    // cleanup our stuff
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   296
    if (sock->async_res)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   297
        sock_tcp_connect_cleanup(sock);
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   299
    // destroy lower level
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   300
    transport_fd_destroy(SOCK_TCP_FD(sock)); 
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301
}
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   303
/**
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   304
 * Public interface
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   305
 */
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   306
err_t sock_tcp_connect (const struct transport_info *info, transport_t **transport_ptr, 
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   307
        const char *host, const char *service, error_t *err)
2
a834f0559939 working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   308
{
a834f0559939 working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   309
    struct sock_tcp *sock;
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   310
 
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   311
    // alloc
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   312
    if ((sock = calloc(1, sizeof(*sock))) == NULL)
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   313
        return ERR_CALLOC;
2
a834f0559939 working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   314
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   315
    // initialize transport
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   316
    transport_init(SOCK_TCP_TRANSPORT(sock), &sock_tcp_type, info);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   317
    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   318
    // init our state
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   319
    sock_tcp_init(sock);
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   320
 
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   321
    // connect    
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   322
    if (sock_tcp_connect_async(sock, host, service, err))
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   323
        goto error;
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   324
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   325
    // good
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   326
    *transport_ptr = SOCK_TCP_TRANSPORT(sock);
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   327
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   328
    return 0;
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   329
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   330
error:
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   331
    // cleanup
155
c59d3eaff0fb most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   332
    sock_tcp_destroy(sock);
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   333
        
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   334
    // return error code
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   335
    return ERROR_CODE(err);
2
a834f0559939 working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   336
}
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   337