Index: head/include/protocols/talkd.h =================================================================== --- head/include/protocols/talkd.h (revision 343160) +++ head/include/protocols/talkd.h (revision 343161) @@ -1,114 +1,123 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)talkd.h 8.1 (Berkeley) 6/2/93 * * $FreeBSD$ */ #ifndef _PROTOCOLS_TALKD_H_ #define _PROTOCOLS_TALKD_H_ /* * This describes the protocol used by the talk server and clients. * * The talk server acts a repository of invitations, responding to * requests by clients wishing to rendezvous for the purpose of * holding a conversation. In normal operation, a client, the caller, * initiates a rendezvous by sending a CTL_MSG to the server of * type LOOK_UP. This causes the server to search its invitation * tables to check if an invitation currently exists for the caller * (to speak to the callee specified in the message). If the lookup * fails, the caller then sends an ANNOUNCE message causing the server * to broadcast an announcement on the callee's login ports requesting * contact. When the callee responds, the local server uses the * recorded invitation to respond with the appropriate rendezvous * address and the caller and callee client programs establish a * stream connection through which the conversation takes place. */ /* + * The talk protocol embeds a 4.3BSD sockaddr. Define our own version + * rather then relying on namespace polution in kernel headers. + */ +struct tsockaddr { + unsigned short sa_family; + char sa_data[14]; +}; + +/* * Client->server request message format. */ typedef struct { u_char vers; /* protocol version */ u_char type; /* request type, see below */ u_char answer; /* not used */ u_char pad; u_int32_t id_num; /* message id */ - struct osockaddr addr; /* old (4.3) style */ - struct osockaddr ctl_addr; /* old (4.3) style */ + struct tsockaddr addr; /* old (4.3) style */ + struct tsockaddr ctl_addr; /* old (4.3) style */ int32_t pid; /* caller's process id */ #define NAME_SIZE 12 char l_name[NAME_SIZE];/* caller's name */ char r_name[NAME_SIZE];/* callee's name */ #define TTY_SIZE 16 char r_tty[TTY_SIZE];/* callee's tty name */ } CTL_MSG; /* * Server->client response message format. */ typedef struct { u_char vers; /* protocol version */ u_char type; /* type of request message, see below */ u_char answer; /* respose to request message, see below */ u_char pad; u_int32_t id_num; /* message id */ - struct osockaddr addr; /* address for establishing conversation */ + struct tsockaddr addr; /* address for establishing conversation */ } CTL_RESPONSE; #define TALK_VERSION 1 /* protocol version */ /* message type values */ #define LEAVE_INVITE 0 /* leave invitation with server */ #define LOOK_UP 1 /* check for invitation by callee */ #define DELETE 2 /* delete invitation by caller */ #define ANNOUNCE 3 /* announce invitation by caller */ /* answer values */ #define SUCCESS 0 /* operation completed properly */ #define NOT_HERE 1 /* callee not logged in */ #define FAILED 2 /* operation failed for unexplained reason */ #define MACHINE_UNKNOWN 3 /* caller's machine name unknown */ #define PERMISSION_DENIED 4 /* callee's tty doesn't permit announce */ #define UNKNOWN_REQUEST 5 /* request has invalid type value */ #define BADVERSION 6 /* request has invalid protocol version */ #define BADADDR 7 /* request has invalid addr value */ #define BADCTLADDR 8 /* request has invalid ctl_addr value */ /* * Operational parameters. */ #define MAX_LIFE 60 /* max time daemon saves invitations */ /* RING_WAIT should be 10's of seconds less than MAX_LIFE */ #define RING_WAIT 30 /* time to wait before resending invitation */ #endif /* !_TALKD_H_ */ Index: head/libexec/talkd/talkd.c =================================================================== --- head/libexec/talkd/talkd.c (revision 343160) +++ head/libexec/talkd/talkd.c (revision 343161) @@ -1,138 +1,139 @@ /* * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1983, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)talkd.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ /* * The top level of the daemon, the format is heavily borrowed * from rwhod.c. Basically: find out who and where you are; * disconnect all descriptors and ttys, and then endless * loop on waiting for and processing requests */ #include <sys/types.h> #include <sys/socket.h> #include <sys/param.h> #include <netinet/in.h> #include <protocols/talkd.h> #include <err.h> #include <errno.h> #include <paths.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <syslog.h> #include <time.h> #include <unistd.h> #include "extern.h" static CTL_MSG request; static CTL_RESPONSE response; int debug = 0; static long lastmsgtime; char hostname[MAXHOSTNAMELEN]; #define TIMEOUT 30 #define MAXIDLE 120 int main(int argc, char *argv[]) { register CTL_MSG *mp = &request; int cc; struct sockaddr ctl_addr; #ifdef NOTDEF /* * removed so ntalkd can run in tty sandbox */ if (getuid()) errx(1, "getuid: not super-user"); #endif openlog("talkd", LOG_PID, LOG_DAEMON); if (gethostname(hostname, sizeof(hostname) - 1) < 0) { syslog(LOG_ERR, "gethostname: %m"); _exit(1); } hostname[sizeof(hostname) - 1] = '\0'; if (chdir(_PATH_DEV) < 0) { syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); _exit(1); } if (argc > 1 && strcmp(argv[1], "-d") == 0) debug = 1; signal(SIGALRM, timeout); alarm(TIMEOUT); for (;;) { cc = recv(0, (char *)mp, sizeof(*mp), 0); if (cc != sizeof (*mp)) { if (cc < 0 && errno != EINTR) syslog(LOG_WARNING, "recv: %m"); continue; } lastmsgtime = time(0); - (void)memcpy(&ctl_addr, &mp->ctl_addr, sizeof(ctl_addr)); + (void)memcpy(&ctl_addr.sa_data, &mp->ctl_addr.sa_data, + sizeof(ctl_addr.sa_data)); ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family); ctl_addr.sa_len = sizeof(ctl_addr); process_request(mp, &response); /* can block here, is this what I want? */ cc = sendto(STDIN_FILENO, (char *)&response, sizeof(response), 0, &ctl_addr, sizeof(ctl_addr)); if (cc != sizeof (response)) syslog(LOG_WARNING, "sendto: %m"); } } void timeout(int sig __unused) { int save_errno = errno; if (time(0) - lastmsgtime >= MAXIDLE) _exit(0); alarm(TIMEOUT); errno = save_errno; } Index: head/usr.bin/talk/invite.c =================================================================== --- head/usr.bin/talk/invite.c (revision 343160) +++ head/usr.bin/talk/invite.c (revision 343161) @@ -1,198 +1,194 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93"; #endif #include <sys/types.h> #include <sys/socket.h> #include <protocols/talkd.h> #include <err.h> #include <errno.h> #include <setjmp.h> #include <signal.h> #include <unistd.h> #include "talk_ctl.h" #include "talk.h" /* * There wasn't an invitation waiting, so send a request containing * our sockt address to the remote talk daemon so it can invite * him */ /* * The msg.id's for the invitations * on the local and remote machines. * These are used to delete the * invitations. */ static int local_id, remote_id; static jmp_buf invitebuf; void invite_remote(void) { int new_sockt; struct itimerval itimer; CTL_RESPONSE response; itimer.it_value.tv_sec = RING_WAIT; itimer.it_value.tv_usec = 0; itimer.it_interval = itimer.it_value; if (listen(sockt, 5) != 0) p_error("Error on attempt to listen for caller"); -#ifdef MSG_EOR /* copy new style sockaddr to old, swap family (short in old) */ - msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/ + msg.addr = *(struct tsockaddr *)&my_addr; msg.addr.sa_family = htons(my_addr.sin_family); -#else - msg.addr = *(struct sockaddr *)&my_addr; -#endif msg.id_num = htonl(-1); /* an impossible id_num */ invitation_waiting = 1; announce_invite(); /* * Shut off the automatic messages for a while, * so we can use the interrupt timer to resend the invitation */ end_msgs(); setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); message("Waiting for your party to respond"); signal(SIGALRM, re_invite); (void) setjmp(invitebuf); while ((new_sockt = accept(sockt, 0, 0)) < 0) { if (errno == EINTR) continue; p_error("Unable to connect with your party"); } close(sockt); sockt = new_sockt; /* * Have the daemons delete the invitations now that we * have connected. */ current_state = "Waiting for your party to respond"; start_msgs(); msg.id_num = htonl(local_id); ctl_transact(my_machine_addr, msg, DELETE, &response); msg.id_num = htonl(remote_id); ctl_transact(his_machine_addr, msg, DELETE, &response); invitation_waiting = 0; } /* * Routine called on interrupt to re-invite the callee */ /* ARGSUSED */ void re_invite(int signo __unused) { message("Ringing your party again"); waddch(my_win.x_win, '\n'); if (current_line < my_win.x_nlines - 1) current_line++; /* force a re-announce */ msg.id_num = htonl(remote_id + 1); announce_invite(); longjmp(invitebuf, 1); } static const char *answers[] = { "answer #0", /* SUCCESS */ "Your party is not logged on", /* NOT_HERE */ "Target machine is too confused to talk to us", /* FAILED */ "Target machine does not recognize us", /* MACHINE_UNKNOWN */ "Your party is refusing messages", /* PERMISSION_REFUSED */ "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */ "Target machine indicates protocol mismatch", /* BADVERSION */ "Target machine indicates protocol botch (addr)",/* BADADDR */ "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */ }; #define NANSWERS (sizeof (answers) / sizeof (answers[0])) /* * Transmit the invitation and process the response */ void announce_invite(void) { CTL_RESPONSE response; current_state = "Trying to connect to your party's talk daemon"; ctl_transact(his_machine_addr, msg, ANNOUNCE, &response); remote_id = response.id_num; if (response.answer != SUCCESS) { if (response.answer < NANSWERS) message(answers[response.answer]); quit(); } /* leave the actual invitation on my talk daemon */ current_state = "Trying to connect to local talk daemon"; ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response); local_id = response.id_num; } /* * Tell the daemon to remove your invitation */ void send_delete(void) { msg.type = DELETE; /* * This is just an extra clean up, so just send it * and don't wait for an answer */ msg.id_num = htonl(remote_id); daemon_addr.sin_addr = his_machine_addr; if (sendto(ctl_sockt, &msg, sizeof (msg), 0, (struct sockaddr *)&daemon_addr, sizeof (daemon_addr)) != sizeof(msg)) warn("send_delete (remote)"); msg.id_num = htonl(local_id); daemon_addr.sin_addr = my_machine_addr; if (sendto(ctl_sockt, &msg, sizeof (msg), 0, (struct sockaddr *)&daemon_addr, sizeof (daemon_addr)) != sizeof (msg)) warn("send_delete (local)"); } Index: head/usr.bin/talk/look_up.c =================================================================== --- head/usr.bin/talk/look_up.c (revision 343160) +++ head/usr.bin/talk/look_up.c (revision 343161) @@ -1,123 +1,119 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)look_up.c 8.1 (Berkeley) 6/6/93"; #endif #include <sys/types.h> #include <sys/socket.h> #include <protocols/talkd.h> #include <errno.h> #include <string.h> #include <unistd.h> #include "talk_ctl.h" #include "talk.h" /* * See if the local daemon has an invitation for us. */ int check_local(void) { CTL_RESPONSE response; CTL_RESPONSE *rp = &response; struct sockaddr addr; /* the rest of msg was set up in get_names */ -#ifdef MSG_EOR /* copy new style sockaddr to old, swap family (short in old) */ - msg.ctl_addr = *(struct osockaddr *)&ctl_addr; + msg.ctl_addr = *(struct tsockaddr *)&ctl_addr; msg.ctl_addr.sa_family = htons(ctl_addr.sin_family); -#else - msg.ctl_addr = *(struct sockaddr *)&ctl_addr; -#endif /* must be initiating a talk */ if (!look_for_invite(rp)) return (0); /* * There was an invitation waiting for us, * so connect with the other (hopefully waiting) party */ current_state = "Waiting to connect with caller"; do { if (rp->addr.sa_family != AF_INET) p_error("Response uses invalid network address"); (void)memcpy(&addr, &rp->addr.sa_family, sizeof(addr)); addr.sa_family = rp->addr.sa_family; addr.sa_len = sizeof(addr); errno = 0; if (connect(sockt, &addr, sizeof(addr)) != -1) return (1); } while (errno == EINTR); if (errno == ECONNREFUSED) { /* * The caller gave up, but his invitation somehow * was not cleared. Clear it and initiate an * invitation. (We know there are no newer invitations, * the talkd works LIFO.) */ ctl_transact(his_machine_addr, msg, DELETE, rp); close(sockt); open_sockt(); return (0); } p_error("Unable to connect with initiator"); /*NOTREACHED*/ return (0); } /* * Look for an invitation on 'machine' */ int look_for_invite(CTL_RESPONSE *rp) { current_state = "Checking for invitation on caller's machine"; ctl_transact(his_machine_addr, msg, LOOK_UP, rp); /* the switch is for later options, such as multiple invitations */ switch (rp->answer) { case SUCCESS: msg.id_num = htonl(rp->id_num); return (1); default: /* there wasn't an invitation waiting for us */ return (0); } }