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/compiler/PervEnv/Sockets/host-db.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/PervEnv/Sockets/host-db.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (view) (download)

1 : monnier 16 (* host-db.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     *)
6 :    
7 :     structure NetHostDB : NET_HOST_DB =
8 :     struct
9 :    
10 :     structure SysW = SysWord
11 :    
12 :     fun netdbFun x = CInterface.c_function "SMLNJ-Sockets" x
13 :    
14 :     type in_addr = PreSock.in_addr
15 :     type addr_family = PreSock.addr_family
16 :    
17 :     datatype entry = HOSTENT of {
18 :     name : string,
19 :     aliases : string list,
20 :     addrType : addr_family,
21 :     addrs : in_addr list
22 :     }
23 :    
24 :     local
25 :     fun conc field (HOSTENT a) = field a
26 :     in
27 :     val name = conc #name
28 :     val aliases = conc #aliases
29 :     val addrType = conc #addrType
30 :     val addrs = conc #addrs
31 :     val addr = List.hd o addrs
32 :     end (* local *)
33 :    
34 :     (* Host DB query functions *)
35 :     local
36 :     type hostent = (string * string list * PreSock.af * PreSock.addr list)
37 :     fun getHostEnt NONE = NONE
38 :     | getHostEnt (SOME(name, aliases, addrType, addrs)) = SOME(HOSTENT{
39 :     name = name, aliases = aliases,
40 :     addrType = PreSock.AF addrType,
41 :     addrs = List.map PreSock.INADDR addrs
42 :     })
43 :     val getHostByName' : string -> hostent option = netdbFun "getHostByName"
44 :     val getHostByAddr' : PreSock.addr -> hostent option = netdbFun "getHostByAddr"
45 :     in
46 :     val getByName = getHostEnt o getHostByName'
47 :     fun getByAddr (PreSock.INADDR addr) = getHostEnt(getHostByAddr' addr)
48 :     end (* local *)
49 :    
50 :     fun scan getc strm = let
51 :     fun w2b w = Word8.fromLargeWord(SysW.toLargeWord w)
52 :     fun getB (w, shft) = SysW.andb(SysW.>>(w, shft), 0wxFF)
53 :     fun mkAddr (a, b, c, d) = PreSock.INADDR(Word8Vector.fromList[
54 :     w2b a, w2b b, w2b c, w2b d
55 :     ])
56 :     in
57 :     case (PreSock.toWords getc strm)
58 :     of SOME([a, b, c, d], strm) =>
59 :     SOME(mkAddr(a, b, c, d), strm)
60 :     | SOME([a, b, c], strm) =>
61 :     SOME(mkAddr(a, b, getB(c, 0w8), getB(c, 0w0)), strm)
62 :     | SOME([a, b], strm) =>
63 :     SOME(mkAddr(a, getB(b, 0w16), getB(b, 0w8), getB(b, 0w0)), strm)
64 :     | SOME([a], strm) =>
65 :     SOME(mkAddr(getB(a, 0w24), getB(a, 0w16), getB(a, 0w8), getB(a, 0w0)), strm)
66 :     | _ => NONE
67 :     (* end case *)
68 :     end
69 :    
70 :     val fromString = StringCvt.scanString scan
71 :    
72 :     fun toString (PreSock.INADDR addr) = let
73 :     fun get i = Word8Vector.sub(addr, i)
74 :     in
75 :     PreSock.fromBytes(get 0, get 1, get 2, get 3)
76 :     end
77 :    
78 :     val getHostName : unit -> string = netdbFun "getHostName"
79 :    
80 :     end
81 :    
82 :     (*
83 : monnier 113 * $Log$
84 : monnier 16 *)

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