MCPcopy Index your code
hub / github.com/shadowsocks/shadowsocks / parse_header

Function parse_header

shadowsocks/common.py:163–199  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

161
162
163def parse_header(data):
164 addrtype = ord(data[0])
165 dest_addr = None
166 dest_port = None
167 header_length = 0
168 if addrtype & ADDRTYPE_MASK == ADDRTYPE_IPV4:
169 if len(data) >= 7:
170 dest_addr = socket.inet_ntoa(data[1:5])
171 dest_port = struct.unpack('>H', data[5:7])[0]
172 header_length = 7
173 else:
174 logging.warn('header is too short')
175 elif addrtype & ADDRTYPE_MASK == ADDRTYPE_HOST:
176 if len(data) > 2:
177 addrlen = ord(data[1])
178 if len(data) >= 4 + addrlen:
179 dest_addr = data[2:2 + addrlen]
180 dest_port = struct.unpack('>H', data[2 + addrlen:4 +
181 addrlen])[0]
182 header_length = 4 + addrlen
183 else:
184 logging.warn('header is too short')
185 else:
186 logging.warn('header is too short')
187 elif addrtype & ADDRTYPE_MASK == ADDRTYPE_IPV6:
188 if len(data) >= 19:
189 dest_addr = socket.inet_ntop(socket.AF_INET6, data[1:17])
190 dest_port = struct.unpack('>H', data[17:19])[0]
191 header_length = 19
192 else:
193 logging.warn('header is too short')
194 else:
195 logging.warn('unsupported addrtype %d, maybe wrong password or '
196 'encryption method' % addrtype)
197 if dest_addr is None:
198 return None
199 return addrtype, to_bytes(dest_addr), dest_port, header_length
200
201
202class IPNetwork(object):

Callers 4

_handle_serverMethod · 0.90
_handle_clientMethod · 0.90
_handle_stage_addrMethod · 0.90
test_parse_headerFunction · 0.70

Calls 1

to_bytesFunction · 0.85

Tested by 1

test_parse_headerFunction · 0.56