From e8f77fc9ecd3033a338e68f95512472fb5d80cfe Mon Sep 17 00:00:00 2001
From: Atsushi Suzuki <suzuki@digital-sensation.jp>
Date: Tue, 8 Oct 2013 14:57:32 +0900
Subject: [PATCH 2/2] fixed wrong error message in dcmnet with win32.

---
 dcmnet/libsrc/dul.cc    | 53 ++++++++++++++++++++------
 dcmnet/libsrc/dulfsm.cc | 98 +++++++++++++++++++++++++++++++++++++++----------
 dcmnet/libsrc/dulpriv.h |  3 ++
 3 files changed, 123 insertions(+), 31 deletions(-)

diff --git a/dcmnet/libsrc/dul.cc b/dcmnet/libsrc/dul.cc
index 71f0652..2367148 100644
--- a/dcmnet/libsrc/dul.cc
+++ b/dcmnet/libsrc/dul.cc
@@ -1563,7 +1563,7 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
       {
           char buf[256];
           OFOStringStream stream;
-          stream << "TCP Initialization Error: " << OFStandard::strerror(errno, buf, sizeof(buf))
+		  stream << "TCP Initialization Error: " << FormatSocketError(GetLastSocketError(), buf, sizeof(buf))
                  << ", getpeername failed on socket " << sock << OFStringStream_ends;
           OFSTRINGSTREAM_GETOFSTRING(stream, msg)
           return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
@@ -1630,13 +1630,17 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
         do
         {
             sock = accept((*network)->networkSpecific.TCP.listenSocket, &from, &len);
+#ifdef HAVE_WINSOCK_H
+		} while (sock == -1 && WSAGetLastError() == WSAEINTR);
+#else
         } while (sock == -1 && errno == EINTR);
+#endif
 
         if (sock < 0)
         {
             char buf[256];
             OFOStringStream stream;
-            stream << "TCP Initialization Error: " << OFStandard::strerror(errno, buf, sizeof(buf))
+            stream << "TCP Initialization Error: " << FormatSocketError(GetLastSocketError(), buf, sizeof(buf))
                    << ", accept failed on socket " << sock << OFStringStream_ends;
             OFSTRINGSTREAM_GETOFSTRING(stream, msg)
             return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
@@ -1819,7 +1823,7 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
     {
         char buf[256];
         OFOStringStream stream;
-        stream << "TCP Initialization Error: " << OFStandard::strerror(errno, buf, sizeof(buf))
+        stream << "TCP Initialization Error: " << FormatSocketError(GetLastSocketError(), buf, sizeof(buf))
                << ", setsockopt failed on socket " << sock << OFStringStream_ends;
         OFSTRINGSTREAM_GETOFSTRING(stream, msg)
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
@@ -1830,7 +1834,7 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
     {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
     }
 #endif
@@ -1857,7 +1861,7 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
         {
             char buf[256];
             OFString msg = "TCP Initialization Error: ";
-            msg += OFStandard::strerror(errno, buf, sizeof(buf));
+            msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
             return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
         }
     }
@@ -1947,7 +1951,7 @@ receiveTransportConnectionTCP(PRIVATE_NETWORKKEY ** network,
 #endif
       char buf[256];
       OFString msg = "TCP Initialization Error: ";
-      msg += OFStandard::strerror(errno, buf, sizeof(buf));
+      msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf)); // where is corresponding socket call?
       return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
     }
 
@@ -2082,7 +2086,7 @@ initializeNetworkTCP(PRIVATE_NETWORKKEY ** key, void *parameter)
       {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
       }
       reuse = 1;
@@ -2093,7 +2097,7 @@ initializeNetworkTCP(PRIVATE_NETWORKKEY ** key, void *parameter)
       {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
       }
 #endif
@@ -2105,7 +2109,7 @@ initializeNetworkTCP(PRIVATE_NETWORKKEY ** key, void *parameter)
       {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
       }
       /* Find out assigned port number and print it out */
@@ -2114,7 +2118,7 @@ initializeNetworkTCP(PRIVATE_NETWORKKEY ** key, void *parameter)
       {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
       }
 #ifdef HAVE_GUSI_H
@@ -2125,7 +2129,7 @@ initializeNetworkTCP(PRIVATE_NETWORKKEY ** key, void *parameter)
       {
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
       }
 #endif
@@ -2731,3 +2735,30 @@ void dumpExtNegList(SOPClassExtendedNegotiationSubItemList& lst)
     OFString str;
     COUT << dumpExtNegList(str, lst) << OFendl;
 }
+
+
+int GetLastSocketError()
+{
+#ifdef _WIN32
+	return WSAGetLastError();
+#else
+	return errno;
+#endif
+}
+
+const char* FormatSocketError(int errnum, char* buf, size_t len)
+{
+#ifdef _WIN32
+	FormatMessage(
+		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+		NULL,
+		errnum,
+		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+		buf,
+		len,
+		NULL);
+	return buf;
+#else
+	return OFStandard::strerror(errnum, buf, len);
+#endif
+}
\ No newline at end of file
diff --git a/dcmnet/libsrc/dulfsm.cc b/dcmnet/libsrc/dulfsm.cc
index 5b63819..6ae0101 100644
--- a/dcmnet/libsrc/dulfsm.cc
+++ b/dcmnet/libsrc/dulfsm.cc
@@ -2199,7 +2199,7 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
     {
       char buf[256];
       OFString msg = "TCP Initialization Error: ";
-      msg += OFStandard::strerror(errno, buf, sizeof(buf));
+      msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
       return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
     }
     server.sin_family = AF_INET;
@@ -2283,6 +2283,10 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
         timeout.tv_usec = 0;
 
         rc = select(s+1, NULL, &fdSet, NULL, &timeout);
+#ifdef HAVE_WINSOCK_H
+		// save errno for later
+		int errnum = WSAGetLastError();
+#endif
 
         // reset socket to blocking mode
 #ifdef HAVE_WINSOCK_H
@@ -2304,9 +2308,13 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
             if ((*association)->connection) delete (*association)->connection;
             (*association)->connection = NULL;
 
+#ifdef HAVE_WINSOCK_H
+			// restore errno from "select" above
+			WSASetLastError(errnum);
+#endif
             char buf[256];
             OFString msg = "TCP Initialization Error: ";
-            msg += OFStandard::strerror(errno, buf, sizeof(buf));
+            msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
             msg += " (Timeout)";
             return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
   }
@@ -2365,6 +2373,11 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
 
     if (rc < 0)
     {
+#if HAVE_WINSOCK_H
+		// save errno for later
+		int errnum = WSAGetLastError();
+#endif
+
         // an error other than timeout in non-blocking mode has occured,
         // either in connect() or in select().
 #ifdef HAVE_WINSOCK_H
@@ -2376,10 +2389,15 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
         (*association)->networkState = NETWORK_DISCONNECTED;
         if ((*association)->connection) delete (*association)->connection;
         (*association)->connection = NULL;
+		
+#ifdef HAVE_WINSOCK_H
+		// restore errno from above "connect"
+		WSASetLastError(errnum);
+#endif
 
         char buf[256];
         OFString msg = "TCP Initialization Error: ";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
     } else {
         // success - we've opened a TCP transport connection
@@ -2405,7 +2423,7 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
 
           char buf[256];
           OFString msg = "TCP Initialization Error: ";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf)); // ???
           return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
         }
         sockarg.l_onoff = 0;
@@ -2418,7 +2436,7 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
         {
           char buf[256];
           OFString msg = "TCP Initialization Error: ";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
         }
 #endif
@@ -2446,7 +2464,7 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
             {
               char buf[256];
               OFString msg = "TCP Initialization Error: ";
-              msg += OFStandard::strerror(errno, buf, sizeof(buf));
+              msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
               return makeDcmnetCondition(DULC_TCPINITERROR, OF_error, msg.c_str());
             }
         }
@@ -2536,12 +2554,17 @@ sendAssociationRQTCP(PRIVATE_NETWORKKEY ** /*network*/,
 
     do {
       nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(associateRequest.length + 6)) : 0;
+#ifdef HAVE_WINSOCK_H
+	} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
     } while (nbytes == -1 && errno == EINTR);
+#endif
+
     if ((unsigned long) nbytes != associateRequest.length + 6)
     {
       char buf[256];
       OFString msg = "TCP I/O Error (";
-      msg += OFStandard::strerror(errno, buf, sizeof(buf));
+      msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
       msg += ") occurred in routine: sendAssociationRQTCP";
       return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
     }
@@ -2620,12 +2643,17 @@ sendAssociationACTCP(PRIVATE_NETWORKKEY ** /*network*/,
 
     do {
       nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(associateReply.length + 6)) : 0;
+#ifdef HAVE_WINSOCK_H
+	} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
     } while (nbytes == -1 && errno == EINTR);
+#endif
+
     if ((unsigned long) nbytes != associateReply.length + 6)
     {
       char buf[256];
       OFString msg = "TCP I/O Error (";
-      msg += OFStandard::strerror(errno, buf, sizeof(buf));
+      msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
       msg += ") occurred in routine: sendAssociationACTCP";
       return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
     }
@@ -2696,12 +2724,17 @@ sendAssociationRJTCP(PRIVATE_NETWORKKEY ** /*network*/,
     {
         do {
           nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(pdu.length + 6)) : 0;
-        } while (nbytes == -1 && errno == EINTR);
+#ifdef HAVE_WINSOCK_H
+		} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
+		} while (nbytes == -1 && errno == EINTR);
+#endif
+
         if ((unsigned long) nbytes != pdu.length + 6)
         {
           char buf[256];
           OFString msg = "TCP I/O Error (";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           msg += ") occurred in routine: sendAssociationRJTCP";
           return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
         }
@@ -2757,12 +2790,17 @@ sendAbortTCP(DUL_ABORTITEMS * abortItems,
     if (cond.good()) {
         do {
           nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(pdu.length + 6)) : 0;
-        } while (nbytes == -1 && errno == EINTR);
+#ifdef HAVE_WINSOCK_H
+		} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
+		} while (nbytes == -1 && errno == EINTR);
+#endif
+
         if ((unsigned long) nbytes != pdu.length + 6)
         {
           char buf[256];
           OFString msg = "TCP I/O Error (";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           msg += ") occurred in routine: sendAbortTCP";
           return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
         }
@@ -2818,12 +2856,16 @@ sendReleaseRQTCP(PRIVATE_ASSOCIATIONKEY ** association)
     if (cond.good()) {
         do {
           nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(pdu.length + 6)) : 0;
-        } while (nbytes == -1 && errno == EINTR);
+#ifdef HAVE_WINSOCK_H
+		} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
+		} while (nbytes == -1 && errno == EINTR);
+#endif
         if ((unsigned long) nbytes != pdu.length + 6)
         {
           char buf[256];
           OFString msg = "TCP I/O Error (";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           msg += ") occurred in routine: sendReleaseRQTCP";
           return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
         }
@@ -2880,12 +2922,16 @@ sendReleaseRPTCP(PRIVATE_ASSOCIATIONKEY ** association)
     if (cond.good()) {
         do {
           nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(pdu.length + 6)) : 0;
-        } while (nbytes == -1 && errno == EINTR);
+#ifdef HAVE_WINSOCK_H
+		} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
+		} while (nbytes == -1 && errno == EINTR);
+#endif
         if ((unsigned long) nbytes != pdu.length + 6)
         {
           char buf[256];
           OFString msg = "TCP I/O Error (";
-          msg += OFStandard::strerror(errno, buf, sizeof(buf));
+          msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
           msg += ") occurred in routine: sendReleaseRPTCP";
           return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
         }
@@ -3033,14 +3079,18 @@ writeDataPDU(PRIVATE_ASSOCIATIONKEY ** association,
     do
     {
       nbytes = (*association)->connection ? (*association)->connection->write((char*)head, size_t(length)) : 0;
+#ifdef HAVE_WINSOCK_H
+	} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
     } while (nbytes == -1 && errno == EINTR);
+#endif
 
     /* if not all head information was sent, return an error */
     if ((unsigned long) nbytes != length)
     {
         char buf[256];
         OFString msg = "TCP I/O Error (";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         msg += ") occurred in routine: writeDataPDU";
         return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
     }
@@ -3050,14 +3100,18 @@ writeDataPDU(PRIVATE_ASSOCIATIONKEY ** association,
     {
       nbytes = (*association)->connection ? (*association)->connection->write((char*)pdu->presentationDataValue.data,
         size_t(pdu->presentationDataValue.length - 2)) : 0;
+#ifdef HAVE_WINSOCK_H
+	} while (nbytes == -1 && WSAGetLastError() == WSAEINTR);
+#else
     } while (nbytes == -1 && errno == EINTR);
+#endif
 
         /* if not all head information was sent, return an error */
     if ((unsigned long) nbytes != pdu->presentationDataValue.length - 2)
     {
         char buf[256];
         OFString msg = "TCP I/O Error (";
-        msg += OFStandard::strerror(errno, buf, sizeof(buf));
+        msg += FormatSocketError(GetLastSocketError(), buf, sizeof(buf));
         msg += ") occurred in routine: writeDataPDU";
         return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
     }
@@ -3637,8 +3691,12 @@ defragmentTCP(DcmTransportConnection *connection, DUL_BLOCKOPTIONS block, time_t
 
             /* data has become available, now call read(). */
             bytesRead = connection->read((char*)b, size_t(l));
-
-        } while (bytesRead == -1 && errno == EINTR);
+			
+#ifdef HAVE_WINSOCK_H
+		} while (bytesRead == -1 && WSAGetLastError() == WSAEINTR);
+#else
+		} while (bytesRead == -1 && errno == EINTR);
+#endif
 
         /* if we actually received data, move the buffer pointer to its own end, update the variable */
         /* that determines the end of the first loop, and update the reference parameter return variable */
diff --git a/dcmnet/libsrc/dulpriv.h b/dcmnet/libsrc/dulpriv.h
index 79a1151..edb22e1 100644
--- a/dcmnet/libsrc/dulpriv.h
+++ b/dcmnet/libsrc/dulpriv.h
@@ -111,4 +111,7 @@ OFCondition
 PRV_NextPDUType(PRIVATE_ASSOCIATIONKEY ** association,
 		DUL_BLOCKOPTIONS block, int timeout, unsigned char *type);
 
+int GetLastSocketError();
+const char* FormatSocketError(int errnum, char* buf, size_t len);
+
 #endif
-- 
1.8.4.msysgit.0

