dccutil.c 18 KB

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