Added SILC Thread Queue API
[silc.git] / lib / silcutil / unix / silcunixsocketstream.c
1 /*
2
3   silcunixsocketstream.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 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 /* $Id$ */
20
21 #include "silc.h"
22
23 /************************ Static utility functions **************************/
24
25 /* The IO process callback that calls the notifier callback to upper layer. */
26
27 SILC_TASK_CALLBACK(silc_socket_stream_io)
28 {
29   SilcSocketStream stream = context;
30
31   if (silc_unlikely(!stream->notifier))
32     return;
33
34   switch (type) {
35   case SILC_TASK_READ:
36     stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context);
37     break;
38
39   case SILC_TASK_WRITE:
40     stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
41     break;
42
43   default:
44     break;
45   }
46 }
47
48 /**************************** Stream Operations *****************************/
49
50 /* QoS read handler, this will call the read and write events to indicate
51    that data is available again after a timeout. */
52
53 SILC_TASK_CALLBACK(silc_socket_read_qos)
54 {
55   SilcSocketQos qos = context;
56   qos->applied = TRUE;
57   silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
58                               (SILC_TASK_READ | SILC_TASK_WRITE), TRUE);
59   qos->applied = FALSE;
60   silc_schedule_set_listen_fd(qos->sock->schedule, qos->sock->sock,
61                               SILC_TASK_READ, FALSE);
62 }
63
64 /* Stream read operation */
65
66 int silc_socket_stream_read(SilcStream stream, unsigned char *buf,
67                             SilcUInt32 buf_len)
68 {
69   SilcSocketStream sock = stream;
70   int len = 0;
71   struct timeval curtime;
72   unsigned char *qosbuf;
73
74   SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock));
75
76   /* Handle the simple non-QoS reading. */
77   if (!sock->qos) {
78     len = read(sock->sock, buf, buf_len);
79     if (len < 0) {
80       silc_set_errno_posix(errno);
81       if (errno == EAGAIN || errno == EINTR) {
82         SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
83         silc_schedule_set_listen_fd(sock->schedule, sock->sock,
84                                     silc_schedule_get_fd_events(sock->schedule,
85                                                                 sock->sock) |
86                                     SILC_TASK_READ, FALSE);
87         return -1;
88       }
89       SILC_LOG_DEBUG(("Cannot read from socket: %d:%s",
90                       sock->sock, strerror(errno)));
91       silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
92       return -2;
93     }
94
95     SILC_LOG_DEBUG(("Read %d bytes", len));
96
97     if (!len)
98       silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
99
100     return len;
101   }
102
103   /* We have QoS set, and reading is done via the QoS system. */
104   qosbuf = sock->qos->buffer;
105
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     sock->qos->data_len = 0;
110     return sock->qos->data_len;
111   }
112
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);
116     return -1;
117   }
118
119   /* Read the data from the socket.  Never read more than the max limit. */
120   len = (buf_len < sock->qos->read_limit_bytes ? buf_len :
121          sock->qos->read_limit_bytes);
122   len = read(sock->sock, qosbuf, len);
123   if (len < 0) {
124     silc_set_errno_posix(errno);
125     if (errno == EAGAIN || errno == EINTR) {
126       SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
127       silc_schedule_set_listen_fd(sock->schedule, sock->sock,
128                                   silc_schedule_get_fd_events(sock->schedule,
129                                                               sock->sock) |
130                                   SILC_TASK_READ, FALSE);
131       return -1;
132     }
133     SILC_LOG_DEBUG(("Cannot read from socket: %d:%s",
134                     sock->sock, strerror(errno)));
135     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
136     silc_schedule_task_del_by_context(sock->schedule, sock->qos);
137     sock->qos->data_len = 0;
138     return -2;
139   }
140
141   SILC_LOG_DEBUG(("Read %d bytes", len));
142
143   if (!len) {
144     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
145     silc_schedule_task_del_by_context(sock->schedule, sock->qos);
146     sock->qos->data_len = 0;
147     return 0;
148   }
149
150   /* If we have passed the rate time limit, set our new time limit,
151      and zero the rate limit.  This limits reads per second. */
152   silc_gettimeofday(&curtime);
153   if (silc_compare_timeval(&curtime, &sock->qos->next_limit) > 0) {
154     curtime.tv_sec++;
155     sock->qos->next_limit = curtime;
156     sock->qos->cur_rate = 0;
157   }
158   sock->qos->cur_rate++;
159
160   /* If we are not within rate limit apply QoS for the read data */
161   if (sock->qos->cur_rate > sock->qos->read_rate) {
162     silc_schedule_task_add_timeout(sock->schedule, silc_socket_read_qos,
163                                    sock->qos, sock->qos->limit_sec,
164                                    sock->qos->limit_usec);
165     sock->qos->data_len = len;
166
167     /* Rate limit kicked in, do not return data yet */
168     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
169     return -1;
170   }
171
172   /* Return the data from the QoS buffer */
173   memcpy(buf, qosbuf, len);
174   return len;
175 }
176
177 /* Stream write operation */
178
179 int silc_socket_stream_write(SilcStream stream, const unsigned char *data,
180                              SilcUInt32 data_len)
181 {
182   SilcSocketStream sock = stream;
183   int ret;
184
185   SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
186
187   ret = write(sock->sock, data, data_len);
188   if (ret < 0) {
189     silc_set_errno_posix(errno);
190     if (errno == EAGAIN || errno == EINTR) {
191       SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
192       silc_schedule_set_listen_fd(sock->schedule, sock->sock,
193                                   SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
194       return -1;
195     }
196     SILC_LOG_DEBUG(("Cannot write to socket: %s", strerror(errno)));
197     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
198     return -2;
199   }
200
201   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
202   if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
203       SILC_TASK_WRITE)
204     silc_schedule_set_listen_fd(sock->schedule, sock->sock,
205                                 SILC_TASK_READ, FALSE);
206
207   return ret;
208 }
209
210 /* Receive UDP packet.  QoS is not supported. */
211
212 int silc_socket_udp_stream_read(SilcStream stream, unsigned char *buf,
213                                 SilcUInt32 buf_len)
214 {
215   return silc_net_udp_receive(stream, NULL, 0, NULL, buf, buf_len);
216 }
217
218 /* Send UDP packet.  This always succeeds. */
219
220 int silc_socket_udp_stream_write(SilcStream stream, const unsigned char *data,
221                                  SilcUInt32 data_len)
222 {
223   SilcSocketStream sock = stream;
224
225   /* In connectionless state check if remote IP and port is provided */
226   if (!sock->connected && sock->ip && sock->port)
227     return silc_net_udp_send(stream, sock->ip, sock->port, data, data_len);
228
229   /* In connected state use normal writing to socket. */
230   return silc_socket_stream_write(stream, data, data_len);
231 }
232
233 /* Closes socket */
234
235 SilcBool silc_socket_stream_close(SilcStream stream)
236 {
237   SilcSocketStream socket_stream = stream;
238
239   if (socket_stream->schedule) {
240     silc_schedule_unset_listen_fd(socket_stream->schedule,
241                                   socket_stream->sock);
242     silc_schedule_task_del_by_fd(socket_stream->schedule,
243                                  socket_stream->sock);
244   }
245   silc_net_close_connection(socket_stream->sock);
246
247   return TRUE;
248 }
249
250 /* Destroys the stream */
251
252 void silc_socket_stream_destroy(SilcStream stream)
253 {
254   SilcSocketStream socket_stream = stream;
255
256   silc_socket_stream_close(socket_stream);
257   silc_free(socket_stream->ip);
258   silc_free(socket_stream->hostname);
259   if (socket_stream->schedule)
260     silc_schedule_task_del_by_fd(socket_stream->schedule, socket_stream->sock);
261
262   if (socket_stream->qos) {
263     silc_schedule_task_del_by_context(socket_stream->schedule,
264                                       socket_stream->qos);
265     if (socket_stream->qos->buffer) {
266       memset(socket_stream->qos->buffer, 0,
267              socket_stream->qos->read_limit_bytes);
268       silc_free(socket_stream->qos->buffer);
269     }
270     silc_free(socket_stream->qos);
271   }
272
273   if (socket_stream->schedule)
274     silc_schedule_wakeup(socket_stream->schedule);
275
276   silc_free(socket_stream);
277 }
278
279 /* Sets stream notification callback for the stream */
280
281 SilcBool silc_socket_stream_notifier(SilcStream stream,
282                                      SilcSchedule schedule,
283                                      SilcStreamNotifier callback,
284                                      void *context)
285 {
286   SilcSocketStream socket_stream = stream;
287
288   SILC_LOG_DEBUG(("Setting stream notifier callback"));
289
290   socket_stream->notifier = callback;
291   socket_stream->notifier_context = context;
292   socket_stream->schedule = schedule;
293
294   if (socket_stream->notifier && socket_stream->schedule) {
295     /* Set the socket to non-blocking mode */
296     silc_net_set_socket_nonblock(socket_stream->sock);
297
298     /* Add the socket to scheduler.  Safe to call if already added. */
299     if (!silc_schedule_task_add_fd(socket_stream->schedule,
300                                    socket_stream->sock,
301                                    silc_socket_stream_io, socket_stream))
302       return FALSE;
303
304     /* Initially set socket for reading */
305     if (!silc_schedule_set_listen_fd(socket_stream->schedule,
306                                      socket_stream->sock,
307                                      SILC_TASK_READ, FALSE))
308       return FALSE;
309   } else if (socket_stream->schedule) {
310     /* Unschedule the socket */
311     silc_schedule_unset_listen_fd(socket_stream->schedule,
312                                   socket_stream->sock);
313     silc_schedule_task_del_by_fd(socket_stream->schedule,
314                                  socket_stream->sock);
315   }
316
317   if (socket_stream->schedule)
318     silc_schedule_wakeup(socket_stream->schedule);
319
320   return TRUE;
321 }