port.c 27 KB

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