dccutil.c 19 KB

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