dccutil.c 19 KB

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