A clone of btpd with my configuration changes.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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