Merged from silc_1_0_branch.
[silc.git] / lib / silcutil / win32 / silcwin32sockconn.c
1 /*
2
3   silcwin32sockconn.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Writes data from encrypted buffer to the socket connection. If the
25    data cannot be written at once, it will be written later with a timeout. 
26    The data is written from the data section of the buffer, not from head
27    or tail section. This automatically pulls the data section towards end
28    after writing the data. */
29
30 int silc_socket_write(SilcSocketConnection sock)
31 {
32   int ret = 0, err;
33   SOCKET fd = sock->sock;
34   SilcBuffer src = sock->outbuf;
35
36   if (!src)
37     return -2;
38   if (SILC_IS_DISABLED(sock))
39     return -1;
40
41   SILC_LOG_DEBUG(("Writing data to socket %d", fd));
42
43   if (src->len > 0) {
44     ret = send(fd, src->data, src->len,  0);
45     if (ret == SOCKET_ERROR) {
46       err = WSAGetLastError();
47       if (err == WSAEWOULDBLOCK) {
48         SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
49         return -2;
50       }
51       SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd));
52       sock->sock_error = err;
53       return -1;
54     }
55
56     if (ret < src->len) {
57       SILC_LOG_DEBUG(("Wrote data %d of %d bytes, will write rest later",
58                       ret, src->len));
59       silc_buffer_pull(src, ret);
60       return -2;
61     }
62
63     silc_buffer_pull(src, ret);
64   }
65
66   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
67
68   return ret;
69 }
70
71 /* Reads data from the socket connection into the incoming data buffer.
72    It reads as much as possible from the socket connection. This returns
73    amount of bytes read or -1 on error or -2 on case where all of the
74    data could not be read at once. */
75
76 int silc_socket_read(SilcSocketConnection sock)
77 {
78   int len = 0, err;
79   unsigned char buf[SILC_SOCKET_READ_SIZE];
80   SOCKET fd = sock->sock;
81   int argp;
82
83   if (SILC_IS_DISABLED(sock))
84     return -1;
85
86   SILC_LOG_DEBUG(("Reading data from socket %d", fd));
87
88   /* Check whether there is data available, without calling recv(). */
89   ioctlsocket(fd, FIONREAD, (unsigned long *)&argp);
90   if (argp == 0) {
91     /* Is this kludge or what? Without this thing this contraption
92        does not work at all!?. */
93     SleepEx(1, TRUE);
94     SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
95     return -2;
96   }
97
98   /* Read the data from the socket. */
99   len = recv(fd, buf, sizeof(buf), 0);
100   if (len == SOCKET_ERROR) {
101     err = WSAGetLastError();
102     if (err == WSAEWOULDBLOCK || err == WSAEINTR) {
103       SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
104       return -2;
105     }
106     SILC_LOG_ERROR(("Cannot read from socket: %d", (int)fd));
107     sock->sock_error = err;
108     return -1;
109   }
110
111   if (!len)
112     return 0;
113
114   /* Insert the data to the buffer. */
115
116   if (!sock->inbuf)
117     sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE);
118   
119   /* If the data does not fit to the buffer reallocate it */
120   if ((sock->inbuf->end - sock->inbuf->tail) < len)
121     sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + 
122                                       (len * 2));
123   silc_buffer_put_tail(sock->inbuf, buf, len);
124   silc_buffer_pull_tail(sock->inbuf, len);
125
126   SILC_LOG_DEBUG(("Read %d bytes", len));
127
128   return len;
129 }
130
131 /* Returns human readable socket error message */
132
133 bool silc_socket_get_error(SilcSocketConnection sock, char *error,
134                            SilcUInt32 error_len)
135 {
136   /* XXX TODO */
137   return FALSE;
138 }