dccutil.c 19 KB

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