A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

273 lignes
5.7 KiB

  1. #include <math.h>
  2. #include <string.h>
  3. #include "btpd.h"
  4. static struct net_buf *m_choke;
  5. static struct net_buf *m_unchoke;
  6. static struct net_buf *m_interest;
  7. static struct net_buf *m_uninterest;
  8. static struct net_buf *m_keepalive;
  9. static void
  10. kill_buf_no(char *buf, size_t len)
  11. {
  12. }
  13. static void
  14. kill_buf_free(char *buf, size_t len)
  15. {
  16. free(buf);
  17. }
  18. static void
  19. kill_buf_abort(char *buf, size_t len)
  20. {
  21. abort();
  22. }
  23. static struct net_buf *
  24. nb_create_alloc(short type, size_t len)
  25. {
  26. struct net_buf *nb = btpd_calloc(1, sizeof(*nb) + len);
  27. nb->type = type;
  28. nb->buf = (char *)(nb + 1);
  29. nb->len = len;
  30. nb->kill_buf = kill_buf_no;
  31. return nb;
  32. }
  33. static struct net_buf *
  34. nb_create_set(short type, char *buf, size_t len,
  35. void (*kill_buf)(char *, size_t))
  36. {
  37. struct net_buf *nb = btpd_calloc(1, sizeof(*nb));
  38. nb->type = type;
  39. nb->buf = buf;
  40. nb->len = len;
  41. nb->kill_buf = kill_buf;
  42. return nb;
  43. }
  44. static struct net_buf *
  45. nb_create_onesized(char mtype, int btype)
  46. {
  47. struct net_buf *out = nb_create_alloc(btype, 5);
  48. net_write32(out->buf, 1);
  49. out->buf[4] = mtype;
  50. return out;
  51. }
  52. static struct net_buf *
  53. nb_singleton(struct net_buf *nb)
  54. {
  55. nb_hold(nb);
  56. nb->kill_buf = kill_buf_abort;
  57. return nb;
  58. }
  59. struct net_buf *
  60. nb_create_keepalive(void)
  61. {
  62. if (m_keepalive == NULL) {
  63. m_keepalive = nb_create_alloc(NB_KEEPALIVE, 4);
  64. net_write32(m_keepalive->buf, 0);
  65. nb_singleton(m_keepalive);
  66. }
  67. return m_keepalive;
  68. }
  69. struct net_buf *
  70. nb_create_piece(uint32_t index, uint32_t begin, size_t blen)
  71. {
  72. struct net_buf *out;
  73. out = nb_create_alloc(NB_PIECE, 13);
  74. net_write32(out->buf, 9 + blen);
  75. out->buf[4] = MSG_PIECE;
  76. net_write32(out->buf + 5, index);
  77. net_write32(out->buf + 9, begin);
  78. return out;
  79. }
  80. struct net_buf *
  81. nb_create_torrentdata(char *block, size_t blen)
  82. {
  83. struct net_buf *out;
  84. out = nb_create_set(NB_TORRENTDATA, block, blen, kill_buf_free);
  85. return out;
  86. }
  87. struct net_buf *
  88. nb_create_request(uint32_t index, uint32_t begin, uint32_t length)
  89. {
  90. struct net_buf *out = nb_create_alloc(NB_REQUEST, 17);
  91. net_write32(out->buf, 13);
  92. out->buf[4] = MSG_REQUEST;
  93. net_write32(out->buf + 5, index);
  94. net_write32(out->buf + 9, begin);
  95. net_write32(out->buf + 13, length);
  96. return out;
  97. }
  98. struct net_buf *
  99. nb_create_cancel(uint32_t index, uint32_t begin, uint32_t length)
  100. {
  101. struct net_buf *out = nb_create_alloc(NB_CANCEL, 17);
  102. net_write32(out->buf, 13);
  103. out->buf[4] = MSG_CANCEL;
  104. net_write32(out->buf + 5, index);
  105. net_write32(out->buf + 9, begin);
  106. net_write32(out->buf + 13, length);
  107. return out;
  108. }
  109. struct net_buf *
  110. nb_create_have(uint32_t index)
  111. {
  112. struct net_buf *out = nb_create_alloc(NB_HAVE, 9);
  113. net_write32(out->buf, 5);
  114. out->buf[4] = MSG_HAVE;
  115. net_write32(out->buf + 5, index);
  116. return out;
  117. }
  118. struct net_buf *
  119. nb_create_multihave(struct torrent *tp)
  120. {
  121. uint32_t have_npieces = cm_pieces(tp);
  122. struct net_buf *out = nb_create_alloc(NB_MULTIHAVE, 9 * have_npieces);
  123. for (uint32_t i = 0, count = 0; count < have_npieces; i++) {
  124. if (cm_has_piece(tp, i)) {
  125. net_write32(out->buf + count * 9, 5);
  126. out->buf[count * 9 + 4] = MSG_HAVE;
  127. net_write32(out->buf + count * 9 + 5, i);
  128. count++;
  129. }
  130. }
  131. return out;
  132. }
  133. struct net_buf *
  134. nb_create_unchoke(void)
  135. {
  136. if (m_unchoke == NULL)
  137. m_unchoke = nb_singleton(nb_create_onesized(MSG_UNCHOKE, NB_UNCHOKE));
  138. return m_unchoke;
  139. }
  140. struct net_buf *
  141. nb_create_choke(void)
  142. {
  143. if (m_choke == NULL)
  144. m_choke = nb_singleton(nb_create_onesized(MSG_CHOKE, NB_CHOKE));
  145. return m_choke;
  146. }
  147. struct net_buf *
  148. nb_create_uninterest(void)
  149. {
  150. if (m_uninterest == NULL)
  151. m_uninterest =
  152. nb_singleton(nb_create_onesized(MSG_UNINTEREST, NB_UNINTEREST));
  153. return m_uninterest;
  154. }
  155. struct net_buf *
  156. nb_create_interest(void)
  157. {
  158. if (m_interest == NULL)
  159. m_interest =
  160. nb_singleton(nb_create_onesized(MSG_INTEREST, NB_INTEREST));
  161. return m_interest;
  162. }
  163. struct net_buf *
  164. nb_create_bitfield(struct torrent *tp)
  165. {
  166. uint32_t plen = ceil(tp->npieces / 8.0);
  167. struct net_buf *out = nb_create_alloc(NB_BITFIELD, 5);
  168. net_write32(out->buf, plen + 1);
  169. out->buf[4] = MSG_BITFIELD;
  170. return out;
  171. }
  172. struct net_buf *
  173. nb_create_bitdata(struct torrent *tp)
  174. {
  175. uint32_t plen = ceil(tp->npieces / 8.0);
  176. struct net_buf *out =
  177. nb_create_set(NB_BITDATA, cm_get_piece_field(tp), plen, kill_buf_no);
  178. return out;
  179. }
  180. struct net_buf *
  181. nb_create_shake(struct torrent *tp)
  182. {
  183. struct net_buf *out = nb_create_alloc(NB_SHAKE, 68);
  184. bcopy("\x13""BitTorrent protocol\0\0\0\0\0\0\0\0", out->buf, 28);
  185. bcopy(tp->tl->hash, out->buf + 28, 20);
  186. bcopy(btpd_get_peer_id(), out->buf + 48, 20);
  187. return out;
  188. }
  189. uint32_t
  190. nb_get_index(struct net_buf *nb)
  191. {
  192. switch (nb->type) {
  193. case NB_CANCEL:
  194. case NB_HAVE:
  195. case NB_PIECE:
  196. case NB_REQUEST:
  197. return net_read32(nb->buf + 5);
  198. default:
  199. abort();
  200. }
  201. }
  202. uint32_t
  203. nb_get_begin(struct net_buf *nb)
  204. {
  205. switch (nb->type) {
  206. case NB_CANCEL:
  207. case NB_PIECE:
  208. case NB_REQUEST:
  209. return net_read32(nb->buf + 9);
  210. default:
  211. abort();
  212. }
  213. }
  214. uint32_t
  215. nb_get_length(struct net_buf *nb)
  216. {
  217. switch (nb->type) {
  218. case NB_CANCEL:
  219. case NB_REQUEST:
  220. return net_read32(nb->buf + 13);
  221. case NB_PIECE:
  222. return net_read32(nb->buf) - 9;
  223. default:
  224. abort();
  225. }
  226. }
  227. int
  228. nb_drop(struct net_buf *nb)
  229. {
  230. assert(nb->refs > 0);
  231. nb->refs--;
  232. if (nb->refs == 0) {
  233. nb->kill_buf(nb->buf, nb->len);
  234. free(nb);
  235. return 1;
  236. } else
  237. return 0;
  238. }
  239. void
  240. nb_hold(struct net_buf *nb)
  241. {
  242. nb->refs++;
  243. }