src/sock.h
author Tero Marttila <terom@fixme.fi>
Tue, 31 Mar 2009 22:09:53 +0300
changeset 98 f357f835f0d5
parent 85 75bc8b164ef8
child 117 9cb405164250
permissions -rw-r--r--
add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#ifndef SOCK_H
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#define SOCK_H
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     4
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     5
 * @file
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     6
 *
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
 * Low-level socket-related functions
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
 */
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
     9
#include "error.h"
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
#include <sys/types.h>
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    11
#include <event2/event.h>
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    13
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    14
 * The generic stream socket handle
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
 */
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
struct sock_stream;
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    18
/**
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    19
 * Callback for connect_async completion notification. If err is NULL, the connection completed succesfully,
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    20
 * otherwise, it failed.
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    21
 */
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    22
typedef void (*sock_stream_connect_cb) (struct sock_stream *sock, struct error_info *err, void *arg);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    23
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    24
/**
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    25
 * Async callbacks for socket operation
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    26
 */
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    27
struct sock_stream_callbacks {
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    28
    /** Socket is readable */
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    29
    void (*on_read) (struct sock_stream *sock, void *arg);
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    30
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    31
    /** Socket is writeable */
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    32
    void (*on_write) (struct sock_stream *sock, void *arg);
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    33
};
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    34
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    35
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    36
 * Socket function error codes
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    37
 */
30
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    38
enum sock_error_code {
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    39
    _ERR_SOCK_BEGIN = _ERR_SOCK,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    40
    ERR_SOCKET,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    41
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    42
    /** connect() error, either direct or async */
30
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    43
    ERR_CONNECT,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    44
    ERR_READ,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    45
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    46
    /** EOF on read() */
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    47
    ERR_READ_EOF,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    48
    ERR_WRITE,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    49
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    50
    /** EOF on write()  */
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    51
    ERR_WRITE_EOF,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    52
    ERR_FCNTL,
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    53
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    54
    /** Lingering error on close() */
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    55
    ERR_CLOSE,
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    56
    ERR_GETSOCKOPT,
30
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    57
};
7f8dd120933f rework error to use a struct error_desc, and move ERR_SOCK/ERR_GNUTLS definitions to sock.h/sock_gnutls.h. error_desc definitions are still in error.c, though :(
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    58
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    59
/**
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    60
 * Initialize the socket module's global state. Call this before calling any other sock_* functions.
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    61
 *
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    62
 * The given \a ev_base is the libevent base to use for nonblocking operation.
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    63
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    64
 * @param ev_base the libevent base to use for events
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    65
 * @param err returned error info
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    66
 */
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    67
err_t sock_init (struct event_base *ev_base, struct error_info *err);
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    68
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    69
/**
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    70
 * A simple TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via *sock_ptr. 
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    71
 * In case of errors, additional error information is stored in *err.
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    72
 *
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    73
 * @param sock_ptr the new sock_stream
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    74
 * @param host the hostname to connect to
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    75
 * @param service the service name (i.e. port) to connect to
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    76
 * @param err returned error info
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
 */
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    78
err_t sock_tcp_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    80
/**
85
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    81
 * Start a non-blocking TCP connect to the given host/service. The socket will not yet be connected once the function
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    82
 * returns, but rather, the readyness of the socket will be indicated later using the given \a cb_func.
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
 * Note that currently it is an error to call sock_stream_event_init before the cb_func has been called.
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
 *
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    86
 * XXX: blocking DNS resolution
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
 *
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    88
 * @param sock_ptr the new sock_stream
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    89
 * @param host the hostname to connect to
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    90
 * @param service the service name (i.e. port) to connect to
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    91
 * @param cb_func the callback used to handle the result of the async operation
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    92
 * @param cb_arg opaque context argument passed back to cb_func
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
 * @param err returned error info
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    94
 */
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    95
err_t sock_tcp_connect_async (struct sock_stream **sock_ptr, const char *host, const char *service, 
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    96
        sock_stream_connect_cb cb_func, void *cb_arg, struct error_info *err);
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    97
75bc8b164ef8 async TCP connects,
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    98
/**
5
a09a0797f6f0 ERROR-ify sock_gnutls
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
    99
 * A simple blocking SSL connect to the given host/service. The connected/handshake'd SSL socket is returned via
a09a0797f6f0 ERROR-ify sock_gnutls
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   100
 * *sock_ptr. In case of errors, additional error information is stored in *err.
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
 *
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   102
 * @param sock_ptr the new sock_stream
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   103
 * @param host the hostname to connect to
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   104
 * @param service the TCP service name (i.e. port) to connect to
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   105
 * @param err returned error info
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   106
 *
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
 * XXX: blocking
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
 * XXX: doesn't do any certificate verification.
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
 */
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 12
diff changeset
   110
err_t sock_ssl_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   112
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   113
 * Read a series of bytes from the socket into the given \a buf (up to \a len bytes). If succesfull, this returns
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   114
 * the number of bytes read (which will be less than or equal to \a len). If the socket is nonblocking (i.e.
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   115
 * sock_stream_event_init() was set), and there is no data available, this returns zero, and one should use
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   116
 * sock_stream_event_enable() to wait for more data.
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   117
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   118
 * On errors, this returns the negative err_t code, and the specific error information can be accessed using
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   119
 * sock_stream_error()..
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   120
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   121
 * @param sock the socket to read the bytes from
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   122
 * @param buf the byte buffer to write the bytes into
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   123
 * @param len the number of bytes to read into the buffer
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   124
 * @return bytes read, zero if none available, -err_t
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   125
 */
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   126
int sock_stream_read (struct sock_stream *sock, void *buf, size_t len);
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   127
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   128
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   129
 * Write a series of bytes from the given \a buf (containing \a len bytes) to the socket. If succesfull, this returns
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   130
 * the number of bytes written (which may be less than \a len if the OS write buffer was full). If the socket is
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   131
 * nonblocking (i.e. sock_stream_event_init() was set), and the operation would have blocked, no data was written, and
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   132
 * this returns zero, and one should use sock_stream_event_enable() to retry.
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   133
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   134
 * On errors, this returns the negative err_t code, and the specific error information can be accessed using
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   135
 * sock_stream_error().
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   136
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   137
 * @param sock the socket to write the bytes to
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   138
 * @param buf the byte buffer
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   139
 * @param len number of bytes to write
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   140
 * @return bytes written, zero if would have blocked, -err_t
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   141
 */
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   142
int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len);
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   143
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   144
/**
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   145
 * Initialize event-based operation for this sock_stream. This will set the stream into nonblocking mode, and the given
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   146
 * callbacks will be fired once enabled using sock_stream_event_enable().
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   147
 *
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   148
 * Note that the callbacks struct isn't copied - it's used as-is-given.
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   149
 *
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   150
 * @param sock the socket to set up for nonblocking operation
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   151
 * @param callbacks the on_read/on_write callbacks to invoke
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   152
 * @param arg the context argument for the callbacks
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
 */
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   154
err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg);
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   155
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   156
/**
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   157
 * Enable some events for this sock, as set up earlier with event_init. Mask should contain EV_READ/EV_WRITE.
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   158
 *
12
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   159
 * The implementation of this is slightly hazy for complex protocols; this should only be used to map from
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   160
 * sock_stream_read/write to the corresponding sock_stream_callback. That is, if sock_stream_read returns zero, then
34
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   161
 * call event_enable(EV_READ), wherepon on_read will later be called. Other operations (such as calling
763f65f9df0c add doxygen.conf, and tweak comments
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   162
 * sock_stream_write with *different* data after it once returns zero) might result in errors.
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   163
 */
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   164
err_t sock_stream_event_enable (struct sock_stream *sock, short mask);
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   165
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   166
/**
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   167
 * Get current error_info for \a sock.
3
cc94ae754e2a error handling magic
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   168
 */
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   169
const struct error_info* sock_stream_error (struct sock_stream *sock);
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
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: 15
diff changeset
   171
/**
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: 15
diff changeset
   172
 * Close and release the given socket, ignoring errors. It must not be used anymore after this.
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: 15
diff changeset
   173
 *
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: 15
diff changeset
   174
 * This is intended to be used to abort in case of errors, and does not close the connection cleanly.
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: 15
diff changeset
   175
 */
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: 15
diff changeset
   176
void sock_stream_release (struct sock_stream *sock);
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: 15
diff changeset
   177
1
cf0e1bb6bcab a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
#endif