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. 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 = realloc(dcc, sizeof(struct dcc_t) * max_dcc);
  38. else
  39. dcc = malloc(sizeof(struct dcc_t) * max_dcc);
  40. MAXSOCKS = max_dcc + 10;
  41. if (socklist)
  42. socklist = (sock_list *) realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  43. else
  44. socklist = (sock_list *) malloc(sizeof(sock_list) * MAXSOCKS);
  45. for (; osock < MAXSOCKS; osock++)
  46. socklist[osock].flags = SOCK_UNUSED;
  47. }
  48. /* Replace \n with \r\n */
  49. char *add_cr(char *buf)
  50. {
  51. static char WBUF[1024];
  52. char *p, *q;
  53. for (p = buf, q = WBUF; *p; p++, q++) {
  54. if (*p == '\n')
  55. *q++ = '\r';
  56. *q = *p;
  57. }
  58. *q = *p;
  59. return WBUF;
  60. }
  61. extern void (*qserver) (int, char *, int);
  62. void dprintf EGG_VARARGS_DEF(int, arg1)
  63. {
  64. static char buf[1024];
  65. char *format;
  66. int idx, len;
  67. va_list va;
  68. idx = EGG_VARARGS_START(int, arg1, va);
  69. format = va_arg(va, char *);
  70. egg_vsnprintf(buf, 1023, format, va);
  71. va_end(va);
  72. /* We can not use the return value vsnprintf() to determine where
  73. * to null terminate. The C99 standard specifies that vsnprintf()
  74. * shall return the number of bytes that would be written if the
  75. * buffer had been large enough, rather then -1.
  76. */
  77. /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
  78. * sizeof(buf) bytes. But we're not doing that anyway.
  79. */
  80. buf[sizeof(buf)-1] = 0;
  81. len = strlen(buf);
  82. /* this is for color on dcc :P */
  83. if (idx < 0) {
  84. tputs(-idx, buf, len);
  85. } else if (idx > 0x7FF0) {
  86. switch (idx) {
  87. case DP_LOG:
  88. putlog(LOG_MISC, "*", "%s", buf);
  89. break;
  90. case DP_STDOUT:
  91. tputs(STDOUT, buf, len);
  92. break;
  93. case DP_STDERR:
  94. tputs(STDERR, buf, len);
  95. break;
  96. case DP_SERVER:
  97. #ifdef HUB
  98. return;
  99. #endif /* HUB */
  100. case DP_HELP:
  101. #ifdef HUB
  102. return;
  103. #endif /* HUB */
  104. case DP_MODE:
  105. #ifdef HUB
  106. return;
  107. #endif /* HUB */
  108. case DP_MODE_NEXT:
  109. #ifdef HUB
  110. return;
  111. #endif /* HUB */
  112. case DP_SERVER_NEXT:
  113. #ifdef HUB
  114. return;
  115. #endif /* HUB */
  116. case DP_HELP_NEXT:
  117. #ifdef HUB
  118. return;
  119. #endif /* HUB */
  120. qserver(idx, buf, len);
  121. break;
  122. }
  123. return;
  124. } else { /* normal chat text */
  125. if ((dcc[idx].status & STAT_COLOR) && (dcc[idx].type == &DCC_CHAT)) {
  126. int i;
  127. char buf3[1024], buf2[1024], c;
  128. buf3[0] = buf2[0] = 0;
  129. for (i = 0 ; i < len ; i++) {
  130. /* FIXME: Trying to fix bug where you do .color on ANSI, then .bc <bot> help help OVER TELNET
  131. if (buf[i] == '\033') {
  132. unsigned char *e;
  133. for (e = buf + 2; *e != 'm' && *e; e++)
  134. i++;
  135. if (i >= len) break;
  136. }
  137. */
  138. c = buf[i];
  139. buf2[0] = 0;
  140. if (c == ':') {
  141. sprintf(buf2, "%s%c%s", LIGHTGREY(idx), c, COLOR_END(idx));
  142. } else if (c == '@') {
  143. sprintf(buf2, "%s%c%s", BOLD(idx), c, BOLD_END(idx));
  144. // } else if (c == ']' || c == '>' || c == ')' || c == '[' || c == '<' || c == '(') {
  145. } else if (c == '>' || c == ')' || c == '<' || c == '(') {
  146. sprintf(buf2, "%s%c%s", GREEN(idx), c, COLOR_END(idx));
  147. } else {
  148. sprintf(buf2, "%c", c);
  149. }
  150. // sprintf(buf3, "%s%s", buf3 ? buf3 : "", buf2 ? buf2 : "");
  151. sprintf(buf3, "%s%s", (buf3 && buf3[0]) ? buf3 : "", (buf2 && buf2[0]) ? buf2 : "");
  152. }
  153. buf3[strlen(buf3)] = 0;
  154. strcpy(buf, buf3);
  155. // strncpyz(buf, buf2, sizeof buf);
  156. }
  157. buf[sizeof(buf) - 1] = 0;
  158. len = strlen(buf);
  159. if (len > 1000) { /* Truncate to fit */
  160. buf[1000] = 0;
  161. strcat(buf, "\n");
  162. len = 1001;
  163. }
  164. if (dcc[idx].simul > 0) {
  165. bounce_simul(idx, buf);
  166. } else {
  167. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  168. char *p = add_cr(buf);
  169. tputs(dcc[idx].sock, p, strlen(p));
  170. } else if (dcc[idx].type && dcc[idx].type->output) {
  171. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  172. } else {
  173. tputs(dcc[idx].sock, buf, len);
  174. }
  175. }
  176. }
  177. }
  178. void chatout EGG_VARARGS_DEF(char *, arg1)
  179. {
  180. int i, len;
  181. char *format;
  182. char s[601];
  183. va_list va;
  184. format = EGG_VARARGS_START(char *, arg1, va);
  185. egg_vsnprintf(s, 511, format, va);
  186. va_end(va);
  187. len = strlen(s);
  188. if (len > 511)
  189. len = 511;
  190. s[len + 1] = 0;
  191. for (i = 0; i < dcc_total; i++)
  192. if ((dcc[i].type == &DCC_CHAT) && !(dcc[i].simul))
  193. if (dcc[i].u.chat->channel >= 0)
  194. dprintf(i, "%s", s);
  195. }
  196. /* Print to all on this channel but one.
  197. */
  198. void chanout_but EGG_VARARGS_DEF(int, arg1)
  199. {
  200. int i, x, chan, len;
  201. char *format;
  202. char s[601];
  203. va_list va;
  204. x = EGG_VARARGS_START(int, arg1, va);
  205. chan = va_arg(va, int);
  206. format = va_arg(va, char *);
  207. egg_vsnprintf(s, 511, format, va);
  208. va_end(va);
  209. len = strlen(s);
  210. if (len > 511)
  211. len = 511;
  212. s[len + 1] = 0;
  213. for (i = 0; i < dcc_total; i++)
  214. if ((dcc[i].type == &DCC_CHAT) && (i != x) && !(dcc[i].simul))
  215. if (dcc[i].u.chat->channel == chan)
  216. dprintf(i, "%s", s);
  217. }
  218. void dcc_chatter(int idx)
  219. {
  220. int i, j;
  221. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  222. get_user_flagrec(dcc[idx].user, &fr, NULL);
  223. dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
  224. show_banner(idx);
  225. show_motd(idx);
  226. if (glob_master(fr)) {
  227. if ((tands+1) > 1)
  228. dprintf(idx, STR("There are \002-%d- bots\002 currently linked.\n"), tands + 1);
  229. else
  230. dprintf(idx, STR("There is \002-%d- bot\002 currently linked.\n"), tands + 1);
  231. }
  232. show_channels(idx, NULL);
  233. if (glob_party(fr)) {
  234. i = dcc[idx].u.chat->channel;
  235. } else {
  236. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  237. i = -1;
  238. }
  239. j = dcc[idx].sock;
  240. strcpy(dcc[idx].u.chat->con_chan, "***");
  241. check_chon(dcc[idx].nick, idx);
  242. dcc[idx].u.chat->channel = 234567;
  243. /* Still there? */
  244. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  245. return; /* Nope */
  246. /* Tcl script may have taken control */
  247. if (dcc[idx].type == &DCC_CHAT) {
  248. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  249. strcpy(dcc[idx].u.chat->con_chan, "*");
  250. if (dcc[idx].u.chat->channel == 234567) {
  251. /* If the chat channel has already been altered it's *highly*
  252. * probably join/part messages have been broadcast everywhere,
  253. * so dont bother sending them
  254. */
  255. if (i == -2)
  256. i = 0;
  257. dcc[idx].u.chat->channel = i;
  258. if (dcc[idx].u.chat->channel >= 0) {
  259. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  260. botnet_send_join_idx(idx, -1);
  261. }
  262. }
  263. check_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  264. geticon(idx), dcc[idx].sock, dcc[idx].host);
  265. }
  266. /* But *do* bother with sending it locally */
  267. if (!dcc[idx].u.chat->channel) {
  268. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  269. } else if (dcc[idx].u.chat->channel > 0) {
  270. chanout_but(-1, dcc[idx].u.chat->channel,
  271. "*** %s joined the channel.\n", dcc[idx].nick);
  272. }
  273. }
  274. }
  275. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  276. * removed from the dcc list in the main loop.
  277. */
  278. void lostdcc(int n)
  279. {
  280. /* Make sure it's a valid dcc index. */
  281. if (n < 0 || n >= max_dcc) return;
  282. if (dcc[n].type && dcc[n].type->kill)
  283. dcc[n].type->kill(n, dcc[n].u.other);
  284. else if (dcc[n].u.other)
  285. free(dcc[n].u.other);
  286. egg_bzero(&dcc[n], sizeof(struct dcc_t));
  287. dcc[n].sock = (-1);
  288. dcc[n].type = &DCC_LOST;
  289. }
  290. /* Remove entry from dcc list. Think twice before using this function,
  291. * because it invalidates any variables that point to a specific dcc
  292. * entry!
  293. *
  294. * Note: The entry will be deconstructed if it was not deconstructed
  295. * already. This case should normally not occur.
  296. */
  297. void removedcc(int n)
  298. {
  299. if (dcc[n].type && dcc[n].type->kill)
  300. dcc[n].type->kill(n, dcc[n].u.other);
  301. else if (dcc[n].u.other)
  302. free(dcc[n].u.other);
  303. dcc_total--;
  304. if (n < dcc_total)
  305. egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
  306. else
  307. egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
  308. }
  309. /* Clean up sockets that were just left for dead.
  310. */
  311. void dcc_remove_lost(void)
  312. {
  313. int i;
  314. for (i = 0; i < dcc_total; i++) {
  315. if (dcc[i].type == &DCC_LOST) {
  316. dcc[i].type = NULL;
  317. dcc[i].sock = (-1);
  318. removedcc(i);
  319. i--;
  320. }
  321. }
  322. }
  323. /* Show list of current dcc's to a dcc-chatter
  324. * positive value: idx given -- negative value: sock given
  325. */
  326. void tell_dcc(int zidx)
  327. {
  328. int i, j;
  329. char other[160];
  330. char format[81];
  331. int nicklen;
  332. /* calculate max nicklen */
  333. nicklen = 0;
  334. for (i = 0; i < dcc_total; i++) {
  335. if(strlen(dcc[i].nick) > nicklen)
  336. nicklen = strlen(dcc[i].nick);
  337. }
  338. if(nicklen < 9) nicklen = 9;
  339. egg_snprintf(format, sizeof format, "%%-4s %%-8s %%-5s %%-%us %%-40s %%s\n",
  340. nicklen);
  341. dprintf(zidx, format, "SOCK", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  342. dprintf(zidx, format, "----", "--------", "-----", "---------",
  343. "----------------------------------------", "----");
  344. egg_snprintf(format, sizeof format, "%%-4d %%08X %%5d %%-%us %%-40s %%s\n",
  345. nicklen);
  346. /* Show server */
  347. for (i = 0; i < dcc_total; i++) {
  348. j = strlen(dcc[i].host);
  349. if (j > 40)
  350. j -= 40;
  351. else
  352. j = 0;
  353. if (dcc[i].type && dcc[i].type->display)
  354. dcc[i].type->display(i, other);
  355. else {
  356. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  357. break;
  358. }
  359. dprintf(zidx, format, dcc[i].sock, dcc[i].addr, dcc[i].port, dcc[i].nick,
  360. dcc[i].host + j, other);
  361. }
  362. }
  363. /* Mark someone on dcc chat as no longer away
  364. */
  365. void not_away(int idx)
  366. {
  367. if (dcc[idx].u.chat->away == NULL) {
  368. dprintf(idx, "You weren't away!\n");
  369. return;
  370. }
  371. if (dcc[idx].u.chat->channel >= 0) {
  372. chanout_but(-1, dcc[idx].u.chat->channel,
  373. "*** %s is no longer away.\n", dcc[idx].nick);
  374. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  375. botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
  376. }
  377. }
  378. dprintf(idx, "You're not away any more.\n");
  379. free(dcc[idx].u.chat->away);
  380. dcc[idx].u.chat->away = NULL;
  381. check_away(botnetnick, dcc[idx].sock, NULL);
  382. }
  383. void set_away(int idx, char *s)
  384. {
  385. if (s == NULL) {
  386. not_away(idx);
  387. return;
  388. }
  389. if (!s[0]) {
  390. not_away(idx);
  391. return;
  392. }
  393. if (dcc[idx].u.chat->away != NULL)
  394. free(dcc[idx].u.chat->away);
  395. dcc[idx].u.chat->away = (char *) malloc(strlen(s) + 1);
  396. strcpy(dcc[idx].u.chat->away, 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_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_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_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. }