[m-rev.] [PATCH 2/2] [net] Make the C code compatible with Windows

Paul Bone paul at bone.id.au
Sat Jan 10 15:20:33 AEDT 2015


extras/net/netdb.m:
extras/net/sockets.m:
extras/net/tcp.m:
extras/net/types.m:
    Include the correct header files.

    Fix the order of statements in a code block.

    Workarround the missing SHUT_RDWR macro on windows.

    Avoid use of socklen_t.
---
 extras/net/netdb.m   | 10 ++++++++--
 extras/net/sockets.m |  7 ++++---
 extras/net/tcp.m     |  4 ++--
 extras/net/types.m   | 13 +++++++++----
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/extras/net/netdb.m b/extras/net/netdb.m
index 8021d7e..fe5ab15 100644
--- a/extras/net/netdb.m
+++ b/extras/net/netdb.m
@@ -82,7 +82,12 @@
 
 :- pragma foreign_decl("C",
 "
-#include <netdb.h>
+#ifdef MR_WIN32
+    #include <winsock2.h>
+    #include <ws2tcpip.h>
+#else
+    #include <netdb.h>
+#endif
 ").
 
 :- pragma foreign_decl("C", local,
@@ -159,9 +164,10 @@ c_protocol_to_protocol(CProto, Proto) :-
     c_protocol_get_aliases(Proto::in, List::uo),
     [may_call_mercury, promise_pure, thread_safe],
 "
-    List = MR_list_empty();
     int i = 0;
 
+    List = MR_list_empty();
+
     while (Proto->p_aliases[i] != NULL) {
         MR_String str;
         MR_make_aligned_string_copy_saved_hp(str, Proto->p_aliases[i], NULL);
diff --git a/extras/net/sockets.m b/extras/net/sockets.m
index 7d0dfdb..71f7dad 100644
--- a/extras/net/sockets.m
+++ b/extras/net/sockets.m
@@ -134,7 +134,8 @@
 :- pragma foreign_decl("C",
 "
 #ifdef MR_WIN32
-  #include <winsock.h>
+  #include <winsock2.h>
+  #include <ws2tcpip.h>
 #else
   #include <errno.h>
   #include <netdb.h>
@@ -180,7 +181,7 @@
 "
 #ifdef MR_WIN32
   #define  error()      WSAGetLastError()
-
+  #define  SHUT_RDWR    SD_BOTH
 #else /* !MR_WIN32 */
   #define  error()      errno
 
@@ -398,7 +399,7 @@ close(Socket, Result, !IO) :-
 "
     struct linger sockets_linger = { MR_TRUE, 2 };
     setsockopt(Socket, SOL_SOCKET, SO_LINGER,
-        &sockets_linger, sizeof(sockets_linger));
+        (char*)&sockets_linger, sizeof(sockets_linger));
     if (-1 == shutdown(Socket, SHUT_RDWR)) {
         Errno = error();
         Success = MR_NO;
diff --git a/extras/net/tcp.m b/extras/net/tcp.m
index 6d70000..b25b3f8 100644
--- a/extras/net/tcp.m
+++ b/extras/net/tcp.m
@@ -171,8 +171,8 @@ tcp.shutdown(tcp(_, Handle), !IO) :-
 
 :- pragma foreign_decl("C", "
 #ifdef MR_WIN32
-  #include <windows.h>
-  #include <winsock.h>
+  #include <winsock2.h>
+  #include <ws2tcpip.h>
   #include <sys/types.h>
 
   #define  ML_error()       WSAGetLastError()
diff --git a/extras/net/types.m b/extras/net/types.m
index 4077492..966bff5 100644
--- a/extras/net/types.m
+++ b/extras/net/types.m
@@ -94,8 +94,13 @@
 
 :- pragma foreign_decl("C",
 "
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef MR_WIN32
+    #include <winsock2.h>
+    #include <ws2tcpip.h>
+#else
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+#endif
 ").
 
 :- pragma foreign_decl("C", local,
@@ -193,12 +198,12 @@ to_string(Addr) = String :-
         struct sockaddr_in  in;
     };
 
-    socklen_t sock_addr_size(union my_sockaddr *addr);
+    size_t sock_addr_size(union my_sockaddr *addr);
 ").
 
 :- pragma foreign_code("C",
 "
-    socklen_t sock_addr_size(union my_sockaddr *addr) {
+    size_t sock_addr_size(union my_sockaddr *addr) {
         switch (addr->raw.sa_family) {
             case AF_INET:
                 return sizeof(struct sockaddr_in);
-- 
2.1.3




More information about the reviews mailing list