@@ -91,16 +91,16 @@ def isBitSetWithinBitfield(fourByteString, n):
9191 return x & 2 ** n != 0
9292
9393
94- # ip addresses
94+ # IP addresses
9595
9696
9797def encodeHost (host ):
9898 """Encode a given host to be used in low-level socket operations"""
9999 if host .find ('.onion' ) > - 1 :
100- return '\xfd \x87 \xd8 \x7e \xeb \x43 ' + base64 .b32decode (
100+ return b '\xfd \x87 \xd8 \x7e \xeb \x43 ' + base64 .b32decode (
101101 host .split ("." )[0 ], True )
102102 elif host .find (':' ) == - 1 :
103- return '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + \
103+ return b '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + \
104104 socket .inet_aton (host )
105105 return socket .inet_pton (socket .AF_INET6 , host )
106106
@@ -147,10 +147,10 @@ def checkIPAddress(host, private=False):
147147 Returns hostStandardFormat if it is a valid IP address,
148148 otherwise returns False
149149 """
150- if host [0 :12 ] == '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
150+ if host [0 :12 ] == b '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
151151 hostStandardFormat = socket .inet_ntop (socket .AF_INET , host [12 :])
152152 return checkIPv4Address (host [12 :], hostStandardFormat , private )
153- elif host [0 :6 ] == '\xfd \x87 \xd8 \x7e \xeb \x43 ' :
153+ elif host [0 :6 ] == b '\xfd \x87 \xd8 \x7e \xeb \x43 ' :
154154 # Onion, based on BMD/bitcoind
155155 hostStandardFormat = base64 .b32encode (host [6 :]).lower () + ".onion"
156156 if private :
@@ -161,7 +161,7 @@ def checkIPAddress(host, private=False):
161161 hostStandardFormat = socket .inet_ntop (socket .AF_INET6 , host )
162162 except ValueError :
163163 return False
164- if hostStandardFormat == "" :
164+ if hostStandardFormat :
165165 # This can happen on Windows systems which are
166166 # not 64-bit compatible so let us drop the IPv6 address.
167167 return False
@@ -173,23 +173,23 @@ def checkIPv4Address(host, hostStandardFormat, private=False):
173173 Returns hostStandardFormat if it is an IPv4 address,
174174 otherwise returns False
175175 """
176- if host [0 ] == '\x7F ' : # 127/8
176+ if host [0 : 1 ] == b '\x7F ' : # 127/8
177177 if not private :
178178 logger .debug (
179179 'Ignoring IP address in loopback range: %s' ,
180180 hostStandardFormat )
181181 return hostStandardFormat if private else False
182- if host [0 ] == '\x0A ' : # 10/8
182+ if host [0 : 1 ] == b '\x0A ' : # 10/8
183183 if not private :
184184 logger .debug (
185185 'Ignoring IP address in private range: %s' , hostStandardFormat )
186186 return hostStandardFormat if private else False
187- if host [0 :2 ] == '\xC0 \xA8 ' : # 192.168/16
187+ if host [0 :2 ] == b '\xC0 \xA8 ' : # 192.168/16
188188 if not private :
189189 logger .debug (
190190 'Ignoring IP address in private range: %s' , hostStandardFormat )
191191 return hostStandardFormat if private else False
192- if host [0 :2 ] >= '\xAC \x10 ' and host [0 :2 ] < '\xAC \x20 ' : # 172.16/12
192+ if host [0 :2 ] >= b '\xAC \x10 ' and host [0 :2 ] < b '\xAC \x20 ' : # 172.16/12
193193 if not private :
194194 logger .debug (
195195 'Ignoring IP address in private range: %s' , hostStandardFormat )
@@ -202,15 +202,19 @@ def checkIPv6Address(host, hostStandardFormat, private=False):
202202 Returns hostStandardFormat if it is an IPv6 address,
203203 otherwise returns False
204204 """
205- if host == ( '\x00 ' * 15 ) + '\x01 ' :
205+ if host == b '\x00 ' * 15 + b '\x01 ' :
206206 if not private :
207207 logger .debug ('Ignoring loopback address: %s' , hostStandardFormat )
208208 return False
209- if host [0 ] == '\xFE ' and (ord (host [1 ]) & 0xc0 ) == 0x80 :
209+ try :
210+ host = [ord (c ) for c in host [:2 ]]
211+ except TypeError : # python3 has ints already
212+ pass
213+ if host [0 :1 ] == b'\xFE ' and host [1 ] & 0xc0 == 0x80 :
210214 if not private :
211215 logger .debug ('Ignoring local address: %s' , hostStandardFormat )
212216 return hostStandardFormat if private else False
213- if ( ord ( host [0 ]) & 0xfe ) == 0xfc :
217+ if host [0 ] & 0xfe == 0xfc :
214218 if not private :
215219 logger .debug (
216220 'Ignoring unique local address: %s' , hostStandardFormat )
@@ -280,7 +284,7 @@ def isProofOfWorkSufficient(
280284# Packet creation
281285
282286
283- def CreatePacket (command , payload = '' ):
287+ def CreatePacket (command , payload = b '' ):
284288 """Construct and return a packet"""
285289 payload_length = len (payload )
286290 checksum = hashlib .sha512 (payload ).digest ()[0 :4 ]
@@ -298,14 +302,14 @@ def assembleVersionMessage(
298302 Construct the payload of a version message,
299303 return the resulting bytes of running `CreatePacket` on it
300304 """
301- payload = ''
305+ payload = b ''
302306 payload += pack ('>L' , 3 ) # protocol version.
303307 # bitflags of the services I offer.
304308 payload += pack (
305309 '>q' ,
306- NODE_NETWORK |
307- (NODE_SSL if haveSSL (server ) else 0 ) |
308- (NODE_DANDELION if state .dandelion else 0 )
310+ NODE_NETWORK
311+ | (NODE_SSL if haveSSL (server ) else 0 )
312+ | (NODE_DANDELION if state .dandelion else 0 )
309313 )
310314 payload += pack ('>q' , int (time .time ()))
311315
@@ -327,13 +331,13 @@ def assembleVersionMessage(
327331 # bitflags of the services I offer.
328332 payload += pack (
329333 '>q' ,
330- NODE_NETWORK |
331- (NODE_SSL if haveSSL (server ) else 0 ) |
332- (NODE_DANDELION if state .dandelion else 0 )
334+ NODE_NETWORK
335+ | (NODE_SSL if haveSSL (server ) else 0 )
336+ | (NODE_DANDELION if state .dandelion else 0 )
333337 )
334338 # = 127.0.0.1. This will be ignored by the remote host.
335339 # The actual remote connected IP will be used.
336- payload += '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + pack (
340+ payload += b '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + pack (
337341 '>L' , 2130706433 )
338342 # we have a separate extPort and incoming over clearnet
339343 # or outgoing through clearnet
@@ -355,7 +359,7 @@ def assembleVersionMessage(
355359 payload += nodeid [0 :8 ]
356360 else :
357361 payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf
358- userAgent = '/PyBitmessage:' + softwareVersion + '/'
362+ userAgent = ( '/PyBitmessage:%s/' % softwareVersion ). encode ( 'utf-8' )
359363 payload += encodeVarint (len (userAgent ))
360364 payload += userAgent
361365
0 commit comments