A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

  1. #include "btpd.h"
  2. #include <evdns.h>
  3. struct udp_tr_req {
  4. enum { UDP_RESOLVE, UDP_CONN_SEND, UDP_CONN_RECV,
  5. UDP_ANN_SEND, UDP_ANN_RECV } state;
  6. int cancel;
  7. char *host;
  8. uint16_t port;
  9. uint32_t ip;
  10. struct event timer;
  11. BTPDQ_ENTRY(udp_tr_req) entry;
  12. };
  13. BTPDQ_HEAD(udp_req_tq, udp_tr_req);
  14. static int m_sd;
  15. static struct event m_recv;
  16. static struct event m_send;
  17. static struct udp_req_tq m_reqq = BTPDQ_HEAD_INITIALIZER(m_reqq);
  18. static void
  19. req_kill(struct udp_tr_req *req)
  20. {
  21. free(req);
  22. }
  23. struct udp_tr_req *
  24. udp_tr_req(struct torrent *tp, enum tr_event event, const char *aurl)
  25. {
  26. return NULL;
  27. }
  28. void
  29. udp_tr_cancel(struct udp_tr_req *req)
  30. {
  31. }
  32. void
  33. udp_tr_init(void)
  34. {
  35. struct sockaddr_in addr;
  36. addr.sin_family = AF_INET;
  37. addr.sin_addr.s_addr = INADDR_ANY;
  38. addr.sin_port = 0;
  39. if ((m_sd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  40. btpd_err("socket: %s\n", strerror(errno));
  41. if (bind(m_sd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  42. btpd_err("bind: %s\n", strerror(errno));
  43. event_set(&m_recv, m_sd, EV_READ, sock_cb, NULL);
  44. event_set(&m_send, m_sd, EV_WRITE, sock_cb, NULL);
  45. }
  46. static void
  47. sock_cb(int sd, short type, void *arg)
  48. {
  49. }
  50. static void
  51. send_conn(struct udp_tr_req *req)
  52. {
  53. req->state = UDP_CONN_SEND;
  54. }
  55. static void
  56. udp_dnscb(int result, char type, int count, int ttl, void *addrs, void *arg)
  57. {
  58. struct udp_tr_req *req = arg;
  59. if (req->cancel)
  60. req_kill(req);
  61. else if (result == DNS_ERR_NONE && type == DNS_IPv4_A && count > 0) {
  62. int addri = rand_between(0, count - 1);
  63. bcopy(addrs + addri * 4, &req->ip, 4);
  64. send_conn(req);
  65. } else {
  66. btpd_log(BTPD_L_ERROR, "failed to lookup '%s'.\n", req->host);
  67. tr_result(req->tp, TR_RES_FAIL, -1);
  68. req_kill(req);
  69. }
  70. }