dccutil.c 19 KB

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