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 2, 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,
83 const struct GNUNET_CONNECTION_Credentials *ucred,
84 const struct sockaddr * addr,
89 * Callback function for data received from the network. Note that
90 * both "available" and "err" would be 0 if the read simply timed out.
93 * @param buf pointer to received data
94 * @param available number of bytes availabe in "buf",
95 * possibly 0 (on errors)
96 * @param addr address of the sender
97 * @param addrlen size of addr
98 * @param errCode value of errno (on errors receiving)
100 typedef void (*GNUNET_CONNECTION_Receiver) (void *cls,
103 const struct sockaddr * addr,
104 socklen_t addrlen, int errCode);
107 * Set the persist option on this connection handle. Indicates
108 * that the underlying socket or fd should never really be closed.
109 * Used for indicating process death.
111 * @param sock the connection to set persistent
114 GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock);
117 * Disable the "CORK" feature for communication with the given socket,
118 * forcing the OS to immediately flush the buffer on transmission
119 * instead of potentially buffering multiple messages. Essentially
120 * reduces the OS send buffers to zero.
121 * Used to make sure that the last messages sent through the connection
122 * reach the other side before the process is terminated.
124 * @param sock the connection to make flushing and blocking
125 * @return GNUNET_OK on success
128 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock);
132 * Create a socket handle by boxing an existing OS socket. The OS
133 * socket should henceforth be no longer used directly.
134 * GNUNET_socket_destroy will close it.
136 * @param osSocket existing socket to box
137 * @return the boxed socket handle
139 struct GNUNET_CONNECTION_Handle
140 *GNUNET_CONNECTION_create_from_existing (struct
141 GNUNET_NETWORK_Handle
146 * Create a socket handle by accepting on a listen socket. This
147 * function may block if the listen socket has no connection ready.
149 * @param access function to use to check if access is allowed
150 * @param access_cls closure for access
151 * @param lsock listen socket
152 * @return the socket handle, NULL on error (for example, access refused)
154 struct GNUNET_CONNECTION_Handle
155 *GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck
156 access, void *access_cls,
158 GNUNET_NETWORK_Handle
163 * Create a socket handle by (asynchronously) connecting to a host.
164 * This function returns immediately, even if the connection has not
165 * yet been established. This function only creates TCP connections.
167 * @param cfg configuration to use
168 * @param hostname name of the host to connect to
169 * @param port port to connect to
170 * @return the socket handle
172 struct GNUNET_CONNECTION_Handle
173 *GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
174 const char *hostname,
179 * Create a socket handle by connecting to a UNIX domain service.
180 * This function returns immediately, even if the connection has not
181 * yet been established. This function only creates UNIX connections.
183 * @param cfg configuration to use
184 * @param unixpath path to connect to)
185 * @return the socket handle, NULL on systems without UNIX support
187 struct GNUNET_CONNECTION_Handle *
188 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
189 GNUNET_CONFIGURATION_Handle *cfg,
190 const char *unixpath);
196 * Create a socket handle by (asynchronously) connecting to a host.
197 * This function returns immediately, even if the connection has not
198 * yet been established. This function only creates TCP connections.
200 * @param af_family address family to use
201 * @param serv_addr server address
202 * @param addrlen length of server address
203 * @return the socket handle
205 struct GNUNET_CONNECTION_Handle
206 *GNUNET_CONNECTION_create_from_sockaddr (int af_family,
207 const struct sockaddr
212 * Check if socket is valid (no fatal errors have happened so far).
213 * Note that a socket that is still trying to connect is considered
216 * @param sock socket to check
217 * @return GNUNET_YES if valid, GNUNET_NO otherwise
219 int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle
224 * Obtain the network address of the other party.
226 * @param sock the client to get the address for
227 * @param addr where to store the address
228 * @param addrlen where to store the length of the address
229 * @return GNUNET_OK on success
231 int GNUNET_CONNECTION_get_address (struct
232 GNUNET_CONNECTION_Handle
238 * Close the socket and free associated resources. Pending
239 * transmissions may be completed or dropped depending on the
240 * arguments. If a receive call is pending and should
241 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
242 * should be called explicitly first.
244 * @param sock socket to destroy
245 * @param finish_pending_write should pending writes be completed or aborted?
246 * (this applies to transmissions where the data has already been
247 * read from the application; all other transmissions should be
248 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
251 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
252 int finish_pending_write);
256 * Receive data from the given socket. Note that this function will
257 * call "receiver" asynchronously using the scheduler. It will
258 * "immediately" return. Note that there MUST only be one active
259 * receive call per socket at any given point in time (so do not
260 * call receive again until the receiver callback has been invoked).
262 * @param sock socket handle
263 * @param max maximum number of bytes to read
264 * @param timeout maximum amount of time to wait
265 * @param receiver function to call with received data
266 * @param receiver_cls closure for receiver
269 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle
271 struct GNUNET_TIME_Relative timeout,
272 GNUNET_CONNECTION_Receiver receiver,
277 * Cancel receive job on the given socket. Note that the
278 * receiver callback must not have been called yet in order
279 * for the cancellation to be valid.
281 * @param sock socket handle
282 * @return closure of the original receiver callback closure
284 void *GNUNET_CONNECTION_receive_cancel (struct
285 GNUNET_CONNECTION_Handle
290 * Function called to notify a client about the socket
291 * begin ready to queue more data. "buf" will be
292 * NULL and "size" zero if the socket was closed for
293 * writing in the meantime.
296 * @param size number of bytes available in buf
297 * @param buf where the callee should write the message
298 * @return number of bytes written to buf
300 typedef size_t (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
301 size_t size, void *buf);
305 * Opaque handle that can be used to cancel
306 * a transmit-ready notification.
308 struct GNUNET_CONNECTION_TransmitHandle;
311 * Ask the socket to call us once the specified number of bytes
312 * are free in the transmission buffer. May call the notify
313 * method immediately if enough space is available. Note that
314 * this function will abort if "size" is greater than
315 * GNUNET_SERVER_MAX_MESSAGE_SIZE.
317 * Note that "notify" will be called either when enough
318 * buffer space is available OR when the socket is destroyed.
319 * The size parameter given to notify is guaranteed to be
320 * larger or equal to size if the buffer is ready, or zero
321 * if the socket was destroyed (or at least closed for
322 * writing). Finally, any time before 'notify' is called, a
323 * client may call "notify_transmit_ready_cancel" to cancel
324 * the transmission request.
326 * Only one transmission request can be scheduled at the same
327 * time. Notify will be run with the same scheduler priority
328 * as that of the caller.
331 * @param size number of bytes to send
332 * @param timeout after how long should we give up (and call
333 * notify with buf NULL and size 0)?
334 * @param notify function to call when buffer space is available
335 * @param notify_cls closure for notify
336 * @return non-NULL if the notify callback was queued,
337 * NULL if we are already going to notify someone else (busy)
339 struct GNUNET_CONNECTION_TransmitHandle
340 *GNUNET_CONNECTION_notify_transmit_ready (struct
341 GNUNET_CONNECTION_Handle
346 GNUNET_CONNECTION_TransmitReadyNotify
347 notify, void *notify_cls);
351 * Cancel the specified transmission-ready
354 * @param h handle for notification to cancel
357 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
358 GNUNET_CONNECTION_TransmitHandle
363 * Configure this connection to ignore shutdown signals.
365 * @param sock socket handle
366 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
369 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
373 #if 0 /* keep Emacsens' auto-indent happy */
381 /* ifndef GNUNET_CONNECTION_LIB_H */
383 /* end of gnunet_connection_lib.h */