@@ -114,10 +114,9 @@ def encodeHost(host):
114
114
return '\xfd \x87 \xd8 \x7e \xeb \x43 ' + base64 .b32decode (
115
115
host .split ("." )[0 ], True )
116
116
elif host .find (':' ) == - 1 :
117
- import pdb ; pdb .set_trace ()
118
117
if PY3 :
119
- return '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + \
120
- socket .inet_aton (host ). decode ( 'utf-8' )
118
+ return b '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + \
119
+ socket .inet_aton (host )
121
120
else :
122
121
return '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + \
123
122
socket .inet_aton (host )
@@ -166,54 +165,99 @@ def checkIPAddress(host, private=False):
166
165
Returns hostStandardFormat if it is a valid IP address,
167
166
otherwise returns False
168
167
"""
169
- if host [0 :12 ] == '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
170
- hostStandardFormat = socket .inet_ntop (socket .AF_INET , host [12 :])
171
- return checkIPv4Address (host [12 :], hostStandardFormat , private )
172
- elif host [0 :6 ] == '\xfd \x87 \xd8 \x7e \xeb \x43 ' :
173
- # Onion, based on BMD/bitcoind
174
- hostStandardFormat = base64 .b32encode (host [6 :]).lower () + ".onion"
175
- if private :
176
- return False
177
- return hostStandardFormat
168
+ if not isinstance (host , str ):
169
+ if host [0 :12 ] == b'\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
170
+ hostStandardFormat = socket .inet_ntop (socket .AF_INET , host [12 :])
171
+ return checkIPv4Address (host [12 :], hostStandardFormat , private )
172
+ elif host [0 :6 ] == b'\xfd \x87 \xd8 \x7e \xeb \x43 ' :
173
+ # Onion, based on BMD/bitcoind
174
+ hostStandardFormat = base64 .b32encode (host [6 :]).lower () + ".onion"
175
+ if private :
176
+ return False
177
+ return hostStandardFormat
178
+ else :
179
+ try :
180
+ hostStandardFormat = socket .inet_ntop (socket .AF_INET6 , host )
181
+ except ValueError :
182
+ return False
183
+ if hostStandardFormat == "" :
184
+ # This can happen on Windows systems which are
185
+ # not 64-bit compatible so let us drop the IPv6 address.
186
+ return False
187
+ return checkIPv6Address (host , hostStandardFormat , private )
178
188
else :
179
- try :
180
- hostStandardFormat = socket .inet_ntop (socket .AF_INET6 , host )
181
- except ValueError :
182
- return False
183
- if hostStandardFormat == "" :
184
- # This can happen on Windows systems which are
185
- # not 64-bit compatible so let us drop the IPv6 address.
186
- return False
187
- return checkIPv6Address (host , hostStandardFormat , private )
189
+ if host [0 :12 ] == '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
190
+ hostStandardFormat = socket .inet_ntop (socket .AF_INET , host [12 :])
191
+ return checkIPv4Address (host [12 :], hostStandardFormat , private )
192
+ elif host [0 :6 ] == '\xfd \x87 \xd8 \x7e \xeb \x43 ' :
193
+ # Onion, based on BMD/bitcoind
194
+ hostStandardFormat = base64 .b32encode (host [6 :]).lower () + ".onion"
195
+ if private :
196
+ return False
197
+ return hostStandardFormat
198
+ else :
199
+ try :
200
+ hostStandardFormat = socket .inet_ntop (socket .AF_INET6 , host )
201
+ except ValueError :
202
+ return False
203
+ if hostStandardFormat == "" :
204
+ # This can happen on Windows systems which are
205
+ # not 64-bit compatible so let us drop the IPv6 address.
206
+ return False
207
+ return checkIPv6Address (host , hostStandardFormat , private )
188
208
189
209
190
210
def checkIPv4Address (host , hostStandardFormat , private = False ):
191
211
"""
192
212
Returns hostStandardFormat if it is an IPv4 address,
193
213
otherwise returns False
194
214
"""
195
- if host [0 ] == '\x7F ' : # 127/8
196
- if not private :
197
- logger .debug (
198
- 'Ignoring IP address in loopback range: %s' ,
199
- hostStandardFormat )
200
- return hostStandardFormat if private else False
201
- if host [0 ] == '\x0A ' : # 10/8
202
- if not private :
203
- logger .debug (
204
- 'Ignoring IP address in private range: %s' , hostStandardFormat )
205
- return hostStandardFormat if private else False
206
- if host [0 :2 ] == '\xC0 \xA8 ' : # 192.168/16
207
- if not private :
208
- logger .debug (
209
- 'Ignoring IP address in private range: %s' , hostStandardFormat )
210
- return hostStandardFormat if private else False
211
- if host [0 :2 ] >= '\xAC \x10 ' and host [0 :2 ] < '\xAC \x20 ' : # 172.16/12
212
- if not private :
213
- logger .debug (
214
- 'Ignoring IP address in private range: %s' , hostStandardFormat )
215
- return hostStandardFormat if private else False
216
- return False if private else hostStandardFormat
215
+ if not isinstance (host , str ):
216
+ if host [0 ] == 127 : # 127/8
217
+ if not private :
218
+ logger .debug (
219
+ 'Ignoring IP address in loopback range: %s' ,
220
+ hostStandardFormat )
221
+ return hostStandardFormat if private else False
222
+ if host [0 ] == 10 : # 10/8
223
+ if not private :
224
+ logger .debug (
225
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
226
+ return hostStandardFormat if private else False
227
+ if host [0 :2 ] == b'\xC0 \xA8 ' : # 192.168/16
228
+ if not private :
229
+ logger .debug (
230
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
231
+ return hostStandardFormat if private else False
232
+ if host [0 :2 ] >= b'\xAC \x10 ' and host [0 :2 ] < '\xAC \x20 ' : # 172.16/12
233
+ if not private :
234
+ logger .debug (
235
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
236
+ return hostStandardFormat if private else False
237
+ return False if private else hostStandardFormat
238
+ else :
239
+ if host [0 ] == '\x7F ' : # 127/8
240
+ if not private :
241
+ logger .debug (
242
+ 'Ignoring IP address in loopback range: %s' ,
243
+ hostStandardFormat )
244
+ return hostStandardFormat if private else False
245
+ if host [0 ] == '\x0A ' : # 10/8
246
+ if not private :
247
+ logger .debug (
248
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
249
+ return hostStandardFormat if private else False
250
+ if host [0 :2 ] == '\xC0 \xA8 ' : # 192.168/16
251
+ if not private :
252
+ logger .debug (
253
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
254
+ return hostStandardFormat if private else False
255
+ if host [0 :2 ] >= '\xAC \x10 ' and host [0 :2 ] < '\xAC \x20 ' : # 172.16/12
256
+ if not private :
257
+ logger .debug (
258
+ 'Ignoring IP address in private range: %s' , hostStandardFormat )
259
+ return hostStandardFormat if private else False
260
+ return False if private else hostStandardFormat
217
261
218
262
219
263
def checkIPv6Address (host , hostStandardFormat , private = False ):
0 commit comments