Created
November 3, 2021 19:51
-
-
Save plambert/6b71a2410fddfd6e6f6fc6c616ff6b12 to your computer and use it in GitHub Desktop.
simple attempt in crystal to use getnameinfo()
This file contains 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
require "socket" | |
# taken from | |
# /Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/netdb.h | |
@[Flags] | |
enum NetDB::Flags | |
NI_NOFQDN = 0x00000001 | |
NI_NUMERICHOST = 0x00000002 | |
NI_NAMEREQD = 0x00000004 | |
NI_NUMERICSERV = 0x00000008 | |
NI_DGRAM = 0x00000010 | |
NI_WITHSCOPEID = 0x00000020 | |
NI_NUMERICSCOPE = 0x00000100 | |
end | |
module DNS | |
NI_MAXHOST = LibC::SocklenT.new(1025) | |
def self.getnameinfo(addr : Socket::Address, service : String, flags : NetDB::Flags) | |
host_ptr = Pointer.malloc(NI_MAXHOST, 0_u8) | |
host_slice = Slice.new(host_ptr, NI_MAXHOST) | |
addr_len = LibC::SocklenT.new(sizeof(typeof(addr))) | |
service_len = LibC::SocklenT.new(service.bytesize) | |
STDERR.puts "calling getnameinfo(addr: #{addr.inspect}, addr_len: #{addr_len}, host_slice: #{host_slice.size}, NI_MAXHOST: #{NI_MAXHOST}, service: #{service.inspect}, service_len: #{service_len}, flags: #{flags})" | |
result = LibC.getnameinfo(addr, addr_len, host_ptr, NI_MAXHOST, service, service_len, flags) | |
STDERR.puts "result: #{result}: #{String.new(LibC.gai_strerror(result))}" | |
end | |
end | |
DNS.getnameinfo addr: Socket::IPAddress.new("8.8.8.8", 8080), service: "80", flags: NetDB::Flags::None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment