Added silc_thread_yield.
[silc.git] / lib / silcutil / silcsocketstream.h
1 /*
2
3   silcsocketstream.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Socket Stream Interface
21  *
22  * DESCRIPTION
23  *
24  * Implementation of SILC Socket Stream.  SILC Socket Stream can be used
25  * read data from and write data to a socket connection.  The SILC Socket
26  * Stream provides also Quality of Service (QoS) support that can be used
27  * to control the throughput of the stream.  It also supports both TCP and
28  * UDP, and IPv4 and IPv6.
29  *
30  * SILC Socket Stream is not thread-safe.  If the same socket stream must be
31  * used in multithreaded environment concurrency control must be employed.
32  *
33  ***/
34
35 #ifndef SILCSOCKETSTREAM_H
36 #define SILCSOCKETSTREAM_H
37
38 /****d* silcutil/SilcSocketStreamAPI/SilcSocketStreamStatus
39  *
40  * NAME
41  *
42  *    typedef enum { ... } SilcStreamStatus;
43  *
44  * DESCRIPTION
45  *
46  *    Socket Stream status.  This status is returned into the
47  *    SilcSocketStreamCallback function after the socket stream is
48  *    created.
49  *
50  * SOURCE
51  */
52 typedef enum {
53   SILC_SOCKET_OK,               /* Normal status */
54   SILC_SOCKET_UNKNOWN_IP,       /* Remote does not have IP address */
55   SILC_SOCKET_UNKNOWN_HOST,     /* Remote does not have host name */
56   SILC_SOCKET_NO_MEMORY,        /* System out of memory */
57   SILC_SOCKET_ERROR,            /* Unknown error */
58 } SilcSocketStreamStatus;
59 /***/
60
61 /****f* silcutil/SilcSocketStreamAPI/SilcSocketStreamCallback
62  *
63  * SYNOPSIS
64  *
65  *    typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
66  *                                             SilcStream stream,
67  *                                             void *context);
68  *
69  * DESCRIPTION
70  *
71  *    Callback function of this type is called after the socket stream
72  *    creation is completed.  If the `stream' is NULL the socket stream could
73  *    not be created or the socket connection is not otherwise allowed.  The
74  *    `status' will indicate the error status.  In case error ocurrs the
75  *    associated socket has already been destroyed.  The `stream' is socket
76  *    stream representing the socket connection and silc_socket_stream_*
77  *    functions can be used to access the stream.  All other silc_stream_*
78  *    functions can also be used to read data, send data, and otherwise
79  *    handle the stream.
80  *
81  ***/
82 typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
83                                          SilcStream stream, void *context);
84
85 /****f* silcutil/SilcSocketStreamAPI/silc_socket_tcp_stream_create
86  *
87  * SYNOPSIS
88  *
89  *    SilcAsyncOperation
90  *    silc_socket_tcp_stream_create(SilcSocket sock, SilcBool lookup,
91  *                                  SilcBool require_fqdn,
92  *                                  SilcSchedule schedule,
93  *                                  SilcSocketStreamCallback callback,
94  *                                  void *context);
95  *
96  * DESCRIPTION
97  *
98  *    Creates TCP socket stream of the TCP connection indicated by `sock'.
99  *    The stream can be destroyed by calling the silc_stream_destroy.  Data
100  *    can be sent and received from the stream by calling silc_stream_write
101  *    and silc_stream_read.  The creation process is asynchronous since
102  *    socket connection information, such as hostname and IP address are
103  *    resolved, so SilcAsyncOperation is returned which can be used to cancel
104  *    the creation process.  The `callback' will be called to return the
105  *    created socket stream.
106  *
107  *    If the `lookup' is TRUE then this will perform IP and hostname lookup
108  *    for the socket.  If the `require_fqdn' is TRUE then the socket must
109  *    have valid hostname and IP address, otherwise the stream creation will
110  *    fail.  If it is FALSE then only valid IP address is required.  Note that,
111  *    if the `lookup' is FALSE then the hostname, IP and port information
112  *    will not be available from the socket stream.  In that case this will
113  *    also return NULL as the `callback' is called immediately.
114  *
115  ***/
116 SilcAsyncOperation
117 silc_socket_tcp_stream_create(SilcSocket sock, SilcBool lookup,
118                               SilcBool require_fqdn,
119                               SilcSchedule schedule,
120                               SilcSocketStreamCallback callback,
121                               void *context);
122
123 /****f* silcutil/SilcSocketStreamAPI/silc_socket_udp_stream_create
124  *
125  * SYNOPSIS
126  *
127  *    SilcStream silc_socket_udp_stream_create(SilcSocket sock,
128  *                                             SilcBool ipv6,
129  *                                             SilcBool connected,
130  *                                             SilcSchedule schedule);
131  *
132  * DESCRIPTION
133  *
134  *    Creates UDP socket stream of the UDP connection indicated by `sock'.
135  *    The stream can be destroyed by calling the silc_stream_destroy.
136  *    The `connected' defines whether the socket is in connected or in
137  *    connectionless state.
138  *
139  *    Note that, UDP packets may be read only through the notifier
140  *    callback (see silc_stream_set_notifier), when SILC_STREAM_CAN_READ
141  *    is returned to the callback.  Because of this the notifier callback
142  *    must be set.
143  *
144  *    Note that, UDP packet sending using silc_stream_write and receiving
145  *    with silc_stream_read works only if the `sock' is a UDP socket in a
146  *    connected state.  In connectionless state sending packets with
147  *    silc_stream_write is possible only if the remote address and port
148  *    has been set with silc_socket_stream_set_info.  If it is not set
149  *    in connectionless state packets may be sent only by using the
150  *    silc_net_udp_send function.  In connectionless state packets may be
151  *    received only by using silc_net_udp_receive.
152  *
153  *    This function returns the created SilcStream or NULL on error.
154  *
155  ***/
156 SilcStream silc_socket_udp_stream_create(SilcSocket sock,
157                                          SilcBool ipv6,
158                                          SilcBool connected,
159                                          SilcSchedule schedule);
160
161 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_is_udp
162  *
163  * SYNOPSIS
164  *
165  *    SilcBool silc_socket_stream_is_udp(SilcStream stream,
166  *                                       SilcBool *connected);
167  *
168  * DESCRIPTION
169  *
170  *    Returns TRUE if the `stream' is UDP stream.  If the `connected' pointer
171  *    is non-NULL it will have indication whether the UDP stream is in
172  *    connected state.  If it is then packets can be read and written using
173  *    silc_stream_read and silc_stream_write.  If it is not then packets
174  *    need to read and written by using silc_net_udp_receive and
175  *    silc_net_udp_send.
176  *
177  ***/
178 SilcBool silc_socket_stream_is_udp(SilcStream stream, SilcBool *connected);
179
180 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_info
181  *
182  * SYNOPSIS
183  *
184  *    SilcBool
185  *    silc_socket_stream_get_info(SilcStream stream,
186  *                                SilcSocket *sock, const char **hostname,
187  *                                const char **ip, SilcUInt16 *port);
188  *
189  * DESCRIPTION
190  *
191  *    Returns socket stream information such as the socket, remote hostname,
192  *    remote IP address and the remote port of the remote socket connection.
193  *    Return FALSE if these informations are not available.
194  *
195  ***/
196 SilcBool silc_socket_stream_get_info(SilcStream stream,
197                                      SilcSocket *sock, const char **hostname,
198                                      const char **ip, SilcUInt16 *port);
199
200 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_info
201  *
202  * SYNOPSIS
203  *
204  *    SilcBool
205  *    silc_socket_stream_set_info(SilcStream stream,
206  *                                const char *hostname,
207  *                                const char *ip, SilcUInt16 port);
208  *
209  * DESCRIPTION
210  *
211  *    Use this function to set the hostname, IP address and remote port
212  *    information to the socket stream indicated by `stream' if you did not
213  *    perform lookup in the silc_socket_tcp_stream_create.  This is not
214  *    mandatory but if you would like to associate the information with the
215  *    stream use this function.  If the lookup was performed when creating
216  *    the stream then calling this function is not necessary.  Use the
217  *    function silc_socket_stream_get_info to get the information from the
218  *    stream.
219  *
220  ***/
221 SilcBool silc_socket_stream_set_info(SilcStream stream,
222                                      const char *hostname,
223                                      const char *ip, SilcUInt16 port);
224
225 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_error
226  *
227  * SYNOPSIS
228  *
229  *    int silc_socket_stream_get_error(SilcStream stream);
230  *
231  * DESCRIPTION
232  *
233  *    If error occurred during socket stream operations, this function
234  *    can be used to retrieve the error number that occurred.
235  *
236  ***/
237 int silc_socket_stream_get_error(SilcStream stream);
238
239 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_qos
240  *
241  * SYNOPSIS
242  *
243  *    SilcBool silc_socket_stream_set_qos(SilcStream stream,
244  *                                        SilcUInt32 read_rate,
245  *                                        SilcUInt32 read_limit_bytes,
246  *                                        SilcUInt32 limit_sec,
247  *                                        SilcUInt32 limit_usec)
248  *
249  * DESCRIPTION
250  *
251  *    Sets a "Quality of Service" settings for socket stream `stream'.
252  *    The `read_rate' specifies the maximum read operations per second.
253  *    If more read operations are executed the limit will be applied for
254  *    the reading.  The `read_limit_bytes' specifies the maximum data
255  *    that is read.  It is guaranteed that silc_stream_read  never returns
256  *    more than `read_limit_bytes' of data.  The `limit_sec' and `limit_usec'
257  *    specifies the time limit that is applied if `read_rate' and/or
258  *    `read_limit_bytes' is reached.  If all arguments except `stream'
259  *    are zero this resets the QoS from the socket stream, all QoS for
260  *    this socket stream that may be pending will be cancelled.
261  *
262  ***/
263 SilcBool silc_socket_stream_set_qos(SilcStream stream,
264                                     SilcUInt32 read_rate,
265                                     SilcUInt32 read_limit_bytes,
266                                     SilcUInt32 limit_sec,
267                                     SilcUInt32 limit_usec);
268
269 #include "silcsocketstream_i.h"
270
271 #endif /* SILCSOCKETSTREAM_H */