5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2006 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. 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
81 typedef void (*SilcSocketStreamCallback)(SilcSocketStreamStatus status,
82 SilcStream stream, void *context);
84 /****f* silcutil/SilcSocketStreamAPI/silc_socket_tcp_stream_create
89 * silc_socket_tcp_stream_create(int sock, SilcBool lookup,
90 * SilcBool require_fqdn,
91 * SilcSchedule schedule,
92 * SilcSocketStreamCallback callback,
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.
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. In that case this will
112 * also return NULL as the `callback' is called immediately.
116 silc_socket_tcp_stream_create(int sock, SilcBool lookup,
117 SilcBool require_fqdn,
118 SilcSchedule schedule,
119 SilcSocketStreamCallback callback,
122 /****f* silcutil/SilcSocketStreamAPI/silc_socket_udp_stream_create
126 * SilcStream silc_socket_udp_stream_create(int sock, SilcBool ipv6,
127 * SilcBool connected,
128 * SilcSchedule schedule);
132 * Creates UDP socket stream of the UDP connection indicated by `sock'.
133 * The stream can be destroyed by calling the silc_stream_destroy.
134 * The `connected' defines whether the socket is in connected or in
135 * connectionless state.
137 * Note that, UDP packets may be read only through the notifier
138 * callback (see silc_stream_set_notifier), when SILC_STREAM_CAN_READ
139 * is returned to the callback. Because of this the notifier callback
142 * Note that, UDP packet sending using silc_stream_write and receiving
143 * with silc_stream_read works only if the `sock' is a UDP socket in a
144 * connected state. In connectionless state sending packets with
145 * silc_stream_write is possible only if the remote address and port
146 * has been set with silc_socket_stream_set_info. If it is not set
147 * in connectionless state packets may be sent only by using the
148 * silc_net_udp_send function. In connectionless state packets may be
149 * received only by using silc_net_udp_receive.
151 * This function returns the created SilcStream or NULL on error.
154 SilcStream silc_socket_udp_stream_create(int sock, SilcBool ipv6,
156 SilcSchedule schedule);
158 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_info
162 * SilcBool silc_socket_stream_is_udp(SilcStream stream,
163 * SilcBool *connected);
167 * Returns TRUE if the `stream' is UDP stream. If the `connected' pointer
168 * is non-NULL indication whether the UDP stream is in connected state.
169 * If it is then packets can be read and written using silc_stream_read
170 * and silc_stream_write. If it is not then packets need to read and
171 * written by using silc_net_udp_receive and silc_net_udp_send.
174 SilcBool silc_socket_stream_is_udp(SilcStream stream, SilcBool *connected);
176 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_info
181 * silc_socket_stream_get_info(SilcStream stream,
182 * int *sock, const char **hostname,
183 * const char **ip, SilcUInt16 *port);
187 * Returns socket stream information such as the socket number, hostname,
188 * IP address and the port of the remote socket connection. Return FALSE
189 * if these informations are not available.
192 SilcBool silc_socket_stream_get_info(SilcStream stream,
193 int *sock, const char **hostname,
194 const char **ip, SilcUInt16 *port);
196 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_info
201 * silc_socket_stream_set_info(SilcStream stream,
202 * const char *hostname,
203 * const char *ip, SilcUInt16 port);
207 * Use this function to set the hostname, IP address and remote port
208 * information to the socket stream indicated by `stream' if you did not
209 * perform lookup in the silc_socket_tcp_stream_create. This is not
210 * mandatory but if you would like to associate the information with the
211 * stream use this function. If the lookup was performed when creating
212 * the stream then calling this function is not necessary. Use the
213 * function silc_socket_stream_get_info to get the information from the
217 SilcBool silc_socket_stream_set_info(SilcStream stream,
218 const char *hostname,
219 const char *ip, SilcUInt16 port);
221 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_get_error
225 * int silc_socket_stream_get_error(SilcStream stream);
229 * If error occurred during socket stream operations, this function
230 * can be used to retrieve the error number that occurred.
233 int silc_socket_stream_get_error(SilcStream stream);
235 /****f* silcutil/SilcSocketStreamAPI/silc_socket_stream_set_qos
239 * SilcBool silc_socket_stream_set_qos(SilcStream stream,
240 * SilcUInt32 read_rate,
241 * SilcUInt32 read_limit_bytes,
242 * SilcUInt32 limit_sec,
243 * SilcUInt32 limit_usec)
247 * Sets a "Quality of Service" settings for socket stream `stream'.
248 * The `read_rate' specifies the maximum read operations per second.
249 * If more read operations are executed the limit will be applied for
250 * the reading. The `read_limit_bytes' specifies the maximum data
251 * that is read. It is guaranteed that silc_stream_read never returns
252 * more than `read_limit_bytes' of data. The `limit_sec' and `limit_usec'
253 * specifies the time limit that is applied if `read_rate' and/or
254 * `read_limit_bytes' is reached. If all arguments except `stream'
255 * are zero this resets the QoS from the socket stream, all QoS for
256 * this socket stream that may be pending will be cancelled.
259 SilcBool silc_socket_stream_set_qos(SilcStream stream,
260 SilcUInt32 read_rate,
261 SilcUInt32 read_limit_bytes,
262 SilcUInt32 limit_sec,
263 SilcUInt32 limit_usec);
265 #include "silcsocketstream_i.h"
267 #endif /* SILCSOCKETSTREAM_H */