| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2019 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
- * *
- * coded by Peter Unold 1992 *
- ******************************************************************************/
- #include "tintin.h"
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <signal.h>
- #include <sys/types.h>
- /*
- IPv6 compatible connect code.
- */
- #ifdef HAVE_GETADDRINFO
- int connect_mud(struct session *ses, char *host, char *port)
- {
- int sock, error;
- struct addrinfo *address;
- static struct addrinfo hints;
- char ip[100];
- if (!is_number(port))
- {
- tintin_puts(ses, "#THE PORT SHOULD BE A NUMBER.");
- return -1;
- }
- // hints.ai_family = AF_UNSPEC;
- hints.ai_family = AF_INET;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_socktype = SOCK_STREAM;
- error = getaddrinfo(host, port, &hints, &address);
- if (error)
- {
- hints.ai_family = AF_INET6;
- error = getaddrinfo(host, port, &hints, &address);
- if (error)
- {
- tintin_printf2(ses, "#SESSION '%s' COULD NOT CONNECT - UNKNOWN HOST.", ses->name);
- return -1;
- }
- }
- sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
- if (sock < 0)
- {
- syserr_printf(ses, "connect_mud: socket");
- freeaddrinfo(address);
- return -1;
- }
- // fcntl(sock, F_SETFL, O_NDELAY);
- ses->connect_error = connect(sock, address->ai_addr, address->ai_addrlen);
- if (ses->connect_error)
- {
- syserr_printf(ses, "connect_mud: connect");
- close(sock);
- freeaddrinfo(address);
- return -1;
- }
- if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
- {
- syserr_printf(ses, "connect_mud: fcntl O_NDELAY|O_NONBLOCK");
- close(sock);
- freeaddrinfo(address);
- return -1;
- }
- error = getnameinfo(address->ai_addr, address->ai_addrlen, ip, 100, NULL, 0, NI_NUMERICHOST);
- if (error)
- {
- syserr_printf(ses, "connect_mud: getnameinfo:");
- }
- else
- {
- RESTRING(ses->session_ip, ip);
- }
- freeaddrinfo(address);
- return sock;
- }
- #else
- int connect_mud(struct session *ses, char *host, char *port)
- {
- int sock, d;
- struct sockaddr_in sockaddr;
- print_stdout("debug: NO ADDRESS INFO?\n");
- if (sscanf(host, "%d.%d.%d.%d", &d, &d, &d, &d) == 4)
- {
- sockaddr.sin_addr.s_addr = inet_addr(host);
- }
- else
- {
- struct hostent *hp;
- if (!(hp = gethostbyname(host)))
- {
- tintin_puts2(ses, "#ERROR - UNKNOWN HOST.");
- return -1;
- }
- memcpy((char *)&sockaddr.sin_addr, hp->h_addr, sizeof(sockaddr.sin_addr));
- }
- if (is_number(port))
- {
- sockaddr.sin_port = htons(atoi(port));
- }
- else
- {
- tintin_puts(ses, "#THE PORT SHOULD BE A NUMBER.");
- return -1;
- }
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- syserr_printf(ses, "old_connect_mud: socket()");
- return -1;
- }
- sockaddr.sin_family = AF_INET;
- ses->connect_error = connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
- if (ses->connect_error)
- {
- syserr_printf(ses, "connect_mud: connect");
- close(sock);
- return 0;
- }
- if (fcntl(sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
- {
- syserr_printf(ses, "connect_mud: fcntl O_NDELAY|O_NONBLOCK");
- }
- RESTRING(ses->session_ip, inet_ntoa(sockaddr.sin_addr));
- return sock;
- }
- #endif
- void write_line_mud(struct session *ses, char *line, int size)
- {
- int result;
- push_call("write_line_mud(%p,%p)",line,ses);
- if (ses == gts)
- {
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_CHILDLOCK))
- {
- tintin_printf2(ses, "#THIS SESSION IS CHILD LOCKED, PRESS CTRL-D TO EXIT.");
- }
- else
- {
- tintin_printf2(ses, "#NO SESSION ACTIVE. USE: %csession {name} {host} {port} TO START ONE.", gtd->tintin_char);
- }
- pop_call();
- return;
- }
- if (!HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
- {
- tintin_printf2(ses, "#THIS SESSION IS NOT CONNECTED, CANNOT SEND: %s", line);
- pop_call();
- return;
- }
- if (!HAS_BIT(ses->telopts, TELOPT_FLAG_TELNET) && HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
- {
- char buf[BUFFER_SIZE];
- size = utf8_to_all(ses, line, buf);
- strcpy(line, buf);
- }
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "SEND OUTPUT", line, ntos(size));
- if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "CATCH SEND OUTPUT", line, ntos(size)))
- {
- if (ses->mccp3)
- {
- result = client_write_compressed(ses, line, size);
- }
- #ifdef HAVE_GNUTLS_H
- else if (ses->ssl)
- {
- result = gnutls_record_send(ses->ssl, line, size);
- while (result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
- {
- result = gnutls_record_send(ses->ssl, 0, 0);
- }
- }
- #endif
- else
- {
- result = write(ses->socket, line, size);
- if (result == -1)
- {
- syserr_printf(ses, "write_line_mud: write");
- }
- }
-
- if (result == -1)
- {
- cleanup_session(ses);
- pop_call();
- return;
- }
- }
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "SENT OUTPUT", line, ntos(size));
- pop_call();
- return;
- }
- int read_buffer_mud(struct session *ses)
- {
- unsigned char buffer[BUFFER_SIZE];
- int size;
- push_call("read_buffer_mud(%p)",ses);
- #ifdef HAVE_GNUTLS_H
- if (ses->ssl)
- {
- do
- {
- size = gnutls_record_recv(ses->ssl, buffer, BUFFER_SIZE - 1);
- }
- while (size == GNUTLS_E_INTERRUPTED || size == GNUTLS_E_AGAIN);
- if (size < 0)
- {
- tintin_printf2(ses, "#SSL ERROR: %s", gnutls_strerror(size));
- }
- }
- else
- #endif
- size = read(ses->socket, buffer, BUFFER_SIZE - 1);
-
- if (size <= 0)
- {
- pop_call();
- return FALSE;
- }
- ses->read_len = client_translate_telopts(ses, buffer, size);
- pop_call();
- return TRUE;
- }
- void readmud(struct session *ses)
- {
- char *line, *next_line;
- char linebuf[BUFFER_SIZE];
- struct session *cts;
- push_call("readmud(%p)", ses);
- if (gtd->mud_output_len < BUFFER_SIZE)
- {
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 1, "RECEIVED OUTPUT", gtd->mud_output_buf);
- }
- gtd->mud_output_len = 0;
- /* separate into lines and print away */
- // cts = current tintin session, may have to make this global to avoid glitches
- cts = gtd->ses;
- if (HAS_BIT(gtd->ses->flags, SES_FLAG_SPLIT))
- {
- save_pos(gtd->ses);
- goto_pos(gtd->ses, gtd->ses->split->bot_row, 1);
- }
- SET_BIT(cts->flags, SES_FLAG_READMUD);
- for (line = gtd->mud_output_buf ; line && *line ; line = next_line)
- {
- next_line = strchr(line, '\n');
- if (next_line)
- {
- if (next_line - line >= BUFFER_SIZE / 3)
- {
- // This is not ideal, but being a rare case it'll suffice for now
- next_line = &line[BUFFER_SIZE / 3];
- }
- *next_line++ = 0;
- }
- else
- {
- if (*line == 0)
- {
- break;
- }
- if (strlen(line) > BUFFER_SIZE / 3)
- {
- next_line = &line[BUFFER_SIZE / 3];
- *next_line++ = 0;
- }
- if (strlen(ses->more_output) < BUFFER_SIZE / 3)
- {
- if (!HAS_BIT(ses->telopts, TELOPT_FLAG_PROMPT))
- {
- if (ses->packet_patch)
- {
- strcat(ses->more_output, line);
- ses->check_output = utime() + ses->packet_patch;
- break;
- }
- else if (HAS_BIT(ses->flags, SES_FLAG_AUTOPATCH))
- {
- if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
- {
- ses->check_output = utime() + 100000ULL;
- }
- }
- }
- }
- }
- if (ses->more_output[0])
- {
- if (ses->check_output)
- {
- strcat(ses->more_output, line);
- strcpy(linebuf, ses->more_output);
- ses->more_output[0] = 0;
- }
- else
- {
- strcpy(linebuf, line);
- }
- }
- else
- {
- strcpy(linebuf, line);
- }
- if (HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
- {
- char tempbuf[BUFFER_SIZE];
- all_to_utf8(ses, linebuf, tempbuf);
- process_mud_output(ses, tempbuf, next_line == NULL);
- }
- else
- {
- process_mud_output(ses, linebuf, next_line == NULL);
- }
- }
- DEL_BIT(cts->flags, SES_FLAG_READMUD);
- if (HAS_BIT(gtd->ses->flags, SES_FLAG_SPLIT))
- {
- restore_pos(gtd->ses);
- }
- pop_call();
- return;
- }
- void process_mud_output(struct session *ses, char *linebuf, int prompt)
- {
- char line[STRING_SIZE];
- push_call("process_mud_output(%p,%p,%d)",ses,linebuf,prompt);
- ses->check_output = 0;
- strip_vt102_codes(linebuf, line);
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "RECEIVED LINE", linebuf, line);
- if (check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "CATCH RECEIVED LINE", linebuf, line))
- {
- pop_call();
- return;
- }
- if (prompt)
- {
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "RECEIVED PROMPT", linebuf, line);
- if (check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "CATCH RECEIVED PROMPT", linebuf, line))
- {
- pop_call();
- return;
- }
- }
- if (HAS_BIT(ses->flags, SES_FLAG_COLORPATCH))
- {
- sprintf(line, "%s%s%s", ses->color_patch, linebuf, "\e[0m");
- get_color_codes(ses->color_patch, linebuf, ses->color_patch, GET_ALL);
- linebuf = line;
- }
- do_one_line(linebuf, ses); /* changes linebuf */
- /*
- Take care of gags, vt102 support still goes
- */
- if (HAS_BIT(ses->flags, SES_FLAG_GAG))
- {
- DEL_BIT(ses->flags, SES_FLAG_GAG);
- strip_non_vt102_codes(linebuf, line);
- print_stdout("%s", line);
- strip_vt102_codes(linebuf, line);
- show_info(ses, LIST_GAG, "#INFO GAG {%s}", line);
- pop_call();
- return;
- }
- if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "CATCH BUFFERED LINE", linebuf, line))
- {
- add_line_buffer(ses, linebuf, prompt);
- }
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "BUFFERED LINE", linebuf, line);
- if (ses == gtd->ses)
- {
- char *output = str_dup(linebuf);
- print_line(ses, &output, prompt);
- str_free(output);
- if (prompt)
- {
- if (!IS_SPLIT(ses))
- {
- gtd->input_off = 1 + strip_vt102_strlen(ses, linebuf);
- }
- }
- }
- pop_call();
- return;
- }
|