44115731d1865fb38923db403bfde363122a2811
[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   SILC_LOG_DEBUG(("Writing data to socket %d", fd));
37
38   if (src->len > 0) {
39     ret = send(fd, src->data, src->len,  0);
40     if (ret == SOCKET_ERROR) {
41       err = WSAGetLastError();
42       if (err == WSAEWOULDBLOCK) {
43         SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
44         return -2;
45       }
46       SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd));
47       return -1;
48     }
49
50     silc_buffer_pull(src, ret);
51   }
52
53   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
54
55   return ret;
56 }
57
58 /* Reads data from the socket connection into the incoming data buffer.
59    It reads as much as possible from the socket connection. This returns
60    amount of bytes read or -1 on error or -2 on case where all of the
61    data could not be read at once. */
62
63 int silc_socket_read(SilcSocketConnection sock)
64 {
65   int len = 0, err;
66   unsigned char buf[SILC_SOCKET_READ_SIZE];
67   SOCKET fd = sock->sock;
68   int argp;
69
70   SILC_LOG_DEBUG(("Reading data from socket %d", fd));
71
72   /* Check whether there is data available, without calling recv(). */
73   ioctlsocket(fd, FIONREAD, (unsigned long *)&argp);
74   if (argp == 0) {
75     /* Is this kludge or what? Without this thing this contraption
76        does not work at all!?. */
77     SleepEx(1, TRUE);
78     SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
79     return -2;
80   }
81
82   /* Read the data from the socket. */
83   len = recv(fd, buf, sizeof(buf), 0);
84   if (len == SOCKET_ERROR) {
85     err = WSAGetLastError();
86     if (err == WSAEWOULDBLOCK || err == WSAEINTR) {
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", (int)fd));
91     return -1;
92   }
93
94   if (!len)
95     return 0;
96
97   /* Insert the data to the buffer. */
98
99   if (!sock->inbuf)
100     sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE);
101   
102   /* If the data does not fit to the buffer reallocate it */
103   if ((sock->inbuf->end - sock->inbuf->tail) < len)
104     sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + 
105                                       (len * 2));
106   silc_buffer_put_tail(sock->inbuf, buf, len);
107   silc_buffer_pull_tail(sock->inbuf, len);
108
109   SILC_LOG_DEBUG(("Read %d bytes", len));
110
111   return len;
112 }