Comment changes.
[silc.git] / lib / silcutil / silcsocketstream.h
1 /*
2
3   silcsocketstream.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 - 2006 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.  The `stream' is socket stream
75  *    representing the socket connection and silc_socket_stream_* functions
76  *    can be used to access the stream.  All other silc_stream_* functions
77  *    can also be used to read data, send data, and otherwise handle the
78  *    stream.
79  *
80  ***/
81 typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
82                                          SilcStream stream, void *context);
83
84 /****f* silcutil/SilcSocketStreamAPI/silc_socket_tcp_stream_create
85  *
86  * SYNOPSIS
87  *
88  *    SilcAsyncOperation
89  *    silc_socket_tcp_stream_create(int sock, SilcBool lookup,
90  *                                  SilcBool require_fqdn,
91  *                                  SilcSchedule schedule,
92  *                                  SilcSocketStreamCallback callback,
93  *                                  void *context);
94  *
95  * DESCRIPTION
96  *
97  *    Creates TCP socket stream of the TCP connection indicated by `sock'.
98  *    The stream can be destroyed by calling the silc_stream_destroy.  Data
99  *    can be sent and received from the stream by calling silc_stream_write
100  *    and silc_stream_read.  The creation process is asynchronous since
101  *    socket connection information, such as hostname and IP address are
102  *    resolved, so SilcAsyncOperation is returned which can be used to cancel
103  *    the creation process.  The `callback' will be called to return the
104  *    created socket stream.  To destroy the stream call silc_stream_destroy.
105  *
106  *    If the `lookup' is TRUE then this will perform IP and hostname lookup
107  *    for the socket.  If the `require_fqdn' is TRUE then the socket must
108  *    have valid hostname and IP address, otherwise the stream creation will
109  *    fail.  If it is FALSE then only valid IP address is required.  Note that,
110  *    if the `lookup' is FALSE then the hostname, IP and port information
111  *    will not be available from the socket stream.
112  *
113  ***/
114 SilcAsyncOperation
115 silc_socket_tcp_stream_create(int sock, SilcBool lookup,
116                               SilcBool require_fqdn,
117                               SilcSchedule schedule,
118                               SilcSocketStreamCallback callback,
119                               void *context);
120
121 /****f* silcutil/SilcSocketStreamAPI/silc_socket_udp_stream_create
122  *
123  * SYNOPSIS
124  *
125  *    SilcStream silc_socket_udp_stream_create(int sock, SilcBool ipv6,
126  *                                             SilcSchedule schedule);
127  *
128  * DESCRIPTION
129  *
130  *    Creates UDP socket stream of the UDP connection indicated by `sock'.
131  *    The stream can be destroyed by calling the silc_stream_destroy.
132  *
133  *    Note that, UDP packets may be read only through the notifier
134  *    callback (see silc_stream_set_notifier), when SILC_STREAM_CAN_READ
135  *    is returned to the callback.  Because of this the notifier callback
136  *    must be set.
137  *
138  *    Note that, UDP packet sending using silc_stream_write and receiving
139  *    with silc_stream_read works only if the `sock' is a UDP socket in a
140  *    connected state.  If it is not the silc_net_udp_send function and
141  *    silc_net_udp_receive functions must be used.  The SILC_STREAM_CAN_WRITE
142  *    is never returned to the notifier callback.
143  *
144  *    This function returns the created SilcStream or NULL on error.
145  *
146  ***/
147 SilcStream silc_socket_udp_stream_create(int sock, SilcBool ipv6,
148                                          SilcSchedule schedule);
149
150 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_info
151  *
152  * SYNOPSIS
153  *
154  *    SilcBool
155  *    silc_socket_stream_get_info(SilcStream stream,
156  *                                int *sock, const char **hostname,
157  *                                const char **ip, SilcUInt16 *port);
158  *
159  * DESCRIPTION
160  *
161  *    Returns socket stream information such as the socket number, hostname,
162  *    IP address and the port of the remote socket connection.  Return FALSE
163  *    if these informations are not available.
164  *
165  ***/
166 SilcBool silc_socket_stream_get_info(SilcStream stream,
167                                      int *sock, const char **hostname,
168                                      const char **ip, SilcUInt16 *port);
169
170 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_info
171  *
172  * SYNOPSIS
173  *
174  *    SilcBool
175  *    silc_socket_stream_set_info(SilcStream stream,
176  *                                const char *hostname,
177  *                                const char *ip, SilcUInt16 port);
178  *
179  * DESCRIPTION
180  *
181  *    Use this function to set the hostname, IP address and remote port
182  *    information to the socket stream indicated by `stream' if you did not
183  *    perform lookup in the silc_socket_create_stream.  This is not mandatory
184  *    but if you would like to associate the information with the stream
185  *    use this function.  If the lookup was performed when creating the
186  *    stream then calling this function is not necessary.  Use the function
187  *    silc_socket_stream_get_info to get the information from the stream.
188  *
189  ***/
190 SilcBool silc_socket_stream_set_info(SilcStream stream,
191                                      const char *hostname,
192                                      const char *ip, SilcUInt16 port);
193
194 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_error
195  *
196  * SYNOPSIS
197  *
198  *    int silc_socket_stream_get_error(SilcStream stream);
199  *
200  * DESCRIPTION
201  *
202  *    If error occurred during socket stream operations, this function
203  *    can be used to retrieve the error number that occurred.
204  *
205  ***/
206 int silc_socket_stream_get_error(SilcStream stream);
207
208 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_qos
209  *
210  * SYNOPSIS
211  *
212  *    SilcBool silc_socket_stream_set_qos(SilcStream stream,
213  *                                        SilcUInt32 read_rate,
214  *                                        SilcUInt32 read_limit_bytes,
215  *                                        SilcUInt32 limit_sec,
216  *                                        SilcUInt32 limit_usec)
217  *
218  * DESCRIPTION
219  *
220  *    Sets a "Quality of Service" settings for socket stream `stream'.
221  *    The `read_rate' specifies the maximum read operations per second.
222  *    If more read operations are executed the limit will be applied for
223  *    the reading.  The `read_limit_bytes' specifies the maximum data
224  *    that is read.  It is guaranteed that silc_stream_read  never returns
225  *    more than `read_limit_bytes' of data.  The `limit_sec' and `limit_usec'
226  *    specifies the time limit that is applied if `read_rate' and/or
227  *    `read_limit_bytes' is reached.  If all arguments except `stream'
228  *    are zero this resets the QoS from the socket stream, all QoS for
229  *    this socket stream that may be pending will be cancelled.
230  *
231  ***/
232 SilcBool silc_socket_stream_set_qos(SilcStream stream,
233                                     SilcUInt32 read_rate,
234                                     SilcUInt32 read_limit_bytes,
235                                     SilcUInt32 limit_sec,
236                                     SilcUInt32 limit_usec);
237
238 #include "silcsocketstream_i.h"
239
240 #endif /* SILCSOCKETSTREAM_H */