e087ddcb43ee98e3235507d5efd9f15f81c8e693
[platform/upstream/libnice.git] / agent / pseudotcp.h
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2010, 2014 Collabora Ltd.
5  *  Contact: Philip Withnall
6
7  *
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/
12  *
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
16  * License.
17  *
18  * The Original Code is the Nice GLib ICE library.
19  *
20  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21  * Corporation. All Rights Reserved.
22  *
23  * Contributors:
24  *   Youness Alaoui, Collabora Ltd.
25  *   Philip Withnall, Collabora Ltd.
26  *
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.
36  */
37
38 #ifndef __LIBNICE_PSEUDOTCP_H__
39 #define __LIBNICE_PSEUDOTCP_H__
40
41 /**
42  * SECTION:pseudotcp
43  * @short_description: Pseudo TCP implementation
44  * @include: pseudotcp.h
45  * @stability: Stable
46  *
47  * The #PseudoTcpSocket is an object implementing a Pseudo Tcp Socket for use
48  * over UDP.
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).
51  *
52  * See the file tests/test-pseudotcp.c in the source package for an example
53  * of how to use the object.
54  *
55  * Since: 0.0.11
56  */
57
58
59
60 #include <glib-object.h>
61
62 #ifndef __GTK_DOC_IGNORE__
63 #ifdef G_OS_WIN32
64 #  include <winsock2.h>
65
66 #ifndef ECONNABORTED
67 #  define ECONNABORTED WSAECONNABORTED
68 #endif
69
70 #ifndef ENOTCONN
71 #  define ENOTCONN WSAENOTCONN
72 #endif
73
74 #ifndef EWOULDBLOCK
75 #  define EWOULDBLOCK WSAEWOULDBLOCK
76 #endif
77
78 #ifndef ECONNRESET
79 #  define ECONNRESET WSAECONNRESET
80 #endif
81
82 #ifndef EMSGSIZE
83 #  define EMSGSIZE WSAEMSGSIZE
84 #endif
85
86 #ifndef ETIMEDOUT
87 #  define ETIMEDOUT WSAETIMEDOUT
88 #endif
89 #endif
90 #endif
91
92 #include "agent.h"
93
94 G_BEGIN_DECLS
95
96 /**
97  * PseudoTcpSocket:
98  *
99  * The #PseudoTcpSocket is the GObject implementing the Pseudo TCP Socket
100  *
101  * Since: 0.0.11
102  */
103 typedef struct _PseudoTcpSocket PseudoTcpSocket;
104
105 typedef struct _PseudoTcpSocketClass PseudoTcpSocketClass;
106
107 GType pseudo_tcp_socket_get_type (void);
108
109 /* TYPE MACROS */
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, \
114                               PseudoTcpSocket))
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))
125
126 /**
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
131  *
132  * Valid values of debug levels to be set.
133  *
134  * Since: 0.0.11
135  */
136 typedef enum {
137   PSEUDO_TCP_DEBUG_NONE = 0,
138   PSEUDO_TCP_DEBUG_NORMAL,
139   PSEUDO_TCP_DEBUG_VERBOSE,
140 } PseudoTcpDebugLevel;
141
142 /**
143  * PseudoTcpState:
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
152  * (Since: 0.1.8)
153  * @PSEUDO_TCP_FIN_WAIT_2: The socket has been closed locally but not remotely
154  * (Since: 0.1.8)
155  * @PSEUDO_TCP_CLOSING: The socket has been closed locally and remotely
156  * (Since: 0.1.8)
157  * @PSEUDO_TCP_TIME_WAIT: The socket has been closed locally and remotely
158  * (Since: 0.1.8)
159  * @PSEUDO_TCP_CLOSE_WAIT: The socket has been closed remotely but not locally
160  * (Since: 0.1.8)
161  * @PSEUDO_TCP_LAST_ACK: The socket has been closed locally and remotely
162  * (Since: 0.1.8)
163  *
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>
167  *
168  * Since: 0.0.11
169  */
170 typedef enum {
171   PSEUDO_TCP_LISTEN,
172   PSEUDO_TCP_SYN_SENT,
173   PSEUDO_TCP_SYN_RECEIVED,
174   PSEUDO_TCP_ESTABLISHED,
175   PSEUDO_TCP_CLOSED,
176   PSEUDO_TCP_FIN_WAIT_1,
177   PSEUDO_TCP_FIN_WAIT_2,
178   PSEUDO_TCP_CLOSING,
179   PSEUDO_TCP_TIME_WAIT,
180   PSEUDO_TCP_CLOSE_WAIT,
181   PSEUDO_TCP_LAST_ACK,
182 } PseudoTcpState;
183
184 /**
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
190  *
191  * An enum representing the result value of the write operation requested by
192  * the #PseudoTcpSocket.
193  * <para> See also: %PseudoTcpCallbacks:WritePacket </para>
194  *
195  * Since: 0.0.11
196  */
197 typedef enum {
198   WR_SUCCESS,
199   WR_TOO_LARGE,
200   WR_FAIL
201 } PseudoTcpWriteResult;
202
203 /**
204  * PseudoTcpShutdown:
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
208  *
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
211  * shutdown().
212  *
213  * Since: 0.1.8
214  */
215 typedef enum {
216   PSEUDO_TCP_SHUTDOWN_RD,
217   PSEUDO_TCP_SHUTDOWN_WR,
218   PSEUDO_TCP_SHUTDOWN_RDWR,
219 } PseudoTcpShutdown;
220
221 /**
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.
229  *
230  * A structure containing callbacks functions that will be called by the
231  * #PseudoTcpSocket when some events happen.
232  * <para> See also: #PseudoTcpWriteResult </para>
233  *
234  * Since: 0.0.11
235  */
236 typedef struct {
237   gpointer user_data;
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;
245
246 /**
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.
251  *
252  * Creates a new #PseudoTcpSocket for the specified conversation
253  *
254  <note>
255    <para>
256      The @callbacks must be non-NULL, in order to get notified of packets the
257      socket needs to send.
258    </para>
259    <para>
260      If the @callbacks structure was dynamicly allocated, it can be freed
261      after the call @pseudo_tcp_socket_new
262    </para>
263  </note>
264  *
265  * Returns: The new #PseudoTcpSocket object, %NULL on error
266  *
267  * Since: 0.0.11
268  */
269 PseudoTcpSocket *pseudo_tcp_socket_new (guint32 conversation,
270     PseudoTcpCallbacks *callbacks);
271
272
273 /**
274  * pseudo_tcp_socket_connect:
275  * @self: The #PseudoTcpSocket object.
276  *
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
280  *
281  * Returns: %TRUE on success, %FALSE on failure (not in %TCP_LISTEN state)
282  * <para> See also: pseudo_tcp_socket_get_error() </para>
283  *
284  * Since: 0.0.11
285  */
286 gboolean pseudo_tcp_socket_connect(PseudoTcpSocket *self);
287
288
289 /**
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
294  *
295  * Receive data from the socket.
296  *
297  <note>
298    <para>
299      Only call this on the %PseudoTcpCallbacks:PseudoTcpReadable callback.
300    </para>
301    <para>
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.
305    </para>
306  </note>
307  *
308  * Returns: The number of bytes received or -1 in case of error
309  * <para> See also: pseudo_tcp_socket_get_error() </para>
310  *
311  * Since: 0.0.11
312  */
313 gint  pseudo_tcp_socket_recv(PseudoTcpSocket *self, char * buffer, size_t len);
314
315
316 /**
317  * pseudo_tcp_socket_send:
318  * @self: The #PseudoTcpSocket object.
319  * @buffer: The buffer with data to send
320  * @len: The length of @buffer
321  *
322  * Send data on the socket.
323  *
324  <note>
325    <para>
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.
329    </para>
330  </note>
331  *
332  * Returns: The number of bytes sent or -1 in case of error
333  * <para> See also: pseudo_tcp_socket_get_error() </para>
334  *
335  * Since: 0.0.11
336  */
337 gint pseudo_tcp_socket_send(PseudoTcpSocket *self, const char * buffer,
338     guint32 len);
339
340
341 /**
342  * pseudo_tcp_socket_close:
343  * @self: The #PseudoTcpSocket object.
344  * @force: %TRUE to close the socket forcefully, %FALSE to close it gracefully
345  *
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
349  * RST segment).
350  *
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.
356  *
357  <note>
358    <para>
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.
363    </para>
364  </note>
365  *
366  * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
367  *
368  * Since: 0.0.11
369  */
370 void pseudo_tcp_socket_close(PseudoTcpSocket *self, gboolean force);
371
372 /**
373  * pseudo_tcp_socket_shutdown:
374  * @self: The #PseudoTcpSocket object.
375  * @how: The directions of the connection to shut down.
376  *
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
382  * forbidden.
383  *
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().
386  *
387  * Since: 0.1.8
388  */
389 void pseudo_tcp_socket_shutdown (PseudoTcpSocket *self, PseudoTcpShutdown how);
390
391 /**
392  * pseudo_tcp_socket_get_error:
393  * @self: The #PseudoTcpSocket object.
394  *
395  * Return the last encountered error.
396  *
397  <note>
398    <para>
399      The return value can be :
400      <para>
401        EINVAL (for pseudo_tcp_socket_connect()).
402      </para>
403      <para>
404        EWOULDBLOCK or ENOTCONN (for pseudo_tcp_socket_recv() and
405        pseudo_tcp_socket_send()).
406      </para>
407    </para>
408  </note>
409  *
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>
414  *
415  * Since: 0.0.11
416  */
417 int pseudo_tcp_socket_get_error(PseudoTcpSocket *self);
418
419
420 /**
421  * pseudo_tcp_socket_get_next_clock:
422  * @self: The #PseudoTcpSocket object.
423  * @timeout: A pointer to be filled with the new timeout.
424  *
425  * Call this to determine the timeout needed before the next time call
426  * to pseudo_tcp_socket_notify_clock() should be made.
427  *
428  * Returns: %TRUE if @timeout was filled, %FALSE if the socket is closed and
429  * ready to be destroyed.
430  *
431  * <para> See also: pseudo_tcp_socket_notify_clock() </para>
432  *
433  * Since: 0.0.11
434  */
435 gboolean pseudo_tcp_socket_get_next_clock(PseudoTcpSocket *self,
436     guint64 *timeout);
437
438
439 /**
440  * pseudo_tcp_socket_notify_clock:
441  * @self: The #PseudoTcpSocket object.
442  *
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.
447  *
448  * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
449  *
450  * Since: 0.0.11
451  */
452 void pseudo_tcp_socket_notify_clock(PseudoTcpSocket *self);
453
454
455 /**
456  * pseudo_tcp_socket_notify_mtu:
457  * @self: The #PseudoTcpSocket object.
458  * @mtu: The new MTU of the socket
459  *
460  * Set the MTU of the socket
461  *
462  * Since: 0.0.11
463  */
464 void pseudo_tcp_socket_notify_mtu(PseudoTcpSocket *self, guint16 mtu);
465
466
467 /**
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
472  *
473  * Notify the #PseudoTcpSocket when a new packet arrives
474  *
475  * Returns: %TRUE if the packet was processed successfully, %FALSE otherwise
476  *
477  * Since: 0.0.11
478  */
479 gboolean pseudo_tcp_socket_notify_packet(PseudoTcpSocket *self,
480     const gchar * buffer, guint32 len);
481
482
483 /**
484  * pseudo_tcp_socket_notify_message:
485  * @self: The #PseudoTcpSocket object.
486  * @message: A #NiceInputMessage containing the received data.
487  *
488  * Notify the #PseudoTcpSocket that a new message has arrived, and enqueue the
489  * data in its buffers to the #PseudoTcpSocket’s receive buffer.
490  *
491  * Returns: %TRUE if the packet was processed successfully, %FALSE otherwise
492  *
493  * Since: 0.1.5
494  */
495 gboolean pseudo_tcp_socket_notify_message (PseudoTcpSocket *self,
496     NiceInputMessage *message);
497
498
499 /**
500  * pseudo_tcp_set_debug_level:
501  * @level: The level of debug to set
502  *
503  * Sets the debug level to enable/disable normal/verbose debug messages.
504  *
505  * Since: 0.0.11
506  */
507 void pseudo_tcp_set_debug_level (PseudoTcpDebugLevel level);
508
509
510 /**
511  * pseudo_tcp_socket_get_available_bytes:
512  * @self: The #PseudoTcpSocket object.
513  *
514  * Gets the number of bytes of data in the buffer that can be read without
515  * receiving more packets from the network.
516  *
517  * Returns: The number of bytes or -1 if the connection is not established
518  *
519  * Since: 0.1.5
520  */
521
522 gint pseudo_tcp_socket_get_available_bytes (PseudoTcpSocket *self);
523
524 /**
525  * pseudo_tcp_socket_can_send:
526  * @self: The #PseudoTcpSocket object.
527  *
528  * Returns if there is space in the send buffer to send any data.
529  *
530  * Returns: %TRUE if data can be sent, %FALSE otherwise
531  *
532  * Since: 0.1.5
533  */
534
535 gboolean pseudo_tcp_socket_can_send (PseudoTcpSocket *self);
536
537 /**
538  * pseudo_tcp_socket_get_available_send_space:
539  * @self: The #PseudoTcpSocket object.
540  *
541  * Gets the number of bytes of space available in the transmission buffer.
542  *
543  * Returns: The number of bytes, or 0 if the connection is not established.
544  *
545  * Since: 0.1.5
546  */
547 gsize pseudo_tcp_socket_get_available_send_space (PseudoTcpSocket *self);
548
549 /**
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.
554  *
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
559  * function again.
560  *
561  * This function is intended for testing only, and should not be used in
562  * production code.
563  *
564  * Since: 0.1.8
565  */
566 void pseudo_tcp_socket_set_time (PseudoTcpSocket *self, guint32 current_time);
567
568 /**
569  * pseudo_tcp_socket_is_closed:
570  * @self: The #PseudoTcpSocket object.
571  *
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.
574  *
575  * Returns: %TRUE if the socket is closed in both directions, %FALSE otherwise
576  * Since: 0.1.8
577  */
578 gboolean pseudo_tcp_socket_is_closed (PseudoTcpSocket *self);
579
580 /**
581  * pseudo_tcp_socket_is_closed_remotely:
582  * @self: The #PseudoTcpSocket object.
583  *
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.
589  *
590  * Returns: %TRUE if the remote peer has closed its side of the connection,
591  * %FALSE otherwise
592  * Since: 0.1.8
593  */
594 gboolean pseudo_tcp_socket_is_closed_remotely (PseudoTcpSocket *self);
595
596 G_END_DECLS
597
598 #endif /* __LIBNICE_PSEUDOTCP_H__ */
599