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 651 - (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 :     (* the raw representation of a socket (a file descriptor for now) *)
25 :     type socket = int
26 :    
27 :     (* an internet address; this is here because it is abstract in the
28 :     * NetHostDB and IP structures.
29 :     *)
30 :     datatype in_addr = INADDR of addr
31 :    
32 :     (* an address family *)
33 :     datatype addr_family = AF of af
34 :    
35 :     (* socket types *)
36 :     datatype sock_type = SOCKTY of CInterface.system_const
37 :    
38 :     (* sockets are polymorphic; the instantiation of the type variables
39 :     * provides a way to distinguish between different kinds of sockets.
40 :     *)
41 :     datatype ('sock, 'af) sock = SOCK of socket
42 :     datatype 'af sock_addr = ADDR of addr
43 :    
44 :     (** Utility functions for parsing/unparsing network addresses **)
45 :     local
46 :     structure SysW = SysWord
47 :     structure SCvt = StringCvt
48 :     fun toW (getc, strm) = let
49 :     fun scan radix strm = (case (SysW.scan radix getc strm)
50 :     of NONE => NONE
51 :     | (SOME(w, strm)) => SOME(w, strm)
52 :     (* end case *))
53 :     in
54 :     case (getc strm)
55 :     of NONE => NONE
56 :     | (SOME(#"0", strm')) => (case (getc strm')
57 :     of NONE => SOME(0w0, strm')
58 :     | (SOME((#"x" | #"X"), strm'')) => scan SCvt.HEX strm''
59 :     | _ => scan SCvt.OCT strm
60 :     (* end case *))
61 :     | _ => scan SCvt.DEC strm
62 :     (* end case *)
63 :     end
64 :     (* check that the word is representable in the given number of bits; raise
65 :     * Overflow if not.
66 :     *)
67 :     fun chk (w, bits) =
68 :     if (SysW.>= (SysW.>>(0wxffffffff, Word.-(0w32, bits)), w))
69 :     then w
70 :     else raise General.Overflow
71 :     (* Scan a sequence of numbers separated by #"." *)
72 :     fun scan getc strm = (case toW (getc, strm)
73 :     of NONE => NONE
74 :     | SOME(w, strm') => scanRest getc ([w], strm')
75 :     (* end case *))
76 :     and scanRest getc (l, strm) = (case getc strm
77 :     of SOME(#".", strm') => (case toW (getc, strm')
78 :     of NONE => SOME(List.rev l, strm)
79 :     | SOME(w, strm'') => scanRest getc (w::l, strm'')
80 :     (* end case *))
81 :     | _ => SOME(List.rev l, strm)
82 :     (* end case *))
83 :     in
84 :     fun toWords getc strm = (case (scan getc strm)
85 :     of SOME([a, b, c, d], strm) =>
86 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w8), chk(d, 0w8)], strm)
87 :     | SOME([a, b, c], strm) =>
88 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w16)], strm)
89 :     | SOME([a, b], strm) =>
90 :     SOME([chk(a, 0w8), chk(b, 0w24)], strm)
91 :     | SOME([a], strm) =>
92 :     SOME([chk(a, 0w32)], strm)
93 :     | _ => NONE
94 :     (* end case *))
95 :     fun fromBytes (a, b, c, d) = let
96 :     val fmt = Word8.fmt StringCvt.DEC
97 :     in
98 :     concat [fmt a, ".", fmt b, ".", fmt c, ".", fmt d]
99 :     end
100 :     end
101 :    
102 :     end (* PreSock *)
103 :     end
104 :    
105 :     (* We alias this structure to Socket so that the signature files will compile.
106 :     * We also need to keep the PreSock structure visible, so that structures
107 :     * compiled after the real Sock structure still have access to the representation
108 :     * types.
109 :     *)
110 :     structure Socket = PreSock;
111 :    
112 :    

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