port.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2017 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <netdb.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <fcntl.h>
  31. #define CALL_TIMEOUT 5
  32. DO_COMMAND(do_port)
  33. {
  34. char cmd[BUFFER_SIZE], arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  35. int cnt;
  36. arg = get_arg_in_braces(ses, arg, cmd, GET_ONE);
  37. if (*cmd == 0)
  38. {
  39. tintin_header(ses, " PORT OPTIONS ");
  40. for (cnt = 0 ; *port_table[cnt].name != 0 ; cnt++)
  41. {
  42. tintin_printf2(ses, " [%-13s] %s", port_table[cnt].name, port_table[cnt].desc);
  43. }
  44. tintin_header(ses, "");
  45. return ses;
  46. }
  47. for (cnt = 0 ; *port_table[cnt].name != 0 ; cnt++)
  48. {
  49. if (!is_abbrev(cmd, port_table[cnt].name))
  50. {
  51. continue;
  52. }
  53. if (port_table[cnt].fun != port_initialize && ses->port == NULL)
  54. {
  55. tintin_printf(ses, "#PORT: You must initialize a port first.");
  56. return ses;
  57. }
  58. arg = sub_arg_in_braces(ses, arg, arg1, port_table[cnt].lval, SUB_VAR|SUB_FUN);
  59. arg = sub_arg_in_braces(ses, arg, arg2, port_table[cnt].rval, SUB_VAR|SUB_FUN);
  60. ses = port_table[cnt].fun(ses, arg1, arg2, arg);
  61. return ses;
  62. }
  63. do_port(ses, "");
  64. return ses;
  65. }
  66. DO_PORT(port_initialize)
  67. {
  68. char temp[BUFFER_SIZE], file[BUFFER_SIZE];
  69. struct sockaddr_in sa;
  70. struct linger ld;
  71. int sock, port, reuse = 1;
  72. arg = sub_arg_in_braces(ses, arg, file, GET_ONE, SUB_VAR|SUB_FUN);
  73. if (*arg1 == 0 || *arg2 == 0)
  74. {
  75. show_error(ses, LIST_COMMAND, "#SYNTAX: #PORT INITIALIZE {NAME} {PORT} {FILE}");
  76. return ses;
  77. }
  78. if (find_session(arg1))
  79. {
  80. tintin_printf(ses, "#PORT INITIALIZE: THERE'S A SESSION NAMED {%s} ALREADY.", arg1);
  81. return ses;
  82. }
  83. if (!is_number(arg2))
  84. {
  85. tintin_printf(ses, "#PORT INITIALIZE: {%s} IS NOT A VALID PORT NUMBER.", arg2);
  86. return ses;
  87. }
  88. tintin_printf(ses, "#TRYING TO LAUNCH '%s' ON PORT '%s'.", arg1, arg2);
  89. sprintf(temp, "{localport} {%d} {%s}", atoi(arg2), file);
  90. port = atoi(arg2);
  91. sa.sin_family = AF_INET;
  92. sa.sin_addr.s_addr = INADDR_ANY;
  93. sa.sin_port = htons(port);
  94. sock = socket(AF_INET, SOCK_STREAM, 0);
  95. if (sock < 0)
  96. {
  97. syserr_printf(ses, "port_initialize: socket");
  98. return ses;
  99. }
  100. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) == -1)
  101. {
  102. syserr_printf(ses, "port_initialize: setsockopt");
  103. return ses;
  104. }
  105. ld.l_onoff = 0;
  106. ld.l_linger = 100;
  107. setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *) &ld, sizeof(ld));
  108. if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  109. {
  110. syserr_printf(ses, "port_initialize: fcntl O_NDELAY|O_NONBLOCK");
  111. return ses;
  112. }
  113. if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
  114. {
  115. tintin_printf(NULL, "#PORT INITIALIZE: PORT %d IS ALREADY IN USE.", port);
  116. close(sock);
  117. return ses;
  118. }
  119. if (listen(sock, 32) == -1)
  120. {
  121. syserr_printf(ses, "port_initialize: listen");
  122. close(sock);
  123. return ses;
  124. }
  125. // kind of dodgy, but should be a mere formality.
  126. ses = new_session(ses, arg1, temp, -1, 0);
  127. ses->port = (struct port_data *) calloc(1, sizeof(struct port_data));
  128. ses->port->fd = sock;
  129. ses->port->port = port;
  130. ses->port->name = strdup(arg1);
  131. ses->port->group = strdup("");
  132. ses->port->color = strdup("\e[0;1;36m");
  133. ses->port->ip = strdup("<Unknown>");
  134. ses->port->prefix = strdup("<PORT> ");
  135. tintin_printf(ses, "#PORT INITIALIZE: SESSION {%s} IS LISTENING ON PORT %d.", ses->name, ses->port->port);
  136. return ses;
  137. }
  138. DO_PORT(port_uninitialize)
  139. {
  140. int port = ses->port->port;
  141. while (ses->port->next)
  142. {
  143. close_port(ses, ses->port->next, TRUE);
  144. }
  145. close_port(ses, ses->port, FALSE);
  146. ses->port = NULL;
  147. tintin_printf(ses, "#PORT UNINITIALIZE: CLOSED PORT {%d}.", port);
  148. return ses;
  149. }
  150. int port_new(struct session *ses, int sock)
  151. {
  152. struct port_data *new_buddy;
  153. struct sockaddr_in sock_addr;
  154. socklen_t len;
  155. int fd;
  156. push_call("port_new(%p,%d)",ses,sock);
  157. len = sizeof(sock);
  158. getsockname(sock, (struct sockaddr *) &sock_addr, &len);
  159. if ((fd = accept(sock, (struct sockaddr *) &sock_addr, &len)) < 0)
  160. {
  161. syserr_printf(ses, "port_new: accept");
  162. pop_call();
  163. return -1;
  164. }
  165. if (fcntl(fd, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  166. {
  167. syserr_printf(ses, "port_new: fcntl O_NDELAY|O_NONBLOCK");
  168. }
  169. if (HAS_BIT(ses->port->flags, PORT_FLAG_DND))
  170. {
  171. close(fd);
  172. pop_call();
  173. return -1;
  174. }
  175. if (HAS_BIT(ses->port->flags, PORT_FLAG_PRIVATE))
  176. {
  177. if (strcmp(inet_ntoa(sock_addr.sin_addr), "127.0.0.1"))
  178. {
  179. port_printf(ses, "%s D%d Refusing remote connection, private flag set.", inet_ntoa(sock_addr.sin_addr), fd);
  180. close(fd);
  181. pop_call();
  182. return -1;
  183. }
  184. }
  185. new_buddy = (struct port_data *) calloc(1, sizeof(struct port_data));
  186. new_buddy->fd = fd;
  187. new_buddy->name = strdup(ntos(fd));
  188. new_buddy->group = strdup("");
  189. new_buddy->ip = strdup(inet_ntoa(sock_addr.sin_addr));
  190. new_buddy->prefix = strdup("");
  191. new_buddy->color = strdup("");
  192. new_buddy->proxy = strdup("");
  193. new_buddy->ttype = strdup("");
  194. new_buddy->port = 0;
  195. ses->port->total++;
  196. LINK(new_buddy, ses->port->next, ses->port->prev);
  197. port_printf(ses, "New connection: %s D%d.", new_buddy->ip, new_buddy->fd);
  198. if (HAS_BIT(ses->flags, SES_FLAG_TELNET))
  199. {
  200. announce_support(ses, new_buddy);
  201. }
  202. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 3, "PORT CONNECTION", new_buddy->name, new_buddy->ip, ntos(new_buddy->port));
  203. pop_call();
  204. return 0;
  205. }
  206. void close_port(struct session *ses, struct port_data *buddy, int unlink)
  207. {
  208. buddy->flags = 0;
  209. push_call("close_port(%p,%p,%d)",ses,buddy,unlink);
  210. if (unlink)
  211. {
  212. ses->port->total--;
  213. if (buddy == ses->port->update)
  214. {
  215. ses->port->update = buddy->next;
  216. }
  217. UNLINK(buddy, ses->port->next, ses->port->prev);
  218. }
  219. SET_BIT(buddy->flags, PORT_FLAG_LINKLOST);
  220. if (buddy != ses->port)
  221. {
  222. if (*buddy->name == 0)
  223. {
  224. port_printf(ses, "Closing connection to %s D%d", buddy->ip, buddy->fd);
  225. }
  226. else
  227. {
  228. port_printf(ses, "Closing connection to %s@%s D%d.", buddy->name, buddy->ip, buddy->fd);
  229. }
  230. }
  231. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 3, "PORT DISCONNECTION", buddy->name, buddy->ip, ntos(buddy->port));
  232. if (close(buddy->fd) == -1)
  233. {
  234. syserr_printf(ses, "close_port: close");
  235. }
  236. end_mccp2(ses, buddy);
  237. end_mccp3(ses, buddy);
  238. free(buddy->group);
  239. free(buddy->ip);
  240. free(buddy->name);
  241. free(buddy->prefix);
  242. free(buddy->color);
  243. free(buddy);
  244. pop_call();
  245. return;
  246. }
  247. void process_port_connections(struct session *ses, fd_set *read_set, fd_set *write_set, fd_set *exc_set)
  248. {
  249. struct port_data *buddy;
  250. push_call("process_port_connections(%p,%p,%p)",read_set,write_set,exc_set);
  251. if (FD_ISSET(ses->port->fd, read_set))
  252. {
  253. port_new(ses, ses->port->fd);
  254. }
  255. for (buddy = ses->port->next ; buddy ; buddy = ses->port->update)
  256. {
  257. ses->port->update = buddy->next;
  258. if (HAS_BIT(buddy->flags, PORT_FLAG_LINKLOST) || FD_ISSET(buddy->fd, exc_set))
  259. {
  260. FD_CLR(buddy->fd, write_set);
  261. FD_CLR(buddy->fd, read_set);
  262. close_port(ses, buddy, TRUE);
  263. }
  264. else if (FD_ISSET(buddy->fd, read_set))
  265. {
  266. if (process_port_input(ses, buddy) < 0)
  267. {
  268. FD_CLR(buddy->fd, write_set);
  269. FD_CLR(buddy->fd, read_set);
  270. close_port(ses, buddy, TRUE);
  271. }
  272. }
  273. if (HAS_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE))
  274. {
  275. msdp_send_update(ses, buddy);
  276. }
  277. }
  278. pop_call();
  279. return;
  280. }
  281. void port_socket_write(struct session *ses, struct port_data *buddy, char *str, int len)
  282. {
  283. if (!HAS_BIT(buddy->flags, PORT_FLAG_LINKLOST))
  284. {
  285. if (buddy->mccp2)
  286. {
  287. write_mccp2(ses, buddy, str, len);
  288. }
  289. else
  290. {
  291. if (write(buddy->fd, str, len) < 0)
  292. {
  293. port_printf(ses, "Lost link to socket '%s'.", buddy->name);
  294. SET_BIT(buddy->flags, PORT_FLAG_LINKLOST);
  295. }
  296. }
  297. }
  298. }
  299. void port_socket_printf(struct session *ses, struct port_data *buddy, char *format, ...)
  300. {
  301. char buf[BUFFER_SIZE];
  302. va_list args;
  303. va_start(args, format);
  304. vsnprintf(buf, BUFFER_SIZE / 3, format, args);
  305. va_end(args);
  306. port_socket_write(ses, buddy, buf, strlen(buf));
  307. }
  308. void port_telnet_printf(struct session *ses, struct port_data *buddy, size_t length, char *format, ...)
  309. {
  310. size_t size;
  311. char buf[BUFFER_SIZE];
  312. va_list args;
  313. va_start(args, format);
  314. size = vsprintf(buf, format, args);
  315. va_end(args);
  316. if (size != length && HAS_BIT(ses->telopts, TELOPT_FLAG_DEBUG))
  317. {
  318. tintin_printf(ses, "DEBUG TELNET: port_telnet_printf size difference: %d vs %d", size, length);
  319. }
  320. port_socket_write(ses, buddy, buf, length);
  321. }
  322. void port_printf(struct session *ses, char *format, ...)
  323. {
  324. char buf[STRING_SIZE], tmp[STRING_SIZE];
  325. va_list args;
  326. va_start(args, format);
  327. vsnprintf(buf, BUFFER_SIZE / 3, format, args);
  328. va_end(args);
  329. sprintf(tmp, "%s%s", ses->port->prefix, buf);
  330. strip_vt102_codes_non_graph(tmp, buf);
  331. sprintf(tmp, "%s%s%s", ses->port->color, buf, "\e[0m");
  332. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "PORT MESSAGE", tmp, buf);
  333. if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "CATCH PORT MESSAGE", tmp, buf))
  334. {
  335. tintin_puts(ses, tmp);
  336. }
  337. }
  338. void port_log_printf(struct session *ses, struct port_data *buddy, char *format, ...)
  339. {
  340. char buf[STRING_SIZE], tmp[STRING_SIZE];
  341. va_list args;
  342. va_start(args, format);
  343. vsnprintf(buf, BUFFER_SIZE / 3, format, args);
  344. va_end(args);
  345. sprintf(tmp, "%s%s@%s %s", ses->port->prefix, buddy->name, buddy->ip, buf);
  346. strip_vt102_codes_non_graph(tmp, buf);
  347. sprintf(tmp, "%s%s\e[0m", ses->port->color, buf);
  348. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 5, "PORT LOG MESSAGE", buddy->name, buddy->ip, ntos(buddy->fd), tmp, buf);
  349. if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 5, "CATCH PORT LOG MESSAGE", buddy->name, buddy->ip, ntos(buddy->fd), tmp, buf))
  350. {
  351. tintin_puts(ses, tmp);
  352. }
  353. }
  354. int process_port_input(struct session *ses, struct port_data *buddy)
  355. {
  356. char input[BUFFER_SIZE], *pt1, *pt2;
  357. int size, echo;
  358. push_call("process_port_input(%p)",buddy);
  359. // while (TRUE)
  360. {
  361. size = read(buddy->fd, input, BUFFER_SIZE / 4);
  362. if (size < 0)
  363. {
  364. if (errno == EWOULDBLOCK)
  365. {
  366. pop_call();
  367. return 0;
  368. }
  369. syserr_printf(ses, "process_port_input: read:");
  370. pop_call();
  371. return -1;
  372. }
  373. if (size == 0)
  374. {
  375. pop_call();
  376. return -1;
  377. }
  378. input[size] = 0;
  379. echo = buddy->intop;
  380. buddy->intop += server_translate_telopts(ses, buddy, (unsigned char *) input, size, (unsigned char *) buddy->inbuf, buddy->intop);
  381. // Handle local echo for Windows telnet
  382. if (HAS_BIT(buddy->comm_flags, COMM_FLAG_REMOTEECHO))
  383. {
  384. while (echo < buddy->intop)
  385. {
  386. switch (input[echo++])
  387. {
  388. case 8:
  389. case 127:
  390. input[echo] = '\b';
  391. port_socket_printf(ses, buddy, "\b \b");
  392. break;
  393. case '\n':
  394. port_socket_printf(ses, buddy, "\r\n");
  395. break;
  396. default:
  397. if (HAS_BIT(buddy->comm_flags, COMM_FLAG_PASSWORD))
  398. {
  399. port_socket_printf(ses, buddy, "*");
  400. }
  401. else
  402. {
  403. port_socket_printf(ses, buddy, "%c", input[echo]);
  404. }
  405. break;
  406. }
  407. }
  408. }
  409. if (buddy->intop > BUFFER_SIZE / 4)
  410. {
  411. port_socket_printf(ses, buddy, "\e[1;31mYou overflowed your input buffer, you must reconnect.\n");
  412. port_log_printf(ses, buddy, "Buffer overflow, closing connection.");
  413. pop_call();
  414. return -1;
  415. }
  416. if (buddy->intop)
  417. {
  418. pt2 = buddy->inbuf;
  419. while (pt2)
  420. {
  421. pt1 = pt2;
  422. pt2 = strchr(pt1, '\n');
  423. if (pt2)
  424. {
  425. *pt2++ = 0;
  426. get_port_commands(ses, buddy, pt1, pt2 - pt1);
  427. }
  428. }
  429. buddy->intop = strlen(pt1);
  430. memmove(buddy->inbuf, pt1, buddy->intop);
  431. }
  432. }
  433. pop_call();
  434. return 0;
  435. }
  436. void get_port_commands(struct session *ses, struct port_data *buddy, char *buf, int len)
  437. {
  438. char txt[STRING_SIZE];
  439. push_call("get_port_commands(%s,%d,%s)",buddy->name,len,buf);
  440. strip_vt102_codes(buf, txt);
  441. if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 5, "CATCH PORT RECEIVED MESSAGE", buddy->name, buddy->ip, ntos(buddy->port), buf, txt))
  442. {
  443. port_receive_message(ses, buddy, buf);
  444. }
  445. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 5, "PORT RECEIVED MESSAGE", buddy->name, buddy->ip, ntos(buddy->port), buf, txt);
  446. pop_call();
  447. return;
  448. }
  449. void port_receive_message(struct session *ses, struct port_data *buddy, char *txt)
  450. {
  451. if (HAS_BIT(buddy->flags, PORT_FLAG_IGNORE))
  452. {
  453. return;
  454. }
  455. port_printf(ses, "%s", txt);
  456. }
  457. DO_PORT(port_call)
  458. {
  459. int sock, error;
  460. char host[BUFFER_SIZE], port[BUFFER_SIZE];
  461. struct addrinfo *address;
  462. static struct addrinfo hints;
  463. struct port_data *new_buddy;
  464. struct timeval to;
  465. fd_set wds, rds;
  466. to.tv_sec = CALL_TIMEOUT;
  467. to.tv_usec = 0;
  468. strcpy(host, arg1);
  469. strcpy(port, arg2);
  470. port_printf(ses, "Attempting to call {%s} {%s} ...", host, port);
  471. hints.ai_family = AF_INET;
  472. hints.ai_protocol = IPPROTO_TCP;
  473. hints.ai_socktype = SOCK_STREAM;
  474. error = getaddrinfo(host, port, &hints, &address);
  475. if (error)
  476. {
  477. hints.ai_family = AF_INET6;
  478. error = getaddrinfo(host, port, &hints, &address);
  479. if (error)
  480. {
  481. port_printf(ses, "Failed to call %s, unknown host.", host);
  482. return ses;
  483. }
  484. }
  485. sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
  486. if (sock < 0)
  487. {
  488. syserr_printf(ses, "port_call: socket");
  489. freeaddrinfo(address);
  490. return ses;
  491. }
  492. switch (address->ai_family)
  493. {
  494. case AF_INET:
  495. inet_ntop(address->ai_family, &((struct sockaddr_in *)address->ai_addr)->sin_addr, host, address->ai_addrlen);
  496. break;
  497. case AF_INET6:
  498. inet_ntop(address->ai_family, &((struct sockaddr_in6 *)address->ai_addr)->sin6_addr, host, address->ai_addrlen);
  499. break;
  500. }
  501. error = connect(sock, address->ai_addr, address->ai_addrlen);
  502. if (error)
  503. {
  504. syserr_printf(ses, "port_call: connect");
  505. port_printf(ses, "Failed to connect to %s:%s", host, port);
  506. close(sock);
  507. freeaddrinfo(address);
  508. return ses;
  509. }
  510. freeaddrinfo(address);
  511. FD_ZERO(&wds);
  512. FD_SET(sock, &wds);
  513. error = select(FD_SETSIZE, NULL, &wds, NULL, &to);
  514. if (error < 0)
  515. {
  516. syserr_printf(ses, "port_call: select wds:");
  517. port_printf(ses, "Failed to connect to %s %s", host, port);
  518. close(sock);
  519. return ses;
  520. }
  521. if (!FD_ISSET(sock, &wds))
  522. {
  523. port_printf(ses, "Connection timed out.");
  524. close(sock);
  525. return ses;
  526. }
  527. new_buddy = calloc(1, sizeof(struct port_data));
  528. new_buddy->fd = sock;
  529. new_buddy->port = atoi(port);
  530. new_buddy->group = strdup("");
  531. new_buddy->ip = strdup(host);
  532. new_buddy->name = strdup(ntos(sock));
  533. new_buddy->color = strdup("");
  534. new_buddy->prefix = strdup("");
  535. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 3, "PORT CONNECTION", new_buddy->name, new_buddy->ip, ntos(new_buddy->port));
  536. FD_ZERO(&rds);
  537. FD_SET(sock, &rds);
  538. to.tv_sec = CALL_TIMEOUT;
  539. to.tv_usec = 0;
  540. error = select(FD_SETSIZE, &rds, NULL, NULL, &to);
  541. if (error < 0)
  542. {
  543. syserr_printf(ses, "port_call: select rds:");
  544. close_port(ses, new_buddy, FALSE);
  545. return ses;
  546. }
  547. if (process_port_input(ses, new_buddy) == -1)
  548. {
  549. FD_CLR(new_buddy->fd, &rds);
  550. close_port(ses, new_buddy, FALSE);
  551. return ses;
  552. }
  553. // NULL check because of threading.
  554. if (ses->port == NULL || *new_buddy->name == 0)
  555. {
  556. close_port(ses, new_buddy, FALSE);
  557. return ses;
  558. }
  559. if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  560. {
  561. syserr_printf(ses, "port_new: fcntl O_NDELAY|O_NONBLOCK");
  562. }
  563. ses->port->total++;
  564. LINK(new_buddy, ses->port->next, ses->port->prev);
  565. port_printf(ses, "Connection made to %s.", new_buddy->name);
  566. return ses;
  567. }
  568. DO_PORT(port_info)
  569. {
  570. tintin_printf2(ses, "Port : %d", ses->port->port);
  571. tintin_printf2(ses, "Prefix : %s", ses->port->prefix);
  572. tintin_printf2(ses, "Color : %s", str_convert_meta(ses->port->color, TRUE));
  573. tintin_printf2(ses, "DND : %s", HAS_BIT(ses->port->flags, PORT_FLAG_DND) ? "On" : "Off");
  574. return ses;
  575. }
  576. DO_PORT(port_name)
  577. {
  578. struct port_data *buddy;
  579. buddy = find_port_buddy(ses, arg1);
  580. substitute(ses, arg2, arg2, SUB_COL|SUB_ESC);
  581. if (buddy == NULL)
  582. {
  583. port_printf(ses, "There is no socket named '%s'.", arg1);
  584. return ses;
  585. }
  586. RESTRING(buddy->name, arg2);
  587. port_printf(ses, "Name of socket '%s' changed to '%s'.", arg1, buddy->name);
  588. return ses;
  589. }
  590. DO_PORT(port_prefix)
  591. {
  592. RESTRING(ses->port->prefix, arg1);
  593. port_printf(ses, "Prefix set to '%s'", ses->port->prefix);
  594. return ses;
  595. }
  596. DO_PORT(port_send)
  597. {
  598. struct port_data *buddy;
  599. substitute(gtd->ses, arg2, arg2, SUB_COL|SUB_ESC|SUB_LNF);
  600. if (!strcasecmp(arg1, "ALL"))
  601. {
  602. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  603. {
  604. port_socket_printf(ses, buddy, "%s", arg2);
  605. }
  606. }
  607. else
  608. {
  609. if ((buddy = find_port_buddy(ses, arg1)) != NULL)
  610. {
  611. port_socket_printf(ses, buddy, "%s", arg2);
  612. }
  613. else if (find_port_group(ses, arg1) != NULL)
  614. {
  615. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  616. {
  617. if (!strcmp(buddy->group, arg1))
  618. {
  619. port_socket_printf(ses, buddy, "%s", arg2);
  620. }
  621. }
  622. }
  623. else
  624. {
  625. port_printf(ses, "There is no socket named '%s'.", arg1);
  626. }
  627. }
  628. return ses;
  629. }
  630. DO_PORT(port_who)
  631. {
  632. struct port_data *buddy;
  633. int cnt = 1;
  634. tintin_printf(ses, " %-15s %-5s %-20s %-5s ", "Name", "Flags", "Address", "Port");
  635. tintin_printf(ses, " =============== ===== ==================== ===== ");
  636. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  637. {
  638. tintin_printf(ses, " %03d %+15s %s%s%s%s%s %+20s %+5u",
  639. cnt++,
  640. buddy->name,
  641. HAS_BIT(buddy->flags, PORT_FLAG_PRIVATE) ? "P" : " ",
  642. HAS_BIT(buddy->flags, PORT_FLAG_IGNORE) ? "I" : " ",
  643. HAS_BIT(buddy->flags, PORT_FLAG_SERVE) ? "S" : " ",
  644. HAS_BIT(buddy->flags, PORT_FLAG_FORWARD) ? "F" :
  645. HAS_BIT(buddy->flags, PORT_FLAG_FORWARDBY) ? "f" : " ",
  646. " ",
  647. buddy->ip,
  648. buddy->port);
  649. }
  650. tintin_printf(ses, " =============== ===== ==================== ===== ");
  651. return ses;
  652. }
  653. DO_PORT(port_zap)
  654. {
  655. struct port_data *buddy;
  656. if (!strcasecmp(arg1, "ALL"))
  657. {
  658. while (ses->port->next)
  659. {
  660. close_port(ses, ses->port->next, TRUE);
  661. }
  662. }
  663. else
  664. {
  665. if ((buddy = find_port_buddy(ses, arg1)))
  666. {
  667. close_port(ses, buddy, TRUE);
  668. }
  669. else
  670. {
  671. port_printf(ses, "There is no socket named '%s'.", arg1);
  672. }
  673. }
  674. return ses;
  675. }
  676. DO_PORT(port_color)
  677. {
  678. if (*arg1 == 0 || get_color_names(gtd->ses, arg1, arg2) == FALSE)
  679. {
  680. port_printf(ses, "Valid colors are:\n\nreset, bold, dim, light, dark, underscore, blink, reverse, black, red, green, yellow, blue, magenta, cyan, white, b black, b red, b green, b yellow, b blue, b magenta, b cyan, b white");
  681. return ses;
  682. }
  683. RESTRING(ses->port->color, arg2);
  684. port_printf(ses, "Color has been set to %s", arg1);
  685. return ses;
  686. }
  687. DO_PORT(port_dnd)
  688. {
  689. TOG_BIT(ses->port->flags, PORT_FLAG_DND);
  690. if (HAS_BIT(ses->port->flags, PORT_FLAG_DND))
  691. {
  692. port_printf(ses, "New connections are no longer accepted.");
  693. }
  694. else
  695. {
  696. port_printf(ses, "New connections are accepted.");
  697. }
  698. return ses;
  699. }
  700. DO_PORT(port_flag)
  701. {
  702. if (*arg1 == 0)
  703. {
  704. tintin_printf2(ses, "#SYNTAX: #PORT FLAG <DND|PRIVATE> [ON|OFF]");
  705. }
  706. else if (is_abbrev(arg1, "DND"))
  707. {
  708. if (!strcasecmp(arg2, "ON"))
  709. {
  710. SET_BIT(ses->port->flags, PORT_FLAG_DND);
  711. }
  712. else if (!strcasecmp(arg2, "OFF"))
  713. {
  714. DEL_BIT(ses->port->flags, PORT_FLAG_DND);
  715. }
  716. else if (*arg2 == 0)
  717. {
  718. TOG_BIT(ses->port->flags, PORT_FLAG_DND);
  719. }
  720. else
  721. {
  722. tintin_printf2(ses, "#SYNTAX: #PORT FLAG DND [ON|OFF]");
  723. return ses;
  724. }
  725. if (HAS_BIT(ses->port->flags, PORT_FLAG_DND))
  726. {
  727. port_printf(ses, "New connections are no longer accepted.");
  728. }
  729. else
  730. {
  731. port_printf(ses, "New connections are accepted.");
  732. }
  733. }
  734. else if (is_abbrev(arg1, "PRIVATE"))
  735. {
  736. if (!strcasecmp(arg2, "ON"))
  737. {
  738. SET_BIT(ses->port->flags, PORT_FLAG_PRIVATE);
  739. }
  740. else if (!strcasecmp(arg2, "OFF"))
  741. {
  742. DEL_BIT(ses->port->flags, PORT_FLAG_PRIVATE);
  743. }
  744. else if (*arg2 == 0)
  745. {
  746. TOG_BIT(ses->port->flags, PORT_FLAG_PRIVATE);
  747. }
  748. else
  749. {
  750. tintin_printf2(ses, "#SYNTAX: #PORT FLAG PRIVATE [ON|OFF]");
  751. return ses;
  752. }
  753. if (HAS_BIT(ses->port->flags, PORT_FLAG_PRIVATE))
  754. {
  755. port_printf(ses, "Remote connections are no longer accepted.");
  756. }
  757. else
  758. {
  759. port_printf(ses, "Remote connections are accepted.");
  760. }
  761. }
  762. return ses;
  763. }
  764. DO_PORT(port_group)
  765. {
  766. struct port_data *buddy;
  767. int cnt = 0;
  768. if (*arg1 == 0)
  769. {
  770. tintin_printf(NULL, " %-15s %-20s %-5s %-15s", "Name", "Address", "Port", "Group");
  771. tintin_printf(NULL, " =============== ==================== ===== ==================== ");
  772. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  773. {
  774. tintin_printf(NULL, " %03d %-15s %-20s %-5u %-20s",
  775. cnt++,
  776. buddy->name,
  777. buddy->ip,
  778. buddy->port,
  779. buddy->group);
  780. }
  781. tintin_printf(NULL, " =============== ==================== ===== ==================== ");
  782. }
  783. else if (!strcasecmp(arg1, "ALL"))
  784. {
  785. port_printf(ses, "You set everyone's group to '%s'", arg2);
  786. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  787. {
  788. RESTRING(buddy->group, arg2);
  789. }
  790. }
  791. else
  792. {
  793. if ((buddy = find_port_buddy(ses, arg1)) != NULL)
  794. {
  795. RESTRING(buddy->group, arg2);
  796. port_printf(ses, "You set %s's group to '%s'", buddy->name, arg2);
  797. }
  798. else
  799. {
  800. port_printf(ses, "You are not connected to anyone named '%s'.", arg1);
  801. }
  802. }
  803. return ses;
  804. }
  805. DO_PORT(port_ignore)
  806. {
  807. struct port_data *buddy;
  808. if ((buddy = find_port_buddy(ses, arg1)) == NULL)
  809. {
  810. port_printf(ses, "You are not connected to anyone named '%s'.", arg1);
  811. return ses;
  812. }
  813. TOG_BIT(buddy->flags, PORT_FLAG_IGNORE);
  814. if (HAS_BIT(buddy->flags, PORT_FLAG_IGNORE))
  815. {
  816. port_printf(ses, "You are now ignoring %s.", buddy->name);
  817. }
  818. else
  819. {
  820. port_printf(ses, "You are no longer ignoring %s.", buddy->name);
  821. }
  822. return ses;
  823. }
  824. struct port_data *find_port_buddy(struct session *ses, char *arg)
  825. {
  826. struct port_data *buddy;
  827. if (*arg == 0)
  828. {
  829. return NULL;
  830. }
  831. if (is_number(arg))
  832. {
  833. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  834. {
  835. if (atoi(arg) == buddy->fd)
  836. {
  837. return buddy;
  838. }
  839. }
  840. }
  841. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  842. {
  843. if (!strcmp(arg, buddy->ip))
  844. {
  845. return buddy;
  846. }
  847. }
  848. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  849. {
  850. if (is_abbrev(arg, buddy->name))
  851. {
  852. return buddy;
  853. }
  854. }
  855. return NULL;
  856. }
  857. struct port_data *find_port_group(struct session *ses, char *arg)
  858. {
  859. struct port_data *buddy;
  860. if (*arg == 0)
  861. {
  862. return NULL;
  863. }
  864. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  865. {
  866. if (!strcmp(arg, buddy->group))
  867. {
  868. return buddy;
  869. }
  870. }
  871. return NULL;
  872. }
  873. DO_PORT(port_rank)
  874. {
  875. struct port_data *buddy;
  876. int cnt, rank;
  877. if (*arg1 == 0)
  878. {
  879. tintin_printf(NULL, " %-15s %-20s %-5s %-15s", "Name", "Address", "Port", "Rank");
  880. tintin_printf(NULL, " =============== ==================== ===== ==================== ");
  881. cnt = 0;
  882. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  883. {
  884. tintin_printf(NULL, " %03d %-15s %-20s %-5u %-20s",
  885. cnt++,
  886. buddy->name,
  887. buddy->ip,
  888. buddy->port,
  889. rank_table[buddy->rank].name);
  890. }
  891. tintin_printf(NULL, " =============== ==================== ===== ==================== ");
  892. return ses;
  893. }
  894. if (*arg2 == 0)
  895. {
  896. show_error(ses, LIST_COMMAND, "#SYNTAX: #PORT RANK <NAME> <SPY|SCOUT>");
  897. return ses;
  898. }
  899. else if (is_abbrev(arg2, "SPY"))
  900. {
  901. rank = PORT_RANK_SPY;
  902. }
  903. else if (is_abbrev(arg2, "SCOUT"))
  904. {
  905. rank = PORT_RANK_SCOUT;
  906. }
  907. else
  908. {
  909. show_error(ses, LIST_COMMAND, "#SYNTAX: #PORT RANK <NAME> <SPY|SCOUT>");
  910. return ses;
  911. }
  912. if (!strcasecmp(arg1, "ALL"))
  913. {
  914. port_printf(ses, "You set everyone's rank to '%s'", rank_table[rank].name);
  915. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  916. {
  917. buddy->rank = rank;
  918. }
  919. }
  920. else
  921. {
  922. if ((buddy = find_port_buddy(ses, arg1)) != NULL)
  923. {
  924. buddy->rank = rank;
  925. port_printf(ses, "YOU SET %s'S RANK TO '%s'", buddy->name, rank_table[buddy->rank]);
  926. }
  927. else
  928. {
  929. port_printf(ses, "YOU ARE NOT CONNECTED TO ANYONE NAMED '%s'.", arg1);
  930. }
  931. }
  932. return ses;
  933. }