[MLton-commit] r5471

Matthew Fluet fluet at mlton.org
Mon Mar 26 19:34:48 PST 2007


Represent an inet socket port (sin_port field of struct sockaddr_in)
as Word16.t.

This appears to fix some of the problems reported with INetSock.


----------------------------------------------------------------------

U   mlton/trunk/basis-library/net/inet-sock.sml
U   mlton/trunk/basis-library/net/net.sig
U   mlton/trunk/basis-library/primitive/basis-ffi.sml
U   mlton/trunk/runtime/basis/Net/Socket/INetSock.c
U   mlton/trunk/runtime/basis-ffi.h
U   mlton/trunk/runtime/gen/basis-ffi.def
U   mlton/trunk/runtime/gen/basis-ffi.h
U   mlton/trunk/runtime/gen/basis-ffi.sml

----------------------------------------------------------------------

Modified: mlton/trunk/basis-library/net/inet-sock.sml
===================================================================
--- mlton/trunk/basis-library/net/inet-sock.sml	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/basis-library/net/inet-sock.sml	2007-03-27 03:34:46 UTC (rev 5471)
@@ -19,18 +19,15 @@
 
       fun toAddr (in_addr, port) =
          let
-            val port = C_Int.fromInt port
-            val port = Net.C_Int.hton port
+            val port = Word16.fromInt port
+                       handle Overflow => PosixError.raiseSys PosixError.inval
+            val port = Net.Word16.hton port
+            val (sa, salen, finish) = Socket.new_sock_addr ()
+            val _ = Prim.toAddr (NetHostDB.inAddrToWord8Vector in_addr,
+                                 port, sa, salen)
+
          in
-            if C_Int.< (port, 0) orelse C_Int.>= (port, 0x10000)
-               then PosixError.raiseSys PosixError.inval
-               else let
-                       val (sa, salen, finish) = Socket.new_sock_addr ()
-                       val _ = Prim.toAddr (NetHostDB.inAddrToWord8Vector in_addr,
-                                            port, sa, salen)
-                    in
-                       finish ()
-                    end
+            finish ()
          end
 
       fun any port = toAddr (NetHostDB.any (), port)
@@ -39,8 +36,8 @@
         let
           val () = Prim.fromAddr (Socket.unpackSockAddr sa)
           val port = Prim.getPort ()
-          val port = Net.C_Int.ntoh port
-          val port = C_Int.toInt port
+          val port = Net.Word16.ntoh port
+          val port = Word16.toInt port
           val (ia, finish) = NetHostDB.new_in_addr ()
           val _ = Prim.getInAddr (NetHostDB.preInAddrToWord8Array ia)
         in

Modified: mlton/trunk/basis-library/net/net.sig
===================================================================
--- mlton/trunk/basis-library/net/net.sig	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/basis-library/net/net.sig	2007-03-27 03:34:46 UTC (rev 5471)
@@ -7,6 +7,11 @@
 
 signature NET =
    sig
+      structure Word16 :
+         sig
+            val hton: Word16.word -> Word16.word
+            val ntoh: Word16.word -> Word16.word
+         end
       structure C_Int :
          sig
             val hton: C_Int.t -> C_Int.t

Modified: mlton/trunk/basis-library/primitive/basis-ffi.sml
===================================================================
--- mlton/trunk/basis-library/primitive/basis-ffi.sml	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/basis-library/primitive/basis-ffi.sml	2007-03-27 03:34:46 UTC (rev 5471)
@@ -1132,8 +1132,8 @@
 end
 val fromAddr = _import "Socket_INetSock_fromAddr" : (Word8.t) vector -> unit;
 val getInAddr = _import "Socket_INetSock_getInAddr" : (Word8.t) array -> unit;
-val getPort = _import "Socket_INetSock_getPort" : unit -> C_Int.t;
-val toAddr = _import "Socket_INetSock_toAddr" : (Word8.t) vector * C_Int.t * (Word8.t) array * (C_Socklen.t) ref -> unit;
+val getPort = _import "Socket_INetSock_getPort" : unit -> Word16.t;
+val toAddr = _import "Socket_INetSock_toAddr" : (Word8.t) vector * Word16.t * (Word8.t) array * (C_Socklen.t) ref -> unit;
 end
 val listen = _import "Socket_listen" : C_Sock.t * C_Int.t -> (C_Int.t) C_Errno.t;
 val MSG_CTRUNC = _const "Socket_MSG_CTRUNC" : C_Int.t;

Modified: mlton/trunk/runtime/basis/Net/Socket/INetSock.c
===================================================================
--- mlton/trunk/runtime/basis/Net/Socket/INetSock.c	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/runtime/basis/Net/Socket/INetSock.c	2007-03-27 03:34:46 UTC (rev 5471)
@@ -1,17 +1,17 @@
 #include "platform.h"
 
 void 
-Socket_INetSock_toAddr (Vector(Word8_t) in_addr, C_Int_t port, 
+Socket_INetSock_toAddr (Vector(Word8_t) in_addr, Word16_t port, 
                         Array(Word8_t) addr, Ref(C_Socklen_t) addrlen) {
   struct sockaddr_in *sa = (struct sockaddr_in*)addr;
 
   sa->sin_family = AF_INET;
-  sa->sin_port = port;
+  sa->sin_port = (uint16_t)port;
   sa->sin_addr = *(const struct in_addr*)in_addr;
   *((socklen_t*)addrlen) = sizeof(struct sockaddr_in);
 }
 
-static int fromAddr_port;
+static uint16_t fromAddr_port;
 static struct in_addr fromAddr_in_addr;
 
 void Socket_INetSock_fromAddr (Vector(Word8_t) addr) {
@@ -22,8 +22,8 @@
   fromAddr_in_addr = sa->sin_addr;
 }
 
-C_Int_t Socket_INetSock_getPort (void) {
-  return fromAddr_port;
+Word16_t Socket_INetSock_getPort (void) {
+  return (Word16_t)fromAddr_port;
 }
 
 void Socket_INetSock_getInAddr (Array(Word8_t) addr) {

Modified: mlton/trunk/runtime/basis-ffi.h
===================================================================
--- mlton/trunk/runtime/basis-ffi.h	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/runtime/basis-ffi.h	2007-03-27 03:34:46 UTC (rev 5471)
@@ -927,8 +927,8 @@
 extern const C_Int_t Socket_INetSock_Ctl_TCP_NODELAY;
 void Socket_INetSock_fromAddr(Vector(Word8_t));
 void Socket_INetSock_getInAddr(Array(Word8_t));
-C_Int_t Socket_INetSock_getPort(void);
-void Socket_INetSock_toAddr(Vector(Word8_t),C_Int_t,Array(Word8_t),Ref(C_Socklen_t));
+Word16_t Socket_INetSock_getPort(void);
+void Socket_INetSock_toAddr(Vector(Word8_t),Word16_t,Array(Word8_t),Ref(C_Socklen_t));
 C_Errno_t(C_Int_t) Socket_listen(C_Sock_t,C_Int_t);
 extern const C_Int_t Socket_MSG_CTRUNC;
 extern const C_Int_t Socket_MSG_DONTROUTE;

Modified: mlton/trunk/runtime/gen/basis-ffi.def
===================================================================
--- mlton/trunk/runtime/gen/basis-ffi.def	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/runtime/gen/basis-ffi.def	2007-03-27 03:34:46 UTC (rev 5471)
@@ -810,8 +810,8 @@
 Socket.INetSock.Ctl.TCP_NODELAY = _const : C_Int.t
 Socket.INetSock.fromAddr = _import : Word8.t vector -> unit
 Socket.INetSock.getInAddr = _import : Word8.t array -> unit
-Socket.INetSock.getPort = _import : unit -> C_Int.t
-Socket.INetSock.toAddr = _import : Word8.t vector * C_Int.t * Word8.t array * C_Socklen.t ref -> unit
+Socket.INetSock.getPort = _import : unit -> Word16.t
+Socket.INetSock.toAddr = _import : Word8.t vector * Word16.t * Word8.t array * C_Socklen.t ref -> unit
 Socket.MSG_CTRUNC = _const : C_Int.t
 Socket.MSG_DONTROUTE = _const : C_Int.t
 Socket.MSG_DONTWAIT = _const : C_Int.t

Modified: mlton/trunk/runtime/gen/basis-ffi.h
===================================================================
--- mlton/trunk/runtime/gen/basis-ffi.h	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/runtime/gen/basis-ffi.h	2007-03-27 03:34:46 UTC (rev 5471)
@@ -927,8 +927,8 @@
 extern const C_Int_t Socket_INetSock_Ctl_TCP_NODELAY;
 void Socket_INetSock_fromAddr(Vector(Word8_t));
 void Socket_INetSock_getInAddr(Array(Word8_t));
-C_Int_t Socket_INetSock_getPort(void);
-void Socket_INetSock_toAddr(Vector(Word8_t),C_Int_t,Array(Word8_t),Ref(C_Socklen_t));
+Word16_t Socket_INetSock_getPort(void);
+void Socket_INetSock_toAddr(Vector(Word8_t),Word16_t,Array(Word8_t),Ref(C_Socklen_t));
 C_Errno_t(C_Int_t) Socket_listen(C_Sock_t,C_Int_t);
 extern const C_Int_t Socket_MSG_CTRUNC;
 extern const C_Int_t Socket_MSG_DONTROUTE;

Modified: mlton/trunk/runtime/gen/basis-ffi.sml
===================================================================
--- mlton/trunk/runtime/gen/basis-ffi.sml	2007-03-26 20:16:16 UTC (rev 5470)
+++ mlton/trunk/runtime/gen/basis-ffi.sml	2007-03-27 03:34:46 UTC (rev 5471)
@@ -1132,8 +1132,8 @@
 end
 val fromAddr = _import "Socket_INetSock_fromAddr" : (Word8.t) vector -> unit;
 val getInAddr = _import "Socket_INetSock_getInAddr" : (Word8.t) array -> unit;
-val getPort = _import "Socket_INetSock_getPort" : unit -> C_Int.t;
-val toAddr = _import "Socket_INetSock_toAddr" : (Word8.t) vector * C_Int.t * (Word8.t) array * (C_Socklen.t) ref -> unit;
+val getPort = _import "Socket_INetSock_getPort" : unit -> Word16.t;
+val toAddr = _import "Socket_INetSock_toAddr" : (Word8.t) vector * Word16.t * (Word8.t) array * (C_Socklen.t) ref -> unit;
 end
 val listen = _import "Socket_listen" : C_Sock.t * C_Int.t -> (C_Int.t) C_Errno.t;
 val MSG_CTRUNC = _const "Socket_MSG_CTRUNC" : C_Int.t;




More information about the MLton-commit mailing list