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.
20 #include "silcruntime.h"
22 /************************ Static utility functions **************************/
24 /* The IO process callback that calls the notifier callback to upper layer. */
26 SILC_TASK_CALLBACK(silc_socket_stream_io)
28 SilcSocketStream stream = context;
30 if (silc_unlikely(!stream->notifier))
35 stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context);
39 stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
47 /**************************** Stream Operations *****************************/
49 /* QoS read handler, this will call the read and write events to indicate
50 that data is available again after a timeout. */
52 SILC_TASK_CALLBACK(silc_socket_read_qos)
54 SilcSocketQos qos = context;
56 silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
57 (SILC_TASK_READ | SILC_TASK_WRITE), TRUE);
59 silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
60 SILC_TASK_READ, FALSE);
63 /* Stream read operation */
65 int silc_socket_stream_read(SilcStream stream, unsigned char *buf,
68 SilcSocketStream sock = stream;
70 struct timeval curtime;
71 unsigned char *qosbuf;
73 SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock));
75 /* Handle the simple non-QoS reading. */
77 len = read(sock->sock, buf, buf_len);
79 silc_set_errno_posix(errno);
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);
94 SILC_LOG_DEBUG(("Read %d bytes", len));
97 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
102 /* We have QoS set, and reading is done via the QoS system. */
103 qosbuf = sock->qos->buffer;
105 /* If QoS was applied, return the data that was pending. */
106 if (sock->qos->applied && sock->qos->data_len) {
107 memcpy(buf, qosbuf, sock->qos->data_len);
108 len = sock->qos->data_len;
109 sock->qos->data_len = 0;
113 /* If we have active QoS data pending, return with no data */
114 if (sock->qos->data_len) {
115 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
119 /* Read the data from the socket. The qosbuf size is always the max
121 len = (buf_len < sock->qos->read_limit_bytes ? buf_len :
122 sock->qos->read_limit_bytes);
123 len = read(sock->sock, qosbuf, len);
125 silc_set_errno_posix(errno);
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;
142 SILC_LOG_DEBUG(("Read %d bytes", len));
145 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
146 silc_schedule_task_del_by_context(sock->schedule, sock->qos);
147 sock->qos->data_len = 0;
151 /* If we have passed the rate time limit, set our new time limit,
152 and zero the rate limit. This limits reads per second. */
153 silc_gettimeofday(&curtime);
154 if (silc_compare_timeval(&curtime, &sock->qos->next_limit) > 0) {
156 sock->qos->next_limit = curtime;
157 sock->qos->cur_rate = 0;
159 sock->qos->cur_rate++;
161 /* If we are not within rate limit apply QoS for the read data */
162 if (sock->qos->cur_rate > sock->qos->read_rate) {
163 silc_schedule_task_add_timeout(sock->schedule, silc_socket_read_qos,
164 sock->qos, sock->qos->limit_sec,
165 sock->qos->limit_usec);
166 sock->qos->data_len = len;
168 /* Rate limit kicked in, do not return data yet */
169 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
173 /* Return the data from the QoS buffer */
174 memcpy(buf, qosbuf, len);
178 /* Stream write operation */
180 int silc_socket_stream_write(SilcStream stream, const unsigned char *data,
183 SilcSocketStream sock = stream;
186 SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
188 ret = write(sock->sock, data, data_len);
190 silc_set_errno_posix(errno);
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);
202 SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
203 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
205 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
206 SILC_TASK_READ, FALSE);
211 /* Receive UDP packet. QoS is not supported. */
213 int silc_socket_udp_stream_read(SilcStream stream, unsigned char *buf,
216 return silc_net_udp_receive(stream, NULL, 0, NULL, buf, buf_len);
219 /* Send UDP packet. This always succeeds. */
221 int silc_socket_udp_stream_write(SilcStream stream, const unsigned char *data,
224 SilcSocketStream sock = stream;
226 /* In connectionless state check if remote IP and port is provided */
227 if (!sock->connected && sock->ip && sock->port)
228 return silc_net_udp_send(stream, sock->ip, sock->port, data, data_len);
230 /* In connected state use normal writing to socket. */
231 return silc_socket_stream_write(stream, data, data_len);
236 SilcBool silc_socket_stream_close(SilcStream stream)
238 SilcSocketStream socket_stream = stream;
240 if (socket_stream->schedule) {
241 silc_schedule_unset_listen_fd(socket_stream->schedule,
242 socket_stream->sock);
243 silc_schedule_task_del_by_fd(socket_stream->schedule,
244 socket_stream->sock);
246 silc_net_close_connection(socket_stream->sock);
251 /* Destroys the stream */
253 void silc_socket_stream_destroy(SilcStream stream)
255 SilcSocketStream socket_stream = stream;
257 silc_socket_stream_close(socket_stream);
258 silc_free(socket_stream->ip);
259 silc_free(socket_stream->hostname);
260 if (socket_stream->schedule)
261 silc_schedule_task_del_by_fd(socket_stream->schedule, socket_stream->sock);
263 if (socket_stream->qos) {
264 silc_schedule_task_del_by_context(socket_stream->schedule,
266 if (socket_stream->qos->buffer) {
267 memset(socket_stream->qos->buffer, 0,
268 socket_stream->qos->read_limit_bytes);
269 silc_free(socket_stream->qos->buffer);
271 silc_free(socket_stream->qos);
274 if (socket_stream->schedule)
275 silc_schedule_wakeup(socket_stream->schedule);
277 silc_free(socket_stream);
280 /* Sets stream notification callback for the stream */
282 SilcBool silc_socket_stream_notifier(SilcStream stream,
283 SilcSchedule schedule,
284 SilcStreamNotifier callback,
287 SilcSocketStream socket_stream = stream;
289 SILC_LOG_DEBUG(("Setting stream notifier callback"));
291 socket_stream->notifier = callback;
292 socket_stream->notifier_context = context;
293 socket_stream->schedule = schedule;
295 if (socket_stream->notifier && socket_stream->schedule) {
296 /* Set the socket to non-blocking mode */
297 silc_net_set_socket_nonblock(socket_stream->sock);
299 /* Add the socket to scheduler. Safe to call if already added. */
300 if (!silc_schedule_task_add_fd(socket_stream->schedule,
302 silc_socket_stream_io, socket_stream))
305 /* Initially set socket for reading */
306 if (!silc_schedule_set_listen_fd(socket_stream->schedule,
308 SILC_TASK_READ, FALSE))
310 } else if (socket_stream->schedule) {
311 /* Unschedule the socket */
312 silc_schedule_unset_listen_fd(socket_stream->schedule,
313 socket_stream->sock);
314 silc_schedule_task_del_by_fd(socket_stream->schedule,
315 socket_stream->sock);
318 if (socket_stream->schedule)
319 silc_schedule_wakeup(socket_stream->schedule);