X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fwin32%2Fsilcwin32sockconn.c;h=31f702f48ec2b913e05b2be89563522e8ac15439;hb=40f8443d8d3a6577336ee66d18e04d9ac4d956bb;hp=8ef70c8b0462a8a34f63f0b8c2379d7fdb700480;hpb=17c97c1222b60f0ab5f43090395ddad45ec202f3;p=silc.git diff --git a/lib/silcutil/win32/silcwin32sockconn.c b/lib/silcutil/win32/silcwin32sockconn.c index 8ef70c8b..31f702f4 100644 --- a/lib/silcutil/win32/silcwin32sockconn.c +++ b/lib/silcutil/win32/silcwin32sockconn.c @@ -4,12 +4,11 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2001 Pekka Riikonen + Copyright (C) 1997 - 2005 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,7 +18,7 @@ */ /* $Id$ */ -#include "silcincludes.h" +#include "silc.h" /* Writes data from encrypted buffer to the socket connection. If the data cannot be written at once, it will be written later with a timeout. @@ -33,10 +32,15 @@ int silc_socket_write(SilcSocketConnection sock) SOCKET fd = sock->sock; SilcBuffer src = sock->outbuf; + if (!src) + return -2; + if (SILC_IS_DISABLED(sock)) + return -1; + SILC_LOG_DEBUG(("Writing data to socket %d", fd)); if (src->len > 0) { - ret = recv(fd, src->data, src->len, 0); + ret = send(fd, src->data, src->len, 0); if (ret == SOCKET_ERROR) { err = WSAGetLastError(); if (err == WSAEWOULDBLOCK) { @@ -44,9 +48,17 @@ int silc_socket_write(SilcSocketConnection sock) return -2; } SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd)); + sock->sock_error = err; return -1; } + if (ret < src->len) { + SILC_LOG_DEBUG(("Wrote data %d of %d bytes, will write rest later", + ret, src->len)); + silc_buffer_pull(src, ret); + return -2; + } + silc_buffer_pull(src, ret); } @@ -65,9 +77,23 @@ int silc_socket_read(SilcSocketConnection sock) int len = 0, err; unsigned char buf[SILC_SOCKET_READ_SIZE]; SOCKET fd = sock->sock; + int argp; + + if (SILC_IS_DISABLED(sock)) + return -1; SILC_LOG_DEBUG(("Reading data from socket %d", fd)); + /* Check whether there is data available, without calling recv(). */ + ioctlsocket(fd, FIONREAD, (unsigned long *)&argp); + if (argp == 0) { + /* Is this kludge or what? Without this thing this contraption + does not work at all!?. */ + SleepEx(1, TRUE); + SILC_LOG_DEBUG(("Could not read immediately, will do it later")); + return -2; + } + /* Read the data from the socket. */ len = recv(fd, buf, sizeof(buf), 0); if (len == SOCKET_ERROR) { @@ -77,6 +103,7 @@ int silc_socket_read(SilcSocketConnection sock) return -2; } SILC_LOG_ERROR(("Cannot read from socket: %d", (int)fd)); + sock->sock_error = err; return -1; } @@ -99,3 +126,12 @@ int silc_socket_read(SilcSocketConnection sock) return len; } + +/* Returns human readable socket error message */ + +SilcBool silc_socket_get_error(SilcSocketConnection sock, char *error, + SilcUInt32 error_len) +{ + /* XXX TODO */ + return FALSE; +}