5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 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.
23 /************************ Static utility functions **************************/
25 /* The IO process callback that calls the notifier callback to upper layer. */
27 SILC_TASK_CALLBACK(silc_socket_stream_io)
29 SilcSocketStream stream = context;
31 if (silc_unlikely(!stream->notifier))
36 stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context);
40 stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
48 /**************************** Stream Operations *****************************/
50 /* QoS read handler, this will call the read and write events to indicate
51 that data is available again after a timeout. */
53 SILC_TASK_CALLBACK(silc_socket_read_qos)
55 SilcSocketQos qos = context;
57 silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
58 (SILC_TASK_READ | SILC_TASK_WRITE), TRUE);
60 silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
61 SILC_TASK_READ, FALSE);
64 /* Stream read operation */
66 int silc_socket_stream_read(SilcStream stream, unsigned char *buf,
69 SilcSocketStream sock = stream;
71 struct timeval curtime;
72 unsigned char *qosbuf;
74 SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock));
76 /* Handle the simple non-QoS reading. */
78 len = read(sock->sock, buf, buf_len);
80 if (errno == EAGAIN || errno == EINTR) {
81 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
82 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
83 silc_schedule_get_fd_events(sock->schedule,
85 SILC_TASK_READ, FALSE);
88 SILC_LOG_DEBUG(("Cannot read from socket: %d:%s",
89 sock->sock, strerror(errno)));
90 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
91 sock->sock_error = errno;
95 SILC_LOG_DEBUG(("Read %d bytes", len));
98 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
103 /* We have QoS set, and reading is done via the QoS system. */
104 qosbuf = sock->qos->buffer;
106 /* If QoS was applied, return the data that was pending. */
107 if (sock->qos->applied && sock->qos->data_len) {
108 memcpy(buf, qosbuf, sock->qos->data_len);
109 len = sock->qos->data_len;
110 sock->qos->data_len = 0;
114 /* If we have active QoS data pending, return with no data */
115 if (sock->qos->data_len) {
116 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
120 /* Read the data from the socket. The qosbuf size is always the max
122 len = (buf_len < sock->qos->read_limit_bytes ? buf_len :
123 sock->qos->read_limit_bytes);
124 len = read(sock->sock, qosbuf, len);
126 if (errno == EAGAIN || errno == EINTR) {
127 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
128 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
129 silc_schedule_get_fd_events(sock->schedule,
131 SILC_TASK_READ, FALSE);
134 SILC_LOG_DEBUG(("Cannot read from socket: %d:%s",
135 sock->sock, strerror(errno)));
136 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
137 silc_schedule_task_del_by_context(sock->schedule, sock->qos);
138 sock->qos->data_len = 0;
139 sock->sock_error = errno;
143 SILC_LOG_DEBUG(("Read %d bytes", len));
146 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
147 silc_schedule_task_del_by_context(sock->schedule, sock->qos);
148 sock->qos->data_len = 0;
152 /* If we have passed the rate time limit, set our new time limit,
153 and zero the rate limit. This limits reads per second. */
154 silc_gettimeofday(&curtime);
155 if (silc_compare_timeval(&curtime, &sock->qos->next_limit) > 0) {
157 sock->qos->next_limit = curtime;
158 sock->qos->cur_rate = 0;
160 sock->qos->cur_rate++;
162 /* If we are not within rate limit apply QoS for the read data */
163 if (sock->qos->cur_rate > sock->qos->read_rate) {
164 silc_schedule_task_add_timeout(sock->schedule, silc_socket_read_qos,
165 sock->qos, sock->qos->limit_sec,
166 sock->qos->limit_usec);
167 sock->qos->data_len = len;
169 /* Rate limit kicked in, do not return data yet */
170 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
174 /* Return the data from the QoS buffer */
175 memcpy(buf, qosbuf, len);
179 /* Stream write operation */
181 int silc_socket_stream_write(SilcStream stream, const unsigned char *data,
184 SilcSocketStream sock = stream;
187 SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
189 ret = write(sock->sock, data, data_len);
191 if (errno == EAGAIN || errno == EINTR) {
192 SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
193 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
194 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
197 SILC_LOG_DEBUG(("Cannot write to socket: %s", strerror(errno)));
198 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
199 sock->sock_error = errno;
203 SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
204 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
206 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
207 SILC_TASK_READ, FALSE);
212 /* Receive UDP packet. QoS is not supported. */
214 int silc_socket_udp_stream_read(SilcStream stream, unsigned char *buf,
217 return silc_net_udp_receive(stream, NULL, 0, NULL, buf, buf_len);
220 /* Send UDP packet. This always succeeds. */
222 int silc_socket_udp_stream_write(SilcStream stream, const unsigned char *data,
225 SilcSocketStream sock = stream;
227 /* In connectionless state check if remote IP and port is provided */
228 if (!sock->connected && sock->ip && sock->port)
229 return silc_net_udp_send(stream, sock->ip, sock->port, data, data_len);
231 /* In connected state use normal writing to socket. */
232 return silc_socket_stream_write(stream, data, data_len);
236 /* Returns human readable socket error message */
238 SilcBool silc_socket_get_error(SilcStream sock, char *error,
239 SilcUInt32 error_len)
243 if (!sock->sock_error)
246 err = strerror(sock->sock_error);
247 if (strlen(err) > error_len)
250 memset(error, 0, error_len);
251 memcpy(error, err, strlen(err));
258 SilcBool silc_socket_stream_close(SilcStream stream)
260 SilcSocketStream socket_stream = stream;
262 if (socket_stream->schedule) {
263 silc_schedule_unset_listen_fd(socket_stream->schedule,
264 socket_stream->sock);
265 silc_schedule_task_del_by_fd(socket_stream->schedule,
266 socket_stream->sock);
268 silc_net_close_connection(socket_stream->sock);
273 /* Destroys the stream */
275 void silc_socket_stream_destroy(SilcStream stream)
277 SilcSocketStream socket_stream = stream;
279 silc_socket_stream_close(socket_stream);
280 silc_free(socket_stream->ip);
281 silc_free(socket_stream->hostname);
282 if (socket_stream->schedule)
283 silc_schedule_task_del_by_fd(socket_stream->schedule, socket_stream->sock);
285 if (socket_stream->qos) {
286 silc_schedule_task_del_by_context(socket_stream->schedule,
288 if (socket_stream->qos->buffer) {
289 memset(socket_stream->qos->buffer, 0,
290 socket_stream->qos->read_limit_bytes);
291 silc_free(socket_stream->qos->buffer);
293 silc_free(socket_stream->qos);
296 if (socket_stream->schedule)
297 silc_schedule_wakeup(socket_stream->schedule);
299 silc_free(socket_stream);
302 /* Sets stream notification callback for the stream */
304 SilcBool silc_socket_stream_notifier(SilcStream stream,
305 SilcSchedule schedule,
306 SilcStreamNotifier callback,
309 SilcSocketStream socket_stream = stream;
311 SILC_LOG_DEBUG(("Setting stream notifier callback"));
313 socket_stream->notifier = callback;
314 socket_stream->notifier_context = context;
315 socket_stream->schedule = schedule;
317 if (socket_stream->notifier && socket_stream->schedule) {
318 /* Set the socket to non-blocking mode */
319 silc_net_set_socket_nonblock(socket_stream->sock);
321 /* Add the socket to scheduler. Safe to call if already added. */
322 if (!silc_schedule_task_add_fd(socket_stream->schedule,
324 silc_socket_stream_io, socket_stream))
327 /* Initially set socket for reading */
328 if (!silc_schedule_set_listen_fd(socket_stream->schedule,
330 SILC_TASK_READ, FALSE))
332 } else if (socket_stream->schedule) {
333 /* Unschedule the socket */
334 silc_schedule_unset_listen_fd(socket_stream->schedule,
335 socket_stream->sock);
336 silc_schedule_task_del_by_fd(socket_stream->schedule,
337 socket_stream->sock);
340 if (socket_stream->schedule)
341 silc_schedule_wakeup(socket_stream->schedule);