Finished winsock code cleanup

This commit is contained in:
mpc
2004-06-27 13:07:06 +00:00
committed by zzz
parent dc04b7cf09
commit f312318fab
3 changed files with 44 additions and 6 deletions

View File

@ -1,3 +1,7 @@
v1.20
* Full winsock compatibility - all Windows functions now return appropriate
error strings
v1.15 2004-06-23 v1.15 2004-06-23
* Added a new example program, warhammer-dgram (use with caution) * Added a new example program, warhammer-dgram (use with caution)
* Fixed some fatal bugs in datagram handling * Fixed some fatal bugs in datagram handling

View File

@ -104,7 +104,6 @@
#endif #endif
#ifdef WINSOCK #ifdef WINSOCK
#include <windows.h> #include <windows.h>
#include <winsock2.h>
#else #else
#include <unistd.h> #include <unistd.h>
#endif #endif

View File

@ -85,20 +85,25 @@ bool sam_close(void)
return true; return true;
#ifdef WINSOCK #ifdef WINSOCK
if (closesocket(samd) == 0) { if (closesocket(samd) == SOCKET_ERROR) {
samd_connected = false; SAMLOG("Failed closing the SAM connection (%s)",
sam_winsock_strerror(WSAGetLastError()));
return false;
}
samd_connected = false;
if (sam_winsock_cleanup() == SAM_OK)
return true; return true;
if (sam_winsock_cleanup() != SAM_OK) else
return false; return false;
#else #else
if (close(samd) == 0) { if (close(samd) == 0) {
samd_connected = false; samd_connected = false;
return true; return true;
#endif
} else { } else {
SAMLOG("Failed closing the SAM connection (%s)", strerror(errno)); SAMLOG("Failed closing the SAM connection (%s)", strerror(errno));
return false; return false;
} }
#endif
} }
/* /*
@ -145,8 +150,13 @@ samerr_t sam_connect(const char *samhost, uint16_t samport,
#endif #endif
if (!sam_socket_connect(samhost, samport)) { if (!sam_socket_connect(samhost, samport)) {
#ifdef WINSOCK
SAMLOG("Couldn't connect to SAM at %s:%u (%s)",
samhost, samport, sam_winsock_strerror(WSAGetLastError()));
#else
SAMLOG("Couldn't connect to SAM at %s:%u (%s)", SAMLOG("Couldn't connect to SAM at %s:%u (%s)",
samhost, samport, strerror(errno)); samhost, samport, strerror(errno));
#endif
SAMLOGS("Is your I2P router running?"); SAMLOGS("Is your I2P router running?");
return SAM_SOCKET_ERROR; return SAM_SOCKET_ERROR;
} }
@ -533,7 +543,12 @@ static ssize_t sam_read1(char *buf, size_t n)
if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */ if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */
continue; continue;
else { else {
#ifdef WINSOCK
SAMLOG("recv() failed: %s",
sam_winsock_strerror(WSAGetLastError()));
#else
SAMLOG("recv() failed: %s", strerror(errno)); SAMLOG("recv() failed: %s", strerror(errno));
#endif
sam_close(); sam_close();
sam_diedback(); sam_diedback();
return -1; return -1;
@ -593,7 +608,12 @@ static ssize_t sam_read2(void *buf, size_t n)
if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */ if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */
continue; continue;
else { else {
#ifdef WINSOCK
SAMLOG("recv() failed: %s",
sam_winsock_strerror(WSAGetLastError()));
#else
SAMLOG("recv() failed: %s", strerror(errno)); SAMLOG("recv() failed: %s", strerror(errno));
#endif
sam_close(); sam_close();
sam_diedback(); sam_diedback();
return -1; return -1;
@ -641,7 +661,11 @@ static bool sam_readable(void)
else if (rc > 0) else if (rc > 0)
return true; return true;
else { else {
#ifdef WINSOCK
SAMLOG("select() failed: %s", sam_winsock_strerror(WSAGetLastError()));
#else
SAMLOG("select() failed: %s", strerror(errno)); SAMLOG("select() failed: %s", strerror(errno));
#endif
return false; return false;
} }
} }
@ -825,16 +849,21 @@ retry:
h = gethostbyname2(hostname, AF_INET); h = gethostbyname2(hostname, AF_INET);
#endif #endif
if (h == NULL) { if (h == NULL) {
if (h_errno == TRY_AGAIN) {
#ifdef WINSOCK #ifdef WINSOCK
if (WSAGetLastError() == WSATRY_AGAIN) {
Sleep(1000); Sleep(1000);
#else #else
if (h_errno == TRY_AGAIN) {
sleep(1); sleep(1);
#endif #endif
goto retry; goto retry;
} else { } else {
SAMLOG("DNS resolution failed for %s", hostname); SAMLOG("DNS resolution failed for %s", hostname);
#ifdef WINSOCK
WSASetLastError(WSAHOST_NOT_FOUND);
#else
errno = ENOENT; errno = ENOENT;
#endif
return false; return false;
} }
} }
@ -842,6 +871,7 @@ retry:
#ifdef NO_INET_NTOP #ifdef NO_INET_NTOP
char *tmp; char *tmp;
tmp = inet_ntoa(a); tmp = inet_ntoa(a);
assert(tmp != NULL);
strlcpy(ipaddr, tmp, INET_ADDRSTRLEN); /* inet_ntoa() was very poorly designed */ strlcpy(ipaddr, tmp, INET_ADDRSTRLEN); /* inet_ntoa() was very poorly designed */
return true; return true;
#else #else
@ -1195,7 +1225,12 @@ static ssize_t sam_write(const void *buf, size_t n)
if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */ if (errno == EINTR) /* see Unix Network Pgming vol 1, Sec. 5.9 */
continue; continue;
else { else {
#ifdef WINSOCK
SAMLOG("send() failed: %s",
sam_winsock_strerror(WSAGetLastError()));
#else
SAMLOG("send() failed: %s", strerror(errno)); SAMLOG("send() failed: %s", strerror(errno));
#endif
sam_close(); sam_close();
sam_diedback(); sam_diedback();
return -1; return -1;