dccutil.c 18 KB

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