Merge branch 'silc.1.1.branch' of silc repo
[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     len = sock->qos->data_len;
109     sock->qos->data_len = 0;
110     return 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.  The qosbuf size is always the max
120      read limit size. */
121   len = (buf_len < sock->qos->read_limit_bytes ? buf_len :
122          sock->qos->read_limit_bytes);
123   len = read(sock->sock, qosbuf, len);
124   if (len < 0) {
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,
130                                                               sock->sock) |
131                                   SILC_TASK_READ, FALSE);
132       return -1;
133     }
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     return -2;
140   }
141
142   SILC_LOG_DEBUG(("Read %d bytes", len));
143
144   if (!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;
148     return 0;
149   }
150
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) {
155     curtime.tv_sec++;
156     sock->qos->next_limit = curtime;
157     sock->qos->cur_rate = 0;
158   }
159   sock->qos->cur_rate++;
160
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;
167
168     /* Rate limit kicked in, do not return data yet */
169     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
170     return -1;
171   }
172
173   /* Return the data from the QoS buffer */
174   memcpy(buf, qosbuf, len);
175   return len;
176 }
177
178 /* Stream write operation */
179
180 int silc_socket_stream_write(SilcStream stream, const unsigned char *data,
181                              SilcUInt32 data_len)
182 {
183   SilcSocketStream sock = stream;
184   int ret;
185
186   SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
187
188   ret = write(sock->sock, data, data_len);
189   if (ret < 0) {
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);
195       return -1;
196     }
197     SILC_LOG_DEBUG(("Cannot write to socket: %s", strerror(errno)));
198     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
199     return -2;
200   }
201
202   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
203   if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
204       SILC_TASK_WRITE)
205     silc_schedule_set_listen_fd(sock->schedule, sock->sock,
206                                 SILC_TASK_READ, FALSE);
207
208   return ret;
209 }
210
211 /* Receive UDP packet.  QoS is not supported. */
212
213 int silc_socket_udp_stream_read(SilcStream stream, unsigned char *buf,
214                                 SilcUInt32 buf_len)
215 {
216   return silc_net_udp_receive(stream, NULL, 0, NULL, buf, buf_len);
217 }
218
219 /* Send UDP packet.  This always succeeds. */
220
221 int silc_socket_udp_stream_write(SilcStream stream, const unsigned char *data,
222                                  SilcUInt32 data_len)
223 {
224   SilcSocketStream sock = stream;
225
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);
229
230   /* In connected state use normal writing to socket. */
231   return silc_socket_stream_write(stream, data, data_len);
232 }
233
234 /* Closes socket */
235
236 SilcBool silc_socket_stream_close(SilcStream stream)
237 {
238   SilcSocketStream socket_stream = stream;
239
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);
245   }
246   silc_net_close_connection(socket_stream->sock);
247
248   return TRUE;
249 }
250
251 /* Destroys the stream */
252
253 void silc_socket_stream_destroy(SilcStream stream)
254 {
255   SilcSocketStream socket_stream = stream;
256
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);
262
263   if (socket_stream->qos) {
264     silc_schedule_task_del_by_context(socket_stream->schedule,
265                                       socket_stream->qos);
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);
270     }
271     silc_free(socket_stream->qos);
272   }
273
274   if (socket_stream->schedule)
275     silc_schedule_wakeup(socket_stream->schedule);
276
277   silc_free(socket_stream);
278 }
279
280 /* Sets stream notification callback for the stream */
281
282 SilcBool silc_socket_stream_notifier(SilcStream stream,
283                                      SilcSchedule schedule,
284                                      SilcStreamNotifier callback,
285                                      void *context)
286 {
287   SilcSocketStream socket_stream = stream;
288
289   SILC_LOG_DEBUG(("Setting stream notifier callback"));
290
291   socket_stream->notifier = callback;
292   socket_stream->notifier_context = context;
293   socket_stream->schedule = schedule;
294
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);
298
299     /* Add the socket to scheduler.  Safe to call if already added. */
300     if (!silc_schedule_task_add_fd(socket_stream->schedule,
301                                    socket_stream->sock,
302                                    silc_socket_stream_io, socket_stream))
303       return FALSE;
304
305     /* Initially set socket for reading */
306     if (!silc_schedule_set_listen_fd(socket_stream->schedule,
307                                      socket_stream->sock,
308                                      SILC_TASK_READ, FALSE))
309       return 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);
316   }
317
318   if (socket_stream->schedule)
319     silc_schedule_wakeup(socket_stream->schedule);
320
321   return TRUE;
322 }