DNS query: Difference between revisions
m
syntax highlighting fixup automation
(Corrected headers) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7:
=={{header|Ada}}==
{{works with|GNAT GPL|Any - package Gnat.Sockets supports only IPv4 as of Jun 2011}}
<
with GNAT.Sockets; use GNAT.Sockets;
Line 19:
Inet_Addr_V4 := Addresses (Host);
Put ("IPv4: " & Image (Value => Inet_Addr_V4));
end DNSQuerying;</
{{works with|GNAT GPL|2019 - support for IPv6 was added to Gnat.Sockets sometime before this}}
<
with GNAT.Sockets; use Gnat.Sockets;
procedure DNSQuerying is
Line 36:
begin
Print_Address_Info ("ipv6.google.com", "https", Family_Inet6);
end DNSQuerying;</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 205:
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
</syntaxhighlight>
=={{header|AutoHotkey}}==
This code uses Windows built-in 'nslookup' command (and a temporary file):
<
Runwait, %comspec% /c nslookup %Url%>%LogFile%, , hide
FileRead, Contents, %LogFile%
FileDelete, %LogFile%
RegExMatch(Contents,"Addresses:.+(`r?`n\s+.+)*",Match)
MsgBox, % RegExReplace(Match,"(Addresses:|[ `t])")</
=={{header|Batch File}}==
<
:: Batch File Implementation
Line 254:
)
endlocal
goto :EOF</
{{Out}}
<pre>DOMAIN: "www.kame.net"
Line 264:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
AF_INET = 2
Line 316:
SYS `WSACleanup`
</syntaxhighlight>
Output:
<pre>IPv4 address = 203.178.141.194
Line 324:
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.
<
#include <sys/socket.h>
#include <netdb.h> /* getaddrinfo, getnameinfo */
Line 381:
return 0;
}</
=={{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.
<
private string LookupDns(string s)
{
Line 404:
}
}
</syntaxhighlight>
=={{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++">
#include <boost/asio.hpp>
#include <iostream>
Line 429:
return rc;
}
</syntaxhighlight>
=={{header|Caché ObjectScript}}==
<
Class Utils.Net Extends %RegisteredObject
{
Line 482:
}
</syntaxhighlight>
<
Class Utils.OS Extends %RegisteredObject
{
Line 519:
}
</syntaxhighlight>
{{out|Examples}}
<pre>
Line 536:
=={{header|Clojure}}==
<
(doseq [addr (InetAddress/getAllByName "www.kame.net")]
(cond
(instance? Inet4Address addr) (println "IPv4:" (.getHostAddress addr))
(instance? Inet6Address addr) (println "IPv6:" (.getHostAddress addr))))</
Output:
<pre>IPv4: 203.178.141.194
Line 547:
=={{header|CoffeeScript}}==
<
# runs under node.js
dns = require 'dns'
Line 558:
console.log 'IP6'
console.log addresses
</syntaxhighlight>
=={{header|Common Lisp}}==
common lisp does not have a standard network api. the following examples are using native implementations
{{works with|SBCL}}
<
(sb-bsd-sockets:get-host-by-name "www.rosettacode.org"))
(#(71 19 147 227))</
{{works with|CMUCL}}
<
(print (map 'list #'extensions:ip-string (host-entry-addr-list hostname))))
("71.19.147.227")</
{{works with|Clozure}}
<
"104.28.10.103"</
{{works with|Clisp}}
<
("104.28.11.103" "104.28.10.103")</
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}}
<
#/IOLIB.SOCKETS:IP/203.178.141.194
Line 586:
"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))</
In Allegro Common Lisp there's a nice standard library called [http://franz.com/support/documentation/current/doc/socket.htm socket].
{{works with|Allegro}}
<
(socket:dns-query "www.rosettacode.org"))
"104.28.10.103"</
In Lispworks the [http://www.lispworks.com/documentation/lw71/LW/html/lw-269.htm COMM] package provides information about IP addresses.
{{works with|Lispworks}}
<
(comm:ip-address-string (comm:get-host-entry "www.rosettacode.org" :fields '(:address)))
"104.28.10.103"
</syntaxhighlight>
=={{header|Crystal}}==
<
Socket::Addrinfo.resolve(
Line 611:
).each { |a|
puts a.ip_address.address
}</
{{out}}
<pre>
Line 619:
=={{header|D}}==
<
void main() {
Line 629:
a = getAddressInfo(domain, port, AddressFamily.INET6);
writefln("IPv6 address for %s: %s", domain, a[0].address);
}</
<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:
The included Indy components wrap GetAddressInfo.
<
{$APPTYPE CONSOLE}
Line 656:
lStack.Free;
end;
end.</
Output:
Line 664:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
33> {ok, {hostent, Host, Aliases, AddrType, Bytes, AddrList}} = inet:gethostbyname("www.kame.net", inet).
{ok,{hostent,"orange.kame.net",
Line 679:
37> [inet_parse:ntoa(Addr) || Addr <- AddrList].
["2001:200:DFF:FFF1:216:3EFF:FEB1:44D7"]
</syntaxhighlight>
=={{header|Factor}}==
<
"www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi
[ message>names second print ] bi@</
{{out}}
<pre>
Line 693:
=={{header|Frink}}==
<
println[a.getHostAddress[]]</
{{out}}
<pre>
Line 703:
=={{header|Go}}==
<
import (
Line 716:
fmt.Println(err)
}
}</
Output:
<pre>
Line 723:
=={{header|Groovy}}==
<
println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}"
println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"</
Output:
<pre>IPv4: 203.178.141.194
Line 731:
=={{header|Haskell}}==
<
import Network.Socket
Line 747:
main = showIPs "www.kame.net"
</syntaxhighlight>
Output:
<pre>
Line 759:
The following was tested only in Unicon:
<
procedure main(A)
host := gethost( A[1] | "www.kame.net") | stop("can't translate")
write(host.name, ": ", host.addresses)
end
</syntaxhighlight>
Sample Run:
Line 785:
Anyways:
<
orange.kame.net.
203.178.141.194
Line 791:
2!:0'dig -6 +short www.kame.net'
|interface error
| 2!:0'dig -6 +short www.kame.net'</
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 798:
=={{header|Java}}==
<
import java.net.Inet4Address;
import java.net.Inet6Address;
Line 819:
}
}
</syntaxhighlight>
Output:
<pre>
Line 827:
=={{header|JavaScript}}==
<
dns.lookup("www.kame.net", {
Line 835:
console.log(addresses);
})
</syntaxhighlight>
Output:
<pre>
Line 844:
=={{header|Julia}}==
As entered at the REPL command line:
<
julia> using Sockets
Line 853:
ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
</syntaxhighlight>
=={{header|Kotlin}}==
<
import java.net.InetAddress
Line 882:
fun main(args: Array<String>) {
showIPAddresses("www.kame.net")
}</
{{out}}
Line 894:
=={{header|Lasso}}==
The DNS lookup methods in Lasso do not support IPv6 addresses at this time, only IPv4.
<
=={{header|Lua}}==
Line 900:
{{libheader|LuaSocket}}
<
local ip_tbl = socket.dns.getaddrinfo('www.kame.net')
Line 906:
io.write(string.format('%s: %s\n', v.family, v.addr))
end
</syntaxhighlight>
{{out}}
<pre>
Line 916:
Neko does not yet support ipv6. ipv4 addresses are returned as Int32.
<
var host_resolve = $loader.loadprim("std@host_resolve", 1);
var host_to_string = $loader.loadprim("std@host_to_string", 1);
Line 924:
$print("www.kame.net: ", ip, ", ", host_to_string(ip), "\n");
$print(host_to_string(ip), ": ", host_reverse(ip), "\n");</
{{out}}
Line 933:
=={{header|NetRexx}}==
<
/* NetRexx */
options replace format comments java crossref symbols nobinary
Line 947:
end
end ir
</syntaxhighlight>
;Output
<pre>
Line 955:
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(define (dnsLookup site , ipv)
Line 968:
(dnsLookup "www.kame.net")
</syntaxhighlight>
;Output
<pre>
Line 977:
=={{header|Nim}}==
<
iterator items(ai: ptr AddrInfo): ptr AddrInfo =
Line 992:
echo getAddrString i.aiAddr
when isMainModule: main()</
{{out}}
Line 1,001:
{{Works with|oo2c version 2}}
IO:Address module supports only IPv4
<
MODULE DNSQuery;
IMPORT
Line 1,018:
Do;
END DNSQuery.
</syntaxhighlight>
{{Out}}
<pre>
Line 1,025:
=={{header|Objeck}}==
<
class Rosetta {
Line 1,034:
};
}
}</
Output:
Line 1,043:
=={{header|OCaml}}==
<
let opts = [
Unix.AI_FAMILY ai_family;
Line 1,062:
Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET);
Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6);
;;</
=={{header|Perl}}==
<
use Socket qw(getaddrinfo getnameinfo);
Line 1,071:
die "getaddrinfo error: $err" if $err;
say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res</
{{out}}
<pre>203.178.141.194
Line 1,079:
Translated from C/MSDN/several man pages.<br>
<b>NB:</b> may warrant further testing, see output.
<!--<
<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>
Line 1,223:
<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>
<!--</
{{out}}
Note that windows nslookup shows an IPv6 that this does not, whereas
Line 1,244:
=={{header|PHP}}==
Works for PHP5 (Windows > 5.3.0)
<
$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";
?></
=={{header|PicoLisp}}==
<
(in '(host "www.kame.net")
(while (from "address ")
(link (till "^J" T)) ) ) )</
Output:
<pre>-> ("203.178.141.194" "2001:200:dff:fff1:216:3eff:feb1:44d7")</pre>
Line 1,261:
=={{header|Pike}}==
<syntaxhighlight lang="pike">
> array ips = Protocols.DNS.gethostbyname("www.kame.net")[1] || ({});
> write(ips*"\n");
</syntaxhighlight>
Output:
<pre>203.178.141.194
Line 1,270:
=={{header|PowerShell}}==
<
Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address</
=={{header|Python}}==
<
>>> 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</
=={{header|R}}==
Line 1,287:
If the following is saved as <tt>dns.cpp</tt>:
<
#include <Rcpp.h>
#include <arpa/inet.h>
Line 1,335:
}
</syntaxhighlight>
It can be used to perform the task in R:
<syntaxhighlight lang="r">
library(Rcpp)
sourceCpp("dns.cpp")
Line 1,345:
## [1] "203.178.141.194"
## [2] "2001:200:dff:fff1:216:3eff:feb1:44d7"
</syntaxhighlight>
=={{header|Racket}}==
Line 1,351:
The following finds an IPv4 address. Currently, the API does not support returning an IPv6 address.
<
#lang racket
(require net/dns)
(dns-get-address "8.8.8.8" "www.kame.net")
</syntaxhighlight>
=={{header|Raku}}==
Line 1,362:
{{works with|Rakudo|2017.01}}
<syntaxhighlight lang="raku"
my $resolver = Net::DNS.new('8.8.8.8');
Line 1,370:
say $ip4[0].octets.join: '.';
say $ip6[0].octets.».fmt("%.2X").join.comb(4).join: ':';</
{{out}}
<pre>203.178.141.194
Line 1,379:
Execution note: if the information for '''PING -6 ···''' is blank, you may need to install (on some <br>Microsoft Windows systems) the IPV6 interface using the command: ''' IPV6 install '''
<
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,394:
end /*j*/ /* └─◄ force (TEMP) file integrity.*/
/*stick a fork in it, we're all done. */
'ERASE' tFID /*clean up (delete) the temporary file.*/</
'''output''' using the default input:
<pre>
Line 1,402:
=={{header|Ruby}}==
<
=> 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"]</
=={{header|Rust}}==
<
fn main() {
Line 1,424:
println!("{}", ip_port.ip());
}
}</
Output:<pre>203.178.141.194
Line 1,431:
=={{header|Scala}}==
{{libheader|Scala}}
<
InetAddress.getAllByName("www.kame.net").foreach(x => println(x.getHostAddress))</
=={{header|Scheme}}==
{{works with|Guile}}
<
(define n (car (hostent:addr-list (gethost "www.kame.net"))))
Line 1,443:
(display (inet-ntoa n))(newline)
(display (inet-ntop AF_INET n))(newline)
(display (inet-ntop AF_INET6 n))(newline)</
Output:<pre>203.178.141.194
203.178.141.194
Line 1,454:
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.
<
include "socket.s7i";
Line 1,460:
begin
writeln(numericAddress(inetSocketAddress("www.kame.net", 1024)));
end func;</
Output:
Line 1,468:
=={{header|Sidef}}==
<
'www.kame.net', 0,
Hash.new(protocol => Socket.IPPROTO_TCP)
Line 1,475:
res.each { |z|
say [Socket.getnameinfo(z{:addr}, Socket.NI_NUMERICHOST)][1];
}</
{{out}}
Line 1,485:
=={{header|Standard ML}}==
The basis library provides IPv4 support only:
<
val it = SOME "203.178.141.194": string option</
=={{header|Tcl}}==
Line 1,492:
{{tcllib|dns}}
{{libheader|udp}}
<
package require dns
Line 1,501:
update; # Let queries complete
}
puts "primary addresses of $host are:\n\tIPv4» [dns::address $v4]\n\tIPv6» [dns::address $v6]"</
Output:
<pre>
Line 1,511:
=={{header|UNIX Shell}}==
===Using dig===
<
Aamrun ~ % dig www.kame.net A www.kame.net AAAA +short
mango.itojun.org.
Line 1,519:
2001:2f0:0:8800::1:1
Aamrun ~ %
</syntaxhighlight>
===Using nslookup===
<
Aamrun ~ % nslookup
> set q=AAAA
Line 1,546:
Aamrun ~ %
</syntaxhighlight>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function dns_query(url,ver)
Set r = New RegExp
Line 1,566:
Call dns_query(WScript.Arguments(0),WScript.Arguments(1))
</syntaxhighlight>
{{Out}}
Line 1,583:
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
<
class Net {
Line 1,591:
var host = "orange.kame.net"
var addrs = Net.lookupHost(host).split(", ")
System.print(addrs.join("\n"))</
which we embed in the following Go program and run it:
{{libheader|WrenGo}}
<
package main
Line 1,624:
vm.InterpretFile(fileName)
vm.Free()
}</
{{out}}
Line 1,634:
=={{header|zkl}}==
The addrInfo method is just a front end to POSIX getaddrinfo.
<
L("orange.kame.net",L("203.178.141.194","2001:200:dff:fff1:216:3eff:feb1:44d7"))</
|