Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[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 - 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 /* $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       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       sock->sock_error = errno;
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     len = sock->qos->data_len;
110     sock->qos->data_len = 0;
111     return len;
112   }
113
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);
117     return -1;
118   }
119
120   /* Read the data from the socket.  The qosbuf size is always the max
121      read limit size. */
122   len = (buf_len < sock->qos->read_limit_bytes ? buf_len :
123          sock->qos->read_limit_bytes);
124   len = read(sock->sock, qosbuf, len);
125   if (len < 0) {
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     sock->sock_error = errno;
140     return -2;
141   }
142
143   SILC_LOG_DEBUG(("Read %d bytes", len));
144
145   if (!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;
149     return 0;
150   }
151
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) {
156     curtime.tv_sec++;
157     sock->qos->next_limit = curtime;
158     sock->qos->cur_rate = 0;
159   }
160   sock->qos->cur_rate++;
161
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;
168
169     /* Rate limit kicked in, do not return data yet */
170     silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
171     return -1;
172   }
173
174   /* Return the data from the QoS buffer */
175   memcpy(buf, qosbuf, len);
176   return len;
177 }
178
179 /* Stream write operation */
180
181 int silc_socket_stream_write(SilcStream stream, const unsigned char *data,
182                              SilcUInt32 data_len)
183 {
184   SilcSocketStream sock = stream;
185   int ret;
186
187   SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
188
189   ret = write(sock->sock, data, data_len);
190   if (ret < 0) {
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     sock->sock_error = errno;
200     return -2;
201   }
202
203   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
204   if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
205       SILC_TASK_WRITE)
206     silc_schedule_set_listen_fd(sock->schedule, sock->sock,
207                                 SILC_TASK_READ, FALSE);
208
209   return ret;
210 }
211
212 /* Receive UDP packet.  QoS is not supported. */
213
214 int silc_socket_udp_stream_read(SilcStream stream, unsigned char *buf,
215                                 SilcUInt32 buf_len)
216 {
217   return silc_net_udp_receive(stream, NULL, 0, NULL, buf, buf_len);
218 }
219
220 /* Send UDP packet.  This always succeeds. */
221
222 int silc_socket_udp_stream_write(SilcStream stream, const unsigned char *data,
223                                  SilcUInt32 data_len)
224 {
225   SilcSocketStream sock = stream;
226
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);
230
231   /* In connected state use normal writing to socket. */
232   return silc_socket_stream_write(stream, data, data_len);
233 }
234
235 #if 0
236 /* Returns human readable socket error message */
237
238 SilcBool silc_socket_get_error(SilcStream sock, char *error,
239                                SilcUInt32 error_len)
240 {
241   char *err;
242
243   if (!sock->sock_error)
244     return FALSE;
245
246   err = strerror(sock->sock_error);
247   if (strlen(err) > error_len)
248     return FALSE;
249
250   memset(error, 0, error_len);
251   memcpy(error, err, strlen(err));
252   return TRUE;
253 }
254 #endif
255
256 /* Closes socket */
257
258 SilcBool silc_socket_stream_close(SilcStream stream)
259 {
260   SilcSocketStream socket_stream = stream;
261
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);
267   }
268   silc_net_close_connection(socket_stream->sock);
269
270   return TRUE;
271 }
272
273 /* Destroys the stream */
274
275 void silc_socket_stream_destroy(SilcStream stream)
276 {
277   SilcSocketStream socket_stream = stream;
278
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);
284
285   if (socket_stream->qos) {
286     silc_schedule_task_del_by_context(socket_stream->schedule,
287                                       socket_stream->qos);
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);
292     }
293     silc_free(socket_stream->qos);
294   }
295
296   if (socket_stream->schedule)
297     silc_schedule_wakeup(socket_stream->schedule);
298
299   silc_free(socket_stream);
300 }
301
302 /* Sets stream notification callback for the stream */
303
304 SilcBool silc_socket_stream_notifier(SilcStream stream,
305                                      SilcSchedule schedule,
306                                      SilcStreamNotifier callback,
307                                      void *context)
308 {
309   SilcSocketStream socket_stream = stream;
310
311   SILC_LOG_DEBUG(("Setting stream notifier callback"));
312
313   socket_stream->notifier = callback;
314   socket_stream->notifier_context = context;
315   socket_stream->schedule = schedule;
316
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);
320
321     /* Add the socket to scheduler.  Safe to call if already added. */
322     if (!silc_schedule_task_add_fd(socket_stream->schedule,
323                                    socket_stream->sock,
324                                    silc_socket_stream_io, socket_stream))
325       return FALSE;
326
327     /* Initially set socket for reading */
328     if (!silc_schedule_set_listen_fd(socket_stream->schedule,
329                                      socket_stream->sock,
330                                      SILC_TASK_READ, FALSE))
331       return 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);
338   }
339
340   if (socket_stream->schedule)
341     silc_schedule_wakeup(socket_stream->schedule);
342
343   return TRUE;
344 }