1414
1515/* ────────────────────────────────────────────────────────────────────────────*/
1616
17+ namespace nodepp { struct dns_t { string_t address, hostname; int family; }; }
1718namespace nodepp { namespace dns {
1819
1920 inline bool is_ipv6 ( const string_t & URL ){
20- thread_local static regex_t reg ( " ([0-9a-fA-F]+\\ :)+[0-9a-fA-F]+ " );
21+ thread_local static regex_t reg ( " ([0-9a-fA-F]+\\ :)+" , true );
2122 reg.clear_memory (); return reg.test ( URL ) ? 1 : 0 ;
2223 }
2324
@@ -29,29 +30,58 @@ namespace nodepp { namespace dns {
2930 /* ─······································································─*/
3031
3132 inline bool is_ip ( const string_t & URL ){
32- if ( URL.empty () )/* - */ { return 0 ; }
33- if ( is_ipv4 (URL) > 0 ){ return 1 ; }
34- if ( is_ipv6 (URL) > 0 ){ return 1 ; } return 0 ;
33+ if ( URL.empty ( ) ){ return 0 ; }
34+ if ( is_ipv4 (URL) ){ return 1 ; }
35+ if ( is_ipv6 (URL) ){ return 1 ; } return 0 ;
3536 }
3637
3738 /* ─······································································─*/
3839
39- inline string_t lookup ( string_t host, int family = AF_UNSPEC ) {
40+ inline ptr_t < dns_t > lookup ( string_t host, int family = AF_UNSPEC ) {
4041 _socket_::start_device ();
4142
42- if ( family == AF_INET6 ) {
43- if ( host == " broadcast" || host == " ::2" ){ return " ::2" ; }
44- elif ( host == " localhost" || host == " ::1" ){ return " ::1" ; }
45- elif ( host == " global" || host == " ::0" ){ return " ::0" ; }
46- elif ( host == " loopback" || host == " ::3" ){ return " ::3" ; }
47- } else {
48- if ( host == " broadcast" || host == " 255.255.255.255" ){ return " 255.255.255.255" ; }
49- elif ( host == " localhost" || host == " 127.0.0.1" ){ return " 127.0.0.1" ; }
50- elif ( host == " global" || host == " 0.0.0.0" ){ return " 0.0.0.0" ; }
51- elif ( host == " loopback" || host == " 1.1.1.1" ){ return " 1.1.1.1" ; }
52- } if ( url::is_valid (host) ){ host = url::hostname (host); }
53-
54- addrinfo hints, *res, *ptr; memset ( &hints, 0 , sizeof (hints) );
43+ if ( family == AF_INET ) {
44+
45+ if ( host == " broadcast" || host == " 255.255.255.255" ){
46+ dns_t tmp ({ " 255.255.255.255" , host, AF_INET });
47+ return ptr_t <dns_t >( 0UL , tmp );
48+ }
49+ elif ( host == " localhost" || host == " 127.0.0.1" ){
50+ dns_t tmp ({ " 127.0.0.1" , host, AF_INET });
51+ return ptr_t <dns_t >( 0UL , tmp );
52+ }
53+ elif ( host == " global" || host == " 0.0.0.0" ){
54+ dns_t tmp ({ " 0.0.0.0" , host, AF_INET });
55+ return ptr_t <dns_t >( 0UL , tmp );
56+ }
57+ elif ( host == " loopback" || host == " 1.1.1.1" ){
58+ dns_t tmp ({ " 1.1.1.1" , host, AF_INET });
59+ return ptr_t <dns_t >( 0UL , tmp );
60+ }
61+
62+ } elif ( family == AF_INET6 ) {
63+
64+ if ( host == " broadcast" || host == " ::2" ){
65+ dns_t tmp ({ " ::2" , host, AF_INET6 });
66+ return ptr_t <dns_t >( 0UL , tmp );
67+ }
68+ elif ( host == " localhost" || host == " ::1" ){
69+ dns_t tmp ({ " ::1" , host, AF_INET6 });
70+ return ptr_t <dns_t >( 0UL , tmp );
71+ }
72+ elif ( host == " global" || host == " ::0" ){
73+ dns_t tmp ({ " ::0" , host, AF_INET6 });
74+ return ptr_t <dns_t >( 0UL , tmp );
75+ }
76+ elif ( host == " loopback" || host == " ::3" ){
77+ dns_t tmp ({ " ::3" , host, AF_INET6 });
78+ return ptr_t <dns_t >( 0UL , tmp );
79+ }
80+
81+ }
82+
83+ addrinfo hints, *res, *ptr; memset ( &hints, 0 , sizeof (hints) );
84+ if ( url::is_valid (host) ){ host = url::hostname (host); }
5585
5686 hints.ai_socktype = SOCK_STREAM;
5787 hints.ai_family = family ;
@@ -60,7 +90,8 @@ namespace nodepp { namespace dns {
6090 if ( getaddrinfo ( host.get (), nullptr , &hints, &res ) != 0 )
6191 { return nullptr ; }
6292
63- char ipstr[INET6_ADDRSTRLEN]; string_t ipAddress;
93+ string_t ipAddress ; char ipstr[INET6_ADDRSTRLEN];
94+ queue_t <dns_t > list;
6495
6596 for ( ptr = res; ptr != nullptr ; ptr = ptr->ai_next ) {
6697 void *addr = nullptr ;
@@ -73,20 +104,21 @@ namespace nodepp { namespace dns {
73104
74105 if ( addr ) {
75106 inet_ntop ( ptr->ai_family , addr, ipstr, sizeof (ipstr) );
76- ipAddress = ipstr; if ( family != AF_UNSPEC ){ break ; }
107+ list.push ( dns_t ({ ipstr, host, ptr->ai_family }) );
108+ if ( family != AF_UNSPEC ){ break ; }
77109 }
78110
79111 }
80112
81- freeaddrinfo (res); return ipAddress ;
113+ freeaddrinfo (res); return list. data () ;
82114 }
83115
84116 /* ─······································································─*/
85117
86118 inline expected_t <ip_t ,except_t > get_host_data (){
87119 auto socket = socket_t ();
88120
89- socket.SOCK = SOCK_DGRAM;
121+ socket.SOCK = SOCK_DGRAM ;
90122 socket.IPPROTO = IPPROTO_UDP;
91123 socket.socket ( " loopback" , 0 );
92124 socket.connect ();
0 commit comments