读BitTorrent码日记2

原创 2004年09月01日 15:33:00

# Written by Bram Cohen
# see LICENSE.txt for license information
# filename:btfield.py btformats.py

# btfield.py
#读码日记:2004-9-1 笔者:
#笔        者:zfive5(醉马不肖 之 [孤舟蓑笠翁, 独钓寒江雪])
#注        释:这个文件主要是进行字节到比特位之间的转化的

 

#功能:主要是实现的列表bits位到byte的转化
#参数:1个逻辑列表 例如:[1,1,0,1,1,1,0,1]
#返回:byte流, 例如:'a/x00'
#效果:
#>>> booleans_to_bitfield([0,1,1,0,0,0,0,1])
#'a'
#>>> booleans_to_bitfield([0,1,1,0,0,0,0,1,0])
#'a/x00'
#
def booleans_to_bitfield(booleans):
    r = []
    for i in xrange(0, len(booleans), 8):
        v = 0
        p = 0x80
        for j in booleans[i:i+8]:
            if j:
                v |= p
            p >>= 1
        r.append(chr(v))
    return ''.join(r)


#功能:主要完成位到byte流到bits列表之间的转化
#参数:byte流,byte个数 例如:'a/x00'
#返回:逻辑bits列表,例如:[False, True, True, False, False, False, False, True]
#效果:
#>>> bitfield_to_booleans('a',8)
#[False, True, True, False, False, False, False, True]
#
def bitfield_to_booleans(bitfield, l):
    extra = len(bitfield) * 8 - l
    if extra < 0 or extra >= 8:
        return None
    r = []
    for c in bitfield:
        v = ord(c)
        for i in xrange(8):
            if v & 0x80 != 0:
                r.append(True)
            else:
                r.append(False)
            v <<= 1
    if extra > 0:
        if r[-extra:] != [0] * extra:
            return None
        del r[-extra:]
    return r

#功能:测试1
def test_basic():
    x = [1, 1, 1, 0, 0, 0, 1, 1, 1]
    y = [1, 1, 1, 0, 0, 0, 1, 1]
    for a in [x, y, []]:
        assert bitfield_to_booleans(booleans_to_bitfield(a), len(a)) == a
       
#功能:测试2
def test_too_long():
    assert bitfield_to_booleans('ab', 8) == None

#功能:测试3
def test_too_short():
    assert bitfield_to_booleans('a', 9) == None
   
#功能:测试4   
def test_nonzero_in_excess():
    assert bitfield_to_booleans(chr(0xFF), 7) == None


# Written by Bram Cohen
# see LICENSE.txt for license information

# btformats.py

from types import StringType, LongType, IntType, ListType, DictType
from re import compile

reg = compile(r'^[^///.~][^///]*$')

ints = [LongType, IntType]

#功能:这里主要是检查种子文件info信息是否合法,其中包括它的子信息,
#如过不合法抛异常,从这个文件里我们可以看出种子文件的info的结构
#  info(DictType){
#          'piecelength':ints(大于零),
#          'pieces':StringType(长度为20个字节的整数倍),
#          'name':StringType(不能是/   *之类的非文件名称字符),
#          'length':ints(大于零,对单个文件) 
#       }
#
#  info(DictType){
#          'piecelength':ints(大于零),
#          'pieces':StringType(长度为20个字节的整数倍),
#          'name':StringType(不能是/   *之类的非文件名称字符),
#          'files'(ListType):[
#                     (DictType)
#                      {
#                     'length':ints(大于零)
#                     'path'(ListType):[ (例如: "1","2","4.txt" 代表 :"/1/2/4.txt"
#                              StringType,
#                              StringType,
#                              StringType,
#                              ....
#                            ]
#                      },
#
#                     (DictType)
                      {
#                     'length':ints(大于零)
#                     'path'(ListType):[
#                              StringType,
#                              StringType,
#                              StringType,
#                              ....
#                            ]
#                      }
#                      ....
#                   ]
#       }
#
#这里顺序可能是种子文件有出入
def check_info(info):
    if type(info) != DictType:
        raise ValueError, 'bad metainfo - not a dictionary'
    pieces = info.get('pieces')
    if type(pieces) != StringType or len(pieces) % 20 != 0:
        raise ValueError, 'bad metainfo - bad pieces key'
    piecelength = info.get('piece length')
    if type(piecelength) not in ints or piecelength <= 0:
        raise ValueError, 'bad metainfo - illegal piece length'
    name = info.get('name')
    if type(name) != StringType:
        raise ValueError, 'bad metainfo - bad name'
    if not reg.match(name):
        raise ValueError, 'name %s disallowed for security reasons' % name
    if info.has_key('files') == info.has_key('length'):
        raise ValueError, 'single/multiple file mix'
    if info.has_key('length'):
        length = info.get('length')
        if type(length) not in ints or length < 0:
            raise ValueError, 'bad metainfo - bad length'
    else:
        files = info.get('files')
        if type(files) != ListType:
            raise ValueError
        for f in files:
            if type(f) != DictType:
                raise ValueError, 'bad metainfo - bad file value'
            length = f.get('length')
            if type(length) not in ints or length < 0:
                raise ValueError, 'bad metainfo - bad length'
            path = f.get('path')
            if type(path) != ListType or path == []:
                raise ValueError, 'bad metainfo - bad path'
            for p in path:
                if type(p) != StringType:
                    raise ValueError, 'bad metainfo - bad path dir'
                if not reg.match(p):
                    raise ValueError, 'path %s disallowed for security reasons' % p
        for i in xrange(len(files)):
            for j in xrange(i):
                if files[i]['path'] == files[j]['path']:
                    raise ValueError, 'bad metainfo - duplicate path'

#功能:这里主要是检查种子文件message信息是否合法,其中包括它的子信息,
#如过不合法抛异常,从这个文件里我们可以看出种子文件的message的结构
#这也是种子的起始地方
#  message(DictType){
#          'info'(DictType):{ check_info(message.get('info'))同上}
#          'announce':StringType
#         }
#这里顺序可能是种子文件有出入
def check_message(message):
    if type(message) != DictType:
        raise ValueError
    check_info(message.get('info'))
    if type(message.get('announce')) != StringType:
        raise ValueError

#功能:这里主要是message信息是否合法,其中包括它的子信息,关于这个结构体,我
#还没有读到能理解的地步,先在这里打一个问号?????
#  message(DictType){
#          'failure reason':{ check_info(message.get('info'))同上}
#          'peers'(ListType):
#                         [
#                            (DictType)
#                            {
#                            'ip':StringType,
#                            'port':ints(大于零),
#                             'peer id':StringType(len(id)==20这里我猜也是sha)
#                            },
#                           
#                            (DictType)
#                            {
#                            'ip':StringType,
#                            'port':ints(大于零),
#                            'peer id':StringType(len(id)==20这里我猜也是sha)
#                            }
#                            ...                           
#
#                         ]
#           ‘interval’:ints(大于零),
#           'min interval':ints(大于零),
#           'tracker id':StringType,
#           'num peers':ints(大于零),
#           'done peers':ints(大于零),
#           'last':ints(大于零)
#         }
#这里顺序可能是实际顺序不出入
def check_peers(message):
    if type(message) != DictType:
        raise ValueError
    if message.has_key('failure reason'):
        if type(message['failure reason']) != StringType:
            raise ValueError
        return
    peers = message.get('peers')
    if type(peers) != ListType:
        raise ValueError
    for p in peers:
        if type(p) != DictType:
            raise ValueError
        if type(p.get('ip')) != StringType:
            raise ValueError
        port = p.get('port')
        if type(port) not in ints or p <= 0:
            raise ValueError
        id = p.get('peer id')
        if type(id) != StringType or len(id) != 20:
            raise ValueError
    interval = message.get('interval', 1)
    if type(interval) not in ints or interval <= 0:
        raise ValueError
    minint = message.get('min interval', 1)
    if type(minint) not in ints or minint <= 0:
        raise ValueError
    if type(message.get('tracker id', '')) != StringType:
        raise ValueError
    npeers = message.get('num peers', 0)
    if type(npeers) not in ints or npeers < 0:
        raise ValueError
    dpeers = message.get('done peers', 0)
    if type(dpeers) not in ints or dpeers < 0:
        raise ValueError
    last = message.get('last', 0)
    if type(last) not in ints or last < 0:
        raise ValueError

#写到这里大家明白bt种子文件的结构了吧!!
#(待续)

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zfive5/article/details/91279

Windows 2000 的安全隐患

Windows 2000 的安全隐患加入时间:01-1-4 下午 08:48:24上一篇  文章分类  下一篇  原文作者avoidnf8@hotmail.com greyzone@compsecur...
  • silversoft
  • silversoft
  • 2001-01-06 00:29:00
  • 484

读BitTorrent码日记4

# Written by Bram Cohen # see LICENSE.txt for license information #文件名称:download.py #读码日记:2004-9-...
  • zfive5
  • zfive5
  • 2004-09-06 09:13:00
  • 1491

读BitTorrent码日记6

# Written by Bram Cohen # see LICENSE.txt for license information #文件名称:Storage.py #读码日记:2004-9-9...
  • zfive5
  • zfive5
  • 2004-09-10 09:49:00
  • 1271

读BitTorrent码日记1

# Written by Petru Paler # see LICENSE.txt for license information """ 读码日记: 2004-8-20 笔者: 笔    ...
  • zfive5
  • zfive5
  • 2004-09-01 09:52:00
  • 1571

读BitTorrent码日记3

#!/usr/bin/env python # Written by Bram Cohen and Myers Carpenter # see LICENSE.txt for license in...
  • zfive5
  • zfive5
  • 2004-09-02 15:59:00
  • 3193

读BitTorrent码日记5

# Written by Bram Cohen # see LICENSE.txt for license information #文件名称:RewServer.py #读码日记:2004...
  • zfive5
  • zfive5
  • 2004-09-07 14:05:00
  • 1245

2018年2月1日训练日记

昨天晚上的cf。。。打的非常惨烈。。。只出了3题。。第三题还被hack了一次。。。好歹最后过了。。。D题没想到dp。。。TLE on test 15。。。赛后看了一眼Q神代码。。。把自己的代码改改一下...
  • LSD20164388
  • LSD20164388
  • 2018-02-01 17:43:09
  • 388

2018年2月8日训练日记

明明思路对了,却是TLE。。。(经过改正变成了RE)无奈之下,又去翻了饶齐大佬的博客,具体内容如下:HDU 1853 Cyclic Tour(最小费用流)题意:给你一个N个点M条边的带权有向图,现在要...
  • LSD20164388
  • LSD20164388
  • 2018-02-08 21:12:44
  • 311

2018年2月2日训练日记

今晚两场比赛,希望不要再掉分了啊... 图论-网络流:目前看了80来道题,也感觉懂了不少东西,然而有的题目还是不会建模。。。还是看的少了吧。 好题:UVA 11082 Matrix Decompr...
  • LSD20164388
  • LSD20164388
  • 2018-02-03 09:27:28
  • 151

BitTorrent通信流程与网络包结构

本文转载自: http://blog.csdn.net/wengpingbo/article/details/9174363 BitTorrent协议支持基于TCP或UTP网络协议进行数据传输,...
  • WangZekun_wolf
  • WangZekun_wolf
  • 2017-11-24 18:40:15
  • 179
收藏助手
不良信息举报
您举报文章:读BitTorrent码日记2
举报原因:
原因补充:

(最多只允许输入30个字)