4b5f0ac11efffb255ed0da5d651c5187ab89ce2a
[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       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     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     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     sock->sock_error = errno;
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     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     sock->sock_error = errno;
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 #if 0
234 /* Returns human readable socket error message */
235
236 SilcBool silc_socket_get_error(SilcStream sock, char *error,
237                                SilcUInt32 error_len)
238 {
239   char *err;
240
241   if (!sock->sock_error)
242     return FALSE;
243
244   err = strerror(sock->sock_error);
245   if (strlen(err) > error_len)
246     return FALSE;
247
248   memset(error, 0, error_len);
249   memcpy(error, err, strlen(err));
250   return TRUE;
251 }
252 #endif
253
254 /* Closes socket */
255
256 SilcBool silc_socket_stream_close(SilcStream stream)
257 {
258   SilcSocketStream socket_stream = stream;
259
260   if (socket_stream->schedule) {
261     silc_schedule_unset_listen_fd(socket_stream->schedule,
262                                   socket_stream->sock);
263     silc_schedule_task_del_by_fd(socket_stream->schedule,
264                                  socket_stream->sock);
265   }
266   silc_net_close_connection(socket_stream->sock);
267
268   return TRUE;
269 }
270
271 /* Destroys the stream */
272
273 void silc_socket_stream_destroy(SilcStream stream)
274 {
275   SilcSocketStream socket_stream = stream;
276
277   silc_socket_stream_close(socket_stream);
278   silc_free(socket_stream->ip);
279   silc_free(socket_stream->hostname);
280   if (socket_stream->schedule)
281     silc_schedule_task_del_by_fd(socket_stream->schedule, socket_stream->sock);
282
283   if (socket_stream->qos) {
284     silc_schedule_task_del_by_context(socket_stream->schedule,
285                                       socket_stream->qos);
286     if (socket_stream->qos->buffer) {
287       memset(socket_stream->qos->buffer, 0,
288              socket_stream->qos->read_limit_bytes);
289       silc_free(socket_stream->qos->buffer);
290     }
291     silc_free(socket_stream->qos);
292   }
293
294   if (socket_stream->schedule)
295     silc_schedule_wakeup(socket_stream->schedule);
296
297   silc_free(socket_stream);
298 }
299
300 /* Sets stream notification callback for the stream */
301
302 SilcBool silc_socket_stream_notifier(SilcStream stream,
303                                      SilcSchedule schedule,
304                                      SilcStreamNotifier callback,
305                                      void *context)
306 {
307   SilcSocketStream socket_stream = stream;
308
309   SILC_LOG_DEBUG(("Setting stream notifier callback"));
310
311   socket_stream->notifier = callback;
312   socket_stream->notifier_context = context;
313   socket_stream->schedule = schedule;
314
315   if (socket_stream->notifier && socket_stream->schedule) {
316     /* Set the socket to non-blocking mode */
317     silc_net_set_socket_nonblock(socket_stream->sock);
318
319     /* Add the socket to scheduler.  Safe to call if already added. */
320     if (!silc_schedule_task_add_fd(socket_stream->schedule,
321                                    socket_stream->sock,
322                                    silc_socket_stream_io, socket_stream))
323       return FALSE;
324
325     /* Initially set socket for reading */
326     if (!silc_schedule_set_listen_fd(socket_stream->schedule,
327                                      socket_stream->sock,
328                                      SILC_TASK_READ, FALSE))
329       return FALSE;
330   } else if (socket_stream->schedule) {
331     /* Unschedule the socket */
332     silc_schedule_unset_listen_fd(socket_stream->schedule,
333                                   socket_stream->sock);
334     silc_schedule_task_del_by_fd(socket_stream->schedule,
335                                  socket_stream->sock);
336   }
337
338   if (socket_stream->schedule)
339     silc_schedule_wakeup(socket_stream->schedule);
340
341   return TRUE;
342 }