terom@1: #ifndef SOCK_H terom@1: #define SOCK_H terom@1: terom@1: /* terom@1: * Low-level socket-related functions terom@1: */ terom@3: #include "error.h" terom@1: #include terom@9: #include terom@1: terom@1: /* terom@1: * The generic socket handle terom@1: */ terom@1: struct sock_stream; terom@1: terom@1: /* terom@9: * Async callbacks for socket operation terom@9: */ terom@9: struct sock_stream_callbacks { terom@9: /* Sockeet is readable */ terom@9: void (*on_read)(struct sock_stream *sock, void *arg); terom@9: terom@9: /* Socket is writeable */ terom@9: void (*on_write)(struct sock_stream *sock, void *arg); terom@9: }; terom@9: terom@9: /* terom@3: * Initialize the socket module's global state. Call this before calling any other sock_* functions. terom@9: * terom@9: * The given \a ev_base is the libevent base to use for nonblocking operation. terom@3: */ terom@9: err_t sock_init (struct event_base *ev_base, struct error_info *err); terom@3: terom@3: /* terom@3: * A simple blocking TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via terom@5: * *sock_ptr. In case of errors, additional error information is stored in *err. terom@3: * terom@3: * @return zero on success, nonzero on error terom@1: * terom@1: * XXX: blocking terom@1: */ terom@3: err_t sock_tcp_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); terom@1: terom@1: /* terom@5: * A simple blocking SSL connect to the given host/service. The connected/handshake'd SSL socket is returned via terom@5: * *sock_ptr. In case of errors, additional error information is stored in *err. terom@1: * terom@1: * XXX: blocking terom@1: * XXX: doesn't do any certificate verification. terom@1: */ terom@15: err_t sock_ssl_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); terom@1: terom@1: /* terom@12: * The generic read/write API for stream sockets. These are mostly identical to the equivalent read/write syscalls, but terom@12: * the handling of EOF and EAGAIN is different. Normally, these return the (positive) number of bytes written. For terom@12: * EAGAIN, these return zero. For EOF, these return -ERR_READ_EOF/ERR_WRITE_EOF. Otherwise, these return the -ERR_* terom@12: * code. terom@9: */ terom@10: int sock_stream_read (struct sock_stream *sock, void *buf, size_t len); terom@10: int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len); terom@9: terom@9: /* terom@10: * Initialize event-based operation for this sock_stream. This will set the stream into nonblocking mode, and the given terom@10: * callbacks will be fired once enabled using sock_stream_event_enable(). terom@10: * terom@10: * Note that the callbacks struct isn't copied - it's used as-is-given. terom@1: */ terom@10: err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg); terom@10: terom@10: /* terom@12: * Enable the events for this sock, as set up earlier with event_init. Mask should contain EV_READ/EV_WRITE. terom@10: * terom@12: * The implementation of this is slightly hazy for complex protocols; this should only be used to map from terom@12: * sock_stream_read/write to the corresponding sock_stream_callback. That is, if sock_stream_read returns zero, then terom@12: * call event_enable(EV_READ), wherepon on_read will later be called. terom@10: */ terom@10: err_t sock_stream_event_enable (struct sock_stream *sock, short mask); terom@3: terom@3: /** terom@8: * Get current error_info for \a sock. terom@3: */ terom@8: const struct error_info* sock_stream_error (struct sock_stream *sock); terom@1: terom@1: #endif