2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
24 * @file include/gnunet_connection_lib.h
25 * Basic, low-level TCP networking interface
27 * @defgroup connection Connection library
28 * Basic, low-level TCP networking interface
31 #ifndef GNUNET_CONNECTION_LIB_H
32 #define GNUNET_CONNECTION_LIB_H
37 #if 0 /* keep Emacsens' auto-indent happy */
42 #include "gnunet_network_lib.h"
43 #include "gnunet_scheduler_lib.h"
44 #include "gnunet_time_lib.h"
47 * Timeout we use on TCP connect before trying another
48 * result from the DNS resolver. Actual value used
49 * is this value divided by the number of address families.
52 #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
55 * @brief handle for a network connection
57 struct GNUNET_CONNECTION_Handle;
61 * Credentials for UNIX domain sockets.
63 struct GNUNET_CONNECTION_Credentials
66 * UID of the other end of the connection.
71 * GID of the other end of the connection.
78 * Function to call for access control checks.
81 * @param ucred credentials, if available, otherwise NULL
83 * @param addrlen length of address
84 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
85 * for unknown address family (will be denied).
87 typedef int (*GNUNET_CONNECTION_AccessCheck) (void *cls,
89 GNUNET_CONNECTION_Credentials *
91 const struct sockaddr * addr,
96 * Callback function for data received from the network. Note that
97 * both "available" and "err" would be 0 if the read simply timed out.
100 * @param buf pointer to received data
101 * @param available number of bytes availabe in "buf",
102 * possibly 0 (on errors)
103 * @param addr address of the sender
104 * @param addrlen size of addr
105 * @param errCode value of errno (on errors receiving)
107 typedef void (*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
109 const struct sockaddr * addr,
110 socklen_t addrlen, int errCode);
113 * Set the persist option on this connection handle. Indicates
114 * that the underlying socket or fd should never really be closed.
115 * Used for indicating process death.
117 * @param connection the connection to set persistent
120 GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *connection);
123 * Disable the "CORK" feature for communication with the given socket,
124 * forcing the OS to immediately flush the buffer on transmission
125 * instead of potentially buffering multiple messages. Essentially
126 * reduces the OS send buffers to zero.
127 * Used to make sure that the last messages sent through the connection
128 * reach the other side before the process is terminated.
130 * @param connection the connection to make flushing and blocking
131 * @return #GNUNET_OK on success
134 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *connection);
138 * Create a connection handle by (asynchronously) connecting to a host.
139 * This function returns immediately, even if the connection has not
140 * yet been established. This function only creates TCP connections.
142 * @param s socket to connect
143 * @param serv_addr server address
144 * @param addrlen length of server address
145 * @return the connection handle
147 struct GNUNET_CONNECTION_Handle *
148 GNUNET_CONNECTION_connect_socket (struct GNUNET_NETWORK_Handle *s,
149 const struct sockaddr *serv_addr,
154 * Create a connection handle by boxing an existing OS socket. The OS
155 * socket should henceforth be no longer used directly.
156 * #GNUNET_CONNECTION_destroy() will close it.
158 * @param osSocket existing socket to box
159 * @return the boxed socket handle
161 struct GNUNET_CONNECTION_Handle *
162 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket);
166 * Create a connection handle by accepting on a listen socket. This
167 * function may block if the listen socket has no connection ready.
169 * @param access_cb function to use to check if access is allowed
170 * @param access_cb_cls closure for @a access_cb
171 * @param lsock listen socket
172 * @return the connection handle, NULL on error (for example, access refused)
174 struct GNUNET_CONNECTION_Handle *
175 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access_cb,
177 struct GNUNET_NETWORK_Handle *lsock);
181 * Create a connection handle by (asynchronously) connecting to a host.
182 * This function returns immediately, even if the connection has not
183 * yet been established. This function only creates TCP connections.
185 * @param cfg configuration to use
186 * @param hostname name of the host to connect to
187 * @param port port to connect to
188 * @return the connection handle
190 struct GNUNET_CONNECTION_Handle *
191 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
192 *cfg, const char *hostname,
197 * Create a connection handle by connecting to a UNIX domain service.
198 * This function returns immediately, even if the connection has not
199 * yet been established. This function only creates UNIX connections.
201 * @param cfg configuration to use
202 * @param unixpath path to connect to)
203 * @return the connection handle, NULL on systems without UNIX support
205 struct GNUNET_CONNECTION_Handle *
206 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
207 GNUNET_CONFIGURATION_Handle
208 *cfg, const char *unixpath);
214 * Create a connection handle by (asynchronously) connecting to a host.
215 * This function returns immediately, even if the connection has not
216 * yet been established. This function only creates TCP connections.
218 * @param af_family address family to use
219 * @param serv_addr server address
220 * @param addrlen length of server address
221 * @return the connection handle
223 struct GNUNET_CONNECTION_Handle *
224 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
225 const struct sockaddr *serv_addr,
229 * Check if connection is valid (no fatal errors have happened so far).
230 * Note that a connection that is still trying to connect is considered
233 * @param connection handle to check
234 * @return GNUNET_YES if valid, GNUNET_NO otherwise
237 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection);
241 * Obtain the network address of the other party.
243 * @param connection the client to get the address for
244 * @param addr where to store the address
245 * @param addrlen where to store the length of the address
246 * @return GNUNET_OK on success
249 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
250 void **addr, size_t * addrlen);
254 * Close the connection and free associated resources. There must
255 * not be any pending requests for reading or writing to the
256 * connection at this time.
258 * @param connection connection to destroy
261 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
265 * Receive data from the given connection. Note that this function will
266 * call "receiver" asynchronously using the scheduler. It will
267 * "immediately" return. Note that there MUST only be one active
268 * receive call per connection at any given point in time (so do not
269 * call receive again until the receiver callback has been invoked).
271 * @param connection connection handle
272 * @param max maximum number of bytes to read
273 * @param timeout maximum amount of time to wait
274 * @param receiver function to call with received data
275 * @param receiver_cls closure for receiver
278 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t max,
279 struct GNUNET_TIME_Relative timeout,
280 GNUNET_CONNECTION_Receiver receiver,
285 * Cancel receive job on the given connection. Note that the
286 * receiver callback must not have been called yet in order
287 * for the cancellation to be valid.
289 * @param connection connection handle
290 * @return closure of the original receiver callback closure
293 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection);
297 * Function called to notify a client about the connection begin ready
298 * to queue more data. @a buf will be NULL and @a size zero if the
299 * connection was closed for writing in the meantime.
302 * @param size number of bytes available in @a buf
303 * @param buf where the callee should write the message
304 * @return number of bytes written to @a buf
307 (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
313 * Opaque handle that can be used to cancel
314 * a transmit-ready notification.
316 struct GNUNET_CONNECTION_TransmitHandle;
319 * Ask the connection to call us once the specified number of bytes
320 * are free in the transmission buffer. Will never call the @a notify
321 * callback in this task, but always first go into the scheduler. Note that
322 * this function will abort if "size" is greater than
323 * #GNUNET_SERVER_MAX_MESSAGE_SIZE.
325 * Note that "notify" will be called either when enough
326 * buffer space is available OR when the connection is destroyed.
327 * The size parameter given to notify is guaranteed to be
328 * larger or equal to size if the buffer is ready, or zero
329 * if the connection was destroyed (or at least closed for
330 * writing). Finally, any time before 'notify' is called, a
331 * client may call "notify_transmit_ready_cancel" to cancel
332 * the transmission request.
334 * Only one transmission request can be scheduled at the same
335 * time. Notify will be run with the same scheduler priority
336 * as that of the caller.
338 * @param connection connection
339 * @param size number of bytes to send
340 * @param timeout after how long should we give up (and call
341 * notify with buf NULL and size 0)?
342 * @param notify function to call when buffer space is available
343 * @param notify_cls closure for notify
344 * @return non-NULL if the notify callback was queued,
345 * NULL if we are already going to notify someone else (busy)
347 struct GNUNET_CONNECTION_TransmitHandle *
348 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connection,
350 struct GNUNET_TIME_Relative timeout,
351 GNUNET_CONNECTION_TransmitReadyNotify
352 notify, void *notify_cls);
356 * Cancel the specified transmission-ready
359 * @param th handle for notification to cancel
362 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
363 GNUNET_CONNECTION_TransmitHandle
368 * Create a connection to be proxied using a given connection.
370 * @param cph connection to proxy server
371 * @return connection to be proxied
373 struct GNUNET_CONNECTION_Handle *
374 GNUNET_CONNECTION_create_proxied_from_handshake (struct GNUNET_CONNECTION_Handle *cph);
378 * Activate proxied connection and destroy initial proxy handshake connection.
379 * There must not be any pending requests for reading or writing to the
380 * proxy hadshake connection at this time.
382 * @param proxied connection connection to proxy server
385 GNUNET_CONNECTION_acivate_proxied (struct GNUNET_CONNECTION_Handle *proxied);
388 #if 0 /* keep Emacsens' auto-indent happy */
395 /* ifndef GNUNET_CONNECTION_LIB_H */
398 /** @} */ /* end of group */
400 /* end of gnunet_connection_lib.h */