5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2007 Pekka Riikonen
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.
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.
20 /****h* silcutil/SILC Socket Stream Interface
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.
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.
35 #ifndef SILCSOCKETSTREAM_H
36 #define SILCSOCKETSTREAM_H
38 /****d* silcutil/SilcSocketStreamAPI/SilcSocketStreamStatus
42 * typedef enum { ... } SilcStreamStatus;
46 * Socket Stream status. This status is returned into the
47 * SilcSocketStreamCallback function after the socket stream is
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;
61 /****f* silcutil/SilcSocketStreamAPI/SilcSocketStreamCallback
65 * typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
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
81 * If the silc_stream_set_notifier is called the stream will be set to
85 typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
86 SilcStream stream, void *context);
88 /****f* silcutil/SilcSocketStreamAPI/silc_socket_tcp_stream_create
93 * silc_socket_tcp_stream_create(SilcSocket sock, SilcBool lookup,
94 * SilcBool require_fqdn,
95 * SilcSchedule schedule,
96 * SilcSocketStreamCallback callback,
101 * Creates TCP socket stream of the TCP connection indicated by `sock'.
102 * The stream can be destroyed by calling the silc_stream_destroy. Data
103 * can be sent and received from the stream by calling silc_stream_write
104 * and silc_stream_read. The creation process is asynchronous since
105 * socket connection information, such as hostname and IP address are
106 * resolved, so SilcAsyncOperation is returned which can be used to cancel
107 * the creation process. The `callback' will be called to return the
108 * created socket stream.
110 * If the `lookup' is TRUE then this will perform IP and hostname lookup
111 * for the socket. If the `require_fqdn' is TRUE then the socket must
112 * have valid hostname and IP address, otherwise the stream creation will
113 * fail. If it is FALSE then only valid IP address is required. Note that,
114 * if the `lookup' is FALSE then the hostname, IP and port information
115 * will not be available from the socket stream. In that case this will
116 * also return NULL as the `callback' is called immediately.
118 * If the silc_stream_set_notifier is called the stream is set to
123 silc_socket_tcp_stream_create(SilcSocket sock, SilcBool lookup,
124 SilcBool require_fqdn,
125 SilcSchedule schedule,
126 SilcSocketStreamCallback callback,
129 /****f* silcutil/SilcSocketStreamAPI/silc_socket_udp_stream_create
133 * SilcStream silc_socket_udp_stream_create(SilcSocket sock,
135 * SilcBool connected,
136 * SilcSchedule schedule);
140 * Creates UDP socket stream of the UDP connection indicated by `sock'.
141 * The stream can be destroyed by calling the silc_stream_destroy.
142 * The `connected' defines whether the socket is in connected or in
143 * connectionless state.
145 * Note that, UDP packets may be read only through the notifier
146 * callback (see silc_stream_set_notifier), when SILC_STREAM_CAN_READ
147 * is returned to the callback. Because of this the notifier callback
150 * Note that, UDP packet sending using silc_stream_write and receiving
151 * with silc_stream_read works only if the `sock' is a UDP socket in a
152 * connected state. In connectionless state sending packets with
153 * silc_stream_write is possible only if the remote address and port
154 * has been set with silc_socket_stream_set_info. If it is not set
155 * in connectionless state packets may be sent only by using the
156 * silc_net_udp_send function. In connectionless state packets may be
157 * received only by using silc_net_udp_receive.
159 * This function returns the created SilcStream or NULL on error.
161 * If the silc_stream_set_notifier is called the stream is set to
165 SilcStream silc_socket_udp_stream_create(SilcSocket sock,
168 SilcSchedule schedule);
170 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_is_udp
174 * SilcBool silc_socket_stream_is_udp(SilcStream stream,
175 * SilcBool *connected);
179 * Returns TRUE if the `stream' is UDP stream. If the `connected' pointer
180 * is non-NULL it will have indication whether the UDP stream is in
181 * connected state. If it is then packets can be read and written using
182 * silc_stream_read and silc_stream_write. If it is not then packets
183 * need to read and written by using silc_net_udp_receive and
187 SilcBool silc_socket_stream_is_udp(SilcStream stream, SilcBool *connected);
189 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_info
194 * silc_socket_stream_get_info(SilcStream stream,
195 * SilcSocket *sock, const char **hostname,
196 * const char **ip, SilcUInt16 *port);
200 * Returns socket stream information such as the socket, remote hostname,
201 * remote IP address and the remote port of the remote socket connection.
202 * Return FALSE if these informations are not available.
205 SilcBool silc_socket_stream_get_info(SilcStream stream,
206 SilcSocket *sock, const char **hostname,
207 const char **ip, SilcUInt16 *port);
209 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_info
214 * silc_socket_stream_set_info(SilcStream stream,
215 * const char *hostname,
216 * const char *ip, SilcUInt16 port);
220 * Use this function to set the hostname, IP address and remote port
221 * information to the socket stream indicated by `stream' if you did not
222 * perform lookup in the silc_socket_tcp_stream_create. This is not
223 * mandatory but if you would like to associate the information with the
224 * stream use this function. If the lookup was performed when creating
225 * the stream then calling this function is not necessary. Use the
226 * function silc_socket_stream_get_info to get the information from the
230 SilcBool silc_socket_stream_set_info(SilcStream stream,
231 const char *hostname,
232 const char *ip, SilcUInt16 port);
234 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_error
238 * int silc_socket_stream_get_error(SilcStream stream);
242 * If error occurred during socket stream operations, this function
243 * can be used to retrieve the error number that occurred.
246 int silc_socket_stream_get_error(SilcStream stream);
248 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_qos
252 * SilcBool silc_socket_stream_set_qos(SilcStream stream,
253 * SilcUInt32 read_rate,
254 * SilcUInt32 read_limit_bytes,
255 * SilcUInt32 limit_sec,
256 * SilcUInt32 limit_usec)
260 * Sets a "Quality of Service" settings for socket stream `stream'.
261 * The `read_rate' specifies the maximum read operations per second.
262 * If more read operations are executed the limit will be applied for
263 * the reading. The `read_limit_bytes' specifies the maximum data
264 * that is read. It is guaranteed that silc_stream_read never returns
265 * more than `read_limit_bytes' of data. The `limit_sec' and `limit_usec'
266 * specifies the time limit that is applied if `read_rate' and/or
267 * `read_limit_bytes' is reached. If all arguments except `stream'
268 * are zero this resets the QoS from the socket stream, all QoS for
269 * this socket stream that may be pending will be cancelled.
272 SilcBool silc_socket_stream_set_qos(SilcStream stream,
273 SilcUInt32 read_rate,
274 SilcUInt32 read_limit_bytes,
275 SilcUInt32 limit_sec,
276 SilcUInt32 limit_usec);
278 #include "silcsocketstream_i.h"
280 #endif /* SILCSOCKETSTREAM_H */