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 * Function to call for access control checks.
60 * @param addrlen length of address
61 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
62 * for unknown address family (will be denied).
64 typedef int (*GNUNET_CONNECTION_AccessCheck) (void *cls,
65 const struct sockaddr * addr,
70 * Callback function for data received from the network. Note that
71 * both "available" and "err" would be 0 if the read simply timed out.
74 * @param buf pointer to received data
75 * @param available number of bytes availabe in "buf",
76 * possibly 0 (on errors)
77 * @param addr address of the sender
78 * @param addrlen size of addr
79 * @param errCode value of errno (on errors receiving)
81 typedef void (*GNUNET_CONNECTION_Receiver) (void *cls,
84 const struct sockaddr * addr,
85 socklen_t addrlen, int errCode);
88 * Set the persist option on this connection handle. Indicates
89 * that the underlying socket or fd should never really be closed.
90 * Used for indicating process death.
92 * @param sock the connection to set persistent
95 GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock);
98 * Create a socket handle by boxing an existing OS socket. The OS
99 * socket should henceforth be no longer used directly.
100 * GNUNET_socket_destroy will close it.
102 * @param osSocket existing socket to box
103 * @return the boxed socket handle
105 struct GNUNET_CONNECTION_Handle
106 *GNUNET_CONNECTION_create_from_existing (struct
107 GNUNET_NETWORK_Handle
112 * Create a socket handle by accepting on a listen socket. This
113 * function may block if the listen socket has no connection ready.
115 * @param access function to use to check if access is allowed
116 * @param access_cls closure for access
117 * @param lsock listen socket
118 * @return the socket handle, NULL on error (for example, access refused)
120 struct GNUNET_CONNECTION_Handle
121 *GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck
122 access, void *access_cls,
124 GNUNET_NETWORK_Handle
129 * Create a socket handle by (asynchronously) connecting to a host.
130 * This function returns immediately, even if the connection has not
131 * yet been established. This function only creates TCP connections.
133 * @param cfg configuration to use
134 * @param hostname name of the host to connect to
135 * @param port port to connect to
136 * @return the socket handle
138 struct GNUNET_CONNECTION_Handle
139 *GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
140 const char *hostname,
145 * Create a socket handle by connecting to a UNIX domain service.
146 * This function returns immediately, even if the connection has not
147 * yet been established. This function only creates UNIX connections.
149 * @param cfg configuration to use
150 * @param unixpath path to connect to)
151 * @return the socket handle, NULL on systems without UNIX support
153 struct GNUNET_CONNECTION_Handle *
154 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
155 GNUNET_CONFIGURATION_Handle *cfg,
156 const char *unixpath);
162 * Create a socket handle by (asynchronously) connecting to a host.
163 * This function returns immediately, even if the connection has not
164 * yet been established. This function only creates TCP connections.
166 * @param af_family address family to use
167 * @param serv_addr server address
168 * @param addrlen length of server address
169 * @return the socket handle
171 struct GNUNET_CONNECTION_Handle
172 *GNUNET_CONNECTION_create_from_sockaddr (int af_family,
173 const struct sockaddr
178 * Check if socket is valid (no fatal errors have happened so far).
179 * Note that a socket that is still trying to connect is considered
182 * @param sock socket to check
183 * @return GNUNET_YES if valid, GNUNET_NO otherwise
185 int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle
190 * Obtain the network address of the other party.
192 * @param sock the client to get the address for
193 * @param addr where to store the address
194 * @param addrlen where to store the length of the address
195 * @return GNUNET_OK on success
197 int GNUNET_CONNECTION_get_address (struct
198 GNUNET_CONNECTION_Handle
204 * Close the socket and free associated resources. Pending
205 * transmissions may be completed or dropped depending on the
206 * arguments. If a receive call is pending and should
207 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
208 * should be called explicitly first.
210 * @param sock socket to destroy
211 * @param finish_pending_write should pending writes be completed or aborted?
212 * (this applies to transmissions where the data has already been
213 * read from the application; all other transmissions should be
214 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
217 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
218 int finish_pending_write);
222 * Receive data from the given socket. Note that this function will
223 * call "receiver" asynchronously using the scheduler. It will
224 * "immediately" return. Note that there MUST only be one active
225 * receive call per socket at any given point in time (so do not
226 * call receive again until the receiver callback has been invoked).
228 * @param sock socket handle
229 * @param max maximum number of bytes to read
230 * @param timeout maximum amount of time to wait
231 * @param receiver function to call with received data
232 * @param receiver_cls closure for receiver
235 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle
237 struct GNUNET_TIME_Relative timeout,
238 GNUNET_CONNECTION_Receiver receiver,
243 * Cancel receive job on the given socket. Note that the
244 * receiver callback must not have been called yet in order
245 * for the cancellation to be valid.
247 * @param sock socket handle
248 * @return closure of the original receiver callback closure
250 void *GNUNET_CONNECTION_receive_cancel (struct
251 GNUNET_CONNECTION_Handle
256 * Function called to notify a client about the socket
257 * begin ready to queue more data. "buf" will be
258 * NULL and "size" zero if the socket was closed for
259 * writing in the meantime.
262 * @param size number of bytes available in buf
263 * @param buf where the callee should write the message
264 * @return number of bytes written to buf
266 typedef size_t (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
267 size_t size, void *buf);
271 * Opaque handle that can be used to cancel
272 * a transmit-ready notification.
274 struct GNUNET_CONNECTION_TransmitHandle;
277 * Ask the socket to call us once the specified number of bytes
278 * are free in the transmission buffer. May call the notify
279 * method immediately if enough space is available. Note that
280 * this function will abort if "size" is greater than
281 * GNUNET_SERVER_MAX_MESSAGE_SIZE.
283 * Note that "notify" will be called either when enough
284 * buffer space is available OR when the socket is destroyed.
285 * The size parameter given to notify is guaranteed to be
286 * larger or equal to size if the buffer is ready, or zero
287 * if the socket was destroyed (or at least closed for
288 * writing). Finally, any time before 'notify' is called, a
289 * client may call "notify_transmit_ready_cancel" to cancel
290 * the transmission request.
292 * Only one transmission request can be scheduled at the same
293 * time. Notify will be run with the same scheduler priority
294 * as that of the caller.
297 * @param size number of bytes to send
298 * @param timeout after how long should we give up (and call
299 * notify with buf NULL and size 0)?
300 * @param notify function to call when buffer space is available
301 * @param notify_cls closure for notify
302 * @return non-NULL if the notify callback was queued,
303 * NULL if we are already going to notify someone else (busy)
305 struct GNUNET_CONNECTION_TransmitHandle
306 *GNUNET_CONNECTION_notify_transmit_ready (struct
307 GNUNET_CONNECTION_Handle
312 GNUNET_CONNECTION_TransmitReadyNotify
313 notify, void *notify_cls);
317 * Cancel the specified transmission-ready
320 * @param h handle for notification to cancel
323 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
324 GNUNET_CONNECTION_TransmitHandle
329 * Configure this connection to ignore shutdown signals.
331 * @param sock socket handle
332 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
335 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
339 #if 0 /* keep Emacsens' auto-indent happy */
347 /* ifndef GNUNET_CONNECTION_LIB_H */
349 /* end of gnunet_connection_lib.h */