look_up.c revision 87710
1233294Sstas/* 2102644Snectar * Copyright (c) 1983, 1993 355682Smarkm * The Regents of the University of California. All rights reserved. 4142403Snectar * 5233294Sstas * Redistribution and use in source and binary forms, with or without 6233294Sstas * modification, are permitted provided that the following conditions 755682Smarkm * are met: 855682Smarkm * 1. Redistributions of source code must retain the above copyright 955682Smarkm * notice, this list of conditions and the following disclaimer. 1055682Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1155682Smarkm * notice, this list of conditions and the following disclaimer in the 1255682Smarkm * documentation and/or other materials provided with the distribution. 1355682Smarkm * 3. All advertising materials mentioning features or use of this software 1455682Smarkm * must display the following acknowledgement: 1555682Smarkm * This product includes software developed by the University of 1690926Snectar * California, Berkeley and its contributors. 1790926Snectar * 4. Neither the name of the University nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 1990926Snectar * without specific prior written permission. 20233294Sstas * 2190926Snectar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2455682Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28102644Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29102644Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30102644Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31127808Snectar * SUCH DAMAGE. 3290926Snectar */ 33127808Snectar 3455682Smarkm#include <sys/cdefs.h> 3555682Smarkm 3655682Smarkm__FBSDID("$FreeBSD: head/usr.bin/talk/look_up.c 87710 2001-12-11 23:51:14Z markm $"); 3755682Smarkm 3855682Smarkm#ifndef lint 3955682Smarkmstatic const char sccsid[] = "@(#)look_up.c 8.1 (Berkeley) 6/6/93"; 40178825Sdfr#endif 4155682Smarkm 42142403Snectar#include <sys/types.h> 43142403Snectar#include <sys/socket.h> 44142403Snectar#include <protocols/talkd.h> 45142403Snectar 46142403Snectar#include <errno.h> 47142403Snectar 48233294Sstas#include "talk_ctl.h" 49142403Snectar#include "talk.h" 50142403Snectar 51142403Snectar/* 52142403Snectar * See if the local daemon has an invitation for us. 53142403Snectar */ 54142403Snectarint 55142403Snectarcheck_local() 56142403Snectar{ 57142403Snectar CTL_RESPONSE response; 58142403Snectar CTL_RESPONSE *rp = &response; 59142403Snectar 60142403Snectar /* the rest of msg was set up in get_names */ 61142403Snectar#ifdef MSG_EOR 62142403Snectar /* copy new style sockaddr to old, swap family (short in old) */ 63233294Sstas msg.ctl_addr = *(struct osockaddr *)&ctl_addr; 64142403Snectar msg.ctl_addr.sa_family = htons(ctl_addr.sin_family); 65142403Snectar#else 66142403Snectar msg.ctl_addr = *(struct sockaddr *)&ctl_addr; 67142403Snectar#endif 68178825Sdfr /* must be initiating a talk */ 69142403Snectar if (!look_for_invite(rp)) 70142403Snectar return (0); 71142403Snectar /* 72142403Snectar * There was an invitation waiting for us, 73142403Snectar * so connect with the other (hopefully waiting) party 74142403Snectar */ 75142403Snectar current_state = "Waiting to connect with caller"; 76142403Snectar do { 77233294Sstas if (rp->addr.sa_family != AF_INET) 78233294Sstas p_error("Response uses invalid network address"); 79233294Sstas errno = 0; 80233294Sstas if (connect(sockt, 81233294Sstas (struct sockaddr *)&rp->addr, sizeof (rp->addr)) != -1) 82233294Sstas return (1); 83178825Sdfr } while (errno == EINTR); 84178825Sdfr if (errno == ECONNREFUSED) { 85178825Sdfr /* 86178825Sdfr * The caller gave up, but his invitation somehow 87178825Sdfr * was not cleared. Clear it and initiate an 88178825Sdfr * invitation. (We know there are no newer invitations, 89178825Sdfr * the talkd works LIFO.) 90233294Sstas */ 91142403Snectar ctl_transact(his_machine_addr, msg, DELETE, rp); 92142403Snectar close(sockt); 93178825Sdfr open_sockt(); 94142403Snectar return (0); 95142403Snectar } 96233294Sstas p_error("Unable to connect with initiator"); 97142403Snectar /*NOTREACHED*/ 98142403Snectar return (0); 99142403Snectar} 100142403Snectar 101178825Sdfr/* 102178825Sdfr * Look for an invitation on 'machine' 103178825Sdfr */ 104178825Sdfrint 105178825Sdfrlook_for_invite(rp) 106178825Sdfr CTL_RESPONSE *rp; 107178825Sdfr{ 108233294Sstas current_state = "Checking for invitation on caller's machine"; 109233294Sstas ctl_transact(his_machine_addr, msg, LOOK_UP, rp); 110233294Sstas /* the switch is for later options, such as multiple invitations */ 111142403Snectar switch (rp->answer) { 112142403Snectar 113178825Sdfr case SUCCESS: 114178825Sdfr msg.id_num = htonl(rp->id_num); 115142403Snectar return (1); 116233294Sstas 117233294Sstas default: 118233294Sstas /* there wasn't an invitation waiting for us */ 119233294Sstas return (0); 120233294Sstas } 121233294Sstas} 122233294Sstas