src/sock.h
author Tero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 10:16:28 +0200
changeset 10 9fe218576d13
parent 9 4c4c906cc649
child 12 4147fae232d9
permissions -rw-r--r--
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
#ifndef SOCK_H
#define SOCK_H

/*
 * Low-level socket-related functions
 */
#include "error.h"
#include <sys/types.h>
#include <event2/event.h>

/*
 * The generic socket handle
 */
struct sock_stream;

/*
 * Async callbacks for socket operation
 */
struct sock_stream_callbacks {
    /* Sockeet is readable */
    void (*on_read)(struct sock_stream *sock, void *arg);

    /* Socket is writeable */
    void (*on_write)(struct sock_stream *sock, void *arg);
};

/*
 * Initialize the socket module's global state. Call this before calling any other sock_* functions.
 *
 * The given \a ev_base is the libevent base to use for nonblocking operation.
 */
err_t sock_init (struct event_base *ev_base, struct error_info *err);

/*
 * A simple blocking TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via
 * *sock_ptr. In case of errors, additional error information is stored in *err.
 *
 * @return zero on success, nonzero on error
 *
 * XXX: blocking
 */
err_t sock_tcp_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);

/*
 * A simple blocking SSL connect to the given host/service. The connected/handshake'd SSL socket is returned via
 * *sock_ptr. In case of errors, additional error information is stored in *err.
 *
 * XXX: blocking
 * XXX: doesn't do any certificate verification.
 */
err_t sock_gnutls_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);

/*
 * The generic read/write API for stream sockets.
 */
int sock_stream_read (struct sock_stream *sock, void *buf, size_t len);
int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len);

/*
 * Initialize event-based operation for this sock_stream. This will set the stream into nonblocking mode, and the given
 * callbacks will be fired once enabled using sock_stream_event_enable().
 *
 * Note that the callbacks struct isn't copied - it's used as-is-given.
 */
err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg);

/*
 * Prime the callbacks set up earlier with sock_stream_event_init to fire once ready.
 *
 * \a mask is a bitmask of EV_* bits, such as EV_READ, EV_WRITE or EV_PERSIST. See event_set() for more info about the
 * behaviour of those.
 */
err_t sock_stream_event_enable (struct sock_stream *sock, short mask);

/**
 * Get current error_info for \a sock.
 */
const struct error_info* sock_stream_error (struct sock_stream *sock);

#endif