transfer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * transfer.c -- part of transfer.mod
  3. *
  4. */
  5. /*
  6. * Small code snippets related to REGET/RESEND support were taken from
  7. * BitchX, copyright by panasync.
  8. */
  9. #include "src/common.h"
  10. #include "src/cmds.h"
  11. #include "src/misc_file.h"
  12. #include "src/misc.h"
  13. #include "src/main.h"
  14. #include "src/userrec.h"
  15. #include "src/userent.h"
  16. #include "src/tandem.h"
  17. #include "src/net.h"
  18. #include "src/users.h"
  19. #define MAKING_TRANSFER
  20. #include "transfer.h"
  21. #include "src/mod/share.mod/share.h"
  22. #include "src/mod/update.mod/update.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <errno.h>
  28. extern int bupdating;
  29. #ifdef HUB
  30. static int copy_to_tmp = 1; /* Copy files to /tmp before transmitting? */
  31. #endif /* HUB */
  32. static int wait_dcc_xfer = 40; /* Timeout time on DCC xfers */
  33. static int dcc_limit = 4; /* Maximum number of simultaneous file
  34. downloads allowed */
  35. static unsigned int dcc_block = 0; /* Size of one dcc block */
  36. static int quiet_reject = 1; /* Quietly reject dcc chat or sends from
  37. users without access? */
  38. /*
  39. * Prototypes
  40. */
  41. struct dcc_table DCC_SEND;
  42. #ifdef HUB
  43. static void wipe_tmp_filename(char *, int);
  44. static int at_limit(char *);
  45. static void dcc_get_pending(int, char *, int);
  46. struct dcc_table DCC_GET;
  47. struct dcc_table DCC_GET_PENDING;
  48. #endif /* HUB */
  49. static fileq_t *fileq = NULL;
  50. /*
  51. * Misc functions
  52. */
  53. #ifdef HUB
  54. static void wipe_tmp_filename(char *fn, int idx)
  55. {
  56. int i, ok = 1;
  57. if (!copy_to_tmp)
  58. return;
  59. for (i = 0; i < dcc_total; i++)
  60. if (i != idx)
  61. if (dcc[i].type == &DCC_GET || dcc[i].type == &DCC_GET_PENDING)
  62. if (!strcmp(dcc[i].u.xfer->filename, fn)) {
  63. ok = 0;
  64. break;
  65. }
  66. if (ok)
  67. unlink(fn);
  68. }
  69. /* Return true if this user has >= the maximum number of file xfers allowed.
  70. */
  71. static int at_limit(char *nick)
  72. {
  73. int i, x = 0;
  74. for (i = 0; i < dcc_total; i++)
  75. if (dcc[i].type == &DCC_GET || dcc[i].type == &DCC_GET_PENDING)
  76. if (!egg_strcasecmp(dcc[i].nick, nick))
  77. x++;
  78. return (x >= dcc_limit);
  79. }
  80. /* Replaces all spaces with underscores (' ' -> '_'). The returned buffer
  81. * needs to be freed after use.
  82. */
  83. static char *replace_spaces(char *fn)
  84. {
  85. register char *ret = NULL, *p = NULL;
  86. p = ret = strdup(fn);
  87. while ((p = strchr(p, ' ')) != NULL)
  88. *p = '_';
  89. return ret;
  90. }
  91. static void deq_this(fileq_t *this)
  92. {
  93. fileq_t *q = fileq, *last = NULL;
  94. while (q && q != this) {
  95. last = q;
  96. q = q->next;
  97. }
  98. if (!q)
  99. return; /* Bogus ptr */
  100. if (last)
  101. last->next = q->next;
  102. else
  103. fileq = q->next;
  104. free(q->dir);
  105. free(q->file);
  106. free(q);
  107. }
  108. /* Remove all files queued to a certain user.
  109. */
  110. static void flush_fileq(char *to)
  111. {
  112. fileq_t *q = fileq;
  113. int fnd = 1;
  114. while (fnd) {
  115. q = fileq;
  116. fnd = 0;
  117. while (q != NULL) {
  118. if (!egg_strcasecmp(q->to, to)) {
  119. deq_this(q);
  120. q = NULL;
  121. fnd = 1;
  122. }
  123. if (q != NULL)
  124. q = q->next;
  125. }
  126. }
  127. }
  128. static void send_next_file(char *to)
  129. {
  130. fileq_t *q = NULL, *this = NULL;
  131. char *s = NULL, *s1 = NULL;
  132. int x;
  133. for (q = fileq; q; q = q->next)
  134. if (!egg_strcasecmp(q->to, to))
  135. this = q;
  136. if (this == NULL)
  137. return; /* None */
  138. /* Copy this file to /tmp */
  139. if (this->dir[0] == '*') { /* Absolute path */
  140. s = (char *) calloc(1, strlen(&this->dir[1]) + strlen(this->file) + 2);
  141. sprintf(s, "%s/%s", &this->dir[1], this->file);
  142. } else {
  143. char *p = strchr(this->dir, '*');
  144. if (p == NULL) { /* if it's messed up */
  145. send_next_file(to);
  146. return;
  147. }
  148. p++;
  149. s = (char *) calloc(1, strlen(p) + strlen(this->file) + 2);
  150. sprintf(s, "%s%s%s", p, p[0] ? "/" : "", this->file);
  151. strcpy(this->dir, &(p[atoi(this->dir)]));
  152. }
  153. if (copy_to_tmp) {
  154. s1 = (char *) calloc(1, strlen(tempdir) + strlen(this->file) + 1);
  155. sprintf(s1, "%s%s", tempdir, this->file);
  156. if (copyfile(s, s1) != 0) {
  157. putlog(LOG_FILES | LOG_MISC, "*",
  158. TRANSFER_COPY_FAILED,
  159. this->file, tempdir);
  160. dprintf(DP_HELP,
  161. TRANSFER_FILESYS_BROKEN,
  162. this->to);
  163. strcpy(s, this->to);
  164. flush_fileq(s);
  165. free(s1);
  166. free(s);
  167. return;
  168. }
  169. } else {
  170. s1 = strdup(s);
  171. }
  172. if (this->dir[0] == '*') {
  173. s = (char *) realloc(s, strlen(&this->dir[1]) + strlen(this->file) + 2);
  174. sprintf(s, "%s/%s", &this->dir[1], this->file);
  175. } else {
  176. s = (char *) realloc(s, strlen(this->dir) + strlen(this->file) + 2);
  177. sprintf(s, "%s%s%s", this->dir, this->dir[0] ? "/" : "", this->file);
  178. }
  179. x = raw_dcc_send(s1, this->to, this->nick, s);
  180. if (x == DCCSEND_OK) {
  181. if (egg_strcasecmp(this->to, this->nick))
  182. dprintf(DP_HELP, TRANSFER_FILE_ARRIVE, this->to,
  183. this->nick);
  184. deq_this(this);
  185. free(s);
  186. free(s1);
  187. return;
  188. }
  189. wipe_tmp_filename(s1, -1);
  190. if (x == DCCSEND_FULL) {
  191. putlog(LOG_FILES, "*",TRANSFER_LOG_CONFULL, s1, this->nick);
  192. dprintf(DP_HELP,
  193. TRANSFER_NOTICE_CONFULL,
  194. this->to);
  195. strcpy(s, this->to);
  196. flush_fileq(s);
  197. } else if (x == DCCSEND_NOSOCK) {
  198. putlog(LOG_FILES, "*", TRANSFER_LOG_SOCKERR, s1, this->nick);
  199. dprintf(DP_HELP, TRANSFER_NOTICE_SOCKERR,
  200. this->to);
  201. strcpy(s, this->to);
  202. flush_fileq(s);
  203. } else {
  204. if (x == DCCSEND_FEMPTY) {
  205. putlog(LOG_FILES, "*", TRANSFER_LOG_FILEEMPTY, this->file);
  206. dprintf(DP_HELP, TRANSFER_NOTICE_FILEEMPTY,
  207. this->to, this->file);
  208. }
  209. deq_this(this);
  210. }
  211. free(s);
  212. free(s1);
  213. return;
  214. }
  215. /*
  216. * DCC routines
  217. */
  218. /* Instead of reading all data intended to go into the DCC block
  219. * in one go, we read it in PMAX_SIZE chunks, feed it to tputs and
  220. * continue until we get to know that the network buffer only
  221. * buffers the data instead of sending it.
  222. *
  223. * In that case, we delay further sending until we receive the
  224. * dcc outdone event.
  225. *
  226. * Note: To optimize buffer sizes, we default to PMAX_SIZE, but
  227. * allocate a smaller buffer for smaller pending_data sizes.
  228. */
  229. #define PMAX_SIZE 4096
  230. static unsigned long pump_file_to_sock(FILE *file, long sock,
  231. register unsigned long pending_data)
  232. {
  233. const unsigned long buf_len = pending_data >= PMAX_SIZE ?
  234. PMAX_SIZE : pending_data;
  235. char *bf = (char *) calloc(1, buf_len);
  236. register unsigned long actual_size;
  237. if (bf) {
  238. do {
  239. actual_size = pending_data >= buf_len ? buf_len : pending_data;
  240. fread(bf, actual_size, 1, file);
  241. tputs(sock, bf, actual_size);
  242. pending_data -= actual_size;
  243. } while (!sock_has_data(SOCK_DATA_OUTGOING, sock) && pending_data != 0);
  244. free(bf);
  245. }
  246. return pending_data;
  247. }
  248. #endif /* HUB */
  249. void eof_dcc_fork_send(int idx)
  250. {
  251. char s1[121] = "", *s2 = NULL;
  252. fclose(dcc[idx].u.xfer->f);
  253. if (!strcmp(dcc[idx].nick, "*users")) {
  254. int x, y = 0;
  255. for (x = 0; x < dcc_total; x++)
  256. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  257. (dcc[x].type->flags & DCT_BOT)) {
  258. y = x;
  259. break;
  260. }
  261. if (y != 0) {
  262. dcc[y].status &= ~STAT_GETTING;
  263. dcc[y].status &= ~STAT_SHARE;
  264. }
  265. putlog(LOG_BOTS, "*", USERF_FAILEDXFER);
  266. unlink(dcc[idx].u.xfer->filename);
  267. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  268. int x, y = 0;
  269. for (x = 0; x < dcc_total; x++)
  270. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  271. (dcc[x].type->flags & DCT_BOT)) {
  272. y = x;
  273. break;
  274. }
  275. if (y != 0) {
  276. dcc[y].status &= ~STAT_GETTINGU;
  277. }
  278. putlog(LOG_BOTS, "*", "Failed binary transfer.");
  279. unlink(dcc[idx].u.xfer->filename);
  280. } else {
  281. strcpy(s1, strerror(errno));
  282. if (!quiet_reject)
  283. dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[idx].nick,
  284. DCC_CONNECTFAILED1, s1);
  285. putlog(LOG_MISC, "*", "%s: SEND %s (%s!%s)", DCC_CONNECTFAILED2,
  286. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  287. putlog(LOG_MISC, "*", " (%s)", s1);
  288. s2 = (char *) calloc(1, strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  289. sprintf(s2, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  290. unlink(s2);
  291. free(s2);
  292. }
  293. killsock(dcc[idx].sock);
  294. lostdcc(idx);
  295. }
  296. static void eof_dcc_send(int idx)
  297. {
  298. int ok, j;
  299. char *ofn = NULL, *nfn = NULL, s[1024] = "", *hand = NULL;
  300. struct userrec *u = NULL;
  301. fflush(dcc[idx].u.xfer->f);
  302. fsync(fileno(dcc[idx].u.xfer->f));
  303. fclose(dcc[idx].u.xfer->f);
  304. if (dcc[idx].u.xfer->length == dcc[idx].status) {
  305. int l;
  306. /* Success */
  307. ok = 0;
  308. if (!strcmp(dcc[idx].nick, "*users")) {
  309. finish_share(idx);
  310. killsock(dcc[idx].sock);
  311. lostdcc(idx);
  312. return;
  313. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  314. finish_update(idx);
  315. killsock(dcc[idx].sock);
  316. lostdcc(idx);
  317. return;
  318. }
  319. putlog(LOG_FILES, "*", TRANSFER_COMPLETED_DCC,
  320. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  321. egg_snprintf(s, sizeof s, "%s!%s", dcc[idx].nick, dcc[idx].host);
  322. u = get_user_by_host(s);
  323. hand = u ? u->handle : "*";
  324. l = strlen(dcc[idx].u.xfer->filename);
  325. if (l > NAME_MAX) {
  326. /* The filename is to long... blow it off */
  327. putlog(LOG_FILES, "*",TRANSFER_FILENAME_TOOLONG , l);
  328. dprintf(DP_HELP, TRANSFER_NOTICE_FNTOOLONG,
  329. dcc[idx].nick, l);
  330. putlog(LOG_FILES, "*", TRANSFER_TOO_BAD );
  331. dprintf(DP_HELP, TRANSFER_NOTICE_TOOBAD,
  332. dcc[idx].nick);
  333. killsock(dcc[idx].sock);
  334. lostdcc(idx);
  335. return;
  336. }
  337. /* Move the file from /tmp */
  338. ofn = (char *) calloc(1, strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  339. nfn = (char *) calloc(1, strlen(dcc[idx].u.xfer->dir)
  340. + strlen(dcc[idx].u.xfer->origname) + 1);
  341. sprintf(ofn, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  342. sprintf(nfn, "%s%s", dcc[idx].u.xfer->dir, dcc[idx].u.xfer->origname);
  343. if (movefile(ofn, nfn))
  344. putlog(LOG_MISC | LOG_FILES, "*", TRANSFER_FAILED_MOVE, nfn, ofn);
  345. free(ofn);
  346. free(nfn);
  347. for (j = 0; j < dcc_total; j++)
  348. if (!ok && (dcc[j].type->flags & (DCT_GETNOTES)) &&
  349. !egg_strcasecmp(dcc[j].nick, hand)) {
  350. ok = 1;
  351. dprintf(j,TRANSFER_THANKS);
  352. }
  353. if (!ok)
  354. dprintf(DP_HELP,TRANSFER_NOTICE_THANKS,
  355. dcc[idx].nick);
  356. killsock(dcc[idx].sock);
  357. lostdcc(idx);
  358. return;
  359. }
  360. /* Failure :( */
  361. if (!strcmp(dcc[idx].nick, "*users")) {
  362. int x, y = 0;
  363. for (x = 0; x < dcc_total; x++)
  364. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  365. (dcc[x].type->flags & DCT_BOT))
  366. y = x;
  367. if (y) {
  368. putlog(LOG_BOTS, "*",TRANSFER_USERFILE_LOST, dcc[y].nick);
  369. unlink(dcc[idx].u.xfer->filename);
  370. /* Drop that bot */
  371. dprintf(y, "bye\n");
  372. egg_snprintf(s, sizeof s,TRANSFER_USERFILE_DISCON, dcc[y].nick);
  373. botnet_send_unlinked(y, dcc[y].nick, s);
  374. chatout("*** %s %s\n", dcc[y].nick, s);
  375. if (y < idx) {
  376. int t = y;
  377. y = idx;
  378. idx = t;
  379. }
  380. if (y != idx) {
  381. killsock(dcc[y].sock);
  382. lostdcc(y);
  383. }
  384. killsock(dcc[idx].sock);
  385. lostdcc(idx);
  386. }
  387. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  388. int x, y = 0;
  389. for (x = 0; x < dcc_total; x++)
  390. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  391. (dcc[x].type->flags & DCT_BOT))
  392. y = x;
  393. if (y) {
  394. putlog(LOG_BOTS, "*", "Lost binary transfer from %s; aborting.", dcc[y].nick);
  395. unlink(dcc[idx].u.xfer->filename);
  396. /* Drop that bot
  397. dprintf(y, "bye\n");
  398. egg_snprintf(s, sizeof s,"Disconnected %s (aborted binary transfer)", dcc[y].nick);
  399. botnet_send_unlinked(y, dcc[y].nick, s);
  400. chatout("*** %s\n", dcc[y].nick, s);
  401. if (y != idx) {
  402. killsock(dcc[y].sock);
  403. lostdcc(y);
  404. }
  405. */
  406. killsock(dcc[idx].sock);
  407. lostdcc(idx);
  408. }
  409. } else {
  410. putlog(LOG_FILES, "*",TRANSFER_LOST_DCCSEND,
  411. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host,
  412. dcc[idx].status, dcc[idx].u.xfer->length);
  413. ofn = (char *) calloc(1, strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  414. sprintf(ofn, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  415. unlink(ofn);
  416. free(ofn);
  417. killsock(dcc[idx].sock);
  418. lostdcc(idx);
  419. }
  420. }
  421. #ifdef HUB
  422. /* Determine byte order. Used for resend DCC startup packets.
  423. */
  424. static inline u_8bit_t byte_order_test(void)
  425. {
  426. u_16bit_t test = TRANSFER_REGET_PACKETID;
  427. if (*((u_8bit_t *)&test) == ((TRANSFER_REGET_PACKETID & 0xff00) >> 8))
  428. return 0;
  429. if (*((u_8bit_t *)&test) == (TRANSFER_REGET_PACKETID & 0x00ff))
  430. return 1;
  431. return 0;
  432. }
  433. /* Parse and handle resend DCC startup packets.
  434. */
  435. inline static void handle_resend_packet(int idx, transfer_reget *reget_data)
  436. {
  437. if (byte_order_test() != reget_data->byte_order) {
  438. /* The sender's byte order does not match our's so we need to switch the
  439. * bytes first, before we can make use of them.
  440. */
  441. reget_data->packet_id = ((reget_data->packet_id & 0x00ff) << 8) |
  442. ((reget_data->packet_id & 0xff00) >> 8);
  443. reget_data->byte_offset = ((reget_data->byte_offset & 0xff000000) >> 24) |
  444. ((reget_data->byte_offset & 0x00ff0000) >> 8) |
  445. ((reget_data->byte_offset & 0x0000ff00) << 8) |
  446. ((reget_data->byte_offset & 0x000000ff) << 24);
  447. }
  448. if (reget_data->packet_id != TRANSFER_REGET_PACKETID)
  449. putlog(LOG_FILES, "*", TRANSFER_REGET_PACKET,
  450. dcc[idx].nick, dcc[idx].u.xfer->origname);
  451. else
  452. dcc[idx].u.xfer->offset = reget_data->byte_offset;
  453. dcc[idx].u.xfer->type = XFER_RESEND;
  454. }
  455. /* Handles DCC packets the client sends us. As soon as the last sent dcc
  456. * block is fully acknowledged we send the next block.
  457. *
  458. * Note: The first received packet during reget is a special 8 bit packet
  459. * containing special information.
  460. */
  461. void dcc_get(int idx, char *buf, int len)
  462. {
  463. char xnick[NICKLEN] = "";
  464. unsigned char bbuf[4] = "";
  465. unsigned long cmp, l;
  466. int w = len + dcc[idx].u.xfer->sofar, p = 0;
  467. dcc[idx].timeval = now; /* Mark as active */
  468. /* Add bytes to our buffer if we don't have a complete response yet.
  469. * This is either a 4 bit ack or the 8 bit reget packet.
  470. */
  471. if (w < 4 ||
  472. (w < 8 && dcc[idx].u.xfer->type == XFER_RESEND_PEND)) {
  473. egg_memcpy(&(dcc[idx].u.xfer->buf[dcc[idx].u.xfer->sofar]), buf, len);
  474. dcc[idx].u.xfer->sofar += len;
  475. return;
  476. /* Waiting for the 8 bit reget packet? */
  477. } else if (dcc[idx].u.xfer->type == XFER_RESEND_PEND) {
  478. /* The 8 bit packet is complete now. Parse it. */
  479. if (w == 8) {
  480. transfer_reget reget_data;
  481. egg_memcpy(&reget_data, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
  482. egg_memcpy(&reget_data + dcc[idx].u.xfer->sofar, buf, len);
  483. handle_resend_packet(idx, &reget_data);
  484. cmp = dcc[idx].u.xfer->offset;
  485. } else
  486. return;
  487. /* Fall through! */
  488. /* No, only want 4 bit ack responses. */
  489. } else {
  490. /* Complete packet? */
  491. if (w == 4) {
  492. egg_memcpy(bbuf, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
  493. egg_memcpy(&(bbuf[dcc[idx].u.xfer->sofar]), buf, len);
  494. } else {
  495. p = ((w - 1) & ~3) - dcc[idx].u.xfer->sofar;
  496. w = w - ((w - 1) & ~3);
  497. if (w < 4) {
  498. egg_memcpy(dcc[idx].u.xfer->buf, &(buf[p]), w);
  499. return;
  500. }
  501. egg_memcpy(bbuf, &(buf[p]), w);
  502. }
  503. /* This is more compatible than ntohl for machines where an int
  504. * is more than 4 bytes:
  505. */
  506. cmp = ((unsigned int) (bbuf[0]) << 24) +
  507. ((unsigned int) (bbuf[1]) << 16) +
  508. ((unsigned int) (bbuf[2]) << 8) + bbuf[3];
  509. dcc[idx].u.xfer->acked = cmp;
  510. }
  511. dcc[idx].u.xfer->sofar = 0;
  512. if (cmp > dcc[idx].u.xfer->length && cmp > dcc[idx].status) {
  513. /* Attempt to resume, but file is not as long as requested... */
  514. putlog(LOG_FILES, "*",
  515. TRANSFER_BEHIND_FILEEND,
  516. dcc[idx].u.xfer->origname, dcc[idx].nick);
  517. } else if (cmp > dcc[idx].status) {
  518. /* Attempt to resume */
  519. if (!strcmp(dcc[idx].nick, "*users")) {
  520. putlog(LOG_BOTS, "*", TRANSFER_TRY_SKIP_AHEAD);
  521. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  522. putlog(LOG_BOTS, "*","!!! Trying to skip ahead on binary transfer");
  523. } else {
  524. fseek(dcc[idx].u.xfer->f, cmp, SEEK_SET);
  525. dcc[idx].status = cmp;
  526. putlog(LOG_FILES, "*",TRANSFER_RESUME_FILE,
  527. (int) (cmp / 1024), dcc[idx].u.xfer->origname,
  528. dcc[idx].nick);
  529. }
  530. } else {
  531. if (dcc[idx].u.xfer->ack_type == XFER_ACK_UNKNOWN) {
  532. if (cmp < dcc[idx].u.xfer->offset)
  533. /* If we don't start at the top of the file, some clients only tell
  534. * us the really received bytes (e.g. bitchx). This seems to be the
  535. * case here.
  536. */
  537. dcc[idx].u.xfer->ack_type = XFER_ACK_WITHOUT_OFFSET;
  538. else
  539. dcc[idx].u.xfer->ack_type = XFER_ACK_WITH_OFFSET;
  540. }
  541. if (dcc[idx].u.xfer->ack_type == XFER_ACK_WITHOUT_OFFSET)
  542. cmp += dcc[idx].u.xfer->offset;
  543. }
  544. if (cmp != dcc[idx].status)
  545. return;
  546. if (dcc[idx].status == dcc[idx].u.xfer->length) {
  547. /* Successful send, we are done */
  548. killsock(dcc[idx].sock);
  549. fclose(dcc[idx].u.xfer->f);
  550. if (!strcmp(dcc[idx].nick, "*users")) {
  551. int x, y = 0;
  552. for (x = 0; x < dcc_total; x++)
  553. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  554. (dcc[x].type->flags & DCT_BOT))
  555. y = x;
  556. if (y != 0)
  557. dcc[y].status &= ~STAT_SENDING;
  558. putlog(LOG_BOTS, "*", TRANSFER_COMPLETED_USERFILE, dcc[y].nick);
  559. unlink(dcc[idx].u.xfer->filename);
  560. /* Any sharebot things that were queued: */
  561. dprintf(y, "s !\n");
  562. xnick[0] = 0;
  563. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  564. int x, y = 0;
  565. for (x = 0; x < dcc_total; x++)
  566. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  567. (dcc[x].type->flags & DCT_BOT))
  568. y = x;
  569. if (y != 0) {
  570. dcc[y].status &= ~STAT_SENDINGU;
  571. dcc[y].status |= STAT_UPDATED;
  572. }
  573. putlog(LOG_BOTS, "*", "Completed binary file send to %s", dcc[y].nick);
  574. xnick[0] = 0;
  575. #ifdef HUB
  576. bupdating = 0;
  577. #endif
  578. } else {
  579. /* Download is credited to the user who requested it
  580. * (not the user who actually received it)
  581. */
  582. putlog(LOG_FILES, "*",TRANSFER_FINISHED_DCCSEND, dcc[idx].u.xfer->origname, dcc[idx].nick);
  583. wipe_tmp_filename(dcc[idx].u.xfer->filename, idx);
  584. strcpy((char *) xnick, dcc[idx].nick);
  585. }
  586. lostdcc(idx);
  587. /* Any to dequeue? */
  588. if (!at_limit(xnick))
  589. send_next_file(xnick);
  590. return;
  591. }
  592. /* Note: No fseek() needed here, because the file position is kept from
  593. * the last run.
  594. */
  595. l = dcc_block;
  596. if (l == 0 || dcc[idx].status + l > dcc[idx].u.xfer->length)
  597. l = dcc[idx].u.xfer->length - dcc[idx].status;
  598. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f, dcc[idx].sock, l);
  599. dcc[idx].status += l;
  600. }
  601. void eof_dcc_get(int idx)
  602. {
  603. char xnick[NICKLEN] = "", s[1024] = "";
  604. fclose(dcc[idx].u.xfer->f);
  605. if (!strcmp(dcc[idx].nick, "*users")) {
  606. int x, y = 0;
  607. for (x = 0; x < dcc_total; x++)
  608. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  609. (dcc[x].type->flags & DCT_BOT))
  610. y = x;
  611. putlog(LOG_BOTS, "*", TRANSFER_ABORT_USERFILE);
  612. /* Note: no need to unlink the xfer file, as it's already unlinked. */
  613. xnick[0] = 0;
  614. /* Drop that bot */
  615. dprintf(-dcc[y].sock, "bye\n");
  616. egg_snprintf(s, sizeof s, TRANSFER_USERFILE_DISCON,
  617. dcc[y].nick);
  618. botnet_send_unlinked(y, dcc[y].nick, s);
  619. chatout("*** %s\n", s);
  620. if (y != idx) {
  621. killsock(dcc[y].sock);
  622. lostdcc(y);
  623. }
  624. killsock(dcc[idx].sock);
  625. lostdcc(idx);
  626. return;
  627. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  628. int x, y = 0;
  629. for (x = 0; x < dcc_total; x++)
  630. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  631. (dcc[x].type->flags & DCT_BOT))
  632. y = x;
  633. putlog(LOG_BOTS, "*", "Lost binary transfer; aborting.");
  634. /* Note: no need to unlink the xfer file, as it's already unlinked. */
  635. xnick[0] = 0;
  636. /* Drop that bot */
  637. dcc[y].status &= ~STAT_SENDINGU;
  638. #ifdef HUB
  639. bupdating = 0;
  640. #endif
  641. /*
  642. dprintf(-dcc[y].sock, "bye\n");
  643. egg_snprintf(s, sizeof s, "Disconnected %s (aborted binary transfer)",
  644. dcc[y].nick);
  645. botnet_send_unlinked(y, dcc[y].nick, s);
  646. chatout("*** %s\n", s);
  647. if (y != idx) {
  648. killsock(dcc[y].sock);
  649. lostdcc(y);
  650. }
  651. */
  652. killsock(dcc[idx].sock);
  653. lostdcc(idx);
  654. return;
  655. } else {
  656. /* Call `lost' DCC trigger now.
  657. */
  658. putlog(LOG_FILES, "*",TRANSFER_LOST_DCCGET,
  659. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  660. wipe_tmp_filename(dcc[idx].u.xfer->filename, idx);
  661. strcpy(xnick, dcc[idx].nick);
  662. }
  663. killsock(dcc[idx].sock);
  664. lostdcc(idx);
  665. /* Send next queued file if there is one */
  666. if (!at_limit(xnick))
  667. send_next_file(xnick);
  668. }
  669. #endif /* HUB */
  670. void dcc_send(int idx, char *buf, int len)
  671. {
  672. char s[SGRAB + 2] = "", *b = NULL;
  673. unsigned long sent;
  674. fwrite(buf, len, 1, dcc[idx].u.xfer->f);
  675. fflush(dcc[idx].u.xfer->f);
  676. fsync(fileno(dcc[idx].u.xfer->f));
  677. dcc[idx].status += len;
  678. /* Put in network byte order */
  679. sent = dcc[idx].status;
  680. s[0] = (sent / (1 << 24));
  681. s[1] = (sent % (1 << 24)) / (1 << 16);
  682. s[2] = (sent % (1 << 16)) / (1 << 8);
  683. s[3] = (sent % (1 << 8));
  684. tputs(dcc[idx].sock, s, 4);
  685. dcc[idx].timeval = now;
  686. if (dcc[idx].status > dcc[idx].u.xfer->length &&
  687. dcc[idx].u.xfer->length > 0) {
  688. dprintf(DP_HELP,TRANSFER_BOGUS_FILE_LENGTH, dcc[idx].nick);
  689. putlog(LOG_FILES, "*",
  690. TRANSFER_FILE_TOO_LONG,
  691. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  692. fclose(dcc[idx].u.xfer->f);
  693. b = (char *) calloc(1, strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  694. sprintf(b, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  695. unlink(b);
  696. free(b);
  697. killsock(dcc[idx].sock);
  698. lostdcc(idx);
  699. }
  700. }
  701. #ifdef HUB
  702. static void transfer_get_timeout(int i)
  703. {
  704. char xx[1024] = "";
  705. fclose(dcc[i].u.xfer->f);
  706. if (strcmp(dcc[i].nick, "*users") == 0) {
  707. int x, y = 0;
  708. for (x = 0; x < dcc_total; x++)
  709. if ((!egg_strcasecmp(dcc[x].nick, dcc[i].host)) &&
  710. (dcc[x].type->flags & DCT_BOT))
  711. y = x;
  712. if (y != 0) {
  713. dcc[y].status &= ~STAT_SENDING;
  714. dcc[y].status &= ~STAT_SHARE;
  715. }
  716. unlink(dcc[i].u.xfer->filename);
  717. putlog(LOG_BOTS, "*",TRANSFER_USERFILE_TIMEOUT);
  718. dprintf(y, "bye\n");
  719. egg_snprintf(xx, sizeof xx,TRANSFER_DICONNECT_TIMEOUT,
  720. dcc[y].nick);
  721. botnet_send_unlinked(y, dcc[y].nick, xx);
  722. chatout("*** %s\n", xx);
  723. if (y < i) {
  724. int t = y;
  725. y = i;
  726. i = t;
  727. }
  728. killsock(dcc[y].sock);
  729. lostdcc(y);
  730. xx[0] = 0;
  731. } else if (strcmp(dcc[i].nick, "*binary") == 0) {
  732. int x, y = 0;
  733. for (x = 0; x < dcc_total; x++)
  734. if ((!egg_strcasecmp(dcc[x].nick, dcc[i].host)) &&
  735. (dcc[x].type->flags & DCT_BOT))
  736. y = x;
  737. if (y != 0) {
  738. dcc[y].status &= ~STAT_SENDINGU;
  739. }
  740. putlog(LOG_BOTS, "*","Timeout on binary transfer.");
  741. dprintf(y, "bye\n");
  742. egg_snprintf(xx, sizeof xx,"Disconnected %s (timed-out binary transfer)",
  743. dcc[y].nick);
  744. botnet_send_unlinked(y, dcc[y].nick, xx);
  745. chatout("*** %s\n", xx);
  746. if (y < i) {
  747. int t = y;
  748. y = i;
  749. i = t;
  750. }
  751. killsock(dcc[y].sock);
  752. lostdcc(y);
  753. xx[0] = 0;
  754. } else {
  755. char *p = NULL;
  756. p = strrchr(dcc[i].u.xfer->origname, '/');
  757. dprintf(DP_HELP, TRANSFER_NOTICE_TIMEOUT,
  758. dcc[i].nick, p ? p + 1 : dcc[i].u.xfer->origname);
  759. /* Call DCC `timeout' trigger now.
  760. */
  761. putlog(LOG_FILES, "*",TRANSFER_DCC_GET_TIMEOUT,
  762. p ? p + 1 : dcc[i].u.xfer->origname, dcc[i].nick, dcc[i].status,
  763. dcc[i].u.xfer->length);
  764. wipe_tmp_filename(dcc[i].u.xfer->filename, i);
  765. strcpy(xx, dcc[i].nick);
  766. }
  767. killsock(dcc[i].sock);
  768. lostdcc(i);
  769. if (!at_limit(xx))
  770. send_next_file(xx);
  771. }
  772. #endif /* HUB */
  773. void tout_dcc_send(int idx)
  774. {
  775. if (!strcmp(dcc[idx].nick, "*users")) {
  776. int x, y = 0;
  777. for (x = 0; x < dcc_total; x++)
  778. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  779. (dcc[x].type->flags & DCT_BOT))
  780. y = x;
  781. if (y != 0) {
  782. dcc[y].status &= ~STAT_GETTING;
  783. dcc[y].status &= ~STAT_SHARE;
  784. }
  785. unlink(dcc[idx].u.xfer->filename);
  786. putlog(LOG_BOTS, "*", TRANSFER_USERFILE_TIMEOUT);
  787. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  788. int x, y = 0;
  789. for (x = 0; x < dcc_total; x++)
  790. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  791. (dcc[x].type->flags & DCT_BOT))
  792. y = x;
  793. if (y != 0) {
  794. dcc[y].status &= ~STAT_GETTINGU;
  795. }
  796. putlog(LOG_BOTS, "*", "Timeout on binary transfer.");
  797. } else {
  798. char *buf = NULL;
  799. dprintf(DP_HELP,TRANSFER_NOTICE_TIMEOUT,
  800. dcc[idx].nick, dcc[idx].u.xfer->origname);
  801. putlog(LOG_FILES, "*",TRANSFER_DCC_SEND_TIMEOUT,
  802. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].status,
  803. dcc[idx].u.xfer->length);
  804. buf = (char *) calloc(1, strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  805. sprintf(buf, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  806. unlink(buf);
  807. free(buf);
  808. }
  809. killsock(dcc[idx].sock);
  810. lostdcc(idx);
  811. }
  812. #ifdef HUB
  813. void display_dcc_get(int idx, char *buf)
  814. {
  815. if (dcc[idx].status == dcc[idx].u.xfer->length)
  816. sprintf(buf, TRANSFER_SEND, dcc[idx].u.xfer->acked,
  817. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  818. else
  819. sprintf(buf,TRANSFER_SEND, dcc[idx].status,
  820. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  821. }
  822. void display_dcc_get_p(int idx, char *buf)
  823. {
  824. sprintf(buf,TRANSFER_SEND_WAITED, now - dcc[idx].timeval,
  825. dcc[idx].u.xfer->origname);
  826. }
  827. #endif /* HUB */
  828. void display_dcc_send(int idx, char *buf)
  829. {
  830. sprintf(buf,TRANSFER_SEND, dcc[idx].status,
  831. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  832. }
  833. void display_dcc_fork_send(int idx, char *buf)
  834. {
  835. sprintf(buf, TRANSFER_CONN_SEND);
  836. }
  837. void kill_dcc_xfer(int idx, void *x)
  838. {
  839. register struct xfer_info *p = (struct xfer_info *) x;
  840. if (p->filename)
  841. free(p->filename);
  842. /* We need to check if origname points to filename before
  843. * attempting to free the memory.
  844. */
  845. if (p->origname && p->origname != p->filename)
  846. free(p->origname);
  847. free(x);
  848. }
  849. void out_dcc_xfer(int idx, char *buf, void *x)
  850. {
  851. }
  852. #ifdef HUB
  853. static void outdone_dcc_xfer(int idx)
  854. {
  855. if (dcc[idx].u.xfer->block_pending)
  856. dcc[idx].u.xfer->block_pending =
  857. pump_file_to_sock(dcc[idx].u.xfer->f, dcc[idx].sock,
  858. dcc[idx].u.xfer->block_pending);
  859. }
  860. #endif /* HUB */
  861. struct dcc_table DCC_SEND =
  862. {
  863. "SEND",
  864. DCT_FILETRAN | DCT_FILESEND | DCT_VALIDIDX,
  865. eof_dcc_send,
  866. dcc_send,
  867. &wait_dcc_xfer,
  868. tout_dcc_send,
  869. display_dcc_send,
  870. kill_dcc_xfer,
  871. out_dcc_xfer,
  872. NULL
  873. };
  874. void dcc_fork_send(int idx, char *x, int y);
  875. struct dcc_table DCC_FORK_SEND =
  876. {
  877. "FORK_SEND",
  878. DCT_FILETRAN | DCT_FORKTYPE | DCT_FILESEND | DCT_VALIDIDX,
  879. eof_dcc_fork_send,
  880. dcc_fork_send,
  881. &wait_dcc_xfer,
  882. eof_dcc_fork_send,
  883. display_dcc_fork_send,
  884. kill_dcc_xfer,
  885. out_dcc_xfer,
  886. NULL
  887. };
  888. void dcc_fork_send(int idx, char *x, int y)
  889. {
  890. char s1[121] = "";
  891. if (dcc[idx].type != &DCC_FORK_SEND)
  892. return;
  893. dcc[idx].type = &DCC_SEND;
  894. dcc[idx].status = 0;
  895. dcc[idx].u.xfer->start_time = now;
  896. egg_snprintf(s1, sizeof s1, "%s!%s", dcc[idx].nick, dcc[idx].host);
  897. if (strcmp(dcc[idx].nick, "*users") && strcmp(dcc[idx].nick, "*binary"))
  898. putlog(LOG_MISC, "*", TRANSFER_DCC_CONN, dcc[idx].u.xfer->origname, s1);
  899. }
  900. #ifdef HUB
  901. struct dcc_table DCC_GET =
  902. {
  903. "GET",
  904. DCT_FILETRAN | DCT_VALIDIDX,
  905. eof_dcc_get,
  906. dcc_get,
  907. &wait_dcc_xfer,
  908. transfer_get_timeout,
  909. display_dcc_get,
  910. kill_dcc_xfer,
  911. out_dcc_xfer,
  912. outdone_dcc_xfer,
  913. };
  914. struct dcc_table DCC_GET_PENDING =
  915. {
  916. "GET_PENDING",
  917. DCT_FILETRAN | DCT_VALIDIDX,
  918. eof_dcc_get,
  919. dcc_get_pending,
  920. &wait_dcc_xfer,
  921. transfer_get_timeout,
  922. display_dcc_get_p,
  923. kill_dcc_xfer,
  924. out_dcc_xfer,
  925. NULL
  926. };
  927. static void dcc_get_pending(int idx, char *buf, int len)
  928. {
  929. IP ip;
  930. port_t port;
  931. int i;
  932. char s[UHOSTLEN] = "";
  933. i = answer(dcc[idx].sock, s, &ip, &port, 1);
  934. killsock(dcc[idx].sock);
  935. dcc[idx].sock = i;
  936. dcc[idx].addr = ip;
  937. dcc[idx].port = (int) port;
  938. if (dcc[idx].sock == -1) {
  939. dprintf(DP_HELP, TRANSFER_NOTICE_BAD_CONN, dcc[idx].nick, strerror(errno));
  940. putlog(LOG_FILES, "*", TRANSFER_LOG_BAD_CONN,
  941. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  942. fclose(dcc[idx].u.xfer->f);
  943. lostdcc(idx);
  944. return;
  945. }
  946. dcc[idx].type = &DCC_GET;
  947. dcc[idx].u.xfer->ack_type = XFER_ACK_UNKNOWN;
  948. /*
  949. * Note: The file was already opened and dcc[idx].u.xfer->f may be
  950. * used immediately. Leave it opened until the file transfer
  951. * is complete.
  952. */
  953. /* Are we resuming? */
  954. if (dcc[idx].u.xfer->type == XFER_RESUME_PEND) {
  955. long unsigned int l;
  956. if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block) {
  957. l = dcc[idx].u.xfer->length - dcc[idx].u.xfer->offset;
  958. dcc[idx].status = dcc[idx].u.xfer->length;
  959. } else {
  960. l = dcc_block;
  961. dcc[idx].status = dcc[idx].u.xfer->offset + dcc_block;
  962. }
  963. /* Seek forward ... */
  964. fseek(dcc[idx].u.xfer->f, dcc[idx].u.xfer->offset, SEEK_SET);
  965. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
  966. dcc[idx].sock, l);
  967. dcc[idx].u.xfer->type = XFER_RESUME;
  968. } else {
  969. dcc[idx].u.xfer->offset = 0;
  970. /* If we're resending the data, wait for the client's response first,
  971. * before sending anything ourself.
  972. */
  973. if (dcc[idx].u.xfer->type != XFER_RESEND_PEND) {
  974. if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block)
  975. dcc[idx].status = dcc[idx].u.xfer->length;
  976. else
  977. dcc[idx].status = dcc_block;
  978. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
  979. dcc[idx].sock,
  980. dcc[idx].status);
  981. } else
  982. dcc[idx].status = 0;
  983. }
  984. dcc[idx].timeval = dcc[idx].u.xfer->start_time = now;
  985. }
  986. /* Starts a new DCC SEND or DCC RESEND connection to `nick', transferring
  987. * `filename' from `dir'.
  988. *
  989. * Use raw_dcc_resend() and raw_dcc_send() instead of this function.
  990. */
  991. static int raw_dcc_resend_send(char *filename, char *nick, char *from, char *dir, int resend)
  992. {
  993. int zz, i;
  994. port_t port;
  995. char *nfn = NULL, *buf = NULL;
  996. long dccfilesize;
  997. FILE *f = NULL, *dccfile = NULL;
  998. sdprintf("raw_dcc_resend_send()");
  999. zz = (-1);
  1000. dccfile = fopen(filename, "rb");
  1001. if (!dccfile) {
  1002. putlog(LOG_MISC, "*", "Failed to open %s: %s", filename, strerror(errno));
  1003. return DCCSEND_FEMPTY;
  1004. }
  1005. fseek(dccfile, 0, SEEK_END);
  1006. dccfilesize = ftell(dccfile);
  1007. fclose(dccfile);
  1008. /* File empty?! */
  1009. if (dccfilesize == 0)
  1010. return DCCSEND_FEMPTY;
  1011. if (conf.portmin > 0 && conf.portmin < conf.portmax) {
  1012. for (port = conf.portmin; port <= conf.portmax; port++)
  1013. #ifdef USE_IPV6
  1014. if ((zz = open_listen_by_af(&port, AF_INET)) != -1) /* no idea how we want to handle this -poptix 02/03/03 */
  1015. #else
  1016. if ((zz = open_listen(&port)) != -1)
  1017. #endif /* USE_IPV6 */
  1018. break;
  1019. } else {
  1020. port = conf.portmin;
  1021. #ifdef USE_IPV6
  1022. zz = open_listen_by_af(&port, AF_INET);
  1023. #else
  1024. zz = open_listen(&port);
  1025. #endif /* USE_IPV6 */
  1026. }
  1027. if (zz == (-1))
  1028. return DCCSEND_NOSOCK;
  1029. nfn = strrchr(dir, '/');
  1030. if (nfn == NULL)
  1031. nfn = dir;
  1032. else
  1033. nfn++;
  1034. f = fopen(filename, "rb");
  1035. if (!f)
  1036. return DCCSEND_BADFN;
  1037. if ((i = new_dcc(&DCC_GET_PENDING, sizeof(struct xfer_info))) == -1)
  1038. return DCCSEND_FULL;
  1039. dcc[i].sock = zz;
  1040. dcc[i].addr = (IP) (-559026163);
  1041. dcc[i].port = port;
  1042. strcpy(dcc[i].nick, nick);
  1043. strcpy(dcc[i].host, "irc");
  1044. dcc[i].u.xfer->filename = (char *) calloc(1, strlen(filename) + 1);
  1045. strcpy(dcc[i].u.xfer->filename, filename);
  1046. if (strchr(nfn, ' '))
  1047. nfn = buf = replace_spaces(nfn);
  1048. dcc[i].u.xfer->origname = (char *) calloc(1, strlen(nfn) + 1);
  1049. strcpy(dcc[i].u.xfer->origname, nfn);
  1050. strncpyz(dcc[i].u.xfer->from, from, NICKLEN);
  1051. strncpyz(dcc[i].u.xfer->dir, dir, DIRLEN);
  1052. dcc[i].u.xfer->length = dccfilesize;
  1053. dcc[i].timeval = now;
  1054. dcc[i].u.xfer->f = f;
  1055. dcc[i].u.xfer->type = resend ? XFER_RESEND_PEND : XFER_SEND;
  1056. if (nick[0] != '*') {
  1057. dprintf(DP_HELP, "PRIVMSG %s :\001DCC %sSEND %s %lu %d %lu\001\n", nick,
  1058. resend ? "RE" : "", nfn,
  1059. iptolong(natip[0] ? (IP) inet_addr(natip) : getmyip()), port,
  1060. dccfilesize);
  1061. putlog(LOG_FILES, "*",TRANSFER_BEGIN_DCC, resend ? TRANSFER_RE : "",
  1062. nfn, nick);
  1063. }
  1064. if (buf)
  1065. free(buf);
  1066. return DCCSEND_OK;
  1067. }
  1068. /* Starts a DCC RESEND connection.
  1069. */
  1070. /*
  1071. static int raw_dcc_resend(char *filename, char *nick, char *from, char *dir)
  1072. {
  1073. return raw_dcc_resend_send(filename, nick, from, dir, 1);
  1074. }
  1075. */
  1076. /* Starts a DCC_SEND connection.
  1077. */
  1078. int raw_dcc_send(char *filename, char *nick, char *from, char *dir)
  1079. {
  1080. return raw_dcc_resend_send(filename, nick, from, dir, 0);
  1081. }
  1082. #endif /* HUB */
  1083. #ifdef LEAF
  1084. /*
  1085. * CTCP functions
  1086. */
  1087. /* This handles DCC RESUME requests.
  1088. */
  1089. /* NOT EVEN USED :D
  1090. static int ctcp_DCC_RESUME(char *nick, char *from, char *handle, char *object, char *keyword, char *text)
  1091. {
  1092. char *action = NULL, *fn = NULL, buf[SGRAB + 2] = "", *msg = buf;
  1093. int i, port;
  1094. unsigned long offset;
  1095. strcpy(msg, text);
  1096. action = newsplit(&msg);
  1097. if (egg_strcasecmp(action, "RESUME"))
  1098. return BIND_RET_LOG;
  1099. fn = newsplit(&msg);
  1100. port = atoi(newsplit(&msg));
  1101. offset = my_atoul(newsplit(&msg));
  1102. // Search for existing SEND
  1103. for (i = 0; i < dcc_total; i++)
  1104. if ((dcc[i].type == &DCC_GET_PENDING) &&
  1105. (!rfc_casecmp(dcc[i].nick, nick)) && (dcc[i].port == port))
  1106. break;
  1107. // No matching transfer found?
  1108. if (i == dcc_total)
  1109. return BIND_RET_LOG;
  1110. if (dcc[i].u.xfer->length <= offset) {
  1111. char *p = strrchr(dcc[i].u.xfer->origname, '/');
  1112. dprintf(DP_HELP,TRANSFER_DCC_IGNORED, nick, p ? p + 1 : dcc[i].u.xfer->origname);
  1113. return BIND_RET_LOG;
  1114. }
  1115. dcc[i].u.xfer->type = XFER_RESUME_PEND;
  1116. dcc[i].u.xfer->offset = offset;
  1117. dprintf(DP_HELP, "PRIVMSG %s :\001DCC ACCEPT %s %d %u\001\n", nick, fn, port, offset);
  1118. // Now we wait for the client to connect.
  1119. return BIND_RET_BREAK;
  1120. }
  1121. static cmd_t transfer_ctcps[] =
  1122. {
  1123. {"DCC", "", ctcp_DCC_RESUME, "transfer:DCC"},
  1124. {NULL, NULL, NULL, NULL}
  1125. };
  1126. */
  1127. /* Add our CTCP bindings if the server module is loaded. */
  1128. static int server_transfer_setup(char *mod)
  1129. {
  1130. /* add_builtins("ctcp", transfer_ctcps); */
  1131. return 1;
  1132. }
  1133. #endif /* LEAF */
  1134. /*
  1135. * Module functions
  1136. */
  1137. void transfer_report(int idx, int details)
  1138. {
  1139. if (details) {
  1140. dprintf(idx,TRANSFER_STAT_BLOCK,
  1141. dcc_block, (dcc_block == 0) ? " (turbo dcc)" : "", dcc_limit);
  1142. }
  1143. }
  1144. void transfer_init()
  1145. {
  1146. fileq = NULL;
  1147. #ifdef LEAF
  1148. server_transfer_setup(NULL);
  1149. #endif /* LEAF */
  1150. }