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.

204 lignes
3.9 KiB

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include "btpd.h"
  7. unsigned long
  8. peer_get_rate(unsigned long *rates)
  9. {
  10. unsigned long ret = 0;
  11. for (int i = 0; i < RATEHISTORY; i++)
  12. ret += rates[i];
  13. return ret;
  14. }
  15. void
  16. peer_kill(struct peer *p)
  17. {
  18. struct iob_link *iol;
  19. struct piece_req *req;
  20. btpd_log(BTPD_L_CONN, "killed peer.\n");
  21. if (p->flags & PF_ATTACHED)
  22. cm_on_lost_peer(p);
  23. else
  24. BTPDQ_REMOVE(&btpd.unattached, p, cm_entry);
  25. if (p->flags & PF_ON_READQ)
  26. BTPDQ_REMOVE(&btpd.readq, p, rq_entry);
  27. if (p->flags & PF_ON_WRITEQ)
  28. BTPDQ_REMOVE(&btpd.writeq, p, wq_entry);
  29. close(p->sd);
  30. event_del(&p->in_ev);
  31. event_del(&p->out_ev);
  32. iol = BTPDQ_FIRST(&p->outq);
  33. while (iol != NULL) {
  34. struct iob_link *next = BTPDQ_NEXT(iol, entry);
  35. iol->kill_buf(&iol->iob);
  36. free(iol);
  37. iol = next;
  38. }
  39. req = BTPDQ_FIRST(&p->p_reqs);
  40. while (req != NULL) {
  41. struct piece_req *next = BTPDQ_NEXT(req, entry);
  42. free(req);
  43. req = next;
  44. }
  45. req = BTPDQ_FIRST(&p->my_reqs);
  46. while (req != NULL) {
  47. struct piece_req *next = BTPDQ_NEXT(req, entry);
  48. free(req);
  49. req = next;
  50. }
  51. p->reader->kill(p->reader);
  52. if (p->piece_field != NULL)
  53. free(p->piece_field);
  54. free(p);
  55. btpd.npeers--;
  56. }
  57. void
  58. peer_request(struct peer *p, uint32_t index, uint32_t begin, uint32_t len)
  59. {
  60. struct piece_req *req = btpd_calloc(1, sizeof(*req));
  61. req->index = index;
  62. req->begin = begin;
  63. req->length = len;
  64. BTPDQ_INSERT_TAIL(&p->my_reqs, req, entry);
  65. net_send_request(p, req);
  66. }
  67. void
  68. peer_cancel(struct peer *p, uint32_t index, uint32_t begin, uint32_t len)
  69. {
  70. struct piece_req *req;
  71. again:
  72. req = BTPDQ_FIRST(&p->my_reqs);
  73. while (req != NULL &&
  74. !(index == req->index &&
  75. begin == req->begin &&
  76. len == req->length))
  77. req = BTPDQ_NEXT(req, entry);
  78. if (req != NULL) {
  79. net_send_cancel(p, req);
  80. BTPDQ_REMOVE(&p->my_reqs, req, entry);
  81. free(req);
  82. goto again;
  83. }
  84. }
  85. void
  86. peer_have(struct peer *p, uint32_t index)
  87. {
  88. net_send_have(p, index);
  89. }
  90. void
  91. peer_unchoke(struct peer *p)
  92. {
  93. p->flags &= ~PF_I_CHOKE;
  94. net_send_unchoke(p);
  95. }
  96. void
  97. peer_choke(struct peer *p)
  98. {
  99. struct piece_req *req;
  100. while ((req = BTPDQ_FIRST(&p->p_reqs)) != NULL)
  101. net_unsend_piece(p, req);
  102. p->flags |= PF_I_CHOKE;
  103. net_send_choke(p);
  104. }
  105. void
  106. peer_want(struct peer *p, uint32_t index)
  107. {
  108. p->nwant++;
  109. if (p->nwant == 1) {
  110. p->flags |= PF_I_WANT;
  111. net_send_interest(p);
  112. }
  113. }
  114. void
  115. peer_unwant(struct peer *p, uint32_t index)
  116. {
  117. p->nwant--;
  118. if (p->nwant == 0) {
  119. p->flags &= ~PF_I_WANT;
  120. net_send_uninterest(p);
  121. }
  122. }
  123. static struct peer *
  124. peer_create_common(int sd)
  125. {
  126. struct peer *p = btpd_calloc(1, sizeof(*p));
  127. p->sd = sd;
  128. p->flags = PF_I_CHOKE | PF_P_CHOKE;
  129. BTPDQ_INIT(&p->p_reqs);
  130. BTPDQ_INIT(&p->my_reqs);
  131. BTPDQ_INIT(&p->outq);
  132. event_set(&p->out_ev, p->sd, EV_WRITE, net_write_cb, p);
  133. event_set(&p->in_ev, p->sd, EV_READ, net_read_cb, p);
  134. event_add(&p->in_ev, NULL);
  135. BTPDQ_INSERT_TAIL(&btpd.unattached, p, cm_entry);
  136. btpd.npeers++;
  137. return p;
  138. }
  139. void
  140. peer_create_in(int sd)
  141. {
  142. struct peer *p = peer_create_common(sd);
  143. net_handshake(p, 1);
  144. }
  145. void
  146. peer_create_out(struct torrent *tp,
  147. const uint8_t *id,
  148. const char *ip,
  149. int port)
  150. {
  151. int sd;
  152. struct peer *p;
  153. if (net_connect(ip, port, &sd) != 0)
  154. return;
  155. p = peer_create_common(sd);
  156. p->tp = tp;
  157. //bcopy(id, p->id, 20);
  158. net_handshake(p, 0);
  159. }
  160. void
  161. peer_create_out_compact(struct torrent *tp, const char *compact)
  162. {
  163. int sd;
  164. struct peer *p;
  165. struct sockaddr_in addr;
  166. addr.sin_family = AF_INET;
  167. addr.sin_addr.s_addr = *(long *)compact;
  168. addr.sin_port = *(short *)(compact + 4);
  169. if (net_connect2((struct sockaddr *)&addr, sizeof(addr), &sd) != 0)
  170. return;
  171. p = peer_create_common(sd);
  172. p->tp = tp;
  173. net_handshake(p, 0);
  174. }