dccutil.c 19 KB

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