Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 99 lines (77 sloc) 2.625 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (*
19 @author Adam Koprowski
20 *)
21
22 type protocol = string
23
24 type fd = Unix.file_descr
25
26 type addr = Unix.inet_addr
27
28 type connection_type = TCP | UDP | SSL | FILE
29
30 (** The address of a connection *)
31 type t =
32 | Tcp of (protocol * Unix.file_descr * Unix.inet_addr)
33 | Udp of (protocol * Unix.file_descr * Unix.inet_addr)
34 | Ssl of (protocol * Ssl.socket * Unix.inet_addr)
35 | File of Unix.file_descr
36
37 let mk_protocol x = x
38
39 let mk_tcp ~protocol ~fd ~addr =
40 Tcp (protocol, fd, addr)
41
42 let mk_udp ~protocol ~fd ~addr =
43 Udp (protocol, fd, addr)
44
45 let mk_file ~fd =
46 File fd
47
48 let get_type = function
49 | Tcp _ -> TCP
50 | Udp _ -> UDP
51 | Ssl _ -> SSL
52 | File _ -> FILE
53
54 let get_addr = function
55 | Tcp (_, _, addr) -> addr
56 | Udp (_, _, addr) -> addr
57 | Ssl (_, _, addr) -> addr
58 | File _ -> raise (Invalid_argument "NetAddr.get_addr on a File")
59
60 let get_type_and_fd = function
61 | Tcp (_, fd, _) -> `Tcp fd
62 | Udp (_, fd, _) -> `Udp fd
63 | Ssl (_, fd, _) -> `Ssl fd
64 | File fd -> `File fd
65
66 let get_fd = function
67 | Tcp (_, fd, _) -> fd
68 | Udp (_, fd, _) -> fd
69 | Ssl (_, s, _) -> Ssl.file_descr_of_socket s
70 | File fd -> fd
71
72 let get_protocol = function
73 | Tcp (prot, _, _) -> prot
74 | Udp (prot, _, _) -> prot
75 | Ssl (prot, _, _) -> prot
76 | File _ -> "FILE"
77
78 let secured_from_normal secured_fd =
79 function
80 | Tcp (prot, _, addr) -> Ssl (prot, secured_fd, addr)
81 | Udp _ -> assert false
82 | Ssl _ -> assert false
83 | File _ -> assert false
84
85 let string_of_sockaddr = function
86 | Unix.ADDR_UNIX fd -> "Unix FD:" ^ fd
87 | Unix.ADDR_INET (addr, port) -> Printf.sprintf "%s:%d" (Unix.string_of_inet_addr addr) port
88
89 let to_string addr =
90 let show t prot addr =
91 Printf.sprintf "%s [%s,%s]"
92 (Unix.string_of_inet_addr addr) t prot
93 in
94 match addr with
95 | Tcp (prot, _, addr) -> show "TCP" prot addr
96 | Udp (prot, _, addr) -> show "UDP" prot addr
97 | Ssl (prot, _, addr) -> show "SSL" prot addr
98 | File fd -> Printf.sprintf "FILE(%d)" (Obj.magic fd : int)
Something went wrong with that request. Please try again.