[m-rev.] diff: [net] Improve compatibility with Windows

Paul Bone paul at bone.id.au
Mon Mar 2 21:35:09 AEDT 2015


Branches: master

---

[net] Improve compatibility with Windows

types.m:
    MSVC doesn't know that abort() cannot return, so add a return statement.

    Use inet_pton rather than inet_aton.

sockets.m:
    Sockets are not file descriptors on windows, so we cannot use read and
    write system calls.  Instead use recv and send.

netdb.m:
    Windows doesn't provide getprotobyname_r() so use getprotobyname() on
    Windows.
---
 extras/net/netdb.m   | 38 ++++++++++++++++++++++++++++++++++++++
 extras/net/sockets.m |  4 ++--
 extras/net/types.m   |  3 ++-
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/extras/net/netdb.m b/extras/net/netdb.m
index 2d1cc43..e4784bf 100644
--- a/extras/net/netdb.m
+++ b/extras/net/netdb.m
@@ -95,6 +95,10 @@
 "
 #ifdef MR_WIN32
   #define  error()      WSAGetLastError()
+
+#ifdef MR_THREAD_SAFE
+  static MercuryLock    lookup_lock = MR_MUTEX_ATTR;
+#endif
 #else
   #define  error()      errno
 #endif
@@ -129,6 +133,8 @@ getprotobyname(Name, MaybeProtocol, !IO) :-
         Found::out, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
+#ifndef MR_WIN32
+
     int result;
     struct protoent *temp = MR_GC_NEW(struct protoent);
     char *buffer = MR_GC_malloc_atomic(BufferSize);
@@ -136,6 +142,38 @@ getprotobyname(Name, MaybeProtocol, !IO) :-
     result = getprotobyname_r(Name, temp, buffer, BufferSize, &Protocol);
     Success = result == 0 ? MR_YES : MR_NO;
     Found = Protocol != NULL ? MR_YES : MR_NO;
+
+#else
+    struct protoent *temp;
+    int             num_aliases;
+    int             i;
+
+#ifdef MR_THREAD_SAFE
+    MR_LOCK(lookup_lock, ""getprotobyname_r"");
+#endif
+
+    temp = getprotobyname(Name);
+    if (temp != NULL) {
+        Protocol = MR_GC_NEW(struct protoent);
+        MR_make_aligned_string_copy(Protocol->p_name, temp->p_name);
+        for (num_aliases = 0; temp->p_aliases[num_aliases]; num_aliases++);
+        Protocol->p_aliases = MR_GC_NEW_ARRAY(char*, num_aliases);
+        for (i = 0; i < num_aliases; i++) {
+            MR_make_aligned_string_copy(Protocol->p_aliases[i],
+                temp->p_aliases[i]);
+        }
+        Protocol->p_proto = temp->p_proto;
+        Found = MR_YES;
+    } else {
+        Found = MR_NO;
+    }
+    Success = MR_YES;
+
+#ifdef MR_THREAD_SAFE
+    MR_UNLOCK(lookup_lock, ""getprotobyname_r"");
+#endif
+
+#endif /* RM_WIN32 */
 ").
 
 %-----------------------------------------------------------------------------%
diff --git a/extras/net/sockets.m b/extras/net/sockets.m
index e6f3897..ac41202 100644
--- a/extras/net/sockets.m
+++ b/extras/net/sockets.m
@@ -432,7 +432,7 @@ read(Socket, Len0, Result, !IO) :-
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
     "
         MR_allocate_bitmap_msg(Bitmap, Len*8, MR_ALLOC_ID);
-        BytesRead = read(Socket, Bitmap->elements, Len);
+        BytesRead = recv(Socket, Bitmap->elements, Len, 0);
         if (BytesRead == -1) {
             Errno = error();
         }
@@ -473,7 +473,7 @@ write(Socket, Bitmap, Offset, Result, !IO) :-
         BytesWritten::out, Errno::out, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
     "
-        BytesWritten = write(Socket, &Bitmap->elements[Offset], Len);
+        BytesWritten = send(Socket, &Bitmap->elements[Offset], Len, 0);
         if (BytesWritten == -1) {
             Errno = error();
         }
diff --git a/extras/net/types.m b/extras/net/types.m
index e0e49d5..d2ced08 100644
--- a/extras/net/types.m
+++ b/extras/net/types.m
@@ -155,7 +155,7 @@
 "
     Addr = MR_GC_NEW(struct in_addr);
 
-    SUCCESS_INDICATOR = inet_aton(String, Addr);
+    SUCCESS_INDICATOR = inet_pton(AF_INET, String, Addr);
 ").
 
 %-----------------------------------------------------------------------------%
@@ -211,6 +211,7 @@ to_string(Addr) = String :-
             default:
                 fprintf(stderr, ""Unhandled family\\n"");
                 abort();
+                return -1; /* MSVC doesn't understand abort(); */
         }
     }
 ").
-- 
2.1.4




More information about the reviews mailing list