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