context.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef __CONTEXT__H
  2. #define __CONTEXT__H
  3. #include <openssl/sha.h>
  4. #include <sys/types.h>
  5. #if WIN32
  6. #include "poll.h"
  7. #else
  8. #include <sys/poll.h>
  9. #include <netinet/in.h>
  10. #endif
  11. #include "types.h"
  12. #include "segmenter.h"
  13. #include "peer.h"
  14. #include "bitset.h"
  15. #define IDSIZE 20
  16. #define KEYSIZE 4
  17. #if WIN32
  18. #define MAXCONN 60
  19. #else
  20. #define MAXCONN 100
  21. #endif
  22. #define SOCKID_MAX 1024
  23. #define TMPLOC SOCKID_MAX
  24. #define TRACKER_MAX 20
  25. #define CTX_STATUS_REVENTS(ctx,ev) ((ctx)->status[ev].revents)
  26. /* TBD */
  27. typedef struct btDownload {
  28. btObject *md; /* tracker metadata */
  29. char *url[TRACKER_MAX]; /* announce url */
  30. int urlrank[TRACKER_MAX]; /* url priority */
  31. int tracker; /* which tracker to check */
  32. int nurl; /* number of announce urls */
  33. char infohash[SHA_DIGEST_LENGTH];
  34. kBitSet requested; /* requested blocks */
  35. kBitSet interested; /* interested blocks */
  36. btFileSet fileset; /* set of files to be written */
  37. int reregister_interval; /* how often to reregister with tracker */
  38. time_t reregister_timer; /* Unix time of last tracker registration */
  39. btPeerset peerset; /* set of peers on this torrent */
  40. int privateflag; /* Set if torrent is tracked by private tracker, disables PEX */
  41. int complete; /* download completed */
  42. int connecting; /* UDP connecting flag */
  43. int txid; /* UDP transaction id */
  44. _int64 cxid; /* UDP connection id */
  45. } btDownload;
  46. typedef struct btContext {
  47. btDownload **downloads; /* Torrents this context keeps track of */
  48. int downloadcount;
  49. struct btPeer *sockpeer[FD_SETSIZE];
  50. char myid[IDSIZE];
  51. char mykey[KEYSIZE];
  52. int minport;
  53. int maxport;
  54. int listenport;
  55. /*struct in_addr ip; / * my ip address */
  56. int statmap[SOCKID_MAX + 1]; /* socket number to status number map */
  57. int nstatus; /* next available status */
  58. struct pollfd status[MAXCONN]; /* socket status */
  59. int x_set[SOCKID_MAX]; /* timeslicing bits */
  60. int xsock; /* number of bits that are set in x_set */
  61. int ss; /* server socket */
  62. int udpsock; /* UDP server socket */
  63. float ulfactor; /* upload multiplier */
  64. } btContext;
  65. btContext *btContext_create(btContext * ctx, float ulfactor, char *rcfile);
  66. /*
  67. * Initialize a context from an rcfile
  68. */
  69. void btContext_destroy(btContext * ctx);
  70. /*
  71. * Free all resources associated with context.
  72. */
  73. btObject *btannounce(btContext * ctx, btDownload * dl, char *state, int err);
  74. int btresponse(btContext * ctx, int download, btObject * resp);
  75. void ctx_setevents(struct btContext *ctx, int fd, int events);
  76. void ctx_clrevents(struct btContext *ctx, int fd, int events);
  77. /*
  78. * Add in or remove events from the set of event flags
  79. * to be polled.
  80. */
  81. void ctx_delstatus(struct btContext *ctx, int fd);
  82. int ctx_addstatus(struct btContext *ctx, int fd);
  83. /*
  84. * Allocate a pollfd status block for 'fd'. If 'fd' is set to TMPLOC
  85. * then the status block will be allocated to a temporary location.
  86. * Calling the _delstatus() with TMPLOC will free the temporary status
  87. * block, or caling _fixtmp() will link it to a real 'fd'.
  88. */
  89. void ctx_fixtmp(struct btContext *ctx, int fd);
  90. int ctx_register(struct btContext *ctx, unsigned download);
  91. /*
  92. * Contact the tracker get back the initial peer set.
  93. */
  94. int ctx_startserver(struct btContext *ctx);
  95. /*
  96. * Starts the server socket
  97. */
  98. int ctx_shutdown(btContext * ctx, unsigned download);
  99. /*
  100. * Tell the tracker that we are shutting down.
  101. */
  102. int ctx_complete(btContext * ctx, unsigned download);
  103. /*
  104. * Tell the tracker that we are done.
  105. */
  106. int ctx_reregister(btContext * ctx, unsigned download);
  107. /*
  108. * Update the tracker with our status.
  109. */
  110. void ctx_exit(int exitcode, void *arg);
  111. /*
  112. * on_exit() callable function to call shutdown
  113. */
  114. struct btStream;
  115. int ctx_loadfile(struct btStream *bts, struct btContext *ctx,
  116. char *downloaddir, int assumeok, char *ignorepattern);
  117. /*
  118. * Load a '.torrent' metadata file into the current context.
  119. * Returns the download ID, which is >=0, or <0 for error.
  120. */
  121. void ctx_closedownload(btContext * ctx, unsigned download);
  122. int ctx_readfastresume(btDownload * dl, kBitSet * partialData);
  123. int ctx_completedecode(unsigned char *digest, int len, const char *buf);
  124. int ctx_writefastresume(btDownload * dl);
  125. int ctx_deletefastresume(btDownload * dl);
  126. btFileSet ctx_checkhashfordata(btDownload * dl);
  127. /* takes a download and calls hashpartialdata, then updates the
  128. // requested/interested tables and prints status to stdout.
  129. */
  130. int ctx_hashpartialdata(btFileSet * templateFileSet, kBitSet * writeCache);
  131. /* Takes a pointer to a fileset to read from, and a temporary allocated bitset
  132. // and writes hash data to it via checkhashforpiece, returns number of good pieces
  133. */
  134. int ctx_checkhashforpiece(btFileSet * templateFileSet, kBitSet * writeCache,
  135. int i);
  136. /* Called from checkhashfordata, returns completion of individual piece
  137. */
  138. int ctx_writehashtodownload(btDownload * dl, kBitSet * partialData);
  139. /* Utilities */
  140. char *ctx_stringfrommd(btObject * md, char *mdkey);
  141. int ctx_hexdecode(unsigned char *digest, int len, const char *buf);
  142. #endif