Added SILC Thread Queue API
[silc.git] / lib / silcutil / os2 / silcos2sockconn.c
1 /*
2
3   silcos2sockconn.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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 /* XXX TODO */
22
23 #include "silc.h"
24
25 /* Writes data from encrypted buffer to the socket connection. If the
26    data cannot be written at once, it will be written later with a timeout. 
27    The data is written from the data section of the buffer, not from head
28    or tail section. This automatically pulls the data section towards end
29    after writing the data. */
30
31 int silc_socket_write(SilcSocketConnection sock)
32 {
33   int ret = 0, err;
34   SilcBuffer src = sock->outbuf;
35
36   if (SILC_IS_DISABLED(sock))
37     return -1;
38
39   SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
40
41   if (src->len > 0) {
42     ret = send(sock->sock, src->data, src->len, 0);
43     if (ret == -1) {
44       if (errno == EWOULDBLOCK) {
45         SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
46         return -2;
47       }
48       SILC_LOG_ERROR(("Cannot write to socket: %d", sock->sock));
49       sock->sock_error = errno;
50       return -1;
51     }
52
53     if (ret < src->len) {
54       SILC_LOG_DEBUG(("Wrote data %d of %d bytes, will write rest later",
55                       ret, src->len));
56       silc_buffer_pull(src, ret);
57       return -2;
58     }
59
60     silc_buffer_pull(src, ret);
61   }
62
63   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
64
65   return ret;
66 }
67
68 /* Reads data from the socket connection into the incoming data buffer.
69    It reads as much as possible from the socket connection. This returns
70    amount of bytes read or -1 on error or -2 on case where all of the
71    data could not be read at once. */
72
73 int silc_socket_read(SilcSocketConnection sock)
74 {
75   int len = 0;
76   unsigned char buf[SILC_SOCKET_READ_SIZE];
77
78   if (SILC_IS_DISABLED(sock))
79     return -1;
80
81   SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock));
82
83   /* Read the data from the socket. */
84   len = recv(sock->sock, buf, sizeof(buf), 0);
85   if (len == -1) {
86     if (errno == EWOULDBLOCK || errno == EINTR) {
87       SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
88       return -2;
89     }
90     SILC_LOG_ERROR(("Cannot read from socket: %d", sock->sock));
91     sock->sock_error = errno;
92     return -1;
93   }
94
95   if (!len)
96     return 0;
97
98   /* Insert the data to the buffer. */
99
100   if (!sock->inbuf)
101     sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE);
102   
103   /* If the data does not fit to the buffer reallocate it */
104   if ((sock->inbuf->end - sock->inbuf->tail) < len)
105     sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + 
106                                       (len * 2));
107   silc_buffer_put_tail(sock->inbuf, buf, len);
108   silc_buffer_pull_tail(sock->inbuf, len);
109
110   SILC_LOG_DEBUG(("Read %d bytes", len));
111
112   return len;
113 }
114
115 /* Returns human readable socket error message */
116
117 SilcBool silc_socket_get_error(SilcSocketConnection sock, char *error,
118                            SilcUInt32 error_len)
119 {
120   /* XXX TODO */
121   return FALSE;
122 }