2 * This file is part of the Nice GLib ICE library.
4 * (C) 2010, 2014 Collabora Ltd.
5 * Contact: Philip Withnall
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
18 * The Original Code is the Nice GLib ICE library.
20 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21 * Corporation. All Rights Reserved.
24 * Youness Alaoui, Collabora Ltd.
25 * Philip Withnall, Collabora Ltd.
27 * Alternatively, the contents of this file may be used under the terms of the
28 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29 * case the provisions of LGPL are applicable instead of those above. If you
30 * wish to allow use of your version of this file only under the terms of the
31 * LGPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replace
33 * them with the notice and other provisions required by the LGPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the LGPL.
38 #ifndef __LIBNICE_PSEUDOTCP_H__
39 #define __LIBNICE_PSEUDOTCP_H__
43 * @short_description: Pseudo TCP implementation
44 * @include: pseudotcp.h
47 * The #PseudoTcpSocket is an object implementing a Pseudo Tcp Socket for use
49 * The socket will implement a subset of the TCP stack to allow for a reliable
50 * transport over non-reliable sockets (such as UDP).
52 * See the file tests/test-pseudotcp.c in the source package for an example
53 * of how to use the object.
60 #include <glib-object.h>
62 #ifndef __GTK_DOC_IGNORE__
64 # include <winsock2.h>
67 # define ECONNABORTED WSAECONNABORTED
71 # define ENOTCONN WSAENOTCONN
75 # define EWOULDBLOCK WSAEWOULDBLOCK
79 # define ECONNRESET WSAECONNRESET
83 # define EMSGSIZE WSAEMSGSIZE
87 # define ETIMEDOUT WSAETIMEDOUT
99 * The #PseudoTcpSocket is the GObject implementing the Pseudo TCP Socket
103 typedef struct _PseudoTcpSocket PseudoTcpSocket;
105 typedef struct _PseudoTcpSocketClass PseudoTcpSocketClass;
107 GType pseudo_tcp_socket_get_type (void);
110 #define PSEUDO_TCP_SOCKET_TYPE \
111 (pseudo_tcp_socket_get_type ())
112 #define PSEUDO_TCP_SOCKET(obj) \
113 (G_TYPE_CHECK_INSTANCE_CAST((obj), PSEUDO_TCP_SOCKET_TYPE, \
115 #define PSEUDO_TCP_SOCKET_CLASS(klass) \
116 (G_TYPE_CHECK_CLASS_CAST((klass), PSEUDO_TCP_SOCKET_TYPE, \
117 PseudoTcpSocketClass))
118 #define IS_PSEUDO_TCP_SOCKET(obj) \
119 (G_TYPE_CHECK_INSTANCE_TYPE((obj), PSEUDO_TCP_SOCKET_TYPE))
120 #define IS_PSEUDO_TCP_SOCKET_CLASS(klass) \
121 (G_TYPE_CHECK_CLASS_TYPE((klass), PSEUDO_TCP_SOCKET_TYPE))
122 #define PSEUDOTCP_SOCKET_GET_CLASS(obj) \
123 (G_TYPE_INSTANCE_GET_CLASS ((obj), PSEUDO_TCP_SOCKET_TYPE, \
124 PseudoTcpSocketClass))
127 * PseudoTcpDebugLevel:
128 * @PSEUDO_TCP_DEBUG_NONE: Disable debug messages
129 * @PSEUDO_TCP_DEBUG_NORMAL: Enable basic debug messages
130 * @PSEUDO_TCP_DEBUG_VERBOSE: Enable verbose debug messages
132 * Valid values of debug levels to be set.
137 PSEUDO_TCP_DEBUG_NONE = 0,
138 PSEUDO_TCP_DEBUG_NORMAL,
139 PSEUDO_TCP_DEBUG_VERBOSE,
140 } PseudoTcpDebugLevel;
144 * @PSEUDO_TCP_LISTEN: The socket's initial state. The socket isn't connected and is
145 * listening for an incoming connection
146 * @PSEUDO_TCP_SYN_SENT: The socket has sent a connection request (SYN) packet and is
147 * waiting for an answer
148 * @PSEUDO_TCP_SYN_RECEIVED: The socket has received a connection request (SYN) packet.
149 * @PSEUDO_TCP_ESTABLISHED: The socket is connected
150 * @PSEUDO_TCP_CLOSED: The socket has been closed
151 * @PSEUDO_TCP_FIN_WAIT_1: The socket has been closed locally but not remotely
153 * @PSEUDO_TCP_FIN_WAIT_2: The socket has been closed locally but not remotely
155 * @PSEUDO_TCP_CLOSING: The socket has been closed locally and remotely
157 * @PSEUDO_TCP_TIME_WAIT: The socket has been closed locally and remotely
159 * @PSEUDO_TCP_CLOSE_WAIT: The socket has been closed remotely but not locally
161 * @PSEUDO_TCP_LAST_ACK: The socket has been closed locally and remotely
164 * An enum representing the state of the #PseudoTcpSocket. These states
165 * correspond to the TCP states in RFC 793.
166 * <para> See also: #PseudoTcpSocket:state </para>
173 PSEUDO_TCP_SYN_RECEIVED,
174 PSEUDO_TCP_ESTABLISHED,
176 PSEUDO_TCP_FIN_WAIT_1,
177 PSEUDO_TCP_FIN_WAIT_2,
179 PSEUDO_TCP_TIME_WAIT,
180 PSEUDO_TCP_CLOSE_WAIT,
185 * PseudoTcpWriteResult:
186 * @WR_SUCCESS: The write operation was successful
187 * @WR_TOO_LARGE: The socket type requires that message be sent atomically
188 * and the size of the message to be sent made this impossible.
189 * @WR_FAIL: There was an error sending the message
191 * An enum representing the result value of the write operation requested by
192 * the #PseudoTcpSocket.
193 * <para> See also: %PseudoTcpCallbacks:WritePacket </para>
201 } PseudoTcpWriteResult;
205 * @PSEUDO_TCP_SHUTDOWN_RD: Shut down the local reader only
206 * @PSEUDO_TCP_SHUTDOWN_WR: Shut down the local writer only
207 * @PSEUDO_TCP_SHUTDOWN_RDWR: Shut down both reading and writing
209 * Options for which parts of a connection to shut down when calling
210 * pseudo_tcp_socket_shutdown(). These correspond to the values passed to POSIX
216 PSEUDO_TCP_SHUTDOWN_RD,
217 PSEUDO_TCP_SHUTDOWN_WR,
218 PSEUDO_TCP_SHUTDOWN_RDWR,
222 * PseudoTcpCallbacks:
223 * @user_data: A user defined pointer to be passed to the callbacks
224 * @PseudoTcpOpened: The #PseudoTcpSocket is now connected
225 * @PseudoTcpReadable: The socket is readable
226 * @PseudoTcpWritable: The socket is writable
227 * @PseudoTcpClosed: The socket was closed (both sides)
228 * @WritePacket: This callback is called when the socket needs to send data.
230 * A structure containing callbacks functions that will be called by the
231 * #PseudoTcpSocket when some events happen.
232 * <para> See also: #PseudoTcpWriteResult </para>
238 void (*PseudoTcpOpened) (PseudoTcpSocket *tcp, gpointer data);
239 void (*PseudoTcpReadable) (PseudoTcpSocket *tcp, gpointer data);
240 void (*PseudoTcpWritable) (PseudoTcpSocket *tcp, gpointer data);
241 void (*PseudoTcpClosed) (PseudoTcpSocket *tcp, guint32 error, gpointer data);
242 PseudoTcpWriteResult (*WritePacket) (PseudoTcpSocket *tcp,
243 const gchar * buffer, guint32 len, gpointer data);
244 } PseudoTcpCallbacks;
247 * pseudo_tcp_socket_new:
248 * @conversation: The conversation id for the socket.
249 * @callbacks: A pointer to the #PseudoTcpCallbacks structure for getting
250 * notified of the #PseudoTcpSocket events.
252 * Creates a new #PseudoTcpSocket for the specified conversation
256 The @callbacks must be non-NULL, in order to get notified of packets the
257 socket needs to send.
260 If the @callbacks structure was dynamicly allocated, it can be freed
261 after the call @pseudo_tcp_socket_new
265 * Returns: The new #PseudoTcpSocket object, %NULL on error
269 PseudoTcpSocket *pseudo_tcp_socket_new (guint32 conversation,
270 PseudoTcpCallbacks *callbacks);
274 * pseudo_tcp_socket_connect:
275 * @self: The #PseudoTcpSocket object.
277 * Connects the #PseudoTcpSocket to the peer with the same conversation id.
278 * The connection will only be successful after the
279 * %PseudoTcpCallbacks:PseudoTcpOpened callback is called
281 * Returns: %TRUE on success, %FALSE on failure (not in %TCP_LISTEN state)
282 * <para> See also: pseudo_tcp_socket_get_error() </para>
286 gboolean pseudo_tcp_socket_connect(PseudoTcpSocket *self);
290 * pseudo_tcp_socket_recv:
291 * @self: The #PseudoTcpSocket object.
292 * @buffer: The buffer to fill with received data
293 * @len: The length of @buffer
295 * Receive data from the socket.
299 Only call this on the %PseudoTcpCallbacks:PseudoTcpReadable callback.
302 This function should be called in a loop. If this function does not
303 return -1 with EWOULDBLOCK as the error, the
304 %PseudoTcpCallbacks:PseudoTcpReadable callback will not be called again.
308 * Returns: The number of bytes received or -1 in case of error
309 * <para> See also: pseudo_tcp_socket_get_error() </para>
313 gint pseudo_tcp_socket_recv(PseudoTcpSocket *self, char * buffer, size_t len);
317 * pseudo_tcp_socket_send:
318 * @self: The #PseudoTcpSocket object.
319 * @buffer: The buffer with data to send
320 * @len: The length of @buffer
322 * Send data on the socket.
326 If this function return -1 with EWOULDBLOCK as the error, or if the return
327 value is lower than @len, then the %PseudoTcpCallbacks:PseudoTcpWritable
328 callback will be called when the socket will become writable.
332 * Returns: The number of bytes sent or -1 in case of error
333 * <para> See also: pseudo_tcp_socket_get_error() </para>
337 gint pseudo_tcp_socket_send(PseudoTcpSocket *self, const char * buffer,
342 * pseudo_tcp_socket_close:
343 * @self: The #PseudoTcpSocket object.
344 * @force: %TRUE to close the socket forcefully, %FALSE to close it gracefully
346 * Close the socket for sending. If @force is set to %FALSE, the socket will
347 * finish sending pending data before closing. If it is set to %TRUE, the socket
348 * will discard pending data and close the connection immediately (sending a TCP
351 * The socket will be closed in both directions – sending and receiving – and
352 * any pending received data must be read before calling this function, by
353 * calling pseudo_tcp_socket_recv() until it blocks. If any pending data is in
354 * the receive buffer when pseudo_tcp_socket_close() is called, a TCP RST
355 * segment will be sent to the peer to notify it of the data loss.
359 The %PseudoTcpCallbacks:PseudoTcpClosed callback will not be called once
360 the socket gets closed. It is only used for aborted connection.
361 Instead, the socket gets closed when the pseudo_tcp_socket_get_next_clock()
362 function returns FALSE.
366 * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
370 void pseudo_tcp_socket_close(PseudoTcpSocket *self, gboolean force);
373 * pseudo_tcp_socket_shutdown:
374 * @self: The #PseudoTcpSocket object.
375 * @how: The directions of the connection to shut down.
377 * Shut down sending, receiving, or both on the socket, depending on the value
378 * of @how. The behaviour of pseudo_tcp_socket_send() and
379 * pseudo_tcp_socket_recv() will immediately change after this function returns
380 * (depending on the value of @how), though the socket may continue to process
381 * network traffic in the background even if sending or receiving data is
384 * This is equivalent to the POSIX shutdown() function. Setting @how to
385 * %PSEUDO_TCP_SHUTDOWN_RDWR is equivalent to calling pseudo_tcp_socket_close().
389 void pseudo_tcp_socket_shutdown (PseudoTcpSocket *self, PseudoTcpShutdown how);
392 * pseudo_tcp_socket_get_error:
393 * @self: The #PseudoTcpSocket object.
395 * Return the last encountered error.
399 The return value can be :
401 EINVAL (for pseudo_tcp_socket_connect()).
404 EWOULDBLOCK or ENOTCONN (for pseudo_tcp_socket_recv() and
405 pseudo_tcp_socket_send()).
410 * Returns: The error code
411 * <para> See also: pseudo_tcp_socket_connect() </para>
412 * <para> See also: pseudo_tcp_socket_recv() </para>
413 * <para> See also: pseudo_tcp_socket_send() </para>
417 int pseudo_tcp_socket_get_error(PseudoTcpSocket *self);
421 * pseudo_tcp_socket_get_next_clock:
422 * @self: The #PseudoTcpSocket object.
423 * @timeout: A pointer to be filled with the new timeout.
425 * Call this to determine the timeout needed before the next time call
426 * to pseudo_tcp_socket_notify_clock() should be made.
428 * Returns: %TRUE if @timeout was filled, %FALSE if the socket is closed and
429 * ready to be destroyed.
431 * <para> See also: pseudo_tcp_socket_notify_clock() </para>
435 gboolean pseudo_tcp_socket_get_next_clock(PseudoTcpSocket *self,
440 * pseudo_tcp_socket_notify_clock:
441 * @self: The #PseudoTcpSocket object.
443 * Start the processing of receiving data, pending data or syn/acks.
444 * Call this based on timeout value returned by
445 * pseudo_tcp_socket_get_next_clock().
446 * It's ok to call this too frequently.
448 * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
452 void pseudo_tcp_socket_notify_clock(PseudoTcpSocket *self);
456 * pseudo_tcp_socket_notify_mtu:
457 * @self: The #PseudoTcpSocket object.
458 * @mtu: The new MTU of the socket
460 * Set the MTU of the socket
464 void pseudo_tcp_socket_notify_mtu(PseudoTcpSocket *self, guint16 mtu);
468 * pseudo_tcp_socket_notify_packet:
469 * @self: The #PseudoTcpSocket object.
470 * @buffer: The buffer containing the received data
471 * @len: The length of @buffer
473 * Notify the #PseudoTcpSocket when a new packet arrives
475 * Returns: %TRUE if the packet was processed successfully, %FALSE otherwise
479 gboolean pseudo_tcp_socket_notify_packet(PseudoTcpSocket *self,
480 const gchar * buffer, guint32 len);
484 * pseudo_tcp_socket_notify_message:
485 * @self: The #PseudoTcpSocket object.
486 * @message: A #NiceInputMessage containing the received data.
488 * Notify the #PseudoTcpSocket that a new message has arrived, and enqueue the
489 * data in its buffers to the #PseudoTcpSocket’s receive buffer.
491 * Returns: %TRUE if the packet was processed successfully, %FALSE otherwise
495 gboolean pseudo_tcp_socket_notify_message (PseudoTcpSocket *self,
496 NiceInputMessage *message);
500 * pseudo_tcp_set_debug_level:
501 * @level: The level of debug to set
503 * Sets the debug level to enable/disable normal/verbose debug messages.
507 void pseudo_tcp_set_debug_level (PseudoTcpDebugLevel level);
511 * pseudo_tcp_socket_get_available_bytes:
512 * @self: The #PseudoTcpSocket object.
514 * Gets the number of bytes of data in the buffer that can be read without
515 * receiving more packets from the network.
517 * Returns: The number of bytes or -1 if the connection is not established
522 gint pseudo_tcp_socket_get_available_bytes (PseudoTcpSocket *self);
525 * pseudo_tcp_socket_can_send:
526 * @self: The #PseudoTcpSocket object.
528 * Returns if there is space in the send buffer to send any data.
530 * Returns: %TRUE if data can be sent, %FALSE otherwise
535 gboolean pseudo_tcp_socket_can_send (PseudoTcpSocket *self);
538 * pseudo_tcp_socket_get_available_send_space:
539 * @self: The #PseudoTcpSocket object.
541 * Gets the number of bytes of space available in the transmission buffer.
543 * Returns: The number of bytes, or 0 if the connection is not established.
547 gsize pseudo_tcp_socket_get_available_send_space (PseudoTcpSocket *self);
550 * pseudo_tcp_socket_set_time:
551 * @self: The #PseudoTcpSocket object.
552 * @current_time: Current monotonic time, in milliseconds; or zero to use the
553 * system monotonic clock.
555 * Sets the current monotonic time to be used by the TCP socket when calculating
556 * timeouts and expiry times. If this function is not called, or is called with
557 * @current_time as zero, g_get_monotonic_time() will be used. Otherwise, the
558 * specified @current_time will be used until it is updated by calling this
561 * This function is intended for testing only, and should not be used in
566 void pseudo_tcp_socket_set_time (PseudoTcpSocket *self, guint32 current_time);
569 * pseudo_tcp_socket_is_closed:
570 * @self: The #PseudoTcpSocket object.
572 * Gets whether the socket is closed, with the shutdown handshake completed,
573 * and both peers no longer able to read or write data to the connection.
575 * Returns: %TRUE if the socket is closed in both directions, %FALSE otherwise
578 gboolean pseudo_tcp_socket_is_closed (PseudoTcpSocket *self);
581 * pseudo_tcp_socket_is_closed_remotely:
582 * @self: The #PseudoTcpSocket object.
584 * Gets whether the socket has been closed on the remote peer’s side of the
585 * connection (i.e. whether pseudo_tcp_socket_close() has been called there).
586 * This is guaranteed to return %TRUE if pseudo_tcp_socket_is_closed() returns
587 * %TRUE. It will not return %TRUE after pseudo_tcp_socket_close() is called
588 * until a FIN segment is received from the remote peer.
590 * Returns: %TRUE if the remote peer has closed its side of the connection,
594 gboolean pseudo_tcp_socket_is_closed_remotely (PseudoTcpSocket *self);
598 #endif /* __LIBNICE_PSEUDOTCP_H__ */