2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_connection_lib.h
23 * @brief basic, low-level TCP networking interface
24 * @author Christian Grothoff
26 #ifndef GNUNET_CONNECTION_LIB_H
27 #define GNUNET_CONNECTION_LIB_H
32 #if 0 /* keep Emacsens' auto-indent happy */
37 #include "gnunet_network_lib.h"
38 #include "gnunet_scheduler_lib.h"
39 #include "gnunet_time_lib.h"
42 * Timeout we use on TCP connect before trying another
43 * result from the DNS resolver. Actual value used
44 * is this value divided by the number of address families.
47 #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50 * @brief handle for a network connection
52 struct GNUNET_CONNECTION_Handle;
56 * Credentials for UNIX domain sockets.
58 struct GNUNET_CONNECTION_Credentials
61 * UID of the other end of the connection.
66 * GID of the other end of the connection.
73 * Function to call for access control checks.
76 * @param ucred credentials, if available, otherwise NULL
78 * @param addrlen length of address
79 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
80 * for unknown address family (will be denied).
82 typedef int (*GNUNET_CONNECTION_AccessCheck) (void *cls,
84 GNUNET_CONNECTION_Credentials *
86 const struct sockaddr * addr,
91 * Callback function for data received from the network. Note that
92 * both "available" and "err" would be 0 if the read simply timed out.
95 * @param buf pointer to received data
96 * @param available number of bytes availabe in "buf",
97 * possibly 0 (on errors)
98 * @param addr address of the sender
99 * @param addrlen size of addr
100 * @param errCode value of errno (on errors receiving)
102 typedef void (*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
104 const struct sockaddr * addr,
105 socklen_t addrlen, int errCode);
108 * Set the persist option on this connection handle. Indicates
109 * that the underlying socket or fd should never really be closed.
110 * Used for indicating process death.
112 * @param connection the connection to set persistent
115 GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *connection);
118 * Disable the "CORK" feature for communication with the given socket,
119 * forcing the OS to immediately flush the buffer on transmission
120 * instead of potentially buffering multiple messages. Essentially
121 * reduces the OS send buffers to zero.
122 * Used to make sure that the last messages sent through the connection
123 * reach the other side before the process is terminated.
125 * @param connection the connection to make flushing and blocking
126 * @return GNUNET_OK on success
129 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *connection);
133 * Create a connection handle by boxing an existing OS socket. The OS
134 * socket should henceforth be no longer used directly.
135 * GNUNET_CONNECTION_destroy will close it.
137 * @param osSocket existing socket to box
138 * @return the boxed socket handle
140 struct GNUNET_CONNECTION_Handle *
141 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket);
145 * Create a connection handle by accepting on a listen socket. This
146 * function may block if the listen socket has no connection ready.
148 * @param access function to use to check if access is allowed
149 * @param access_cls closure for access
150 * @param lsock listen socket
151 * @return the connection handle, NULL on error (for example, access refused)
153 struct GNUNET_CONNECTION_Handle *
154 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
156 struct GNUNET_NETWORK_Handle *lsock);
160 * Create a connection handle by (asynchronously) connecting to a host.
161 * This function returns immediately, even if the connection has not
162 * yet been established. This function only creates TCP connections.
164 * @param cfg configuration to use
165 * @param hostname name of the host to connect to
166 * @param port port to connect to
167 * @return the connection handle
169 struct GNUNET_CONNECTION_Handle *
170 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
171 *cfg, const char *hostname,
176 * Create a connection handle by connecting to a UNIX domain service.
177 * This function returns immediately, even if the connection has not
178 * yet been established. This function only creates UNIX connections.
180 * @param cfg configuration to use
181 * @param unixpath path to connect to)
182 * @return the connection handle, NULL on systems without UNIX support
184 struct GNUNET_CONNECTION_Handle *
185 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
186 GNUNET_CONFIGURATION_Handle
187 *cfg, const char *unixpath);
193 * Create a connection handle by (asynchronously) connecting to a host.
194 * This function returns immediately, even if the connection has not
195 * yet been established. This function only creates TCP connections.
197 * @param af_family address family to use
198 * @param serv_addr server address
199 * @param addrlen length of server address
200 * @return the connection handle
202 struct GNUNET_CONNECTION_Handle *
203 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
204 const struct sockaddr *serv_addr,
208 * Check if connection is valid (no fatal errors have happened so far).
209 * Note that a connection that is still trying to connect is considered
212 * @param connection handle to check
213 * @return GNUNET_YES if valid, GNUNET_NO otherwise
216 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection);
220 * Obtain the network address of the other party.
222 * @param connection the client to get the address for
223 * @param addr where to store the address
224 * @param addrlen where to store the length of the address
225 * @return GNUNET_OK on success
228 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
229 void **addr, size_t * addrlen);
233 * Close the connection and free associated resources. There must
234 * not be any pending requests for reading or writing to the
235 * connection at this time.
237 * @param connection connection to destroy
240 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
244 * Receive data from the given connection. Note that this function will
245 * call "receiver" asynchronously using the scheduler. It will
246 * "immediately" return. Note that there MUST only be one active
247 * receive call per connection at any given point in time (so do not
248 * call receive again until the receiver callback has been invoked).
250 * @param connection connection handle
251 * @param max maximum number of bytes to read
252 * @param timeout maximum amount of time to wait
253 * @param receiver function to call with received data
254 * @param receiver_cls closure for receiver
257 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t max,
258 struct GNUNET_TIME_Relative timeout,
259 GNUNET_CONNECTION_Receiver receiver,
264 * Cancel receive job on the given connection. Note that the
265 * receiver callback must not have been called yet in order
266 * for the cancellation to be valid.
268 * @param connection connection handle
269 * @return closure of the original receiver callback closure
272 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection);
276 * Function called to notify a client about the connection begin ready
277 * to queue more data. @a buf will be NULL and @a size zero if the
278 * connection was closed for writing in the meantime.
281 * @param size number of bytes available in @a buf
282 * @param buf where the callee should write the message
283 * @return number of bytes written to @a buf
286 (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
292 * Opaque handle that can be used to cancel
293 * a transmit-ready notification.
295 struct GNUNET_CONNECTION_TransmitHandle;
298 * Ask the connection to call us once the specified number of bytes
299 * are free in the transmission buffer. May call the notify
300 * method immediately if enough space is available. Note that
301 * this function will abort if "size" is greater than
302 * #GNUNET_SERVER_MAX_MESSAGE_SIZE.
304 * Note that "notify" will be called either when enough
305 * buffer space is available OR when the connection is destroyed.
306 * The size parameter given to notify is guaranteed to be
307 * larger or equal to size if the buffer is ready, or zero
308 * if the connection was destroyed (or at least closed for
309 * writing). Finally, any time before 'notify' is called, a
310 * client may call "notify_transmit_ready_cancel" to cancel
311 * the transmission request.
313 * Only one transmission request can be scheduled at the same
314 * time. Notify will be run with the same scheduler priority
315 * as that of the caller.
317 * @param connection connection
318 * @param size number of bytes to send
319 * @param timeout after how long should we give up (and call
320 * notify with buf NULL and size 0)?
321 * @param notify function to call when buffer space is available
322 * @param notify_cls closure for notify
323 * @return non-NULL if the notify callback was queued,
324 * NULL if we are already going to notify someone else (busy)
326 struct GNUNET_CONNECTION_TransmitHandle *
327 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connection,
329 struct GNUNET_TIME_Relative timeout,
330 GNUNET_CONNECTION_TransmitReadyNotify
331 notify, void *notify_cls);
335 * Cancel the specified transmission-ready
338 * @param th handle for notification to cancel
341 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
342 GNUNET_CONNECTION_TransmitHandle
347 #if 0 /* keep Emacsens' auto-indent happy */
355 /* ifndef GNUNET_CONNECTION_LIB_H */
357 /* end of gnunet_connection_lib.h */