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