1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 33 34 /* 35 * This file contains the I/O handling and the exchange of 36 * edit characters. This connection itself is established in 37 * ctl.c 38 */ 39 40 #include <sys/filio.h> 41 42 #include <errno.h> 43 #include <signal.h> 44 #include <netdb.h> 45 #include <poll.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #define _XOPEN_SOURCE_EXTENDED 51 #include <curses.h> 52 53 #include "talk.h" 54 #include "talk_ctl.h" 55 56 extern void display(xwin_t *, wchar_t *); 57 58 volatile sig_atomic_t gotwinch = 0; 59 60 /* 61 * The routine to do the actual talking 62 */ 63 void 64 talk(void) 65 { 66 struct hostent *hp, *hp2; 67 struct pollfd fds[2]; 68 int nb; 69 wchar_t buf[BUFSIZ]; 70 char **addr, *his_machine_name; 71 FILE *sockfp; 72 73 his_machine_name = NULL; 74 hp = gethostbyaddr((const char *)&his_machine_addr.s_addr, 75 sizeof(his_machine_addr.s_addr), AF_INET); 76 if (hp != NULL) { 77 hp2 = gethostbyname(hp->h_name); 78 if (hp2 != NULL && hp2->h_addrtype == AF_INET && 79 hp2->h_length == sizeof(his_machine_addr)) 80 for (addr = hp2->h_addr_list; *addr != NULL; addr++) 81 if (memcmp(*addr, &his_machine_addr, 82 sizeof(his_machine_addr)) == 0) { 83 his_machine_name = strdup(hp->h_name); 84 break; 85 } 86 } 87 if (his_machine_name == NULL) 88 his_machine_name = strdup(inet_ntoa(his_machine_addr)); 89 snprintf((char *)buf, sizeof(buf), "Connection established with %s@%s.", 90 msg.r_name, his_machine_name); 91 free(his_machine_name); 92 message((char *)buf); 93 write(STDOUT_FILENO, "\007\007\007", 3); 94 95 current_line = 0; 96 97 if ((sockfp = fdopen(sockt, "w+")) == NULL) 98 p_error("fdopen"); 99 100 setvbuf(sockfp, NULL, _IONBF, 0); 101 setvbuf(stdin, NULL, _IONBF, 0); 102 103 /* 104 * Wait on both the other process (sockt) and standard input. 105 */ 106 for (;;) { 107 fds[0].fd = fileno(stdin); 108 fds[0].events = POLLIN; 109 fds[1].fd = sockt; 110 fds[1].events = POLLIN; 111 nb = poll(fds, 2, INFTIM); 112 if (gotwinch) { 113 resize_display(); 114 gotwinch = 0; 115 } 116 if (nb <= 0) { 117 if (errno == EINTR) 118 continue; 119 /* Panic, we don't know what happened. */ 120 p_error("Unexpected error from poll"); 121 quit(); 122 } 123 if (fds[1].revents & POLLIN) { 124 wint_t w; 125 126 /* There is data on sockt. */ 127 w = fgetwc(sockfp); 128 if (w == WEOF) { 129 message("Connection closed. Exiting"); 130 quit(); 131 } 132 display(&his_win, &w); 133 } 134 if (fds[0].revents & POLLIN) { 135 wint_t w; 136 137 if ((w = getwchar()) != WEOF) { 138 display(&my_win, &w); 139 (void )fputwc(w, sockfp); 140 (void )fflush(sockfp); 141 } 142 } 143 } 144 } 145 146 /* 147 * p_error prints the system error message on the standard location 148 * on the screen and then exits. (i.e. a curses version of perror) 149 */ 150 void 151 p_error(const char *string) 152 { 153 wmove(my_win.x_win, current_line, 0); 154 wprintw(my_win.x_win, "[%s : %s (%d)]\n", 155 string, strerror(errno), errno); 156 wrefresh(my_win.x_win); 157 move(LINES-1, 0); 158 refresh(); 159 quit(); 160 } 161 162 /* 163 * Display string in the standard location 164 */ 165 void 166 message(const char *string) 167 { 168 wmove(my_win.x_win, current_line, 0); 169 wprintw(my_win.x_win, "[%s]\n", string); 170 if (current_line < my_win.x_nlines - 1) 171 current_line++; 172 wrefresh(my_win.x_win); 173 } 174