DNS query: Difference between revisions

33,464 bytes added ,  5 months ago
m
m (→‎{{header|Perl}}: display output properly)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 13 users not shown)
Line 7:
=={{header|Ada}}==
{{works with|GNAT GPL|Any - package Gnat.Sockets supports only IPv4 as of Jun 2011}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use GNAT.Sockets;
 
Line 19:
Inet_Addr_V4 := Addresses (Host);
Put ("IPv4: " & Image (Value => Inet_Addr_V4));
end DNSQuerying;</langsyntaxhighlight>
 
{{works with|GNAT GPL|2019 - support for IPv6 was added to Gnat.Sockets sometime before this}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use Gnat.Sockets;
procedure DNSQuerying is
Line 36:
begin
Print_Address_Info ("ipv6.google.com", "https", Family_Inet6);
end DNSQuerying;</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 205:
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
</syntaxhighlight>
</lang>
 
=={{header|ATS}}==
{{trans|C}}
 
This has been tested on Gentoo Linux with PATSHOME properly set.
 
You need some X/Open standard stuff in your libc, so running the default patscc (which sets -std=c99) will not work. I have written the code so you can compile with myatscc.
 
<syntaxhighlight lang="ats">
(*
 
This program has to be compiled without -std=c99, which patscc will
insert unless you override the setting.
 
##myatsccdef=\
patscc \
-atsccomp gcc \
-I"${PATSHOME}" \
-I"${PATSHOME}/ccomp/runtime" \
-L"${PATSHOME}/ccomp/atslib/lib" \
-DATS_MEMALLOC_LIBC \
-o $fname($1) $1
 
*)
 
(* The code below is largely C, but the ATS interface enforces memory
management. For instance, try removing the call to "addrinfo_free",
and see what happens. You will get an error message from the
compiler. *)
 
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
 
#define ATS_EXTERN_PREFIX "rosetta_code_"
 
%{^
 
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
 
ATSinline() atstype_int
rosetta_code__getaddrinfo (atstype_ptr node,
atstype_ptr service,
atstype_int ai_flags,
atstype_int ai_family,
atstype_int ai_socktype,
atstype_int ai_protocol,
atstype_ptr res)
{
struct addrinfo hints;
memset (&hints, 0, sizeof hints);
hints.ai_flags = ai_flags;
hints.ai_family = ai_family;
hints.ai_socktype = ai_socktype;
hints.ai_protocol = ai_protocol;
return getaddrinfo ((const char *) node,
(const char *) service,
&hints,
(struct addrinfo **) res);
}
 
ATSinline() atstype_void
rosetta_code__freeaddrinfo (atstype_ptr res)
{
freeaddrinfo ((struct addrinfo *) res);
}
 
ATSinline() atstype_string
rosetta_code__gai_strerror (atstype_int errcode)
{
return (atstype_string) gai_strerror (errcode);
}
 
ATSinline() atstype_ptr
rosetta_code__ai_next (atstype_ptr p)
{
return (atstype_ptr) ((struct addrinfo *) p)->ai_next;
}
 
ATSinline() void
rosetta_code__ai_get_numeric_host (atstype_ptr addrinfo,
atstype_ptr host,
atstype_ptr errcode)
{
struct addrinfo *p_addrinfo = (struct addrinfo *) addrinfo;
char **p_host = (char **) host;
int *p_errcode = (int *) errcode;
 
char buf[NI_MAXHOST];
*p_errcode = getnameinfo (p_addrinfo->ai_addr,
p_addrinfo->ai_addrlen,
buf, sizeof buf, NULL, 0,
NI_NUMERICHOST);
*p_host = ( *p_errcode == 0 ) ? (strdup (buf)) : NULL;
}
 
%}
 
#define NIL list_vt_nil ()
#define :: list_vt_cons
 
implement list_vt_freelin$clear<Strptr1> s = strptr_free s
 
macdef AF_UNSPEC = $extval (int, "AF_UNSPEC")
macdef SOCK_DGRAM = $extval (int, "SOCK_DGRAM")
 
absview addrinfo_v (p : addr,
freeable : bool)
 
vtypedef addrinfo (p : addr,
freeable : bool) =
[null < p]
@(addrinfo_v (p, freeable) |
ptr p)
 
vtypedef addrinfo (p : addr) =
[freeable : bool]
addrinfo (p, freeable)
 
vtypedef addrinfo (freeable : bool) =
[p : addr]
addrinfo (p, freeable)
 
vtypedef addrinfo =
[p : addr]
[freeable : bool]
addrinfo (p, freeable)
 
vtypedef freeable_addrinfo (p : addr) = addrinfo (p, true)
vtypedef freeable_addrinfo = addrinfo true
vtypedef unfreeable_addrinfo (p : addr) = addrinfo (p, false)
vtypedef unfreeable_addrinfo = addrinfo false
 
fn
addrinfo_strerror (errcode : int)
: string =
let
extern fn _gai_strerror : int -<> string = "mac#%"
in
_gai_strerror errcode
end
 
fn
addrinfo_fetch (node : !Strptr0,
service : !Strptr0,
ai_flags : int,
ai_family : int,
ai_socktype : int,
ai_protocol : int,
error : &int? >> int)
: Option_vt ([p : agz] addrinfo (p, true)) =
let
extern fn
_getaddrinfo (node : !Strptr0,
service : !Strptr0,
ai_flags : int,
ai_family : int,
ai_socktype : int,
ai_protocol : int,
result : &ptr? >> ptr p)
: #[p : addr]
int = "mac#%"
 
var p : ptr
val err = _getaddrinfo (node, service, ai_flags, ai_family,
ai_socktype, ai_protocol, p)
prval [p : addr] EQADDR () = eqaddr_make_ptr p
in
error := err;
if (err = 0) * (isneqz p) then
let
extern praxi make_view : () -<prf> addrinfo_v (p, true)
in
Some_vt @(make_view () | p)
end
else
None_vt ()
end
 
fn
addrinfo_free {p : addr}
(addrinfo : freeable_addrinfo p)
: void =
let
extern fn _freeaddrinfo : ptr -> void = "mac#%"
extern praxi consume_view : addrinfo_v (p, true) -<prf> void
 
val @(pf | p) = addrinfo
prval () = consume_view pf
in
_freeaddrinfo p
end
 
prfn
unfreeable_addrinfo_finalize {p : addr}
(addrinfo : unfreeable_addrinfo p)
:<prf> void =
let
extern praxi consume_view : addrinfo_v (p, false) -<prf> void
in
consume_view (addrinfo.0)
end
 
fn
addrinfo_next (addrinfo : !addrinfo)
:<> Option_vt (addrinfo false) =
let
extern fn _ai_next : ptr -<> [q : agez] ptr q = "mac#%"
val [q : addr] q = _ai_next (addrinfo.1)
in
if iseqz q then
None_vt ()
else
let
extern praxi make_view : () -<prf> addrinfo_v (q, false)
in
Some_vt @(make_view () | q)
end
end
 
fn
addrinfo_get_numeric_host (addrinfo : !addrinfo,
errcode : &int? >> int)
: Option_vt Strptr1 =
let
extern fn
_ai_get_numeric_host (addrinfo : Ptr,
host : &ptr? >> Ptr,
errcode : &int? >> int)
: void = "mac#%"
 
var host : ptr
in
_ai_get_numeric_host (addrinfo.1, host, errcode);
if (errcode = 0) * (isneqz host) then
Some_vt ($UN.castvwtp0 host)
else
None_vt ()
end
 
fn
get_numeric_hosts (addrinfo : !freeable_addrinfo)
: Option_vt (List1_vt Strptr1) =
let
fun
loop (ainfo : !unfreeable_addrinfo,
accum : List1_vt Strptr1)
: Option_vt (List1_vt Strptr1) =
let
var errcode : int
val ai_opt = addrinfo_get_numeric_host (ainfo, errcode)
in
case+ ai_opt of
| ~ None_vt () =>
begin
list_vt_freelin<Strptr1> accum;
fprintln! (stderr_ref, "Error: ",
addrinfo_strerror errcode);
None_vt ()
end
| ~ Some_vt host =>
let
val next_ai_opt = addrinfo_next ainfo
and accum = host :: accum
in
case+ next_ai_opt of
| ~ None_vt () => Some_vt (list_vt_reverse accum)
| ~ Some_vt next_ai =>
let
val retval = loop (next_ai, accum)
prval () = unfreeable_addrinfo_finalize next_ai
in
retval
end
end
end
 
var errcode : int
val ai_opt = addrinfo_get_numeric_host (addrinfo, errcode)
in
case+ ai_opt of
| ~ None_vt () =>
begin
fprintln! (stderr_ref, "Error: ", addrinfo_strerror errcode);
None_vt ()
end
| ~ Some_vt host =>
let
val next_ai_opt = addrinfo_next addrinfo
and accum = host :: NIL
in
case+ next_ai_opt of
| ~ None_vt () => Some_vt accum
| ~ Some_vt next_ai =>
let
val retval = loop (next_ai, accum)
prval () = unfreeable_addrinfo_finalize next_ai
in
retval
end
end
end
 
implement
main0 () =
let
val hostname = string0_copy "www.kame.net"
val service = strptr_null ()
var errcode : int
val ai_opt = addrinfo_fetch (hostname, service, 0, AF_UNSPEC,
SOCK_DGRAM, 0, errcode)
val () = strptr_free hostname
prval () = strptr_free_null service
in
case+ ai_opt of
| ~ None_vt () =>
begin
fprintln! (stderr_ref, "Error: ", addrinfo_strerror errcode);
exit 1
end
| ~ Some_vt addrinfo =>
let
val hosts_opt = get_numeric_hosts addrinfo
in
addrinfo_free addrinfo;
case+ hosts_opt of
| ~ None_vt () => exit 1
| ~ Some_vt hosts =>
begin
println! ($UN.castvwtp1{List1 string} hosts);
list_vt_freelin hosts
end
end
end
</syntaxhighlight>
 
{{out}}
<pre>$ myatscc dns-query.dats && ./dns-query
210.155.141.200, 2001:2f0:0:8800:226:2dff:fe0b:4311, 2001:2f0:0:8800::1:1</pre>
 
=={{header|AutoHotkey}}==
This code uses Windows built-in 'nslookup' command (and a temporary file):
<langsyntaxhighlight AutoHotkeylang="autohotkey">Url := "www.kame.net" , LogFile := "Ping_" A_Now ".log"
Runwait, %comspec% /c nslookup %Url%>%LogFile%, , hide
FileRead, Contents, %LogFile%
FileDelete, %LogFile%
RegExMatch(Contents,"Addresses:.+(`r?`n\s+.+)*",Match)
MsgBox, % RegExReplace(Match,"(Addresses:|[ `t])")</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">:: DNS Query Task from Rosetta Code Wiki
:: Batch File Implementation
 
Line 254 ⟶ 594:
)
endlocal
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre>DOMAIN: "www.kame.net"
Line 264 ⟶ 604:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> name$ = "www.kame.net"
AF_INET = 2
Line 316 ⟶ 656:
SYS `WSACleanup`
</syntaxhighlight>
</lang>
Output:
<pre>IPv4 address = 203.178.141.194
Line 324 ⟶ 664:
This solution uses <code>getaddrinfo()</code>, a standard function from RFC 3493. This code resembles an example from [http://www.openbsd.org/cgi-bin/man.cgi?query=getaddrinfo&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html getaddrinfo(3)], the [[BSD]] manual page. Whereas the man page code connects to <code>www.kame.net</code>, this code only prints the numeric addresses.
 
<langsyntaxhighlight lang="c">#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h> /* getaddrinfo, getnameinfo */
Line 381 ⟶ 721:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Implementation takes a host name string as a parameter, and returns the IP addresses in a comma-delimited string. Note that a failed lookup throws a SocketException.
<langsyntaxhighlight lang="csharp">
private string LookupDns(string s)
{
Line 404 ⟶ 744:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
This example makes use of the Boost library. The asio bit is header-only, but requires linking boost-system (e.g. -lboost_system). The compiler may also need to be told to use C++11 semantics (e.g. -std=c++11).
<syntaxhighlight lang="c++">
<lang C++>
#include <boost/asio.hpp>
#include <iostream>
Line 429 ⟶ 769:
return rc;
}
</syntaxhighlight>
</lang>
 
=={{header|Caché ObjectScript}}==
 
<langsyntaxhighlight lang="cos">
Class Utils.Net Extends %RegisteredObject
{
Line 482 ⟶ 822:
 
}
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="cos">
Class Utils.OS Extends %RegisteredObject
{
Line 519 ⟶ 859:
 
}
</syntaxhighlight>
</lang>
{{out|Examples}}
<pre>
Line 536 ⟶ 876:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(import java.net.InetAddress java.net.Inet4Address java.net.Inet6Address)
 
(doseq [addr (InetAddress/getAllByName "www.kame.net")]
(cond
(instance? Inet4Address addr) (println "IPv4:" (.getHostAddress addr))
(instance? Inet6Address addr) (println "IPv6:" (.getHostAddress addr))))</langsyntaxhighlight>
Output:
<pre>IPv4: 203.178.141.194
Line 547 ⟶ 887:
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# runs under node.js
dns = require 'dns'
Line 558 ⟶ 898:
console.log 'IP6'
console.log addresses
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
common lisp does not have a standard network api. the following examples are using native implementations
{{works with|SBCL}}
<langsyntaxhighlight lang="lisp">(sb-bsd-sockets:host-ent-addresses
(sb-bsd-sockets:get-host-by-name "www.rosettacode.org"))
(#(71 19 147 227))</langsyntaxhighlight>
{{works with|CMUCL}}
<langsyntaxhighlight lang="lisp">(let ((hostname (extensions:lookup-host-entry "www.rosettacode.org")))
(print (map 'list #'extensions:ip-string (host-entry-addr-list hostname))))
("71.19.147.227")</langsyntaxhighlight>
{{works with|Clozure}}
<langsyntaxhighlight lang="lisp">(ipaddr-to-dotted (lookup-hostname "www.rosettacode.org"))
"104.28.10.103"</langsyntaxhighlight>
{{works with|Clisp}}
<langsyntaxhighlight lang="lisp">(hostent-addr-list (resolve-host-ipaddr "www.rosettacode.org"))
("104.28.11.103" "104.28.10.103")</langsyntaxhighlight>
the usocket library contains a (get-hosts-by-name) function in all of its [http://trac.common-lisp.net/usocket/browser/usocket/trunk/backend backends]. unfortunately it does not expose the functions in its public interface. but since the license is MIT, it may be a suitable source to copy code for your own use.
 
{{libheader|iolib}} is a portable library that:
{{works with|SBCL}}{{works with|CMUCL}}{{works with|Clisp}}{{works with|Clozure}}
<langsyntaxhighlight lang="lisp">(iolib:lookup-hostname "www.kame.net" :ipv6 t)
 
#/IOLIB.SOCKETS:IP/203.178.141.194
Line 586 ⟶ 926:
"orange.kame.net"
(("orange.kame.net" . #/IOLIB.SOCKETS:IP/203.178.141.194)
("orange.kame.net" . #/IOLIB.SOCKETS:IP/2001:200:dff:fff1:216:3eff:feb1:44d7))</langsyntaxhighlight>
 
In Allegro Common Lisp there's a nice standard library called [http://franz.com/support/documentation/current/doc/socket.htm socket].
{{works with|Allegro}}
<langsyntaxhighlight lang="lisp">(socket:ipaddr-to-dotted
(socket:dns-query "www.rosettacode.org"))
"104.28.10.103"</langsyntaxhighlight>
 
In Lispworks the [http://www.lispworks.com/documentation/lw71/LW/html/lw-269.htm COMM] package provides information about IP addresses.
 
{{works with|Lispworks}}
<langsyntaxhighlight lang="lisp">(require "comm")
(comm:ip-address-string (comm:get-host-entry "www.rosettacode.org" :fields '(:address)))
"104.28.10.103"
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">require "socket"
 
Socket::Addrinfo.resolve(
Line 611 ⟶ 951:
).each { |a|
puts a.ip_address.address
}</langsyntaxhighlight>
{{out}}
<pre>
Line 619 ⟶ 959:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.socket;
 
void main() {
Line 629 ⟶ 969:
a = getAddressInfo(domain, port, AddressFamily.INET6);
writefln("IPv6 address for %s: %s", domain, a[0].address);
}</langsyntaxhighlight>
<pre>IPv4 address for www.kame.net: 203.178.141.194:80
IPv6 address for www.kame.net: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80</pre>
Line 636 ⟶ 976:
The included Indy components wrap GetAddressInfo.
 
<langsyntaxhighlight Delphilang="delphi">program DNSQuerying;
 
{$APPTYPE CONSOLE}
Line 656 ⟶ 996:
lStack.Free;
end;
end.</langsyntaxhighlight>
 
Output:
Line 664 ⟶ 1,004:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
33> {ok, {hostent, Host, Aliases, AddrType, Bytes, AddrList}} = inet:gethostbyname("www.kame.net", inet).
{ok,{hostent,"orange.kame.net",
Line 679 ⟶ 1,019:
37> [inet_parse:ntoa(Addr) || Addr <- AddrList].
["2001:200:DFF:FFF1:216:3EFF:FEB1:44D7"]
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: dns io kernel sequences ;
 
"www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi
[ message>names second print ] bi@</langsyntaxhighlight>
{{out}}
<pre>
203.178.141.194
2001:200:dff:fff1:216:3eff:feb1:44d7
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">#include "win\winsock2.bi"
 
Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long
Dim As WSADATA _wsadate
Dim As in_addr addr
Dim As hostent Ptr res
Dim As Integer i = 0
WSAStartup(MAKEWORD(2,2),@_wsadate)
res = gethostbyname(stuff)
If res Then
Print !"\nURL: "; stuff
While (res->h_addr_list[i] <> 0)
addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i]))
Print "IPv4 address: ";*inet_ntoa(addr)
i+=1
Wend
WSACleanup()
Return 1
Else
Print "website error?"
Return 0
End If
End Function
 
GetSiteAddress "rosettacode.org"
GetSiteAddress "www.kame.net"
 
Sleep</syntaxhighlight>
{{out}}
<pre>URL: rosettacode.org
IPv4 address: 108.175.15.182
IPv4 address: 74.208.203.152
 
URL: www.kame.net
IPv4 address: 210.155.141.200</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">for a = callJava["java.net.InetAddress", "getAllByName", "www.kame.net"]
println[a.getHostAddress[]]</syntaxhighlight>
{{out}}
<pre>
210.155.141.200
2001:2f0:0:8800:226:2dff:fe0b:4311
2001:2f0:0:8800:0:0:1:1
</pre>
 
 
 
=={{header|FutureBasic}}==
FB has several ways to query a DNS server. In FB 7.0.23 the unix statement/function was added making such queries trivial:
<syntaxhighlight lang="futurebasic">
print unix @"nslookup -querytype=A www.kame.net"
HandleEvents
</syntaxhighlight>
Classic FB:
<syntaxhighlight>
Str255 UnixCommand
Str255 UnixResponse
 
print "DNS IPv4 resolved for www.kame.net:"
UnixCommand = "nslookup -querytype=A www.kame.net"
open "UNIX", 1, UnixCommand
while ( not eof( 1 ) )
input #1, UnixResponse
print UnixResponse
wend
close 1
 
print ""
 
print "DNS IPv6 resolved for www.kame.net:"
UnixCommand = "nslookup -querytype=AAAA www.kame.net"
open "UNIX", 1, UnixCommand
while ( not eof( 1 ) )
input #1, UnixResponse
print UnixResponse
wend
close 1
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
DNS IPv4 resolved for www.kame.net:
Server: 2001:1998:f00:2::1
Address: 2001:1998:f00:2::1#53
 
Non-authoritative answer:
www.kame.net canonical name = mango.itojun.org.
Name: mango.itojun.org
Address: 210.155.141.200
 
 
DNS IPv6 resolved for www.kame.net:
Server: 2001:1998:f00:2::1
Address: 2001:1998:f00:2::1#53
 
Non-authoritative answer:
www.kame.net canonical name = mango.itojun.org.
mango.itojun.org has AAAA address 2001:2f0:0:8800::1:1
mango.itojun.org has AAAA address 2001:2f0:0:8800:226:2dff:fe0b:4311
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 706 ⟶ 1,152:
fmt.Println(err)
}
}</langsyntaxhighlight>
Output:
<pre>
Line 713 ⟶ 1,159:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def addresses = InetAddress.getAllByName('www.kame.net')
println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}"
println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"</langsyntaxhighlight>
Output:
<pre>IPv4: 203.178.141.194
Line 721 ⟶ 1,167:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">module Main where
 
import Network.Socket
Line 737 ⟶ 1,183:
main = showIPs "www.kame.net"
</syntaxhighlight>
</lang>
Output:
<pre>
Line 749 ⟶ 1,195:
The following was tested only in Unicon:
 
<langsyntaxhighlight lang="unicon">
procedure main(A)
host := gethost( A[1] | "www.kame.net") | stop("can't translate")
write(host.name, ": ", host.addresses)
end
</syntaxhighlight>
</lang>
 
Sample Run:
Line 775 ⟶ 1,221:
Anyways:
 
<langsyntaxhighlight Jlang="j"> 2!:0'dig -4 +short www.kame.net'
orange.kame.net.
203.178.141.194
Line 781 ⟶ 1,227:
2!:0'dig -6 +short www.kame.net'
|interface error
| 2!:0'dig -6 +short www.kame.net'</langsyntaxhighlight>
 
Put differently: in the IPv4 DNS system, www.kame.net is currently a CNAME record for orange.kame.net which had the address 203.178.141.194.
Line 788 ⟶ 1,234:
 
=={{header|Java}}==
This is the same implementation as below, just less code
<lang java>import java.net.InetAddress;
<syntaxhighlight lang="java">
import java.net.InetAddress;
import java.net.UnknownHostException;
</syntaxhighlight>
<syntaxhighlight lang="java">
public static void main(String[] args) throws UnknownHostException {
/* 'getAllByName' will use the system configured 'resolver' */
for (InetAddress ip : InetAddress.getAllByName("www.kame.net"))
System.out.println(ip.getHostAddress());
}
</syntaxhighlight>
<pre>
210.155.141.200
2001:2f0:0:8800:226:2dff:fe0b:4311
2001:2f0:0:8800:0:0:1:1
</pre>
<br />
An alternate demonstration
<syntaxhighlight lang="java">import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
Line 809 ⟶ 1,274:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 817 ⟶ 1,282:
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">const dns = require("dns");
 
dns.lookup("www.kame.net", {
Line 825 ⟶ 1,290:
console.log(addresses);
})
</syntaxhighlight>
</lang>
Output:
<pre>
Line 834 ⟶ 1,299:
=={{header|Julia}}==
As entered at the REPL command line:
<langsyntaxhighlight lang="julia">
julia> using Sockets
 
Line 843 ⟶ 1,308:
ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
 
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.net.InetAddress
Line 872 ⟶ 1,337:
fun main(args: Array<String>) {
showIPAddresses("www.kame.net")
}</langsyntaxhighlight>
 
{{out}}
Line 884 ⟶ 1,349:
=={{header|Lasso}}==
The DNS lookup methods in Lasso do not support IPv6 addresses at this time, only IPv4.
<langsyntaxhighlight Lassolang="lasso">dns_lookup('www.kame.net', -type='A')</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 890 ⟶ 1,355:
{{libheader|LuaSocket}}
 
<langsyntaxhighlight lang="lua">local socket = require('socket')
local ip_tbl = socket.dns.getaddrinfo('www.kame.net')
 
Line 896 ⟶ 1,361:
io.write(string.format('%s: %s\n', v.family, v.addr))
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 906 ⟶ 1,371:
Neko does not yet support ipv6. ipv4 addresses are returned as Int32.
 
<langsyntaxhighlight ActionScriptlang="actionscript">/* dns in neko */
var host_resolve = $loader.loadprim("std@host_resolve", 1);
var host_to_string = $loader.loadprim("std@host_to_string", 1);
Line 914 ⟶ 1,379:
 
$print("www.kame.net: ", ip, ", ", host_to_string(ip), "\n");
$print(host_to_string(ip), ": ", host_reverse(ip), "\n");</langsyntaxhighlight>
 
{{out}}
Line 923 ⟶ 1,388:
 
=={{header|NetRexx}}==
<langsyntaxhighlight lang="netrexx">
/* NetRexx */
options replace format comments java crossref symbols nobinary
Line 937 ⟶ 1,402:
end
end ir
</syntaxhighlight>
</lang>
;Output
<pre>
Line 945 ⟶ 1,410:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang newLISP>
 
(define (dnsLookup site , ipv)
Line 958 ⟶ 1,423:
 
(dnsLookup "www.kame.net")
</syntaxhighlight>
</lang>
;Output
<pre>
Line 967 ⟶ 1,432:
=={{header|Nim}}==
 
<langsyntaxhighlight lang="nim">import nativesockets
 
iterator items(ai: ptr AddrInfo): ptr AddrInfo =
Line 982 ⟶ 1,447:
echo getAddrString i.aiAddr
 
when isMainModule: main()</langsyntaxhighlight>
 
{{out}}
Line 991 ⟶ 1,456:
{{Works with|oo2c version 2}}
IO:Address module supports only IPv4
<langsyntaxhighlight lang="oberon2">
MODULE DNSQuery;
IMPORT
Line 1,008 ⟶ 1,473:
Do;
END DNSQuery.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,015 ⟶ 1,480:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use System.IO.Net;
 
class Rosetta {
Line 1,024 ⟶ 1,489:
};
}
}</langsyntaxhighlight>
 
Output:
Line 1,033 ⟶ 1,498:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let dns_query ~host ~ai_family =
let opts = [
Unix.AI_FAMILY ai_family;
Line 1,052 ⟶ 1,517:
Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET);
Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6);
;;</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use feature 'say';
use Socket qw(getaddrinfo getnameinfo);
 
Line 1,061 ⟶ 1,526:
die "getaddrinfo error: $err" if $err;
 
say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res</langsyntaxhighlight>
{{out}}
<pre>203.178.141.194
Line 1,069 ⟶ 1,534:
Translated from C/MSDN/several man pages.<br>
<b>NB:</b> may warrant further testing, see output.
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>include builtins\cffi.e
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">cffi</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
constant AF_UNSPEC = 0,
-- AF_INET = 2,
<span style="color: #008080;">constant</span> <span style="color: #000000;">AF_UNSPEC</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
-- AF_INET6 = 23,
<span style="color: #000080;font-style:italic;">-- AF_INET = 2,
-- SOCK_STREAM = 1,
-- SOCK_DGRAMAF_INET6 = 223,
-- IPPROTO_TCPSOCK_STREAM = 61,</span>
<span style="color: #000000;">SOCK_DGRAM</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
NI_MAXHOST = 1025,
<span style="color: #000080;font-style:italic;">-- IPPROTO_TCP = 6,</span>
NI_NUMERICHOST = iff(platform()=LINUX?1:2)
<span style="color: #000000;">NI_MAXHOST</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1025</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">NI_NUMERICHOST</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">LINUX</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
constant tWAD = """
typedef struct WSAData {
<span style="color: #008080;">constant</span> <span style="color: #000000;">tWAD</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
WORD wVersion;
typedef struct WSAData {
WORD wHighVersion;
char WORD szDescription[257] wVersion;
char WORD szSystemStatus[129] wHighVersion;
char szDescription[257];
unsigned short iMaxSockets;
char szSystemStatus[129];
unsigned short iMaxUdpDg;
unsigned short iMaxSockets;
char *lpVendorInfo;
unsigned short iMaxUdpDg;
} WSADATA, *LPWSADATA;
char *lpVendorInfo;
""",
} WSADATA, *LPWSADATA;
tWAS = """
"""</span><span style="color: #0000FF;">,</span>
int WSAStartup(
<span style="color: #000000;">tWAS</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
_In_ WORD wVersionRequested,
int WSAStartup(
_Out_ LPWSADATA lpWSAData
_In_ WORD wVersionRequested,
);
_Out_ LPWSADATA lpWSAData
""",
);
tWAC = """
"""</span><span style="color: #0000FF;">,</span>
int WSACleanup(void);
<span style="color: #000000;">tWAC</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
""",
int WSACleanup(void);
tAI_W="""
"""</span><span style="color: #0000FF;">,</span>
typedef struct addrinfo {
<span style="color: #000000;">tAI_W</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"""
int ai_flags;
typedef struct addrinfo {
int ai_family;
int ai_socktypeai_flags;
int ai_protocolai_family;
size_t int ai_addrlen ai_socktype;
char int *ai_canonname ai_protocol;
size_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
struct sockaddr *ai_addr;
} ADDRINFOA, *PADDRINFOA;
struct addrinfo *ai_next;
""",
} ADDRINFOA, *PADDRINFOA;
tAI_L="""
"""</span><span style="color: #0000FF;">,</span>
typedef struct addrinfo {
<span style="color: #000000;">tAI_L</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"""
int ai_flags;
typedef struct addrinfo {
int ai_family;
int ai_socktypeai_flags;
int ai_protocolai_family;
int ai_addrlenai_socktype;
int ai_protocol;
struct sockaddr *ai_addr;
char int *ai_canonname ai_addrlen;
struct addrinfosockaddr *ai_nextai_addr;
char *ai_canonname;
};
struct addrinfo *ai_next;
""",
};
tGAI = """
"""</span><span style="color: #0000FF;">,</span>
int getaddrinfo(
<span style="color: #000000;">tGAI</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
_In_opt_ PCSTR pNodeName,
int getaddrinfo(
_In_opt_ PCSTR pServiceName,
_In_opt_ PCSTR pNodeName,
_In_opt_ const ADDRINFOA *pHints,
_In_opt_ PCSTR pServiceName,
_Out_ PADDRINFOA *ppResult
_In_opt_ const ADDRINFOA *pHints,
);
_Out_ PADDRINFOA *ppResult
""",
);
--int getaddrinfo(const char *node, const char *service,
"""</span><span style="color: #0000FF;">,</span>
-- const struct addrinfo *hints,
<span style="color: #000080;font-style:italic;">--int getaddrinfo(const char *node, const char *service,
-- struct addrinfo **res);
-- const struct addrinfo *hints,
tGNI = """
-- struct addrinfo **res);</span>
int getnameinfo(
<span style="color: #000000;">tGNI</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
_In_ sockaddr *sa,
int getnameinfo(
_In_ int salen,
_Out_ _In_ char sockaddr *hostsa,
_In_ DWORDint hostlensalen,
_Out_ char *servhost,
_In_ DWORD servlenhostlen,
_In_ _Out_ intchar flags*serv,
_In_ DWORD servlen,
);
_In_ int flags
""",
);
--int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
"""</span><span style="color: #0000FF;">,</span>
-- char *host, socklen_t hostlen,
<span style="color: #000080;font-style:italic;">--int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
-- char *serv, socklen_t servlen, int flags);
-- char *host, socklen_t hostlen,
tFAI = """
-- char *serv, socklen_t servlen, int flags);</span>
void freeaddrinfo(
<span style="color: #000000;">tFAI</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
_In_ struct addrinfo *ai
void freeaddrinfo(
);
_In_ struct addrinfo *ai
"""
);
--void freeaddrinfo(struct addrinfo *res);
"""</span>
 
<span style="color: #000080;font-style:italic;">--void freeaddrinfo(struct addrinfo *res);</span>
integer xgetaddrinfo = NULL, xgetnameinfo, xfreeaddrinfo, idAI,
xwsastartup, xwsacleanup, error
<span style="color: #004080;">integer</span> <span style="color: #000000;">xgetaddrinfo</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xgetnameinfo</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xfreeaddrinfo</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">xwsastartup</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xwsacleanup</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">error</span>
function get_name_info(string fqdn)
if xgetaddrinfo=NULL then
<span style="color: #008080;">function</span> <span style="color: #000000;">get_name_info</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">fqdn</span><span style="color: #0000FF;">)</span>
atom lib
<span style="color: #008080;">if</span> <span style="color: #000000;">xgetaddrinfo</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span>
if platform()=WINDOWS then
<span style="color: #004080;">atom</span> <span style="color: #000000;">lib</span>
integer idWAD = define_struct(tWAD)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span>
atom pWAD = allocate_struct(idWAD,cleanup:=true)
<span style="color: #004080;">integer</span> <span style="color: #000000;">idWAD</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_struct</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tWAD</span><span style="color: #0000FF;">)</span>
lib = open_dll("Ws2_32.dll")
<span style="color: #004080;">atom</span> <span style="color: #000000;">pWAD</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allocate_struct</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idWAD</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cleanup</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
xwsastartup = define_cffi_func(lib,tWAS)
<span style="color: #000000;">lib</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_dll</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Ws2_32.dll"</span><span style="color: #0000FF;">)</span>
xwsacleanup = define_cffi_func(lib,tWAC)
<span style="color: #000000;">xwsastartup</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_cffi_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tWAS</span><span style="color: #0000FF;">)</span>
error = c_func(xwsastartup,{#00020002,pWAD})
<span style="color: #000000;">xwsacleanup</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_cffi_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tWAC</span><span style="color: #0000FF;">)</span>
if error then ?9/0 end if
<span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xwsastartup</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">#00020002</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pWAD</span><span style="color: #0000FF;">})</span>
idAI = define_struct(tAI_W)
<span style="color: #008080;">if</span> <span style="color: #000000;">error</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
elsif platform()=LINUX then
<span style="color: #000000;">idAI</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_struct</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tAI_W</span><span style="color: #0000FF;">)</span>
lib = open_dll("libc.so.6")
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">LINUX</span> <span style="color: #008080;">then</span>
idAI = define_struct(tAI_L)
<span style="color: #000000;">lib</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_dll</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"libc.so.6"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">idAI</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_struct</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tAI_L</span><span style="color: #0000FF;">)</span>
xgetaddrinfo = define_cffi_func(lib,tGAI)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
xgetnameinfo = define_cffi_func(lib,tGNI)
<span style="color: #000000;">xgetaddrinfo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_cffi_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tGAI</span><span style="color: #0000FF;">)</span>
xfreeaddrinfo = define_cffi_proc(lib,tFAI)
<span style="color: #000000;">xgetnameinfo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_cffi_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tGNI</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">xfreeaddrinfo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_cffi_proc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tFAI</span><span style="color: #0000FF;">)</span>
atom hints = allocate_struct(idAI,cleanup:=true),
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res = allocate(machine_word(),cleanup:=true),
<span style="color: #004080;">atom</span> <span style="color: #000000;">hints</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allocate_struct</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cleanup</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span>
host = allocate(NI_MAXHOST,cleanup:=true)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">cleanup</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span>
set_struct_field(idAI,hints,"ai_family",AF_UNSPEC)
<span style="color: #000000;">host</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">NI_MAXHOST</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cleanup</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
-- set_struct_field(idAI,hints,"ai_socktype",SOCK_STREAM)
<span style="color: #000000;">set_struct_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hints</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ai_family"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">AF_UNSPEC</span><span style="color: #0000FF;">)</span>
set_struct_field(idAI,hints,"ai_socktype",SOCK_DGRAM)
<span style="color: #000080;font-style:italic;">-- set_struct_field(idAI,hints,"ai_socktype",SOCK_STREAM)</span>
error = c_func(xgetaddrinfo,{fqdn,NULL,hints,res})
<span style="color: #000000;">set_struct_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hints</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ai_socktype"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SOCK_DGRAM</span><span style="color: #0000FF;">)</span>
if error then ?9/0 end if
<span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xgetaddrinfo</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">fqdn</span><span style="color: #0000FF;">,</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hints</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
res = peekNS(res,machine_word(),false)
<span style="color: #008080;">if</span> <span style="color: #000000;">error</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
atom ptr = res
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peekNS</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
sequence results = {}
<span style="color: #004080;">atom</span> <span style="color: #000000;">ptr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span>
while ptr!=NULL do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
atom addr = get_struct_field(idAI,ptr,"ai_addr")
<span style="color: #008080;">while</span> <span style="color: #000000;">ptr</span><span style="color: #0000FF;">!=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">do</span>
integer len = get_struct_field(idAI,ptr,"ai_addrlen")
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_struct_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ptr</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ai_addr"</span><span style="color: #0000FF;">)</span>
error = c_func(xgetnameinfo,{addr, len, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST})
<span style="color: #004080;">integer</span> <span style="color: #000000;">len</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_struct_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ptr</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ai_addrlen"</span><span style="color: #0000FF;">)</span>
if error then ?9/0 end if
<span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xgetnameinfo</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">host</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NI_MAXHOST</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NI_NUMERICHOST</span><span style="color: #0000FF;">})</span>
results = append(results,peek_string(host))
<span style="color: #008080;">if</span> <span style="color: #000000;">error</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ptr = get_struct_field(idAI,ptr,"ai_next")
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">peek_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">host</span><span style="color: #0000FF;">))</span>
end while
<span style="color: #000000;">ptr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_struct_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">idAI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ptr</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ai_next"</span><span style="color: #0000FF;">)</span>
c_proc(xfreeaddrinfo,{res})
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return results
<span style="color: #7060A8;">c_proc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xfreeaddrinfo</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">results</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure WSACleanup()
if platform()=WINDOWS then
<span style="color: #008080;">procedure</span> <span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span>
error = c_func(xwsacleanup,{})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span>
if error then crash("WSACleanup failed: %d\n",{error}) end if
<span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xwsacleanup</span><span style="color: #0000FF;">,{})</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">error</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"WSACleanup failed: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">error</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
?get_name_info("www.kame.net")
WSACleanup()</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">get_name_info</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"www.kame.net"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
Note that windows nslookup shows an IPv6 that this does not, whereas
Line 1,231 ⟶ 1,699:
=={{header|PHP}}==
Works for PHP5 (Windows > 5.3.0)
<langsyntaxhighlight lang="php"><?php
$ipv4_record = dns_get_record("www.kame.net",DNS_A);
$ipv6_record = dns_get_record("www.kame.net",DNS_AAAA);
print "ipv4: " . $ipv4_record[0]["ip"] . "\n";
print "ipv6: " . $ipv6_record[0]["ipv6"] . "\n";
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(make
(in '(host "www.kame.net")
(while (from "address ")
(link (till "^J" T)) ) ) )</langsyntaxhighlight>
Output:
<pre>-> ("203.178.141.194" "2001:200:dff:fff1:216:3eff:feb1:44d7")</pre>
Line 1,248 ⟶ 1,716:
=={{header|Pike}}==
 
<syntaxhighlight lang="pike">
<lang Pike>
> array ips = Protocols.DNS.gethostbyname("www.kame.net")[1] || ({});
> write(ips*"\n");
</syntaxhighlight>
</lang>
Output:
<pre>203.178.141.194
Line 1,257 ⟶ 1,725:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">$DNS = Resolve-DnsName -Name www.kame.net
Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> import socket
>>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80))
>>> for ip in ips: print ip
...
2001:200:dff:fff1:216:3eff:feb1:44d7
203.178.141.194</langsyntaxhighlight>
 
=={{header|R}}==
Line 1,274 ⟶ 1,742:
If the following is saved as <tt>dns.cpp</tt>:
 
<langsyntaxhighlight lang="cpp">
#include <Rcpp.h>
#include <arpa/inet.h>
Line 1,322 ⟶ 1,790:
 
}
</syntaxhighlight>
</lang>
 
It can be used to perform the task in R:
 
<syntaxhighlight lang="r">
<lang R>
library(Rcpp)
sourceCpp("dns.cpp")
Line 1,332 ⟶ 1,800:
## [1] "203.178.141.194"
## [2] "2001:200:dff:fff1:216:3eff:feb1:44d7"
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,338 ⟶ 1,806:
The following finds an IPv4 address. Currently, the API does not support returning an IPv6 address.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
(require net/dns)
(dns-get-address "8.8.8.8" "www.kame.net")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,349 ⟶ 1,817:
{{works with|Rakudo|2017.01}}
 
<syntaxhighlight lang="raku" perl6line>use Net::DNS;
 
my $resolver = Net::DNS.new('8.8.8.8');
Line 1,357 ⟶ 1,825:
 
say $ip4[0].octets.join: '.';
say $ip6[0].octets.».fmt("%.2X").join.comb(4).join: ':';</langsyntaxhighlight>
{{out}}
<pre>203.178.141.194
Line 1,366 ⟶ 1,834:
 
Execution note: &nbsp; if the information for &nbsp; &nbsp; '''PING &nbsp; -6 &nbsp; ···''' &nbsp; &nbsp; is blank, you may need to install &nbsp; (on some <br>Microsoft Windows systems) &nbsp; the IPV6 interface using the command: &nbsp; &nbsp; ''' IPV6 &nbsp; install '''
<langsyntaxhighlight lang="rexx">/*REXX program displays IPV4 and IPV6 addresses for a supplied domain name.*/
parse arg tar . /*obtain optional domain name from C.L.*/
if tar=='' then tar= 'www.kame.net' /*Not specified? Then use the default.*/
Line 1,381 ⟶ 1,849:
end /*j*/ /* └─◄ force (TEMP) file integrity.*/
/*stick a fork in it, we're all done. */
'ERASE' tFID /*clean up (delete) the temporary file.*/</langsyntaxhighlight>
'''output''' &nbsp; using the default input:
<pre>
Line 1,389 ⟶ 1,857:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">irb(main):001:0> require 'socket'
=> true
irb(main):002:0> Addrinfo.getaddrinfo("www.kame.net", nil, nil, :DGRAM) \
irb(main):003:0* .map! { |ai| ai.ip_address }
=> ["203.178.141.194", "2001:200:dff:fff1:216:3eff:feb1:44d7"]</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::net::ToSocketAddrs;
 
fn main() {
Line 1,411 ⟶ 1,879:
println!("{}", ip_port.ip());
}
}</langsyntaxhighlight>
 
Output:<pre>203.178.141.194
Line 1,418 ⟶ 1,886:
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">import java.net._
 
InetAddress.getAllByName("www.kame.net").foreach(x => println(x.getHostAddress))</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Guile}}
<langsyntaxhighlight lang="scheme">; Query DNS
(define n (car (hostent:addr-list (gethost "www.kame.net"))))
 
Line 1,430 ⟶ 1,898:
(display (inet-ntoa n))(newline)
(display (inet-ntop AF_INET n))(newline)
(display (inet-ntop AF_INET6 n))(newline)</langsyntaxhighlight>
Output:<pre>203.178.141.194
203.178.141.194
Line 1,441 ⟶ 1,909:
The function [http://seed7.sourceforge.net/libraries/socket.htm#numericAddress%28in_socketAddress%29 numericAddress]
is used to get the IP address of the specified host.
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "socket.s7i";
 
Line 1,447 ⟶ 1,915:
begin
writeln(numericAddress(inetSocketAddress("www.kame.net", 1024)));
end func;</langsyntaxhighlight>
 
Output:
Line 1,455 ⟶ 1,923:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var (err, *res) = Socket.getaddrinfo(
'www.kame.net', 0,
Hash.new(protocol => Socket.IPPROTO_TCP)
Line 1,462 ⟶ 1,930:
res.each { |z|
say [Socket.getnameinfo(z{:addr}, Socket.NI_NUMERICHOST)][1];
}</langsyntaxhighlight>
 
{{out}}
Line 1,472 ⟶ 1,940:
=={{header|Standard ML}}==
The basis library provides IPv4 support only:
<langsyntaxhighlight lang="sml">- Option.map (NetHostDB.toString o NetHostDB.addr) (NetHostDB.getByName "www.kame.net");
val it = SOME "203.178.141.194": string option</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 1,479 ⟶ 1,947:
{{tcllib|dns}}
{{libheader|udp}}
<langsyntaxhighlight lang="tcl">package require udp; # Query by UDP more widely supported, but requires external package
package require dns
 
Line 1,488 ⟶ 1,956:
update; # Let queries complete
}
puts "primary addresses of $host are:\n\tIPv4» [dns::address $v4]\n\tIPv6» [dns::address $v6]"</langsyntaxhighlight>
Output:
<pre>
Line 1,495 ⟶ 1,963:
IPv6» 2001:200:dff:fff1:216:3eff:feb1:44d7
</pre>
 
=={{header|TXR}}==
At the listener prompt:
<pre>This is the TXR Lisp interactive listener of TXR 283.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR's no-spray organic production means every bug is carefully removed by hand.
1> (flow (getaddrinfo "www.kame.net") (mapcar .(str-addr)) uniq)
("210.155.141.200" "2001:2f0:0:8800::1:1" "2001:2f0:0:8800:226:2dff:fe0b:4311")</pre>
 
=={{header|UNIX Shell}}==
===Using dig===
<syntaxhighlight lang="bash">
Aamrun ~ % dig www.kame.net A www.kame.net AAAA +short
mango.itojun.org.
210.155.141.200
mango.itojun.org.
2001:2f0:0:8800:226:2dff:fe0b:4311
2001:2f0:0:8800::1:1
Aamrun ~ %
</syntaxhighlight>
===Using nslookup===
<syntaxhighlight lang="bash">
Aamrun ~ % nslookup
> set q=AAAA
> www.kame.net
Server: 192.168.1.1
Address: 192.168.1.1#53
 
Non-authoritative answer:
www.kame.net canonical name = mango.itojun.org.
mango.itojun.org has AAAA address 2001:2f0:0:8800:226:2dff:fe0b:4311
mango.itojun.org has AAAA address 2001:2f0:0:8800::1:1
 
Authoritative answers can be found from:
> set q=A
> www.kame.net
Server: 192.168.1.1
Address: 192.168.1.1#53
 
Non-authoritative answer:
www.kame.net canonical name = mango.itojun.org.
Name: mango.itojun.org
Address: 210.155.141.200
> exit
 
Aamrun ~ %
</syntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function dns_query(url,ver)
Set r = New RegExp
Line 1,514 ⟶ 2,029:
 
Call dns_query(WScript.Arguments(0),WScript.Arguments(1))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 1,525 ⟶ 2,040:
URL: www.kame.net
IP Version 6: 2001:200:dff:fff1:216:3eff:feb1:44d7
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="v (vlang)">
import net
 
fn main() {
addr := 'www.kame.net:80'
@type := net.SocketType.tcp
family := net.AddrFamily.unspec
mut addrs := []net.Addr{}
mut results :=''
 
addrs = net.resolve_addrs(addr, family, @type) or {println('Error: nothing resolved') exit(1)}
for each in addrs {
results += '${addr.split(':')[0]} * ${each} * ${each.family()} * ${@type} \n'
}
println(results)
 
}
</syntaxhighlight>
 
{{out}}
<pre>
www.kame.net * 210.155.141.200:80 * ip * tcp
www.kame.net * [2001:2f0:0:8800:226:2dff:fe0b:4311]:80 * ip6 * tcp
www.kame.net * [2001:2f0:0:8800::1:1]:80 * ip6 * tcp
</pre>
 
=={{header|Wren}}==
Wren CLI doesn't currently expose a way to make a DNS query.
 
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
<syntaxhighlight lang="wren">/* DNS_query.wren */
 
class Net {
foreign static lookupHost(host)
}
 
var host = "orange.kame.net"
var addrs = Net.lookupHost(host).split(", ")
System.print(addrs.join("\n"))</syntaxhighlight>
which we embed in the following Go program and run it:
{{libheader|WrenGo}}
<syntaxhighlight lang="go">/* go run DNS_query.go */
 
package main
 
import(
wren "github.com/crazyinfin8/WrenGo"
"net"
"strings"
)
 
type any = interface{}
 
func lookupHost(vm *wren.VM, parameters []any) (any, error) {
host := parameters[1].(string)
addrs, err := net.LookupHost(host)
if err != nil {
return nil, nil
}
return strings.Join(addrs, ", "), nil
}
 
func main() {
vm := wren.NewVM()
fileName := "DNS_query.wren"
methodMap := wren.MethodMap{"static lookupHost(_)": lookupHost}
classMap := wren.ClassMap{"Net": wren.NewClass(nil, nil, methodMap)}
module := wren.NewModule(classMap)
vm.SetModule(fileName, module)
vm.InterpretFile(fileName)
vm.Free()
}</syntaxhighlight>
 
{{out}}
<pre>
2001:200:dff:fff1:216:3eff:feb1:44d7
203.178.141.194
</pre>
 
=={{header|zkl}}==
The addrInfo method is just a front end to POSIX getaddrinfo.
<langsyntaxhighlight lang="zkl">zkl: Network.TCPClientSocket.addrInfo("www.kame.net")
L("orange.kame.net",L("203.178.141.194","2001:200:dff:fff1:216:3eff:feb1:44d7"))</langsyntaxhighlight>
 
 
9,476

edits