Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / basis-library / net / unix-sock.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2002-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure UnixSock : UNIX_SOCK =
9 struct
10 structure Prim = PrimitiveFFI.Socket.UnixSock
11
12 datatype unix = UNIX
13 type 'sock_type sock = (unix, 'sock_type) Socket.sock
14 type 'mode stream_sock = 'mode Socket.stream sock
15 type dgram_sock = Socket.dgram sock
16 type sock_addr = unix Socket.sock_addr
17 val unixAF = Net.AddrFamily.fromRep PrimitiveFFI.Socket.AF.UNIX
18
19 fun toAddr s =
20 let
21 val (sa, salen, finish) = Socket.newSockAddr ()
22 val _ = Prim.toAddr (NullString.nullTerm s,
23 C_Size.fromInt (String.size s),
24 sa, salen)
25 in
26 finish ()
27 end
28
29 fun fromAddr sa =
30 let
31 val sa = Socket.unpackSockAddr sa
32 val len = Prim.pathLen sa
33 val a = CharArray.array (C_Size.toInt len, #"\000")
34 val _ = Prim.fromAddr (sa, CharArray.toPoly a, len)
35 in
36 CharArraySlice.vector (CharArraySlice.slice (a, 0, SOME (C_Size.toInt len)))
37 end
38
39 structure Strm =
40 struct
41 fun socket () = GenericSock.socket (unixAF, Socket.SOCK.stream)
42 fun socketPair () = GenericSock.socketPair (unixAF, Socket.SOCK.stream)
43 end
44 structure DGrm =
45 struct
46 fun socket () = GenericSock.socket (unixAF, Socket.SOCK.dgram)
47 fun socketPair () = GenericSock.socketPair (unixAF, Socket.SOCK.dgram)
48 end
49 end