50 |
55 |
51 /** Lingering error on close() */ |
56 /** Lingering error on close() */ |
52 ERR_CLOSE, |
57 ERR_CLOSE, |
53 }; |
58 }; |
54 |
59 |
55 /* |
60 /** |
56 * Initialize the socket module's global state. Call this before calling any other sock_* functions. |
61 * Initialize the socket module's global state. Call this before calling any other sock_* functions. |
57 * |
62 * |
58 * The given \a ev_base is the libevent base to use for nonblocking operation. |
63 * The given \a ev_base is the libevent base to use for nonblocking operation. |
|
64 * |
|
65 * @param ev_base the libevent base to use for events |
|
66 * @param err returned error info |
59 */ |
67 */ |
60 err_t sock_init (struct event_base *ev_base, struct error_info *err); |
68 err_t sock_init (struct event_base *ev_base, struct error_info *err); |
61 |
69 |
62 /* |
70 /** |
63 * A simple blocking TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via |
71 * A simple blocking TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via |
64 * *sock_ptr. In case of errors, additional error information is stored in *err. |
72 * *sock_ptr. In case of errors, additional error information is stored in *err. |
65 * |
73 * |
66 * @return zero on success, nonzero on error |
74 * @param sock_ptr the new sock_stream |
|
75 * @param host the hostname to connect to |
|
76 * @param service the service name (i.e. port) to connect to |
|
77 * @param err returned error info |
67 * |
78 * |
68 * XXX: blocking |
79 * XXX: blocking |
69 */ |
80 */ |
70 err_t sock_tcp_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); |
81 err_t sock_tcp_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); |
71 |
82 |
72 /* |
83 /* |
73 * A simple blocking SSL connect to the given host/service. The connected/handshake'd SSL socket is returned via |
84 * A simple blocking SSL connect to the given host/service. The connected/handshake'd SSL socket is returned via |
74 * *sock_ptr. In case of errors, additional error information is stored in *err. |
85 * *sock_ptr. In case of errors, additional error information is stored in *err. |
75 * |
86 * |
|
87 * @param sock_ptr the new sock_stream |
|
88 * @param host the hostname to connect to |
|
89 * @param service the TCP service name (i.e. port) to connect to |
|
90 * @param err returned error info |
|
91 * |
76 * XXX: blocking |
92 * XXX: blocking |
77 * XXX: doesn't do any certificate verification. |
93 * XXX: doesn't do any certificate verification. |
78 */ |
94 */ |
79 err_t sock_ssl_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); |
95 err_t sock_ssl_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err); |
80 |
96 |
81 /* |
97 /** |
82 * The generic read/write API for stream sockets. These are mostly identical to the equivalent read/write syscalls, but |
98 * Read a series of bytes from the socket into the given \a buf (up to \a len bytes). If succesfull, this returns |
83 * the handling of EOF and EAGAIN is different. Normally, these return the (positive) number of bytes written. For |
99 * the number of bytes read (which will be less than or equal to \a len). If the socket is nonblocking (i.e. |
84 * EAGAIN, these return zero. For EOF, these return -ERR_READ_EOF/ERR_WRITE_EOF. Otherwise, these return the -ERR_* |
100 * sock_stream_event_init() was set), and there is no data available, this returns zero, and one should use |
85 * code. |
101 * sock_stream_event_enable() to wait for more data. |
|
102 * |
|
103 * On errors, this returns the negative err_t code, and the specific error information can be accessed using |
|
104 * sock_stream_error().. |
|
105 * |
|
106 * @param sock the socket to read the bytes from |
|
107 * @param buf the byte buffer to write the bytes into |
|
108 * @param len the number of bytes to read into the buffer |
|
109 * @return bytes read, zero if none available, -err_t |
86 */ |
110 */ |
87 int sock_stream_read (struct sock_stream *sock, void *buf, size_t len); |
111 int sock_stream_read (struct sock_stream *sock, void *buf, size_t len); |
|
112 |
|
113 /** |
|
114 * Write a series of bytes from the given \a buf (containing \a len bytes) to the socket. If succesfull, this returns |
|
115 * the number of bytes written (which may be less than \a len if the OS write buffer was full). If the socket is |
|
116 * nonblocking (i.e. sock_stream_event_init() was set), and the operation would have blocked, no data was written, and |
|
117 * this returns zero, and one should use sock_stream_event_enable() to retry. |
|
118 * |
|
119 * On errors, this returns the negative err_t code, and the specific error information can be accessed using |
|
120 * sock_stream_error(). |
|
121 * |
|
122 * @param sock the socket to write the bytes to |
|
123 * @param buf the byte buffer |
|
124 * @param len number of bytes to write |
|
125 * @return bytes written, zero if would have blocked, -err_t |
|
126 */ |
88 int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len); |
127 int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len); |
89 |
128 |
90 /* |
129 /** |
91 * Initialize event-based operation for this sock_stream. This will set the stream into nonblocking mode, and the given |
130 * Initialize event-based operation for this sock_stream. This will set the stream into nonblocking mode, and the given |
92 * callbacks will be fired once enabled using sock_stream_event_enable(). |
131 * callbacks will be fired once enabled using sock_stream_event_enable(). |
93 * |
132 * |
94 * Note that the callbacks struct isn't copied - it's used as-is-given. |
133 * Note that the callbacks struct isn't copied - it's used as-is-given. |
|
134 * |
|
135 * @param sock the socket to set up for nonblocking operation |
|
136 * @param callbacks the on_read/on_write callbacks to invoke |
|
137 * @param arg the context argument for the callbacks |
95 */ |
138 */ |
96 err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg); |
139 err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg); |
97 |
140 |
98 /* |
141 /** |
99 * Enable the events for this sock, as set up earlier with event_init. Mask should contain EV_READ/EV_WRITE. |
142 * Enable some events for this sock, as set up earlier with event_init. Mask should contain EV_READ/EV_WRITE. |
100 * |
143 * |
101 * The implementation of this is slightly hazy for complex protocols; this should only be used to map from |
144 * The implementation of this is slightly hazy for complex protocols; this should only be used to map from |
102 * sock_stream_read/write to the corresponding sock_stream_callback. That is, if sock_stream_read returns zero, then |
145 * sock_stream_read/write to the corresponding sock_stream_callback. That is, if sock_stream_read returns zero, then |
103 * call event_enable(EV_READ), wherepon on_read will later be called. |
146 * call event_enable(EV_READ), wherepon on_read will later be called. Other operations (such as calling |
|
147 * sock_stream_write with *different* data after it once returns zero) might result in errors. |
104 */ |
148 */ |
105 err_t sock_stream_event_enable (struct sock_stream *sock, short mask); |
149 err_t sock_stream_event_enable (struct sock_stream *sock, short mask); |
106 |
150 |
107 /** |
151 /** |
108 * Get current error_info for \a sock. |
152 * Get current error_info for \a sock. |