net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 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. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Peter Unold 1992 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #include <arpa/inet.h>
  27. #include <fcntl.h>
  28. #include <netinet/in.h>
  29. #include <netdb.h>
  30. #include <signal.h>
  31. #include <sys/socket.h>
  32. #include <sys/types.h>
  33. /*
  34. IPv6 compatible connect code.
  35. */
  36. int wait_on_connect(struct session *ses, int sock, int connect_error)
  37. {
  38. static struct timeval timeout;
  39. static fd_set rfd;
  40. static fd_set wfd;
  41. socklen_t len, val;
  42. FD_SET(sock, &rfd);
  43. FD_SET(sock, &wfd);
  44. timeout.tv_sec = 4;
  45. if (connect_error)
  46. {
  47. switch (select(FD_SETSIZE, &rfd, &wfd, NULL, &timeout))
  48. {
  49. case 0:
  50. syserr_printf(ses, "wait_on_connect:");
  51. return -1;
  52. case -1:
  53. syserr_printf(ses, "wait_on_connect: select");
  54. return -1;
  55. }
  56. }
  57. else
  58. {
  59. switch (select(sock+1, NULL, &wfd, NULL, &timeout))
  60. {
  61. case 0:
  62. syserr_printf(ses, "wait_on_connect2:");
  63. return -1;
  64. case -1:
  65. syserr_printf(ses, "wait_on_connect2: select");
  66. return -1;
  67. }
  68. }
  69. len = sizeof(val);
  70. if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &val, &len) == -1)
  71. {
  72. syserr_printf(ses, "wait_on_connect: getsockopt:");
  73. return -1;
  74. }
  75. if (val)
  76. {
  77. errno = val;
  78. syserr_printf(ses, "wait_on_connect: getsockopt:");
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. #ifdef HAVE_GETADDRINFO
  84. int connect_mud(struct session *ses, char *host, char *port)
  85. {
  86. int sock, error;
  87. struct addrinfo *address;
  88. static struct addrinfo hints;
  89. char ip[100];
  90. if (!is_number(port))
  91. {
  92. show_error(ses, LIST_COMMAND, "#CONNECT: THE PORT {%s} SHOULD BE A NUMBER.", port);
  93. return -1;
  94. }
  95. // hints.ai_family = AF_UNSPEC;
  96. hints.ai_family = AF_INET;
  97. hints.ai_protocol = IPPROTO_TCP;
  98. hints.ai_socktype = SOCK_STREAM;
  99. error = getaddrinfo(host, port, &hints, &address);
  100. if (error)
  101. {
  102. hints.ai_family = AF_INET6;
  103. error = getaddrinfo(host, port, &hints, &address);
  104. if (error)
  105. {
  106. tintin_printf2(ses, "#SESSION '%s' COULD NOT CONNECT - UNKNOWN HOST.", ses->name);
  107. return -1;
  108. }
  109. }
  110. sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
  111. if (sock < 0)
  112. {
  113. syserr_printf(ses, "connect_mud: socket");
  114. freeaddrinfo(address);
  115. return -1;
  116. }
  117. int optval = 1;
  118. socklen_t optlen = sizeof(optval);
  119. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
  120. ses->connect_error = connect(sock, address->ai_addr, address->ai_addrlen);
  121. if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  122. {
  123. syserr_printf(ses, "connect_mud: fcntl O_NDELAY|O_NONBLOCK");
  124. close(sock);
  125. freeaddrinfo(address);
  126. return -1;
  127. }
  128. // ses->connect_error = connect(sock, address->ai_addr, address->ai_addrlen);
  129. if (ses->connect_error)
  130. {
  131. // ses->connect_error = wait_on_connect(ses, sock, ses->connect_error);
  132. if (ses->connect_error)
  133. {
  134. syserr_printf(ses, "connect_mud: connect");
  135. close(sock);
  136. freeaddrinfo(address);
  137. return 0;
  138. }
  139. }
  140. error = getnameinfo(address->ai_addr, address->ai_addrlen, ip, 100, NULL, 0, NI_NUMERICHOST);
  141. if (error)
  142. {
  143. syserr_printf(ses, "connect_mud: getnameinfo:");
  144. }
  145. else
  146. {
  147. RESTRING(ses->session_ip, ip);
  148. }
  149. freeaddrinfo(address);
  150. return sock;
  151. }
  152. #else
  153. int connect_mud(struct session *ses, char *host, char *port)
  154. {
  155. int sock, d;
  156. struct sockaddr_in sockaddr;
  157. if (sscanf(host, "%d.%d.%d.%d", &d, &d, &d, &d) == 4)
  158. {
  159. sockaddr.sin_addr.s_addr = inet_addr(host);
  160. }
  161. else
  162. {
  163. struct hostent *hp;
  164. if (!(hp = gethostbyname(host)))
  165. {
  166. tintin_puts2(ses, "#ERROR - UNKNOWN HOST.");
  167. return -1;
  168. }
  169. memcpy((char *)&sockaddr.sin_addr, hp->h_addr, sizeof(sockaddr.sin_addr));
  170. }
  171. if (is_number(port))
  172. {
  173. sockaddr.sin_port = htons(atoi(port));
  174. }
  175. else
  176. {
  177. show_error(ses, LIST_COMMAND, "#CONNECT: THE PORT {%s} SHOULD BE A NUMBER.", port);
  178. return -1;
  179. }
  180. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  181. {
  182. syserr_printf(ses, "old_connect_mud: socket()");
  183. return -1;
  184. }
  185. sockaddr.sin_family = AF_INET;
  186. ses->connect_error = connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  187. if (ses->connect_error)
  188. {
  189. syserr_printf(ses, "connect_mud: connect");
  190. close(sock);
  191. return 0;
  192. }
  193. if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  194. {
  195. syserr_printf(ses, "connect_mud: fcntl O_NDELAY|O_NONBLOCK");
  196. }
  197. RESTRING(ses->session_ip, inet_ntoa(sockaddr.sin_addr));
  198. return sock;
  199. }
  200. #endif
  201. void write_line_mud(struct session *ses, char *line, int size)
  202. {
  203. int result;
  204. push_call("write_line_mud(%p,%p)",line,ses);
  205. check_all_events(ses, SUB_SEC|EVENT_FLAG_INPUT, 0, 2, "SEND OUTPUT", line, ntos(size));
  206. if (check_all_events(ses, SUB_SEC|EVENT_FLAG_CATCH, 0, 2, "CATCH SEND OUTPUT", line, ntos(size)))
  207. {
  208. pop_call();
  209. return;
  210. }
  211. if (ses == gts)
  212. {
  213. if (HAS_BIT(gtd->flags, TINTIN_FLAG_CHILDLOCK))
  214. {
  215. tintin_printf2(ses, "#NO SESSION ACTIVE. TINTIN IS CHILD LOCKED, PRESS CTRL-D TO EXIT.");
  216. }
  217. else
  218. {
  219. tintin_printf2(ses, "#NO SESSION ACTIVE. USE: %csession {name} {host} {port} TO START ONE.", gtd->tintin_char);
  220. }
  221. pop_call();
  222. return;
  223. }
  224. if (!HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  225. {
  226. tintin_printf2(ses, "#THIS SESSION IS NOT CONNECTED, CANNOT SEND: %s", line);
  227. pop_call();
  228. return;
  229. }
  230. if (!HAS_BIT(ses->telopts, TELOPT_FLAG_TELNET) && HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
  231. {
  232. char buf[BUFFER_SIZE];
  233. size = utf8_to_all(ses, line, buf);
  234. memcpy(line, buf, size);
  235. line[size] = 0;
  236. }
  237. {
  238. if (ses->mccp3)
  239. {
  240. result = client_write_compressed(ses, line, size);
  241. }
  242. #ifdef HAVE_GNUTLS_H
  243. else if (ses->ssl)
  244. {
  245. result = gnutls_record_send(ses->ssl, line, size);
  246. while (result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
  247. {
  248. result = gnutls_record_send(ses->ssl, 0, 0);
  249. }
  250. }
  251. #endif
  252. else
  253. {
  254. result = write(ses->socket, line, size);
  255. if (result == -1)
  256. {
  257. syserr_printf(ses, "write_line_mud: write");
  258. }
  259. }
  260. if (result == -1)
  261. {
  262. cleanup_session(ses);
  263. pop_call();
  264. return;
  265. }
  266. }
  267. check_all_events(ses, SUB_SEC|EVENT_FLAG_INPUT, 0, 2, "SENT OUTPUT", line, ntos(size));
  268. pop_call();
  269. return;
  270. }
  271. int read_buffer_mud(struct session *ses)
  272. {
  273. unsigned char buffer[BUFFER_SIZE];
  274. int size;
  275. push_call("read_buffer_mud(%p)",ses);
  276. #ifdef HAVE_GNUTLS_H
  277. if (ses->ssl)
  278. {
  279. do
  280. {
  281. size = gnutls_record_recv(ses->ssl, buffer, BUFFER_SIZE - 1);
  282. }
  283. while (size == GNUTLS_E_INTERRUPTED || size == GNUTLS_E_AGAIN);
  284. if (size < 0)
  285. {
  286. tintin_printf2(ses, "#SSL ERROR: %s", gnutls_strerror(size));
  287. }
  288. }
  289. else
  290. #endif
  291. size = read(ses->socket, buffer, BUFFER_SIZE - 1);
  292. if (size <= 0)
  293. {
  294. pop_call();
  295. return FALSE;
  296. }
  297. buffer[size] = 0;
  298. ses->read_len = client_translate_telopts(ses, buffer, size);
  299. pop_call();
  300. return TRUE;
  301. }
  302. int detect_prompt(struct session *ses, char *original)
  303. {
  304. char strip[BUFFER_SIZE];
  305. struct listroot *root = ses->list[LIST_PROMPT];
  306. struct listnode *node;
  307. if (HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
  308. {
  309. all_to_utf8(ses, original, strip);
  310. strcpy(original, strip);
  311. }
  312. strip_vt102_codes(original, strip);
  313. for (root->update = 0 ; root->update < root->used ; root->update++)
  314. {
  315. node = root->list[root->update];
  316. if (check_one_regexp(ses, node, strip, original, 0))
  317. {
  318. return TRUE;
  319. }
  320. }
  321. return FALSE;
  322. }
  323. void readmud(struct session *ses)
  324. {
  325. char *line, *next_line;
  326. char linebuf[BUFFER_SIZE];
  327. int len;
  328. struct session *cts;
  329. push_call("readmud(%p)", ses);
  330. gtd->mud_output_len = 0;
  331. if (gtd->mud_output_len < BUFFER_SIZE)
  332. {
  333. check_all_events(ses, SUB_SEC|EVENT_FLAG_OUTPUT, 0, 1, "RECEIVED OUTPUT", gtd->mud_output_buf);
  334. if (check_all_events(ses, SUB_SEC|EVENT_FLAG_CATCH, 0, 1, "CATCH RECEIVED OUTPUT", gtd->mud_output_buf))
  335. {
  336. pop_call();
  337. return;
  338. }
  339. }
  340. /* separate into lines and print away */
  341. // cts = current tintin session, may have to make this global to avoid glitches
  342. cts = gtd->ses;
  343. if (HAS_BIT(gtd->ses->flags, SES_FLAG_SPLIT))
  344. {
  345. save_pos(gtd->ses);
  346. goto_pos(gtd->ses, gtd->ses->split->bot_row, 1);
  347. }
  348. SET_BIT(cts->flags, SES_FLAG_READMUD);
  349. for (line = gtd->mud_output_buf ; line && *line ; line = next_line)
  350. {
  351. next_line = strchr(line, '\n');
  352. if (next_line)
  353. {
  354. if (next_line - line >= BUFFER_SIZE / 3)
  355. {
  356. // This is not ideal, but being a rare case it'll suffice for now
  357. next_line = &line[BUFFER_SIZE / 3];
  358. }
  359. *next_line++ = 0;
  360. }
  361. else
  362. {
  363. if (*line == 0)
  364. {
  365. break;
  366. }
  367. len = strlen(line);
  368. if (strlen(line) > BUFFER_SIZE / 3)
  369. {
  370. len = BUFFER_SIZE / 3;
  371. next_line = &line[len];
  372. *next_line++ = 0;
  373. }
  374. if (str_len(ses->more_output) < BUFFER_SIZE / 3)
  375. {
  376. if (!HAS_BIT(ses->telopts, TELOPT_FLAG_PROMPT))
  377. {
  378. if (ses->packet_patch)
  379. {
  380. str_cat(&ses->more_output, line);
  381. ses->check_output = gtd->utime + ses->packet_patch;
  382. break;
  383. }
  384. else if (HAS_BIT(ses->config_flags, CONFIG_FLAG_AUTOPATCH))
  385. {
  386. if (ses->list[LIST_PROMPT]->list[0])
  387. {
  388. if (!detect_prompt(ses, line))
  389. {
  390. str_cat(&ses->more_output, line);
  391. ses->check_output = gtd->utime + 500000ULL;
  392. break;
  393. }
  394. }
  395. else if (HAS_BIT(ses->config_flags, CONFIG_FLAG_AUTOPROMPT))
  396. {
  397. str_cat(&ses->more_output, line);
  398. ses->check_output = gtd->utime + 500000ULL;
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. if (ses->more_output[0])
  406. {
  407. if (ses->check_output)
  408. {
  409. str_cat(&ses->more_output, line);
  410. strcpy(linebuf, ses->more_output);
  411. str_cpy(&ses->more_output, "");
  412. }
  413. else
  414. {
  415. strcpy(linebuf, line);
  416. }
  417. }
  418. else
  419. {
  420. strcpy(linebuf, line);
  421. }
  422. if (HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
  423. {
  424. char tempbuf[BUFFER_SIZE];
  425. all_to_utf8(ses, linebuf, tempbuf);
  426. process_mud_output(ses, tempbuf, next_line == NULL);
  427. }
  428. else
  429. {
  430. process_mud_output(ses, linebuf, next_line == NULL);
  431. }
  432. }
  433. DEL_BIT(cts->flags, SES_FLAG_READMUD);
  434. if (HAS_BIT(gtd->ses->flags, SES_FLAG_SPLIT))
  435. {
  436. restore_pos(gtd->ses);
  437. }
  438. pop_call();
  439. return;
  440. }
  441. void process_mud_output(struct session *ses, char *linebuf, int prompt)
  442. {
  443. char line[STRING_SIZE];
  444. int str_len, raw_len;
  445. push_call("process_mud_output(%p,%p,%d)",ses,linebuf,prompt);
  446. ses->check_output = 0;
  447. raw_len = strlen(linebuf);
  448. str_len = strip_vt102_codes(linebuf, line);
  449. check_all_events(ses, SUB_SEC|EVENT_FLAG_OUTPUT, 0, 2, "RECEIVED LINE", linebuf, line);
  450. if (check_all_events(ses, SUB_SEC|EVENT_FLAG_CATCH, 0, 2, "CATCH RECEIVED LINE", linebuf, line))
  451. {
  452. pop_call();
  453. return;
  454. }
  455. if (str_len && prompt)
  456. {
  457. check_all_events(ses, SUB_SEC|EVENT_FLAG_OUTPUT, 0, 4, "RECEIVED PROMPT", linebuf, line, ntos(raw_len), ntos(str_len));
  458. if (check_all_events(ses, SUB_SEC|EVENT_FLAG_CATCH, 0, 4, "CATCH RECEIVED PROMPT", linebuf, line, ntos(raw_len), ntos(str_len)))
  459. {
  460. pop_call();
  461. return;
  462. }
  463. }
  464. if (HAS_BIT(ses->config_flags, CONFIG_FLAG_COLORPATCH))
  465. {
  466. sprintf(line, "%s%s%s", ses->color_patch, linebuf, "\e[0m");
  467. get_color_codes(ses->color_patch, linebuf, ses->color_patch, GET_ALL);
  468. linebuf = line;
  469. }
  470. do_one_line(linebuf, ses); /* changes linebuf */
  471. /*
  472. Take care of gags, vt102 support still goes
  473. */
  474. if (ses->gagline > 0)
  475. {
  476. ses->gagline--;
  477. strip_non_vt102_codes(linebuf, line);
  478. print_stdout(0, 0, "%s", line);
  479. strip_vt102_codes(linebuf, line);
  480. show_debug(ses, LIST_GAG, "#DEBUG GAG {%d} {%s}", ses->gagline + 1, line);
  481. pop_call();
  482. return;
  483. }
  484. add_line_buffer(ses, linebuf, prompt);
  485. if (ses == gtd->ses)
  486. {
  487. char *output = str_alloc_stack(0);
  488. str_cpy(&output, linebuf);
  489. print_line(ses, &output, prompt);
  490. if (!IS_SPLIT(ses))
  491. {
  492. if (prompt)
  493. {
  494. int height, width;
  495. word_wrap_split(ses, linebuf, line, ses->wrap, 0, 0, FLAG_NONE, &height, &width);
  496. if (height > 1)
  497. {
  498. word_wrap_split(ses, linebuf, line, ses->wrap, height, height, WRAP_FLAG_SPLIT, &height, &width);
  499. }
  500. ses->input->str_off = 1 + width;
  501. }
  502. else
  503. {
  504. ses->input->str_off = 1;
  505. }
  506. }
  507. else
  508. {
  509. if (!HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  510. {
  511. ses->input->str_off = 1;
  512. }
  513. }
  514. }
  515. pop_call();
  516. return;
  517. }