5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 Pekka Riikonen
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.
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.
21 /* On BONE we can use Unix code */
23 #include "../unix/silcunixsockconn.c"
25 #include "silcincludes.h"
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. */
33 int silc_socket_write(SilcSocketConnection sock)
36 SilcBuffer src = sock->outbuf;
38 if (SILC_IS_DISABLED(sock))
41 SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock));
44 ret = send(sock->sock, src->data, src->len, 0);
46 if (errno == EWOULDBLOCK) {
47 SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
50 SILC_LOG_ERROR(("Cannot write to socket: %d", sock->sock));
51 sock->sock_error = errno;
56 SILC_LOG_DEBUG(("Wrote data %d of %d bytes, will write rest later",
58 silc_buffer_pull(src, ret);
62 silc_buffer_pull(src, ret);
65 SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
70 /* Reads data from the socket connection into the incoming data buffer.
71 It reads as much as possible from the socket connection. This returns
72 amount of bytes read or -1 on error or -2 on case where all of the
73 data could not be read at once. */
75 int silc_socket_read(SilcSocketConnection sock)
78 unsigned char buf[SILC_SOCKET_READ_SIZE];
80 if (SILC_IS_DISABLED(sock))
83 SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock));
85 /* Read the data from the socket. */
86 len = recv(sock->sock, buf, sizeof(buf), 0);
88 if (errno == EWOULDBLOCK || errno == EINTR) {
89 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
92 SILC_LOG_ERROR(("Cannot read from socket: %d", sock->sock));
93 sock->sock_error = errno;
100 /* Insert the data to the buffer. */
103 sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE);
105 /* If the data does not fit to the buffer reallocate it */
106 if ((sock->inbuf->end - sock->inbuf->tail) < len)
107 sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen +
109 silc_buffer_put_tail(sock->inbuf, buf, len);
110 silc_buffer_pull_tail(sock->inbuf, len);
112 SILC_LOG_DEBUG(("Read %d bytes", len));
117 /* Returns human readable socket error message */
119 bool silc_socket_get_error(SilcSocketConnection sock, char *error,
120 SilcUInt32 error_len)
124 if (!sock->sock_error)
127 err = strerror(sock->sock_error);
128 if (strlen(err) > error_len)
131 memset(error, 0, error_len);
132 memcpy(error, err, strlen(err));
136 #endif /* SILC_BEOS_BONE */