A clone of btpd with my configuration changes.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

285 Zeilen
6.0 KiB

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