dccutil.c 18 KB

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