BT源码阅读兼移植三:种子文件的生成(1)

BT源码阅读兼移植二:种子文件编码(2) 接昨天: bencoding的编码实现位于 Bittorrent/bencode.py内。另外此文件中还提供了解码功能,以后再管他。先看一下编码的实现:from types import StringType, IntType, LongType, DictType, ListType, TupleType class Bencached(object): 阅读详情

知道了bencoding编码,我们来看一下种子文件是如何生成的。

第一篇文章中提到Bittorrent/makematafle.py中的函数实现了生成种子功能。

生成种子的总的思路就是将文件切片后用sha1取得摘要,摘要和目录结构、TrackerUrl等信息bencoding后生成一个文件。

 

下面说明细节:

 

先看一下sha1是啥:

sha1是security hash algrithom 的缩写,其实跟MD5类似,也是一种单向哈希映射。我们一般把它用作数字签名(因为雪崩效应)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。 在BT种子中,把文件按照2^n位进行切分,每段用sha1生成一个160位的摘要信息。这段摘要信息能够帮助确定下载的片段是否正确。

 

再看一下官方协议:

http://wiki.theory.org/BitTorrentSpecification 的MetaInfo File Structure部分。

 -----------------------------------------------------------------转帖协议----------------------------------------------------------------------

All data in a metainfo file is bencoded. The specification for bencoding is defined above.

The content of a metainfo file (the file ending in ".torrent") is a bencoded dictionary, containing the keys listed below. All character string values are UTF-8 encoded.

  • info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details)
  • announce: The announce URL of the tracker (string)
  • announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings).
    • The official request for a specification change is here.
  • creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
  • comment: (optional) free-form textual comments of the author (string)
  • created by: (optional) name and version of the program used to create the .torrent (string)
  • encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string)

Info Dictionary

This section contains the field which are common to both mode, "single file" and "multiple file".

  • piece length: number of bytes in each piece (integer)
  • pieces: string consisting of the concatenation of all 20-byte SHA1 hash values, one per piece (byte string, i.e. not urlencoded)
  • private: (optional) this field is an integer. If it is set to "1", the client MUST publish its presence to get other peers ONLY via the trackers explicitly described in the metainfo file. If this field is set to "0" or is not present, the client may obtain peer from other means, e.g. PEX peer exchange, dht. Here, "private" may be read as "no external peer source".
    • NOTE: There is much debate surrounding private trackers.
    • The official request for a specification change is here.
    • Azureus was the first client to respect private trackers, see their wiki for more details.

Info in Single File Mode

For the case of the single-file mode, the info dictionary contains the following structure:

  • name: the filename. This is purely advisory. (string)
  • length: length of the file in bytes (integer)
  • md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.

Info in Multiple File Mode

For the case of the multi-file mode, the info dictionary contains the following structure:

  • name: the filename of the directory in which to store all the files. This is purely advisory. (string)
  • files: a list of dictionaries, one for each file. Each dictionary in this list contains the following keys:
    • length: length of the file in bytes (integer)
    • md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.
    • path: a list containing one or more string elements that together represent the path and filename. Each element in the list corresponds to either a directory name or (in the case of the final element) the filename. For example, a the file "dir1/dir2/file.ext" would consist of three string elements: "dir1", "dir2", and "file.ext". This is encoded as a bencoded list of strings such as l4:dir14:dir28:file.exte

Notes

  • The piece length specifies the nominal piece size, and is usually a power of 2. The piece size is typically chosen based on the total amount of file data in the torrent, and is constrained by the fact that too-large piece sizes cause inefficiency, and too-small piece sizes cause large .torrent metadata file. Historically, piece size was chosen to result in a .torrent file no greater than approx. 50 - 75 kB (presumably to ease the load on the server hosting the torrent files).
    • Current best-practice is to keep the piece size to 512KB or less, for torrents around 8-10GB, even if that results in a larger .torrent file. This results in a more efficient swarm for sharing files. The most common sizes are 256 kB, 512 kB, and 1 MB.
    • Every piece is of equal length except for the final piece, which is irregular. The number of pieces is thus determined by 'ceil( total length / piece size )'.
    • For the purposes of piece boundaries in the multi-file case, consider the file data as one long continuous stream, composed of the concatenation of each file in the order listed in the files list. The number of pieces and their boundaries are then determined in the same manner as the case of a single file. Pieces may overlap file boundaries.
  • Each piece has a corresponding SHA1 hash of the data contained within that piece. These hashes are concatenated to form the pieces value in the above info dictionary. Note that this is not a list but rather a single string. The length of the string must be a multiple of 20.

--------------------------------------------------转帖完毕-----------------------------------------------------------------------------

协议明确的规定出了文件的结构,可以直接看一下python的代码了。

makematafle.py 的主函数 makematafile 实现了单个文件的torrent文件的制作,makemetafiles实现了多个文件的torrent文件制作。 makeinfo完成了infodictionary的制作。

 

先看一下makeinfo函数

 

 

蓝牙协议栈内存池 bt_memp 深度解析 蓝牙协议栈内存池 bt_memp 深度解析:lwIP 思想在嵌入式 BT 栈中的工程化实践 阅读详情

相关推荐

【BlueZ】源码目录结构全解析:一分钟看懂各文件夹核心作用

BlueZ 作为 Linux 平台唯一官方蓝牙协议栈,采用模块化分层架构设计。绝大多数开发者编译裁剪失败、功能代码找不到、问题无从定位,本质是不熟悉源码目录职责边界。本文承接系列前文内核决定兼容上限、依赖库决定编译下限核心理论,基于 BlueZ 5.87 原版源码,系统性拆解全版本通用目录架构,打通目录结构→编译参数→功能模块→问题定位完整链路,是 BlueZ 源码阅读、二次开发、移植适配的官方级手册。

byte轻骑兵的技术小窝 1万+

基于 FUSE 的 Bittorrent 文件系统

Bittorrent 已经存在了很长时间,它可以从互联网上共享和下载数据。市场上有大量的 GUI 和 CLI 的 Bittorrent 客户端。有时,你不能坐下来等待你的下载完成。你可能想要立即观看内容。这就是 BTFS 这个不起眼的文件系统派上用场的地方。使用 BTFS,你可以将种子文件或磁力链接挂载为目录,然后在文件树中作为只读目录。这些文件的内容将...

weixin_34255055的博客 364

用Proteus8.9自带STM32F401VE的Controller仿真STM32F407ZGT6,F429IGT6

@用Proteus8.9自带STM32F401VE的Controller仿真STM32F407ZGT6,F429IGT6 一, 目前得到的 Proteus8.9版本软件能够支持的Cortex-M4固件库项目,只能到达STM32F401VE (如下图1所示)。 (图1) 作为ST公司Cortex-M4更为广泛应用的F407,F429系列芯片;现在的Proteus8.9版本软件还没有固件库支持,不能...

kaillen的博客 3万+

bt种子磁力播放器 android,(安卓)种子磁力在线播放器 — 可离线至本地

本文最后更新于2018-11-16 23:22:14,某些文章具有时效性,若有错误或已失效,请在下方留言或联系老夜。日常下载资源所接触到的磁力链接、BT、torrent等还有以magnet:?xt=urn:btih:开头的特殊字串,一些小白总是傻傻分不清楚,这里老夜就简单的科普一下下,他们的主要作用就是识别资源进行下载,只不过有几种形式的分支而已,并且互相之间可以相互转化,最终目的就是通过P2P技...

weixin_32596769的博客 1万+

Python 静态导入与动态导入

python中模块的静态导入 与 动态导入(运行时指定模块),以及具体对应示例。

慢也好,步子小也好,往前走就好~ 717

手眼标定搞定了,手眼矩阵不知道怎么用?快戳!!

手眼标定得到手眼矩阵到底该咋用? 大家好,我是小鱼。上几期给大家分享了关于手眼标定的方法,如果还有不清楚如何让进行手眼标定的同学可以参考下面的文章: 机器人手眼标定都能搞不定?快戳,最好用的手眼标定库!!! 很多同学做完了手眼标定之后得到了手眼矩阵,但是不知道怎么用于程序当中去,还有很多同学搞不清楚各个坐标系之间的关系,今天小智就给大家讲讲手眼矩阵的使用问题。 一、手眼矩阵为什么会长的不一样? 做完手眼标定,得到的手眼矩阵,有可能会长的不一样,这是因为旋转变换是有很多种表示方法的。 1.旋转的表

生活不止眼前的苟且 5342

推荐10个常用的国外BT种子下载网站

这些都是国外较为知名的BT种子下载站点,其上的资源一般是很丰富的。各位务必搜藏起来,总有它们派上用场的时候。 1. Mininova特点:东西比较多资格比较老,地球人都知道资格老的总能排第一位。 2. The Pirate Bay特点:东西比较多而且有中文,找东西方便,可惜速度有点慢。 3. IsoHunt特点:更新比较快,速度也不错。 4. Torrentz特点:这个网址比较好记,随...

心语家园 - www.xinyucn.cc 34万+

高级电机控制场景实例:基于Simulink的永磁同步电机基于Luenberger观测器的弱磁控制仿真

本文基于Simulink实现表贴式永磁同步电机(SPMSM)的Luenberger观测器弱磁控制仿真,通过构建“观测-弱磁-控制”协同逻辑,突破传统矢量控制的基速限制。利用线性化模型设计观测器实时估计电流与磁链,结合电压极限约束动态注入负d轴电流,实现转速扩展至12000rpm(4倍基速),弱磁区电流THD低于3%,动态响应时间小于5ms,验证了高速运行下系统稳定性与鲁棒性。附完整建模步骤、MATLAB函数代码及性能分析,适用于电动汽车、机床等高速驱动场景,为高级电机控制提供可复现的技术范式。

amy_mhd的博客 31

偷偷收藏这5个在线资源网站,第二个BT搜索很强大!

偷偷收藏这5个在线资源网站,第二个BT搜索很强大! 好用的网站有那么多,今天给大家分享几个在线资源网站,不管是电影视频、还是图片资源以及办公资源,都应有尽有,肯定有你需要的哦,别告诉别人哦! 1、飘花电影网 飘花电影网分享最新、最好看的迅雷电影下载地址,用户体验度很高,是寻找免费高清电影资源的下载和在线观看网站。   2、BtSou BT搜索提供种子搜索、磁力链接搜索等热门资源的搜...

Tybyqi的博客 41万+

ENVI无法打开Landsat8的头文件问题和解决

再次将txt文件拖入ENVI中,就会得到7个波段组合后的真彩色图像,而且辐射定标所需的参数信息都已经定义好了。按住CTRL+F,查找GROUP = LEVEL1_PROCESSING_RECORD。第一行GROUP = LANDSAT_METADATA_FILE。会查到两个包含的句子,把这两个中间的部分全部删掉。双击打开该xxx_MTL.txt的头文件。删除空行后,应该为如下图的样子。把LANDSAT改为L1。

所有专家都曾始于新手 9697

复现论文《孤岛微电网环境下基于物理模型引导的深度强化学习VSG频率控制方法》

本文提出了一种结合物理模型与深度强化学习的孤岛微电网VSG频率控制方法。通过建立VSG小信号模型并设计可变系数控制器,将物理知识融入深度强化学习框架,解决了高维参数和机组耦合带来的控制难题。复现代码实现了三个核心组件:VSGSmallSignalModel建模VSG动态特性,VariableCoefficientController实现参数自适应调节,PhysicsGuidedDRL整合DQN算法与物理模型引导的奖励机制。该方法通过模型驱动与数据驱动的优势互补,提升了频率控制性能,其中物理模型既作为先验知识

huanghm88的专栏 127

BT Protocol 规范1.0

BitTorrent 协议规范1.0版 2007-11-15 11:25 摘自http://wiki.theory.org /BitTorrentSpecification,标题为Bittorrent Protocol Specification v1.0。它

Walter的专栏 1768

电阻抗成像肺功能测试数据分析与直方图生成

本文介绍了基于MATLAB的电阻抗成像(EIT)肺功能测试数据分析方法。研究针对32×32像素的EIT图像序列,通过三步算法处理:首先计算特定时间点与谷值图像的差值,然后分析1秒后峰值与1秒值的差值,最终计算比值矩阵并处理异常值。系统展示了数据预处理、关键时间点检测、比值计算及统计分析的全流程,包括生成概率分布直方图和空间分布图等可视化方法。该分析为无创肺功能评估提供量化指标,可应用于临床呼吸监测和疾病诊断。

huanghm88的专栏 197

BT源码阅读兼移植一:种子文件编码

几点声明: 1.由于工作原因,好久不能编写代码了,因此这份工作纯属业余爱好,因此文章水平必定很凹。2.写这些博文完全是为了记录自己的心得,好脑子不如烂笔头子。3.如果您不幸进来,觉得写得太烂,sorry,浪费您时间了,就请不要回复了;如果想一起讨论或者给与指教,小弟非常感谢! 主要目标:一年内读完BT的PYTHON源码(4.4版本),并且转移到C#上去。其它目标:深入学习一

DarkTower的专栏 1618

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

bt解析 开源 java_开源一个BT客户端:BitWave

BitWave的Host:源码放在github上,采用NEW BSD LICENSE发布。地址:https://github.com/airtrack/bitwave写BitWave起因去年买新笔记本之前,老的笔记本装的是Windows 7的RC版本,而RC版本在去年3月就停止支持,每2个小时重启一次。因为打算买新笔记本,所以也就懒得装XP了。在买新笔记本之前的两周内就一直在用Linux,虽然以前...

weixin_30068505的博客 508

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

CTorrent程序源码分析

CTorrent程序源码分析2007-01-31 20:26 我在大四上学期时曾做过BT客户端的移植工作。当时程序有一些bug,需要查看源代码才能解决。然后便进入了先是痛苦万分然后茅塞顿开最后十分享受的代码阅读。 互联网上讨论BT的的不少,但真正涉及底

zhhxidian2005的专栏 2485

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

UltraScale系列FPGA实现SDI视频编解码+UDP以太网传输,基于UltraScale GTH高速接口,提供2套工程源码和技术支持

UltraScale系列FPGA实现SDI视频编解码+UDP以太网传输,基于UltraScale GTH高速接口,提供2套工程源码和技术支持

qq_41667729的博客 2413

FPGA实现SDI视频缩放转GTY光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供工程源码和技术支持

FPGA实现SDI视频缩放转GTY光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供工程源码和技术支持

qq_41667729的博客 1566

Artix7系列FPGA实现SDI视频编解码+图像缩放,基于GTP高速收发器,提供4套工程源码和技术支持

Artix7系列FPGA实现SDI视频编解码+图像缩放,基于GTP高速收发器,提供4套工程源码和技术支持

qq_41667729的博客 1227

UltraScale系列FPGA实现SDI视频编解码+图像缩放,基于UltraScale GTH高速接口,提供4套工程源码和技术支持

UltraScale系列FPGA实现SDI视频编解码+图像缩放,基于UltraScale GTH高速接口,提供4套工程源码和技术支持

qq_41667729的博客 2249

国产紫光同创FPGA实现SDI视频编解码+图像缩放,基于HSSTHP高速接口,提供2套工程源码和技术支持

国产紫光同创FPGA实现SDI视频编解码+图像缩放,基于HSSTHP高速接口,提供2套工程源码和技术支持

qq_41667729的博客 1183

UltraScale系列FPGA实现SDI视频编解码+图像缩放+多路视频拼接,基于UltraScale GTH高速接口,提供8套工程源码和技术支持

UltraScale系列FPGA实现SDI视频编解码+图像缩放+多路视频拼接,基于UltraScale GTH高速接口,提供8套工程源码和技术支持

qq_41667729的博客 2568

FPGA实现SDI视频缩放转UltraScale GTH光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供2套工程源码和技术支持

FPGA实现SDI视频缩放转UltraScale GTH光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供2套工程源码和技术支持

qq_41667729的博客 1706

FPGA实现SDI视频缩放转GTX光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供2套工程源码和技术支持

FPGA实现SDI视频缩放转GTX光口传输,基于GS2971+Aurora 8b/10b编解码架构,提供2套工程源码和技术支持

qq_41667729的博客 1343

1、Python 1

Python 是由 Guido van Rossum (吉多·范罗苏姆)在八十年代末和九十年代初,在荷兰国家数学和计算机科学研究所设计出来的。Python 本身也是由诸多其他语言发展而来的,这包括 ABC、lisp、perl、C、C++、Unix shell 和其他的脚本语言等等。像 Perl 语言一样,Python 源代码同样遵循 GPL(GNU General Public License)协议。

2301_77428746的博客 959
上一篇: BT源码阅读兼移植二:种子文件编码(2)
下一篇: 俨然已经废掉了(重新捡起Java)
DarkTower
博客等级 码龄18年 0粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值