@@ -60,6 +60,31 @@ function errnoException(err, syscall, hostname) {
6060 return ex ;
6161}
6262
63+ const digits = [
64+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 0-15
65+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 16-31
66+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 32-47
67+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , // 48-63
68+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 64-79
69+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 80-95
70+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // 96-111
71+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 112-127
72+ ] ;
73+ function isIPv4 ( str ) {
74+ if ( ! digits [ str . charCodeAt ( 0 ) ] ) return false ;
75+ if ( str . length === 1 ) return false ;
76+ if ( str . charCodeAt ( 1 ) === 46 /*'.'*/ )
77+ return true ;
78+ else if ( ! digits [ str . charCodeAt ( 1 ) ] )
79+ return false ;
80+ if ( str . length === 2 ) return false ;
81+ if ( str . charCodeAt ( 2 ) === 46 /*'.'*/ )
82+ return true ;
83+ else if ( ! digits [ str . charCodeAt ( 2 ) ] )
84+ return false ;
85+ return ( str . length > 3 && str . charCodeAt ( 3 ) === 46 /*'.'*/ ) ;
86+ }
87+
6388
6489function onlookup ( err , addresses ) {
6590 if ( err ) {
@@ -68,25 +93,26 @@ function onlookup(err, addresses) {
6893 if ( this . family ) {
6994 this . callback ( null , addresses [ 0 ] , this . family ) ;
7095 } else {
71- this . callback ( null , addresses [ 0 ] , addresses [ 0 ] . indexOf ( ':' ) >= 0 ? 6 : 4 ) ;
96+ this . callback ( null , addresses [ 0 ] , isIPv4 ( addresses [ 0 ] ) ? 4 : 6 ) ;
7297 }
7398}
7499
75100
76101function onlookupall ( err , addresses ) {
77- var results = [ ] ;
78102 if ( err ) {
79103 return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
80104 }
81105
106+ var family = this . family ;
82107 for ( var i = 0 ; i < addresses . length ; i ++ ) {
83- results . push ( {
84- address : addresses [ i ] ,
85- family : this . family || ( addresses [ i ] . indexOf ( ':' ) >= 0 ? 6 : 4 )
86- } ) ;
108+ const addr = addresses [ i ] ;
109+ addresses [ i ] = {
110+ address : addr ,
111+ family : family || ( isIPv4 ( addr ) ? 4 : 6 )
112+ } ;
87113 }
88114
89- this . callback ( null , results ) ;
115+ this . callback ( null , addresses ) ;
90116}
91117
92118
0 commit comments