Changeset c27409d8add39f283ce96c098e8dc42758cc8436

Show
Ignore:
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:
12 modified

Legend:

Unmodified
Added
Removed
  • Anomos/AnomosNeighborInitializer.py

    re5b840b rc27409d  
    1616# Written by John M. Schanck 
    1717 
    18 from Anomos.Protocol import NAT_CHECK_ID, NAME as protocol_name 
    19 from Anomos import LOG as log 
     18from Anomos.Protocol import NAT_CHECK_ID, HDR_EXT_LEN, HDR_NID_LEN, NAME as protocol_name 
     19from Anomos import LOG as log, fnid 
    2020 
    2121class AnomosNeighborInitializer(object): 
     
    5151            return 
    5252        self._message = '' 
    53         yield 1  # NID 
     53        yield HDR_NID_LEN 
    5454        if self.id is None: 
    5555            self.id = self._message 
     
    5858            return 
    5959        self._message = '' 
    60         yield 7  # reserved bytes (ignore these for now) 
     60        yield HDR_EXT_LEN  # reserved bytes (ignore these for now) 
    6161        self._got_full_header() 
    6262        self._message = '' 
     
    7373        self.socket = None 
    7474    def protocol_extensions(self): 
    75         """Anomos puts [1:nid][7:null char] into the 
     75        """Anomos puts [1:nid][6:null char] into the 
    7676           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' 
    7878    def write_header(self): 
    7979        """Return properly formatted Anomos connection header 
    8080           example with id 255: 
    81                 \x06Anomos\xff\x00\x00\x00\x00\x00\x00\x00 
     81                \x06Anomos\xff\xff\x00\x00\x00\x00\x00\x00 
    8282        """ 
    8383        hdr = chr(len(protocol_name)) + protocol_name + \ 
     
    8686    def socket_closed(self): 
    8787        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)) 
    8989        if not self.complete: 
    9090            self.manager.initializer_failed(self.id) 
  • Anomos/EndPoint.py

    r8c85782 rc27409d  
    1515 
    1616from Anomos.Protocol.AnomosEndPointProtocol import AnomosEndPointProtocol 
    17 from Anomos import LOG as log 
     17from Anomos import LOG as log, fnid 
    1818 
    1919class EndPoint(AnomosEndPointProtocol): 
     
    110110 
    111111    def uniq_id(self): 
    112         return "[%02x:%04x]" % (ord(self.neighbor.id), self.stream_id) 
     112        return fnid(self.neighbor.id, self.stream_id) 
    113113 
    114114    def send_partial(self, amount): 
  • Anomos/NatCheck.py

    rfb52fa8 rc27409d  
    1616from Anomos.AnomosNeighborInitializer import AnomosNeighborInitializer 
    1717from Anomos.P2PConnection import P2PConnection 
     18from Anomos.Protocol import NAT_CHECK_ID 
    1819 
    1920from Anomos import LOG as log 
     
    2627        self.ip = ip 
    2728        self.port = port 
    28         self.id = chr(255) 
    2929 
    3030        self.socket = P2PConnection(addr=(ip,port), 
     
    4545                self.answer(False) 
    4646 
    47             AnomosNeighborInitializer(self, self.socket, self.id) 
     47            AnomosNeighborInitializer(self, self.socket, NAT_CHECK_ID) 
    4848        else: 
    4949            self.answer(False) 
  • Anomos/NeighborLink.py

    rf160149 rc27409d  
    2020from Anomos.Protocol.AnomosNeighborProtocol import AnomosNeighborProtocol 
    2121from Anomos.Protocol import NAT_CHECK_ID 
    22 from Anomos import LOG as log 
     22from Anomos import LOG as log, fnid 
    2323 
    2424from Anomos.Protocol import toint 
     
    192192 
    193193    def uniq_id(self): 
    194         return "%02x:*" % (ord(self.id)) 
     194        return "%s:*" % fnid(self.id) 
  • Anomos/NeighborManager.py

    r8c85782 rc27409d  
    2424from Anomos.Protocol import NAT_CHECK_ID 
    2525from Anomos.Measure import Measure 
    26 from Anomos import BTFailure, LOG as log 
     26from Anomos import BTFailure, LOG as log, fnid 
    2727 
    2828class NeighborManager(object): 
     
    5757            if self.nid_collision(id, loc): 
    5858                # 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)) 
    6060                # To be safe, kill connection with the neighbor we already 
    6161                # had with the requested ID and add ID to the failed list 
     
    9898            for k,v in self.incomplete.iteritems(): 
    9999                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) 
    103103 
    104104    def failed_connections(self): 
     
    114114        self.neighbors[id] = NeighborLink(self, socket, id, \ 
    115115                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])) 
    118118 
    119119    def rm_neighbor(self, nid): 
     
    124124        if nid is not None: 
    125125            self.failedPeers.append(nid) 
    126             log.info("Removed Neighbor: \\x%02x" % ord(nid)) 
     126            log.info("Removed Neighbor: %s" % fnid(nid)) 
    127127            if self.waiting_tcs.has_key(nid): 
    128128                del self.waiting_tcs[nid] 
     
    207207            log.error("Not starting circuit -- Unknown torrent") 
    208208        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)) 
    210210        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)) 
    212212            self.schedule_tc(nid, infohash, aeskey, nextTC) 
    213213        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)) 
    215215        else: 
    216216            self.neighbors[nid].start_endpoint_stream(torrent, aeskey, data=nextTC) 
  • Anomos/NetworkModel.py

    r78ce04c rc27409d  
    1717 
    1818import random 
     19import struct 
    1920from sys import maxint as INFINITY 
    2021import Anomos.Crypto 
     
    133134        return self.sessionid 
    134135 
    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  
    143136    def get_nid(self, peerid, default=None): 
    144137        """ Return the relative ID associated with peerid 
     
    299292        p1 = self.get(v1) 
    300293        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) 
    306301            p1.add_neighbor(v2, nid, p2.ip, p2.port) 
    307302            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 
    311305 
    312306    def rand_connect(self, peerid, numpeers): 
     
    338332            if not allow_close and opid_ip in used_ips: 
    339333                continue 
    340             self.connect(peerid, opid) 
     334            if not self.connect(peerid, opid): 
     335                continue 
    341336            used_ips.append(opid_ip) 
    342337            numpeers -= 1 
  • Anomos/Protocol/__init__.py

    r8c0ee5d rc27409d  
    1616 
    1717NAME = "Anomos10" 
     18HDR_NID_LEN = 2 
     19HDR_EXT_LEN = 6 
    1820 
    19 NAT_CHECK_ID = chr(255) 
     21NAT_CHECK_ID = chr(255)+chr(255) 
    2022 
    2123#--Message Control Characters--# 
  • Anomos/Relayer.py

    r53cdbdb rc27409d  
    1818from Anomos.Protocol.AnomosRelayerProtocol import AnomosRelayerProtocol 
    1919from Anomos.Measure import Measure 
    20 from Anomos import LOG as log 
     20from Anomos import LOG as log, fnid 
    2121 
    2222class Relayer(AnomosRelayerProtocol): 
     
    147147 
    148148    def uniq_id(self): 
    149         return "[%02x:%04x]" % (ord(self.neighbor.id), self.stream_id) 
     149        return fnid(self.neighbor.id, self.stream_id) 
  • Anomos/TCReader.py

    r390eee0 rc27409d  
    3434    TypeRange = (0,1) 
    3535    SIDRange = (1,9) 
    36     NIDRange = (9,10) 
     36    NIDRange = (9,11) 
    3737    IHashRange = (9,29) 
    3838    KeyRange = (29,93) 
  • Anomos/TwistedNatCheck.py

    rb7b199c rc27409d  
    55import M2Crypto.SSL as SSL 
    66  
    7 from Anomos.Protocol import NAT_CHECK_ID, NAME as PROTOCOL_NAME 
     7from Anomos.Protocol import NAT_CHECK_ID, HDR_EXT_LEN, HDR_NID_LEN, NAME as PROTOCOL_NAME 
    88from Anomos.P2PConnection import PostConnectionChecker 
    99from Anomos import LOG as log 
     
    3333    state = "init" 
    3434    nid = NAT_CHECK_ID 
    35     protocol_extensions = '\0'*7 
     35    protocol_extensions = '\0'*6 
    3636    expecting = 1 
    3737    msgbuf = "" 
     
    9090            self.check_failed("Protocol name mismatch") 
    9191            return "done" 
    92         self.expecting = 1 
     92        self.expecting = HDR_NID_LEN 
    9393        return "nid" 
    9494 
     
    9797            self.check_failed("Neighbor ID mismatch") 
    9898            return "done" 
    99         self.expecting = 7 
     99        self.expecting = HDR_EXT_LEN 
    100100        return "flags" 
    101101 
    102102    def proto_flags(self, msg): 
    103         if len(msg) != 7: 
     103        if len(msg) != HDR_EXT_LEN: 
    104104            self.check_failed("Invalid header") 
    105105        elif len(self.msgbuf) > 0: 
  • Anomos/__init__.py

    rb75351b rc27409d  
    143143        chr((i >> 8) & 0xFF) + chr(i & 0xFF)) 
    144144 
     145def fnid(nid, stream=None): 
     146    if stream: 
     147        return "[%s:%04x]" % (b2a_hex(nid), stream) 
     148    return "[%s]" % b2a_hex(nid) 
    145149 
    146150import socket 
  • Anomos/btformats.py

    r515582f rc27409d  
    108108        if p.has_key('nid'): 
    109109            nid = p.get('nid') 
    110             if type(nid) != str or len(nid) != 1: 
     110            if type(nid) != str or len(nid) != 2: 
    111111                raise BTFailure('invalid entry in peer list') 
    112112        #PeerID only used in BitTorrent