Last active
December 1, 2019 16:57
-
-
Save toots/0becc642420033184f22fa47f6865255 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Ctypes | |
include Sys_socket_types.SaFamily | |
include Sys_socket_stubs.Def(Sys_socket_generated_stubs) | |
type socklen = Types.socklen | |
let socklen_t = Types.socklen_t | |
let int_of_socklen = Types.int_of_socklen | |
let socklen_of_int = Types.socklen_of_int | |
module Sockaddr = struct | |
include Types.Sockaddr | |
let from_sockaddr_storage = from_sockaddr_storage t | |
let sa_data_len = Types.sa_data_len | |
end | |
let getnameinfo sockaddr_ptr = | |
let maxhost = Types.ni_maxhost in | |
let s = allocate_n char ~count:maxhost in | |
let maxserv = Types.ni_maxserv in | |
let p = allocate_n char ~count:maxserv in | |
match getnameinfo sockaddr_ptr (socklen_of_int (sizeof sockaddr_t)) | |
s (socklen_of_int maxhost) | |
p (socklen_of_int maxserv) | |
(Types.ni_numerichost lor | |
Types.ni_numericserv) with | |
| 0 -> | |
let host = | |
let length = | |
Unsigned.Size_t.to_int | |
(strnlen s (Unsigned.Size_t.of_int maxhost)) | |
in | |
string_from_ptr s ~length | |
in | |
let port = | |
let length = | |
Unsigned.Size_t.to_int | |
(strnlen p (Unsigned.Size_t.of_int maxserv)) | |
in | |
let port = | |
string_from_ptr p ~length | |
in | |
try | |
int_of_string port | |
with _ -> | |
match getservbyname p null with | |
| ptr when is_null ptr -> failwith "getnameinfo" | |
| ptr -> | |
Unsigned.UInt16.to_int | |
(ntohs (!@ (ptr |-> Types.Servent.s_port))) | |
in | |
host, port | |
| _ -> failwith "getnameinfo" | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment