@@ -60,6 +60,31 @@ function errnoException(err, syscall, hostname) {
60
60
return ex ;
61
61
}
62
62
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
+
63
88
64
89
function onlookup ( err , addresses ) {
65
90
if ( err ) {
@@ -68,25 +93,26 @@ function onlookup(err, addresses) {
68
93
if ( this . family ) {
69
94
this . callback ( null , addresses [ 0 ] , this . family ) ;
70
95
} else {
71
- this . callback ( null , addresses [ 0 ] , addresses [ 0 ] . indexOf ( ':' ) >= 0 ? 6 : 4 ) ;
96
+ this . callback ( null , addresses [ 0 ] , isIPv4 ( addresses [ 0 ] ) ? 4 : 6 ) ;
72
97
}
73
98
}
74
99
75
100
76
101
function onlookupall ( err , addresses ) {
77
- var results = [ ] ;
78
102
if ( err ) {
79
103
return this . callback ( errnoException ( err , 'getaddrinfo' , this . hostname ) ) ;
80
104
}
81
105
106
+ var family = this . family ;
82
107
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
+ } ;
87
113
}
88
114
89
- this . callback ( null , results ) ;
115
+ this . callback ( null , addresses ) ;
90
116
}
91
117
92
118
0 commit comments