Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/system/Basis/Implementation/Sockets/pre-sock.sml
ViewVC logotype

Annotation of /sml/trunk/src/system/Basis/Implementation/Sockets/pre-sock.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1389 - (view) (download)

1 : monnier 416 (* pre-sock.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * These are some common type definitions used in the sockets library. This
6 :     * structure is called Sock, so that the signatures will compile.
7 :     *
8 :     *)
9 :    
10 :     local
11 :     structure SysWord = SysWordImp
12 :     structure Word8 = Word8Imp
13 :     structure Word = WordImp
14 :     in
15 :     structure PreSock =
16 :     struct
17 :    
18 :     (* the raw representation address data *)
19 :     type addr = Word8Vector.vector
20 :    
21 :     (* the raw representation of an address family *)
22 :     type af = CInterface.system_const
23 :    
24 : mblume 1389 (* the raw representation of a socket:
25 :     * a file descriptor for now and a mutable flag indicating
26 :     * (with a value of true) if the socket is currently set
27 :     * to non-blocking *)
28 :     type sockFD = int
29 :     type socket = { fd: sockFD, nb: bool ref }
30 : monnier 416
31 :     (* an internet address; this is here because it is abstract in the
32 :     * NetHostDB and IP structures.
33 :     *)
34 :     datatype in_addr = INADDR of addr
35 :    
36 :     (* an address family *)
37 :     datatype addr_family = AF of af
38 :    
39 :     (* socket types *)
40 :     datatype sock_type = SOCKTY of CInterface.system_const
41 :    
42 :     (* sockets are polymorphic; the instantiation of the type variables
43 :     * provides a way to distinguish between different kinds of sockets.
44 :     *)
45 :     datatype ('sock, 'af) sock = SOCK of socket
46 :     datatype 'af sock_addr = ADDR of addr
47 :    
48 :     (** Utility functions for parsing/unparsing network addresses **)
49 :     local
50 :     structure SysW = SysWord
51 :     structure SCvt = StringCvt
52 :     fun toW (getc, strm) = let
53 :     fun scan radix strm = (case (SysW.scan radix getc strm)
54 :     of NONE => NONE
55 :     | (SOME(w, strm)) => SOME(w, strm)
56 :     (* end case *))
57 :     in
58 :     case (getc strm)
59 :     of NONE => NONE
60 :     | (SOME(#"0", strm')) => (case (getc strm')
61 :     of NONE => SOME(0w0, strm')
62 :     | (SOME((#"x" | #"X"), strm'')) => scan SCvt.HEX strm''
63 :     | _ => scan SCvt.OCT strm
64 :     (* end case *))
65 :     | _ => scan SCvt.DEC strm
66 :     (* end case *)
67 :     end
68 :     (* check that the word is representable in the given number of bits; raise
69 :     * Overflow if not.
70 :     *)
71 :     fun chk (w, bits) =
72 :     if (SysW.>= (SysW.>>(0wxffffffff, Word.-(0w32, bits)), w))
73 :     then w
74 :     else raise General.Overflow
75 :     (* Scan a sequence of numbers separated by #"." *)
76 :     fun scan getc strm = (case toW (getc, strm)
77 :     of NONE => NONE
78 :     | SOME(w, strm') => scanRest getc ([w], strm')
79 :     (* end case *))
80 :     and scanRest getc (l, strm) = (case getc strm
81 :     of SOME(#".", strm') => (case toW (getc, strm')
82 :     of NONE => SOME(List.rev l, strm)
83 :     | SOME(w, strm'') => scanRest getc (w::l, strm'')
84 :     (* end case *))
85 :     | _ => SOME(List.rev l, strm)
86 :     (* end case *))
87 :     in
88 :     fun toWords getc strm = (case (scan getc strm)
89 :     of SOME([a, b, c, d], strm) =>
90 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w8), chk(d, 0w8)], strm)
91 :     | SOME([a, b, c], strm) =>
92 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w16)], strm)
93 :     | SOME([a, b], strm) =>
94 :     SOME([chk(a, 0w8), chk(b, 0w24)], strm)
95 :     | SOME([a], strm) =>
96 :     SOME([chk(a, 0w32)], strm)
97 :     | _ => NONE
98 :     (* end case *))
99 :     fun fromBytes (a, b, c, d) = let
100 :     val fmt = Word8.fmt StringCvt.DEC
101 :     in
102 :     concat [fmt a, ".", fmt b, ".", fmt c, ".", fmt d]
103 :     end
104 :     end
105 :    
106 :     end (* PreSock *)
107 :     end
108 :    
109 :     (* We alias this structure to Socket so that the signature files will compile.
110 :     * We also need to keep the PreSock structure visible, so that structures
111 :     * compiled after the real Sock structure still have access to the representation
112 :     * types.
113 :     *)
114 :     structure Socket = PreSock;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0