dccutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. && ((dcc[idx].status & STAT_COLORM) || (dcc[idx].status & STAT_COLORA))) {
  140. int i, a = 0, m = 0;
  141. char buf3[1024] = "", buf2[1024] = "", c;
  142. if (dcc[idx].status & STAT_COLORM)
  143. m = 1;
  144. else if (dcc[idx].status & STAT_COLORA)
  145. a = 1;
  146. buf3[0] = '\0';
  147. for (i = 0 ; i < len ; i++) {
  148. c = buf[i];
  149. buf2[0] = '\0';
  150. if (c == ':') {
  151. if (a)
  152. sprintf(buf2, "\e[%d;%dm%c\e[0m", 0, 37, c);
  153. else
  154. sprintf(buf2, "\003%d%c\003\002\002", 15, c);
  155. } else if (c == '@') {
  156. if (a)
  157. sprintf(buf2, "\e[1m%c\e[0m", c);
  158. else
  159. sprintf(buf2, "\002%c\002", c);
  160. } else if (c == ']' || c == '>' || c == ')') {
  161. if (a)
  162. sprintf(buf2, "\e[%d;%dm%c\e[0m", 0, 32, c);
  163. else
  164. sprintf(buf2, "\00303%c\003\002\002", c);
  165. } else if (c == '[' || c == '<' || c == '(') {
  166. if (a)
  167. sprintf(buf2, "\e[%d;%dm%c\e[0m", 0, 32, c);
  168. else
  169. sprintf(buf2, "\00303%c\003\002\002", c);
  170. } else {
  171. sprintf(buf2, "%c", c);
  172. }
  173. sprintf(buf3, "%s%s", buf3 ? buf3 : "", buf2 ? buf2 : "");
  174. }
  175. buf3[strlen(buf3)] = '\0';
  176. strcpy(buf, buf3);
  177. }
  178. buf[sizeof(buf)-1] = 0;
  179. len = strlen(buf);
  180. if (len > 1000) { /* Truncate to fit */
  181. buf[1000] = 0;
  182. strcat(buf, "\n");
  183. len = 1001;
  184. }
  185. if (dcc[idx].simul > 0) {
  186. bounce_simul(idx, buf);
  187. } else {
  188. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  189. char *p = add_cr(buf);
  190. tputs(dcc[idx].sock, p, strlen(p));
  191. } else if (dcc[idx].type && dcc[idx].type->output) {
  192. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  193. } else {
  194. tputs(dcc[idx].sock, buf, len);
  195. }
  196. }
  197. }
  198. }
  199. void chatout EGG_VARARGS_DEF(char *, arg1)
  200. {
  201. int i, len;
  202. char *format;
  203. char s[601];
  204. va_list va;
  205. format = EGG_VARARGS_START(char *, arg1, va);
  206. egg_vsnprintf(s, 511, format, va);
  207. va_end(va);
  208. len = strlen(s);
  209. if (len > 511)
  210. len = 511;
  211. s[len + 1] = 0;
  212. for (i = 0; i < dcc_total; i++)
  213. if ((dcc[i].type == &DCC_CHAT) && !(dcc[i].simul))
  214. if (dcc[i].u.chat->channel >= 0)
  215. dprintf(i, "%s", s);
  216. }
  217. /* Print to all on this channel but one.
  218. */
  219. void chanout_but EGG_VARARGS_DEF(int, arg1)
  220. {
  221. int i, x, chan, len;
  222. char *format;
  223. char s[601];
  224. va_list va;
  225. x = EGG_VARARGS_START(int, arg1, va);
  226. chan = va_arg(va, int);
  227. format = va_arg(va, char *);
  228. egg_vsnprintf(s, 511, format, va);
  229. va_end(va);
  230. len = strlen(s);
  231. if (len > 511)
  232. len = 511;
  233. s[len + 1] = 0;
  234. for (i = 0; i < dcc_total; i++)
  235. if ((dcc[i].type == &DCC_CHAT) && (i != x) && !(dcc[i].simul))
  236. if (dcc[i].u.chat->channel == chan)
  237. dprintf(i, "%s", s);
  238. }
  239. void dcc_chatter(int idx)
  240. {
  241. int i, j;
  242. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  243. get_user_flagrec(dcc[idx].user, &fr, NULL);
  244. dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
  245. show_banner(idx);
  246. show_motd(idx);
  247. if (glob_master(fr)) {
  248. if ((tands+1) > 1)
  249. dprintf(idx, STR("There are \002-%d- bots\002 currently linked.\n"), tands + 1);
  250. else
  251. dprintf(idx, STR("There is \002-%d- bot\002 currently linked.\n"), tands + 1);
  252. }
  253. show_channels(idx, NULL);
  254. if (glob_party(fr)) {
  255. i = dcc[idx].u.chat->channel;
  256. } else {
  257. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  258. i = -1;
  259. }
  260. j = dcc[idx].sock;
  261. strcpy(dcc[idx].u.chat->con_chan, "***");
  262. check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
  263. dcc[idx].u.chat->channel = 234567;
  264. /* Still there? */
  265. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  266. return; /* Nope */
  267. /* Tcl script may have taken control */
  268. if (dcc[idx].type == &DCC_CHAT) {
  269. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  270. strcpy(dcc[idx].u.chat->con_chan, "*");
  271. if (dcc[idx].u.chat->channel == 234567) {
  272. /* If the chat channel has already been altered it's *highly*
  273. * probably join/part messages have been broadcast everywhere,
  274. * so dont bother sending them
  275. */
  276. if (i == -2)
  277. i = 0;
  278. dcc[idx].u.chat->channel = i;
  279. if (dcc[idx].u.chat->channel >= 0) {
  280. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  281. botnet_send_join_idx(idx, -1);
  282. }
  283. }
  284. check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  285. geticon(idx), dcc[idx].sock, dcc[idx].host);
  286. }
  287. /* But *do* bother with sending it locally */
  288. if (!dcc[idx].u.chat->channel) {
  289. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  290. } else if (dcc[idx].u.chat->channel > 0) {
  291. chanout_but(-1, dcc[idx].u.chat->channel,
  292. "*** %s joined the channel.\n", dcc[idx].nick);
  293. }
  294. }
  295. }
  296. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  297. * removed from the dcc list in the main loop.
  298. */
  299. void lostdcc(int n)
  300. {
  301. Context;
  302. /* Make sure it's a valid dcc index. */
  303. if (n < 0 || n >= max_dcc) return;
  304. if (dcc[n].type && dcc[n].type->kill)
  305. dcc[n].type->kill(n, dcc[n].u.other);
  306. else if (dcc[n].u.other)
  307. nfree(dcc[n].u.other);
  308. egg_bzero(&dcc[n], sizeof(struct dcc_t));
  309. dcc[n].sock = (-1);
  310. dcc[n].type = &DCC_LOST;
  311. }
  312. /* Remove entry from dcc list. Think twice before using this function,
  313. * because it invalidates any variables that point to a specific dcc
  314. * entry!
  315. *
  316. * Note: The entry will be deconstructed if it was not deconstructed
  317. * already. This case should normally not occur.
  318. */
  319. void removedcc(int n)
  320. {
  321. Context;
  322. if (dcc[n].type && dcc[n].type->kill)
  323. dcc[n].type->kill(n, dcc[n].u.other);
  324. else if (dcc[n].u.other)
  325. nfree(dcc[n].u.other);
  326. dcc_total--;
  327. if (n < dcc_total)
  328. egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
  329. else
  330. egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
  331. }
  332. /* Clean up sockets that were just left for dead.
  333. */
  334. void dcc_remove_lost(void)
  335. {
  336. int i;
  337. for (i = 0; i < dcc_total; i++) {
  338. if (dcc[i].type == &DCC_LOST) {
  339. dcc[i].type = NULL;
  340. dcc[i].sock = (-1);
  341. removedcc(i);
  342. i--;
  343. }
  344. }
  345. }
  346. /* Show list of current dcc's to a dcc-chatter
  347. * positive value: idx given -- negative value: sock given
  348. */
  349. void tell_dcc(int zidx)
  350. {
  351. int i, j;
  352. char other[160];
  353. char format[81];
  354. int nicklen;
  355. /* calculate max nicklen */
  356. nicklen = 0;
  357. for (i = 0; i < dcc_total; i++) {
  358. if(strlen(dcc[i].nick) > nicklen)
  359. nicklen = strlen(dcc[i].nick);
  360. }
  361. if(nicklen < 9) nicklen = 9;
  362. egg_snprintf(format, sizeof format, "%%-4s %%-8s %%-5s %%-%us %%-40s %%s\n",
  363. nicklen);
  364. dprintf(zidx, format, "SOCK", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  365. dprintf(zidx, format, "----", "--------", "-----", "---------",
  366. "----------------------------------------", "----");
  367. egg_snprintf(format, sizeof format, "%%-4d %%08X %%5d %%-%us %%-40s %%s\n",
  368. nicklen);
  369. /* Show server */
  370. for (i = 0; i < dcc_total; i++) {
  371. j = strlen(dcc[i].host);
  372. if (j > 40)
  373. j -= 40;
  374. else
  375. j = 0;
  376. if (dcc[i].type && dcc[i].type->display)
  377. dcc[i].type->display(i, other);
  378. else {
  379. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  380. break;
  381. }
  382. dprintf(zidx, format, dcc[i].sock, dcc[i].addr, dcc[i].port, dcc[i].nick,
  383. dcc[i].host + j, other);
  384. }
  385. }
  386. /* Mark someone on dcc chat as no longer away
  387. */
  388. void not_away(int idx)
  389. {
  390. if (dcc[idx].u.chat->away == NULL) {
  391. dprintf(idx, "You weren't away!\n");
  392. return;
  393. }
  394. if (dcc[idx].u.chat->channel >= 0) {
  395. chanout_but(-1, dcc[idx].u.chat->channel,
  396. "*** %s is no longer away.\n", dcc[idx].nick);
  397. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  398. botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
  399. }
  400. }
  401. dprintf(idx, "You're not away any more.\n");
  402. nfree(dcc[idx].u.chat->away);
  403. dcc[idx].u.chat->away = NULL;
  404. check_tcl_away(botnetnick, dcc[idx].sock, NULL);
  405. }
  406. void set_away(int idx, char *s)
  407. {
  408. if (s == NULL) {
  409. not_away(idx);
  410. return;
  411. }
  412. if (!s[0]) {
  413. not_away(idx);
  414. return;
  415. }
  416. if (dcc[idx].u.chat->away != NULL)
  417. nfree(dcc[idx].u.chat->away);
  418. dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
  419. strcpy(dcc[idx].u.chat->away, s);
  420. if (dcc[idx].u.chat->channel >= 0) {
  421. chanout_but(-1, dcc[idx].u.chat->channel,
  422. "*** %s is now away: %s\n", dcc[idx].nick, s);
  423. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  424. botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
  425. }
  426. }
  427. dprintf(idx, "You are now away.\n");
  428. check_tcl_away(botnetnick, dcc[idx].sock, s);
  429. }
  430. /* This helps the memory debugging
  431. */
  432. void *_get_data_ptr(int size, char *file, int line)
  433. {
  434. char *p;
  435. #ifdef DEBUG_MEM
  436. char x[1024];
  437. p = strrchr(file, '/');
  438. egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
  439. p = n_malloc(size, x, line);
  440. #else
  441. p = nmalloc(size);
  442. #endif
  443. egg_bzero(p, size);
  444. return p;
  445. }
  446. /* Make a password, 10-15 random letters and digits
  447. */
  448. void makepass(char *s)
  449. {
  450. int i;
  451. i = 10 + (random() % 6);
  452. make_rand_str(s, i);
  453. }
  454. void flush_lines(int idx, struct chat_info *ci)
  455. {
  456. int c = ci->line_count;
  457. struct msgq *p = ci->buffer, *o;
  458. while (p && c < (ci->max_line)) {
  459. ci->current_lines--;
  460. tputs(dcc[idx].sock, p->msg, p->len);
  461. nfree(p->msg);
  462. o = p->next;
  463. nfree(p);
  464. p = o;
  465. c++;
  466. }
  467. if (p != NULL) {
  468. if (dcc[idx].status & STAT_TELNET)
  469. tputs(dcc[idx].sock, "[More]: ", 8);
  470. else
  471. tputs(dcc[idx].sock, "[More]\n", 7);
  472. }
  473. ci->buffer = p;
  474. ci->line_count = 0;
  475. }
  476. int new_dcc(struct dcc_table *type, int xtra_size)
  477. {
  478. int i = dcc_total;
  479. if (dcc_total == max_dcc)
  480. return -1;
  481. dcc_total++;
  482. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  483. dcc[i].type = type;
  484. if (xtra_size) {
  485. dcc[i].u.other = nmalloc(xtra_size);
  486. egg_bzero(dcc[i].u.other, xtra_size);
  487. }
  488. return i;
  489. }
  490. /* Changes the given dcc entry to another type.
  491. */
  492. void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  493. {
  494. Context;
  495. /* Free old structure. */
  496. if (dcc[i].type && dcc[i].type->kill)
  497. dcc[i].type->kill(i, dcc[i].u.other);
  498. else if (dcc[i].u.other) {
  499. nfree(dcc[i].u.other);
  500. dcc[i].u.other = NULL;
  501. }
  502. dcc[i].type = type;
  503. if (xtra_size) {
  504. dcc[i].u.other = nmalloc(xtra_size);
  505. egg_bzero(dcc[i].u.other, xtra_size);
  506. }
  507. }
  508. int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  509. {
  510. time_t t;
  511. if (!dcc_flood_thr)
  512. return 0;
  513. t = now;
  514. if (*timer != t) {
  515. *timer = t;
  516. chat->msgs_per_sec = 0;
  517. } else {
  518. chat->msgs_per_sec++;
  519. if (chat->msgs_per_sec > dcc_flood_thr) {
  520. /* FLOOD */
  521. dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
  522. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  523. if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
  524. (chat->channel >= 0)) {
  525. char x[1024];
  526. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  527. chanout_but(idx, chat->channel, "*** %s", x);
  528. if (chat->channel < GLOBAL_CHANS)
  529. botnet_send_part_idx(idx, x);
  530. }
  531. check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
  532. if ((dcc[idx].sock != STDOUT) || backgrd) {
  533. killsock(dcc[idx].sock);
  534. lostdcc(idx);
  535. } else {
  536. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  537. dcc_chatter(idx);
  538. }
  539. return 1; /* <- flood */
  540. }
  541. }
  542. return 0;
  543. }
  544. /* Handle someone being booted from dcc chat.
  545. */
  546. void do_boot(int idx, char *by, char *reason)
  547. {
  548. int files = (dcc[idx].type != &DCC_CHAT);
  549. Context;
  550. dprintf(idx, DCC_BOOTED1);
  551. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot",
  552. by, reason[0] ? ": " : ".", reason);
  553. /* If it's a partyliner (chatterer :) */
  554. /* Horrible assumption that DCT_CHAT using structure uses same format
  555. * as DCC_CHAT */
  556. if ((dcc[idx].type->flags & DCT_CHAT) &&
  557. (dcc[idx].u.chat->channel >= 0)) {
  558. char x[1024];
  559. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick,
  560. reason[0] ? ": " : "", reason);
  561. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  562. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  563. botnet_send_part_idx(idx, x);
  564. }
  565. check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
  566. if ((dcc[idx].sock != STDOUT) || backgrd) {
  567. killsock(dcc[idx].sock);
  568. lostdcc(idx);
  569. /* Entry must remain in the table so it can be logged by the caller */
  570. } else {
  571. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  572. dcc_chatter(idx);
  573. }
  574. return;
  575. }
  576. int listen_all(int lport, int off)
  577. {
  578. int i,
  579. idx = (-1),
  580. port,
  581. #ifdef USE_IPV6
  582. i6 = 0,
  583. #endif /* USE_IPV6 */
  584. realport;
  585. struct portmap *pmap = NULL,
  586. *pold = NULL;
  587. Context;
  588. port = realport = lport;
  589. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  590. if (pmap->realport == port) {
  591. port = pmap->mappedto;
  592. break;
  593. }
  594. for (i = 0; i < dcc_total; i++) {
  595. if ((dcc[i].type == &DCC_TELNET) && (dcc[i].port == port)) {
  596. idx = i;
  597. if (off) {
  598. if (pmap) {
  599. if (pold)
  600. pold->next = pmap->next;
  601. else
  602. root = pmap->next;
  603. nfree(pmap);
  604. }
  605. #ifdef USE_IPV6
  606. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  607. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  608. else
  609. #endif /* USE_IPV6 */
  610. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  611. killsock(dcc[idx].sock);
  612. lostdcc(idx);
  613. return idx;
  614. }
  615. }
  616. }
  617. if (idx < 0) {
  618. if (off) {
  619. putlog(LOG_ERRORS, "*", STR("No such listening port open - %d"), lport);
  620. return idx;
  621. }
  622. /* make new one */
  623. if (dcc_total >= max_dcc) {
  624. putlog(LOG_ERRORS, "*", STR("Can't open listening port - no more DCC Slots"));
  625. } else {
  626. #ifdef USE_IPV6
  627. i6 = open_listen_by_af(&port, AF_INET6);
  628. if (i6 < 0)
  629. putlog(LOG_ERRORS, "*", STR("Can't open IPv6 listening port %d - %s"), port,
  630. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  631. else {
  632. idx = new_dcc(&DCC_TELNET, 0);
  633. dcc[idx].addr = notalloc;
  634. strcpy(dcc[idx].addr6, myipstr(6));
  635. dcc[idx].port = port;
  636. dcc[idx].sock = i6;
  637. dcc[idx].timeval = now;
  638. strcpy(dcc[idx].nick, STR("(telnet6)"));
  639. strcpy(dcc[idx].host, "*");
  640. putlog(LOG_DEBUG, "*", STR("Listening on IPv6 at telnet port %d"), port);
  641. }
  642. i = open_listen_by_af(&port, AF_INET);
  643. #else
  644. i = open_listen(&port);
  645. #endif /* USE_IPV6 */
  646. if (i < 0)
  647. putlog(LOG_ERRORS, "*", STR("Can't open IPv4 listening port %d - %s"), port,
  648. i == -1 ? "it's taken." : "couldn't assign ip.");
  649. else {
  650. idx = (-1); /* now setup ipv4 listening port */
  651. idx = new_dcc(&DCC_TELNET, 0);
  652. dcc[idx].addr = iptolong(getmyip());
  653. dcc[idx].port = port;
  654. dcc[idx].sock = i;
  655. dcc[idx].timeval = now;
  656. strcpy(dcc[idx].nick, STR("(telnet)"));
  657. strcpy(dcc[idx].host, "*");
  658. putlog(LOG_DEBUG, "*", STR("Listening on IPv4 at telnet port %d"), port);
  659. }
  660. #ifdef USE_IPV6
  661. if (i > 0 || i6 > 0) {
  662. #else
  663. if (i > 0) {
  664. #endif /* USE_IPV6 */
  665. if (!pmap) {
  666. pmap = nmalloc(sizeof(struct portmap));
  667. pmap->next = root;
  668. root = pmap;
  669. }
  670. pmap->realport = realport;
  671. pmap->mappedto = port;
  672. }
  673. }
  674. }
  675. /* if one of the protocols failed, the one which worked will be returned
  676. * if both were successful, it wont matter which idx is returned, because the
  677. * code reading listen_all will only be reading dcc[idx].port, which would be
  678. * open on both protocols.
  679. * -bryan (10/29/03)
  680. */
  681. return idx;
  682. }