Changeset c27409d8add39f283ce96c098e8dc42758cc8436
- Timestamp:
- 07/02/10 00:26:34 (2 months ago)
- Author:
- John M. Schanck <john@…>
- git-author:
- John M. Schanck <john@anomos.info> / 2010-06-23T17:27:19Z-0400
- Parents:
- b75351bd5291518a8815ccb172af3b384ecdd35b
- Children:
- 189303090586c95b7df7c6dd0b9bcdb8eab2e5a8
- git-committer:
- John M. Schanck <john@anomos.info> / 2010-07-01T20:26:34Z-0400
- Message:
-
2-byte NIDs
- Location:
- Anomos
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
re5b840b
|
rc27409d
|
|
| 16 | 16 | # Written by John M. Schanck |
| 17 | 17 | |
| 18 | | from Anomos.Protocol import NAT_CHECK_ID, NAME as protocol_name |
| 19 | | from Anomos import LOG as log |
| | 18 | from Anomos.Protocol import NAT_CHECK_ID, HDR_EXT_LEN, HDR_NID_LEN, NAME as protocol_name |
| | 19 | from Anomos import LOG as log, fnid |
| 20 | 20 | |
| 21 | 21 | class AnomosNeighborInitializer(object): |
| … |
… |
|
| 51 | 51 | return |
| 52 | 52 | self._message = '' |
| 53 | | yield 1 # NID |
| | 53 | yield HDR_NID_LEN |
| 54 | 54 | if self.id is None: |
| 55 | 55 | self.id = self._message |
| … |
… |
|
| 58 | 58 | return |
| 59 | 59 | self._message = '' |
| 60 | | yield 7 # reserved bytes (ignore these for now) |
| | 60 | yield HDR_EXT_LEN # reserved bytes (ignore these for now) |
| 61 | 61 | self._got_full_header() |
| 62 | 62 | self._message = '' |
| … |
… |
|
| 73 | 73 | self.socket = None |
| 74 | 74 | def protocol_extensions(self): |
| 75 | | """Anomos puts [1:nid][7:null char] into the |
| | 75 | """Anomos puts [1:nid][6:null char] into the |
| 76 | 76 | BitTorrent reserved header bytes""" |
| 77 | | return self.id + '\0\0\0\0\0\0\0' |
| | 77 | return self.id + '\0\0\0\0\0\0' |
| 78 | 78 | def write_header(self): |
| 79 | 79 | """Return properly formatted Anomos connection header |
| 80 | 80 | example with id 255: |
| 81 | | \x06Anomos\xff\x00\x00\x00\x00\x00\x00\x00 |
| | 81 | \x06Anomos\xff\xff\x00\x00\x00\x00\x00\x00 |
| 82 | 82 | """ |
| 83 | 83 | hdr = chr(len(protocol_name)) + protocol_name + \ |
| … |
… |
|
| 86 | 86 | def socket_closed(self): |
| 87 | 87 | if self.id and self.id != NAT_CHECK_ID: |
| 88 | | log.info("Failed to initialize connection to \\x%02x" % ord(self.id)) |
| | 88 | log.info("Failed to initialize connection to %s" % fnid(self.id)) |
| 89 | 89 | if not self.complete: |
| 90 | 90 | self.manager.initializer_failed(self.id) |
-
|
r8c85782
|
rc27409d
|
|
| 15 | 15 | |
| 16 | 16 | from Anomos.Protocol.AnomosEndPointProtocol import AnomosEndPointProtocol |
| 17 | | from Anomos import LOG as log |
| | 17 | from Anomos import LOG as log, fnid |
| 18 | 18 | |
| 19 | 19 | class EndPoint(AnomosEndPointProtocol): |
| … |
… |
|
| 110 | 110 | |
| 111 | 111 | def uniq_id(self): |
| 112 | | return "[%02x:%04x]" % (ord(self.neighbor.id), self.stream_id) |
| | 112 | return fnid(self.neighbor.id, self.stream_id) |
| 113 | 113 | |
| 114 | 114 | def send_partial(self, amount): |
-
|
rfb52fa8
|
rc27409d
|
|
| 16 | 16 | from Anomos.AnomosNeighborInitializer import AnomosNeighborInitializer |
| 17 | 17 | from Anomos.P2PConnection import P2PConnection |
| | 18 | from Anomos.Protocol import NAT_CHECK_ID |
| 18 | 19 | |
| 19 | 20 | from Anomos import LOG as log |
| … |
… |
|
| 26 | 27 | self.ip = ip |
| 27 | 28 | self.port = port |
| 28 | | self.id = chr(255) |
| 29 | 29 | |
| 30 | 30 | self.socket = P2PConnection(addr=(ip,port), |
| … |
… |
|
| 45 | 45 | self.answer(False) |
| 46 | 46 | |
| 47 | | AnomosNeighborInitializer(self, self.socket, self.id) |
| | 47 | AnomosNeighborInitializer(self, self.socket, NAT_CHECK_ID) |
| 48 | 48 | else: |
| 49 | 49 | self.answer(False) |
-
|
rf160149
|
rc27409d
|
|
| 20 | 20 | from Anomos.Protocol.AnomosNeighborProtocol import AnomosNeighborProtocol |
| 21 | 21 | from Anomos.Protocol import NAT_CHECK_ID |
| 22 | | from Anomos import LOG as log |
| | 22 | from Anomos import LOG as log, fnid |
| 23 | 23 | |
| 24 | 24 | from Anomos.Protocol import toint |
| … |
… |
|
| 192 | 192 | |
| 193 | 193 | def uniq_id(self): |
| 194 | | return "%02x:*" % (ord(self.id)) |
| | 194 | return "%s:*" % fnid(self.id) |
-
|
r8c85782
|
rc27409d
|
|
| 24 | 24 | from Anomos.Protocol import NAT_CHECK_ID |
| 25 | 25 | from Anomos.Measure import Measure |
| 26 | | from Anomos import BTFailure, LOG as log |
| | 26 | from Anomos import BTFailure, LOG as log, fnid |
| 27 | 27 | |
| 28 | 28 | class NeighborManager(object): |
| … |
… |
|
| 57 | 57 | if self.nid_collision(id, loc): |
| 58 | 58 | # Already had neighbor by the given id at a different location |
| 59 | | log.warning('NID collision - x%02x' % ord(id)) |
| | 59 | log.warning('NID collision - [%s]' % fnid(id)) |
| 60 | 60 | # To be safe, kill connection with the neighbor we already |
| 61 | 61 | # had with the requested ID and add ID to the failed list |
| … |
… |
|
| 98 | 98 | for k,v in self.incomplete.iteritems(): |
| 99 | 99 | if v == sock.addr: |
| 100 | | log.info('Discarding neighbor \\x%02x @ %s:%d' % |
| 101 | | (ord(k), sock.addr[0], sock.addr[1])) |
| 102 | | self.rm_neighbor(k) |
| | 100 | log.info('Discarding neighbor %s @ %s:%d' % |
| | 101 | (fnid(k), sock.addr[0], sock.addr[1])) |
| | 102 | self.rm_neighbor(j) |
| 103 | 103 | |
| 104 | 104 | def failed_connections(self): |
| … |
… |
|
| 114 | 114 | self.neighbors[id] = NeighborLink(self, socket, id, \ |
| 115 | 115 | self.config, self.ratelimiter) |
| 116 | | log.info("Added Neighbor: \\x%02x @ %s:%d" % |
| 117 | | (ord(id), socket.addr[0], socket.addr[1])) |
| | 116 | log.info("Added Neighbor: %s @ %s:%d" % |
| | 117 | (fnid(id), socket.addr[0], socket.addr[1])) |
| 118 | 118 | |
| 119 | 119 | def rm_neighbor(self, nid): |
| … |
… |
|
| 124 | 124 | if nid is not None: |
| 125 | 125 | self.failedPeers.append(nid) |
| 126 | | log.info("Removed Neighbor: \\x%02x" % ord(nid)) |
| | 126 | log.info("Removed Neighbor: %s" % fnid(nid)) |
| 127 | 127 | if self.waiting_tcs.has_key(nid): |
| 128 | 128 | del self.waiting_tcs[nid] |
| … |
… |
|
| 207 | 207 | log.error("Not starting circuit -- Unknown torrent") |
| 208 | 208 | elif nid in self.failedPeers: |
| 209 | | log.info("Not starting circuit -- no longer connected to \\x%02x" % ord(nid)) |
| | 209 | log.info("Not starting circuit -- no longer connected to %s" % fnid(nid)) |
| 210 | 210 | elif nid in self.incomplete: |
| 211 | | log.info("Postponing circuit until neighbor \\x%02x completes " % ord(nid)) |
| | 211 | log.info("Postponing circuit until neighbor %s completes " % fnid(nid)) |
| 212 | 212 | self.schedule_tc(nid, infohash, aeskey, nextTC) |
| 213 | 213 | elif nid not in self.neighbors: |
| 214 | | log.error("Not starting circuit -- NID \\x%02x is not assigned" % ord(nid)) |
| | 214 | log.error("Not starting circuit -- NID %s is not assigned" % fnid(nid)) |
| 215 | 215 | else: |
| 216 | 216 | self.neighbors[nid].start_endpoint_stream(torrent, aeskey, data=nextTC) |
-
|
r78ce04c
|
rc27409d
|
|
| 17 | 17 | |
| 18 | 18 | import random |
| | 19 | import struct |
| 19 | 20 | from sys import maxint as INFINITY |
| 20 | 21 | import Anomos.Crypto |
| … |
… |
|
| 133 | 134 | return self.sessionid |
| 134 | 135 | |
| 135 | | def get_avail_nids(self): |
| 136 | | """ |
| 137 | | @return: set object containing NIDs in range 0 -> 254 which are not in use |
| 138 | | @rtype: set of ints |
| 139 | | """ |
| 140 | | used = set(self.id_map.keys()) |
| 141 | | return SimPeer.ID_RANGE.copy() - used |
| 142 | | |
| 143 | 136 | def get_nid(self, peerid, default=None): |
| 144 | 137 | """ Return the relative ID associated with peerid |
| … |
… |
|
| 299 | 292 | p1 = self.get(v1) |
| 300 | 293 | p2 = self.get(v2) |
| 301 | | nidsP1 = p1.get_avail_nids() |
| 302 | | nidsP2 = p2.get_avail_nids() |
| 303 | | l = list(nidsP1.intersection(nidsP2)) |
| 304 | | if len(l): |
| 305 | | nid = random.choice(l) |
| | 294 | # Generate 10 random NIDs |
| | 295 | tmp = set([struct.pack('H', random.randint(0, 0xffff)) for i in range(10)]) |
| | 296 | p1avail = set(p1.id_map.keys()) |
| | 297 | p2avail = set(p2.id_map.keys()) |
| | 298 | usable = list((tmp - p1avail) & (tmp - p2avail)) |
| | 299 | if len(usable) > 0: |
| | 300 | nid = random.choice(usable) |
| 306 | 301 | p1.add_neighbor(v2, nid, p2.ip, p2.port) |
| 307 | 302 | p2.add_neighbor(v1, nid, p1.ip, p2.port) |
| 308 | | else: |
| 309 | | raise RuntimeError("No available NeighborIDs. It's possible the \ |
| 310 | | network is being attacked.") |
| | 303 | return True |
| | 304 | return False |
| 311 | 305 | |
| 312 | 306 | def rand_connect(self, peerid, numpeers): |
| … |
… |
|
| 338 | 332 | if not allow_close and opid_ip in used_ips: |
| 339 | 333 | continue |
| 340 | | self.connect(peerid, opid) |
| | 334 | if not self.connect(peerid, opid): |
| | 335 | continue |
| 341 | 336 | used_ips.append(opid_ip) |
| 342 | 337 | numpeers -= 1 |
-
|
r8c0ee5d
|
rc27409d
|
|
| 16 | 16 | |
| 17 | 17 | NAME = "Anomos10" |
| | 18 | HDR_NID_LEN = 2 |
| | 19 | HDR_EXT_LEN = 6 |
| 18 | 20 | |
| 19 | | NAT_CHECK_ID = chr(255) |
| | 21 | NAT_CHECK_ID = chr(255)+chr(255) |
| 20 | 22 | |
| 21 | 23 | #--Message Control Characters--# |
-
|
r53cdbdb
|
rc27409d
|
|
| 18 | 18 | from Anomos.Protocol.AnomosRelayerProtocol import AnomosRelayerProtocol |
| 19 | 19 | from Anomos.Measure import Measure |
| 20 | | from Anomos import LOG as log |
| | 20 | from Anomos import LOG as log, fnid |
| 21 | 21 | |
| 22 | 22 | class Relayer(AnomosRelayerProtocol): |
| … |
… |
|
| 147 | 147 | |
| 148 | 148 | def uniq_id(self): |
| 149 | | return "[%02x:%04x]" % (ord(self.neighbor.id), self.stream_id) |
| | 149 | return fnid(self.neighbor.id, self.stream_id) |
-
|
r390eee0
|
rc27409d
|
|
| 34 | 34 | TypeRange = (0,1) |
| 35 | 35 | SIDRange = (1,9) |
| 36 | | NIDRange = (9,10) |
| | 36 | NIDRange = (9,11) |
| 37 | 37 | IHashRange = (9,29) |
| 38 | 38 | KeyRange = (29,93) |
-
|
rb7b199c
|
rc27409d
|
|
| 5 | 5 | import M2Crypto.SSL as SSL |
| 6 | 6 | |
| 7 | | from Anomos.Protocol import NAT_CHECK_ID, NAME as PROTOCOL_NAME |
| | 7 | from Anomos.Protocol import NAT_CHECK_ID, HDR_EXT_LEN, HDR_NID_LEN, NAME as PROTOCOL_NAME |
| 8 | 8 | from Anomos.P2PConnection import PostConnectionChecker |
| 9 | 9 | from Anomos import LOG as log |
| … |
… |
|
| 33 | 33 | state = "init" |
| 34 | 34 | nid = NAT_CHECK_ID |
| 35 | | protocol_extensions = '\0'*7 |
| | 35 | protocol_extensions = '\0'*6 |
| 36 | 36 | expecting = 1 |
| 37 | 37 | msgbuf = "" |
| … |
… |
|
| 90 | 90 | self.check_failed("Protocol name mismatch") |
| 91 | 91 | return "done" |
| 92 | | self.expecting = 1 |
| | 92 | self.expecting = HDR_NID_LEN |
| 93 | 93 | return "nid" |
| 94 | 94 | |
| … |
… |
|
| 97 | 97 | self.check_failed("Neighbor ID mismatch") |
| 98 | 98 | return "done" |
| 99 | | self.expecting = 7 |
| | 99 | self.expecting = HDR_EXT_LEN |
| 100 | 100 | return "flags" |
| 101 | 101 | |
| 102 | 102 | def proto_flags(self, msg): |
| 103 | | if len(msg) != 7: |
| | 103 | if len(msg) != HDR_EXT_LEN: |
| 104 | 104 | self.check_failed("Invalid header") |
| 105 | 105 | elif len(self.msgbuf) > 0: |
-
|
rb75351b
|
rc27409d
|
|
| 143 | 143 | chr((i >> 8) & 0xFF) + chr(i & 0xFF)) |
| 144 | 144 | |
| | 145 | def fnid(nid, stream=None): |
| | 146 | if stream: |
| | 147 | return "[%s:%04x]" % (b2a_hex(nid), stream) |
| | 148 | return "[%s]" % b2a_hex(nid) |
| 145 | 149 | |
| 146 | 150 | import socket |
-
|
r515582f
|
rc27409d
|
|
| 108 | 108 | if p.has_key('nid'): |
| 109 | 109 | nid = p.get('nid') |
| 110 | | if type(nid) != str or len(nid) != 1: |
| | 110 | if type(nid) != str or len(nid) != 2: |
| 111 | 111 | raise BTFailure('invalid entry in peer list') |
| 112 | 112 | #PeerID only used in BitTorrent |