dccutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * dccutil.c -- handles:
  3. * lots of little functions to send formatted text to
  4. * varying types of connections
  5. * '.who', '.whom', and '.dccstat' code
  6. * memory management for dcc structures
  7. * timeout checking for dcc connections
  8. *
  9. */
  10. #include <sys/stat.h>
  11. #include "main.h"
  12. #include <errno.h>
  13. #include "chan.h"
  14. #include "modules.h"
  15. #include "tandem.h"
  16. extern struct dcc_t *dcc;
  17. extern int dcc_total, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS, tands;
  18. #ifdef USE_IPV6
  19. extern unsigned long notalloc;
  20. #endif /* USE_IPV6 */
  21. extern char botnetnick[], version[];
  22. extern time_t now;
  23. extern sock_list *socklist;
  24. extern Tcl_Interp *interp;
  25. static struct portmap *root = NULL;
  26. char motdfile[121] = "text/motd"; /* File where the motd is stored */
  27. int connect_timeout = 15; /* How long to wait before a telnet
  28. connection times out */
  29. int reserved_port_min = 0;
  30. int reserved_port_max = 0;
  31. void init_dcc_max()
  32. {
  33. int osock = MAXSOCKS;
  34. if (max_dcc < 1)
  35. max_dcc = 1;
  36. if (dcc)
  37. dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
  38. else
  39. dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
  40. MAXSOCKS = max_dcc + 10;
  41. if (socklist)
  42. socklist = (sock_list *) nrealloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  43. else
  44. socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
  45. for (; osock < MAXSOCKS; osock++)
  46. socklist[osock].flags = SOCK_UNUSED;
  47. }
  48. int expmem_dccutil()
  49. {
  50. int tot, i;
  51. struct portmap *pmap;
  52. tot = sizeof(struct dcc_t) * max_dcc + sizeof(sock_list) * MAXSOCKS;
  53. for (pmap = root; pmap; pmap = pmap->next)
  54. tot += sizeof(struct portmap);
  55. for (i = 0; i < dcc_total; i++) {
  56. if (dcc[i].type && dcc[i].type->expmem)
  57. tot += dcc[i].type->expmem(dcc[i].u.other);
  58. }
  59. return tot;
  60. }
  61. /* Replace \n with \r\n */
  62. char *add_cr(char *buf)
  63. {
  64. static char WBUF[1024];
  65. char *p, *q;
  66. for (p = buf, q = WBUF; *p; p++, q++) {
  67. if (*p == '\n')
  68. *q++ = '\r';
  69. *q = *p;
  70. }
  71. *q = *p;
  72. return WBUF;
  73. }
  74. extern void (*qserver) (int, char *, int);
  75. void dprintf EGG_VARARGS_DEF(int, arg1)
  76. {
  77. static char buf[1024];
  78. char *format;
  79. int idx, len;
  80. va_list va;
  81. idx = EGG_VARARGS_START(int, arg1, va);
  82. format = va_arg(va, char *);
  83. egg_vsnprintf(buf, 1023, format, va);
  84. va_end(va);
  85. /* We can not use the return value vsnprintf() to determine where
  86. * to null terminate. The C99 standard specifies that vsnprintf()
  87. * shall return the number of bytes that would be written if the
  88. * buffer had been large enough, rather then -1.
  89. */
  90. /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
  91. * sizeof(buf) bytes. But we're not doing that anyway.
  92. */
  93. buf[sizeof(buf)-1] = 0;
  94. len = strlen(buf);
  95. /* this is for color on dcc :P */
  96. if (idx < 0) {
  97. tputs(-idx, buf, len);
  98. } else if (idx > 0x7FF0) {
  99. switch (idx) {
  100. case DP_LOG:
  101. putlog(LOG_MISC, "*", "%s", buf);
  102. break;
  103. case DP_STDOUT:
  104. tputs(STDOUT, buf, len);
  105. break;
  106. case DP_STDERR:
  107. tputs(STDERR, buf, len);
  108. break;
  109. case DP_SERVER:
  110. #ifdef HUB
  111. return;
  112. #endif /* HUB */
  113. case DP_HELP:
  114. #ifdef HUB
  115. return;
  116. #endif /* HUB */
  117. case DP_MODE:
  118. #ifdef HUB
  119. return;
  120. #endif /* HUB */
  121. case DP_MODE_NEXT:
  122. #ifdef HUB
  123. return;
  124. #endif /* HUB */
  125. case DP_SERVER_NEXT:
  126. #ifdef HUB
  127. return;
  128. #endif /* HUB */
  129. case DP_HELP_NEXT:
  130. #ifdef HUB
  131. return;
  132. #endif /* HUB */
  133. qserver(idx, buf, len);
  134. break;
  135. }
  136. return;
  137. } else { /* normal chat text */
  138. if ((dcc[idx].status & STAT_COLOR) && (dcc[idx].type == &DCC_CHAT)) {
  139. int i;
  140. char buf3[1024], buf2[1024], c;
  141. buf3[0] = buf2[0] = 0;
  142. for (i = 0 ; i < len ; i++) {
  143. /* FIXME: Trying to fix bug where you do .color on ANSI, then .bc <bot> help help OVER TELNET
  144. if (buf[i] == '\033') {
  145. unsigned char *e;
  146. for (e = buf + 2; *e != 'm' && *e; e++)
  147. i++;
  148. if (i >= len) break;
  149. }
  150. */
  151. c = buf[i];
  152. buf2[0] = 0;
  153. if (c == ':') {
  154. sprintf(buf2, "%s%c%s", LIGHTGREY(idx), c, COLOR_END(idx));
  155. } else if (c == '@') {
  156. sprintf(buf2, "%s%c%s", BOLD(idx), c, BOLD_END(idx));
  157. // } else if (c == ']' || c == '>' || c == ')' || c == '[' || c == '<' || c == '(') {
  158. } else if (c == '>' || c == ')' || c == '<' || c == '(') {
  159. sprintf(buf2, "%s%c%s", GREEN(idx), c, COLOR_END(idx));
  160. } else {
  161. sprintf(buf2, "%c", c);
  162. }
  163. // sprintf(buf3, "%s%s", buf3 ? buf3 : "", buf2 ? buf2 : "");
  164. sprintf(buf3, "%s%s", (buf3 && buf3[0]) ? buf3 : "", (buf2 && buf2[0]) ? buf2 : "");
  165. }
  166. buf3[strlen(buf3)] = 0;
  167. strcpy(buf, buf3);
  168. // strncpyz(buf, buf2, sizeof buf);
  169. }
  170. buf[sizeof(buf) - 1] = 0;
  171. len = strlen(buf);
  172. if (len > 1000) { /* Truncate to fit */
  173. buf[1000] = 0;
  174. strcat(buf, "\n");
  175. len = 1001;
  176. }
  177. if (dcc[idx].simul > 0) {
  178. bounce_simul(idx, buf);
  179. } else {
  180. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  181. char *p = add_cr(buf);
  182. tputs(dcc[idx].sock, p, strlen(p));
  183. } else if (dcc[idx].type && dcc[idx].type->output) {
  184. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  185. } else {
  186. tputs(dcc[idx].sock, buf, len);
  187. }
  188. }
  189. }
  190. }
  191. void chatout EGG_VARARGS_DEF(char *, arg1)
  192. {
  193. int i, len;
  194. char *format;
  195. char s[601];
  196. va_list va;
  197. format = EGG_VARARGS_START(char *, arg1, va);
  198. egg_vsnprintf(s, 511, format, va);
  199. va_end(va);
  200. len = strlen(s);
  201. if (len > 511)
  202. len = 511;
  203. s[len + 1] = 0;
  204. for (i = 0; i < dcc_total; i++)
  205. if ((dcc[i].type == &DCC_CHAT) && !(dcc[i].simul))
  206. if (dcc[i].u.chat->channel >= 0)
  207. dprintf(i, "%s", s);
  208. }
  209. /* Print to all on this channel but one.
  210. */
  211. void chanout_but EGG_VARARGS_DEF(int, arg1)
  212. {
  213. int i, x, chan, len;
  214. char *format;
  215. char s[601];
  216. va_list va;
  217. x = EGG_VARARGS_START(int, arg1, va);
  218. chan = va_arg(va, int);
  219. format = va_arg(va, char *);
  220. egg_vsnprintf(s, 511, format, va);
  221. va_end(va);
  222. len = strlen(s);
  223. if (len > 511)
  224. len = 511;
  225. s[len + 1] = 0;
  226. for (i = 0; i < dcc_total; i++)
  227. if ((dcc[i].type == &DCC_CHAT) && (i != x) && !(dcc[i].simul))
  228. if (dcc[i].u.chat->channel == chan)
  229. dprintf(i, "%s", s);
  230. }
  231. void dcc_chatter(int idx)
  232. {
  233. int i, j;
  234. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  235. get_user_flagrec(dcc[idx].user, &fr, NULL);
  236. dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
  237. show_banner(idx);
  238. show_motd(idx);
  239. if (glob_master(fr)) {
  240. if ((tands+1) > 1)
  241. dprintf(idx, STR("There are \002-%d- bots\002 currently linked.\n"), tands + 1);
  242. else
  243. dprintf(idx, STR("There is \002-%d- bot\002 currently linked.\n"), tands + 1);
  244. }
  245. show_channels(idx, NULL);
  246. if (glob_party(fr)) {
  247. i = dcc[idx].u.chat->channel;
  248. } else {
  249. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  250. i = -1;
  251. }
  252. j = dcc[idx].sock;
  253. strcpy(dcc[idx].u.chat->con_chan, "***");
  254. check_chon(dcc[idx].nick, idx);
  255. dcc[idx].u.chat->channel = 234567;
  256. /* Still there? */
  257. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  258. return; /* Nope */
  259. /* Tcl script may have taken control */
  260. if (dcc[idx].type == &DCC_CHAT) {
  261. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  262. strcpy(dcc[idx].u.chat->con_chan, "*");
  263. if (dcc[idx].u.chat->channel == 234567) {
  264. /* If the chat channel has already been altered it's *highly*
  265. * probably join/part messages have been broadcast everywhere,
  266. * so dont bother sending them
  267. */
  268. if (i == -2)
  269. i = 0;
  270. dcc[idx].u.chat->channel = i;
  271. if (dcc[idx].u.chat->channel >= 0) {
  272. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  273. botnet_send_join_idx(idx, -1);
  274. }
  275. }
  276. check_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  277. geticon(idx), dcc[idx].sock, dcc[idx].host);
  278. }
  279. /* But *do* bother with sending it locally */
  280. if (!dcc[idx].u.chat->channel) {
  281. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  282. } else if (dcc[idx].u.chat->channel > 0) {
  283. chanout_but(-1, dcc[idx].u.chat->channel,
  284. "*** %s joined the channel.\n", dcc[idx].nick);
  285. }
  286. }
  287. }
  288. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  289. * removed from the dcc list in the main loop.
  290. */
  291. void lostdcc(int n)
  292. {
  293. /* Make sure it's a valid dcc index. */
  294. if (n < 0 || n >= max_dcc) return;
  295. if (dcc[n].type && dcc[n].type->kill)
  296. dcc[n].type->kill(n, dcc[n].u.other);
  297. else if (dcc[n].u.other)
  298. nfree(dcc[n].u.other);
  299. egg_bzero(&dcc[n], sizeof(struct dcc_t));
  300. dcc[n].sock = (-1);
  301. dcc[n].type = &DCC_LOST;
  302. }
  303. /* Remove entry from dcc list. Think twice before using this function,
  304. * because it invalidates any variables that point to a specific dcc
  305. * entry!
  306. *
  307. * Note: The entry will be deconstructed if it was not deconstructed
  308. * already. This case should normally not occur.
  309. */
  310. void removedcc(int n)
  311. {
  312. if (dcc[n].type && dcc[n].type->kill)
  313. dcc[n].type->kill(n, dcc[n].u.other);
  314. else if (dcc[n].u.other)
  315. nfree(dcc[n].u.other);
  316. dcc_total--;
  317. if (n < dcc_total)
  318. egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
  319. else
  320. egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
  321. }
  322. /* Clean up sockets that were just left for dead.
  323. */
  324. void dcc_remove_lost(void)
  325. {
  326. int i;
  327. for (i = 0; i < dcc_total; i++) {
  328. if (dcc[i].type == &DCC_LOST) {
  329. dcc[i].type = NULL;
  330. dcc[i].sock = (-1);
  331. removedcc(i);
  332. i--;
  333. }
  334. }
  335. }
  336. /* Show list of current dcc's to a dcc-chatter
  337. * positive value: idx given -- negative value: sock given
  338. */
  339. void tell_dcc(int zidx)
  340. {
  341. int i, j;
  342. char other[160];
  343. char format[81];
  344. int nicklen;
  345. /* calculate max nicklen */
  346. nicklen = 0;
  347. for (i = 0; i < dcc_total; i++) {
  348. if(strlen(dcc[i].nick) > nicklen)
  349. nicklen = strlen(dcc[i].nick);
  350. }
  351. if(nicklen < 9) nicklen = 9;
  352. egg_snprintf(format, sizeof format, "%%-4s %%-8s %%-5s %%-%us %%-40s %%s\n",
  353. nicklen);
  354. dprintf(zidx, format, "SOCK", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  355. dprintf(zidx, format, "----", "--------", "-----", "---------",
  356. "----------------------------------------", "----");
  357. egg_snprintf(format, sizeof format, "%%-4d %%08X %%5d %%-%us %%-40s %%s\n",
  358. nicklen);
  359. /* Show server */
  360. for (i = 0; i < dcc_total; i++) {
  361. j = strlen(dcc[i].host);
  362. if (j > 40)
  363. j -= 40;
  364. else
  365. j = 0;
  366. if (dcc[i].type && dcc[i].type->display)
  367. dcc[i].type->display(i, other);
  368. else {
  369. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  370. break;
  371. }
  372. dprintf(zidx, format, dcc[i].sock, dcc[i].addr, dcc[i].port, dcc[i].nick,
  373. dcc[i].host + j, other);
  374. }
  375. }
  376. /* Mark someone on dcc chat as no longer away
  377. */
  378. void not_away(int idx)
  379. {
  380. if (dcc[idx].u.chat->away == NULL) {
  381. dprintf(idx, "You weren't away!\n");
  382. return;
  383. }
  384. if (dcc[idx].u.chat->channel >= 0) {
  385. chanout_but(-1, dcc[idx].u.chat->channel,
  386. "*** %s is no longer away.\n", dcc[idx].nick);
  387. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  388. botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
  389. }
  390. }
  391. dprintf(idx, "You're not away any more.\n");
  392. nfree(dcc[idx].u.chat->away);
  393. dcc[idx].u.chat->away = NULL;
  394. check_away(botnetnick, dcc[idx].sock, NULL);
  395. }
  396. void set_away(int idx, char *s)
  397. {
  398. if (s == NULL) {
  399. not_away(idx);
  400. return;
  401. }
  402. if (!s[0]) {
  403. not_away(idx);
  404. return;
  405. }
  406. if (dcc[idx].u.chat->away != NULL)
  407. nfree(dcc[idx].u.chat->away);
  408. dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
  409. strcpy(dcc[idx].u.chat->away, s);
  410. if (dcc[idx].u.chat->channel >= 0) {
  411. chanout_but(-1, dcc[idx].u.chat->channel,
  412. "*** %s is now away: %s\n", dcc[idx].nick, s);
  413. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  414. botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
  415. }
  416. }
  417. dprintf(idx, "You are now away.\n");
  418. check_away(botnetnick, dcc[idx].sock, s);
  419. }
  420. /* This helps the memory debugging
  421. */
  422. void *_get_data_ptr(int size, char *file, int line)
  423. {
  424. char *p;
  425. #ifdef DEBUG_MEM
  426. char x[1024];
  427. p = strrchr(file, '/');
  428. egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
  429. p = n_malloc(size, x, line);
  430. #else
  431. p = nmalloc(size);
  432. #endif
  433. egg_bzero(p, size);
  434. return p;
  435. }
  436. /* Make a password, 10-15 random letters and digits
  437. */
  438. void makepass(char *s)
  439. {
  440. int i;
  441. i = 10 + (random() % 6);
  442. make_rand_str(s, i);
  443. }
  444. void flush_lines(int idx, struct chat_info *ci)
  445. {
  446. int c = ci->line_count;
  447. struct msgq *p = ci->buffer, *o;
  448. while (p && c < (ci->max_line)) {
  449. ci->current_lines--;
  450. tputs(dcc[idx].sock, p->msg, p->len);
  451. nfree(p->msg);
  452. o = p->next;
  453. nfree(p);
  454. p = o;
  455. c++;
  456. }
  457. if (p != NULL) {
  458. if (dcc[idx].status & STAT_TELNET)
  459. tputs(dcc[idx].sock, "[More]: ", 8);
  460. else
  461. tputs(dcc[idx].sock, "[More]\n", 7);
  462. }
  463. ci->buffer = p;
  464. ci->line_count = 0;
  465. }
  466. int new_dcc(struct dcc_table *type, int xtra_size)
  467. {
  468. int i = dcc_total;
  469. if (dcc_total == max_dcc)
  470. return -1;
  471. dcc_total++;
  472. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  473. dcc[i].type = type;
  474. if (xtra_size) {
  475. dcc[i].u.other = nmalloc(xtra_size);
  476. egg_bzero(dcc[i].u.other, xtra_size);
  477. }
  478. return i;
  479. }
  480. /* Changes the given dcc entry to another type.
  481. */
  482. void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  483. {
  484. /* Free old structure. */
  485. if (dcc[i].type && dcc[i].type->kill)
  486. dcc[i].type->kill(i, dcc[i].u.other);
  487. else if (dcc[i].u.other) {
  488. nfree(dcc[i].u.other);
  489. dcc[i].u.other = NULL;
  490. }
  491. dcc[i].type = type;
  492. if (xtra_size) {
  493. dcc[i].u.other = nmalloc(xtra_size);
  494. egg_bzero(dcc[i].u.other, xtra_size);
  495. }
  496. }
  497. int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  498. {
  499. time_t t;
  500. if (!dcc_flood_thr)
  501. return 0;
  502. t = now;
  503. if (*timer != t) {
  504. *timer = t;
  505. chat->msgs_per_sec = 0;
  506. } else {
  507. chat->msgs_per_sec++;
  508. if (chat->msgs_per_sec > dcc_flood_thr) {
  509. /* FLOOD */
  510. dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
  511. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  512. if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
  513. (chat->channel >= 0)) {
  514. char x[1024];
  515. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  516. chanout_but(idx, chat->channel, "*** %s", x);
  517. if (chat->channel < GLOBAL_CHANS)
  518. botnet_send_part_idx(idx, x);
  519. }
  520. check_chof(dcc[idx].nick, idx);
  521. if ((dcc[idx].sock != STDOUT) || backgrd) {
  522. killsock(dcc[idx].sock);
  523. lostdcc(idx);
  524. } else {
  525. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  526. dcc_chatter(idx);
  527. }
  528. return 1; /* <- flood */
  529. }
  530. }
  531. return 0;
  532. }
  533. /* Handle someone being booted from dcc chat.
  534. */
  535. void do_boot(int idx, char *by, char *reason)
  536. {
  537. int files = (dcc[idx].type != &DCC_CHAT);
  538. dprintf(idx, DCC_BOOTED1);
  539. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot",
  540. by, reason[0] ? ": " : ".", reason);
  541. /* If it's a partyliner (chatterer :) */
  542. /* Horrible assumption that DCT_CHAT using structure uses same format
  543. * as DCC_CHAT */
  544. if ((dcc[idx].type->flags & DCT_CHAT) &&
  545. (dcc[idx].u.chat->channel >= 0)) {
  546. char x[1024];
  547. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick,
  548. reason[0] ? ": " : "", reason);
  549. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  550. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  551. botnet_send_part_idx(idx, x);
  552. }
  553. check_chof(dcc[idx].nick, idx);
  554. if ((dcc[idx].sock != STDOUT) || backgrd) {
  555. killsock(dcc[idx].sock);
  556. lostdcc(idx);
  557. /* Entry must remain in the table so it can be logged by the caller */
  558. } else {
  559. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  560. dcc_chatter(idx);
  561. }
  562. return;
  563. }
  564. int listen_all(int lport, int off)
  565. {
  566. int i,
  567. idx = (-1),
  568. port,
  569. #ifdef USE_IPV6
  570. i6 = 0,
  571. #endif /* USE_IPV6 */
  572. realport;
  573. struct portmap *pmap = NULL,
  574. *pold = NULL;
  575. port = realport = lport;
  576. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  577. if (pmap->realport == port) {
  578. port = pmap->mappedto;
  579. break;
  580. }
  581. for (i = 0; i < dcc_total; i++) {
  582. if ((dcc[i].type == &DCC_TELNET) && (dcc[i].port == port)) {
  583. idx = i;
  584. if (off) {
  585. if (pmap) {
  586. if (pold)
  587. pold->next = pmap->next;
  588. else
  589. root = pmap->next;
  590. nfree(pmap);
  591. }
  592. #ifdef USE_IPV6
  593. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  594. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  595. else
  596. #endif /* USE_IPV6 */
  597. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  598. killsock(dcc[idx].sock);
  599. lostdcc(idx);
  600. return idx;
  601. }
  602. }
  603. }
  604. if (idx < 0) {
  605. if (off) {
  606. putlog(LOG_ERRORS, "*", STR("No such listening port open - %d"), lport);
  607. return idx;
  608. }
  609. /* make new one */
  610. if (dcc_total >= max_dcc) {
  611. putlog(LOG_ERRORS, "*", STR("Can't open listening port - no more DCC Slots"));
  612. } else {
  613. #ifdef USE_IPV6
  614. i6 = open_listen_by_af(&port, AF_INET6);
  615. if (i6 < 0)
  616. putlog(LOG_ERRORS, "*", STR("Can't open IPv6 listening port %d - %s"), port,
  617. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  618. else {
  619. idx = new_dcc(&DCC_TELNET, 0);
  620. dcc[idx].addr = notalloc;
  621. strcpy(dcc[idx].addr6, myipstr(6));
  622. dcc[idx].port = port;
  623. dcc[idx].sock = i6;
  624. dcc[idx].timeval = now;
  625. strcpy(dcc[idx].nick, STR("(telnet6)"));
  626. strcpy(dcc[idx].host, "*");
  627. putlog(LOG_DEBUG, "*", STR("Listening on IPv6 at telnet port %d"), port);
  628. }
  629. i = open_listen_by_af(&port, AF_INET);
  630. #else
  631. i = open_listen(&port);
  632. #endif /* USE_IPV6 */
  633. if (i < 0)
  634. putlog(LOG_ERRORS, "*", STR("Can't open IPv4 listening port %d - %s"), port,
  635. i == -1 ? "it's taken." : "couldn't assign ip.");
  636. else {
  637. idx = (-1); /* now setup ipv4 listening port */
  638. idx = new_dcc(&DCC_TELNET, 0);
  639. dcc[idx].addr = iptolong(getmyip());
  640. dcc[idx].port = port;
  641. dcc[idx].sock = i;
  642. dcc[idx].timeval = now;
  643. strcpy(dcc[idx].nick, STR("(telnet)"));
  644. strcpy(dcc[idx].host, "*");
  645. putlog(LOG_DEBUG, "*", STR("Listening on IPv4 at telnet port %d"), port);
  646. }
  647. #ifdef USE_IPV6
  648. if (i > 0 || i6 > 0) {
  649. #else
  650. if (i > 0) {
  651. #endif /* USE_IPV6 */
  652. if (!pmap) {
  653. pmap = nmalloc(sizeof(struct portmap));
  654. pmap->next = root;
  655. root = pmap;
  656. }
  657. pmap->realport = realport;
  658. pmap->mappedto = port;
  659. }
  660. }
  661. }
  662. /* if one of the protocols failed, the one which worked will be returned
  663. * if both were successful, it wont matter which idx is returned, because the
  664. * code reading listen_all will only be reading dcc[idx].port, which would be
  665. * open on both protocols.
  666. * -bryan (10/29/03)
  667. */
  668. return idx;
  669. }