look_up.c revision 141563
1188840Sscottl/* 2188840Sscottl * Copyright (c) 1983, 1993 3188840Sscottl * The Regents of the University of California. All rights reserved. 4188840Sscottl * 5188840Sscottl * Redistribution and use in source and binary forms, with or without 6188840Sscottl * modification, are permitted provided that the following conditions 7188840Sscottl * are met: 8188840Sscottl * 1. Redistributions of source code must retain the above copyright 9188840Sscottl * notice, this list of conditions and the following disclaimer. 10188840Sscottl * 2. Redistributions in binary form must reproduce the above copyright 11188840Sscottl * notice, this list of conditions and the following disclaimer in the 12188840Sscottl * documentation and/or other materials provided with the distribution. 13188840Sscottl * 3. All advertising materials mentioning features or use of this software 14188840Sscottl * must display the following acknowledgement: 15188840Sscottl * This product includes software developed by the University of 16188840Sscottl * California, Berkeley and its contributors. 17188840Sscottl * 4. Neither the name of the University nor the names of its contributors 18188840Sscottl * may be used to endorse or promote products derived from this software 19188840Sscottl * without specific prior written permission. 20188840Sscottl * 21188840Sscottl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22188840Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23188840Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24188840Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25188840Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26188840Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27188840Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28188840Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29188840Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30188840Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31188840Sscottl * SUCH DAMAGE. 32188840Sscottl */ 33188840Sscottl 34188840Sscottl#include <sys/cdefs.h> 35188840Sscottl 36188840Sscottl__FBSDID("$FreeBSD: head/usr.bin/talk/look_up.c 141563 2005-02-09 09:13:36Z stefanf $"); 37188840Sscottl 38188840Sscottl#ifndef lint 39188840Sscottlstatic const char sccsid[] = "@(#)look_up.c 8.1 (Berkeley) 6/6/93"; 40188840Sscottl#endif 41188840Sscottl 42188840Sscottl#include <sys/types.h> 43188840Sscottl#include <sys/socket.h> 44188840Sscottl#include <protocols/talkd.h> 45188840Sscottl 46188840Sscottl#include <errno.h> 47188840Sscottl#include <string.h> 48188840Sscottl 49188840Sscottl#include "talk_ctl.h" 50188840Sscottl#include "talk.h" 51188840Sscottl 52188840Sscottl/* 53188840Sscottl * See if the local daemon has an invitation for us. 54188840Sscottl */ 55188840Sscottlint 56188840Sscottlcheck_local() 57188840Sscottl{ 58188840Sscottl CTL_RESPONSE response; 59188840Sscottl CTL_RESPONSE *rp = &response; 60188840Sscottl struct sockaddr addr; 61188840Sscottl 62188840Sscottl /* the rest of msg was set up in get_names */ 63188840Sscottl#ifdef MSG_EOR 64188840Sscottl /* copy new style sockaddr to old, swap family (short in old) */ 65188840Sscottl msg.ctl_addr = *(struct osockaddr *)&ctl_addr; 66188840Sscottl msg.ctl_addr.sa_family = htons(ctl_addr.sin_family); 67188840Sscottl#else 68188840Sscottl msg.ctl_addr = *(struct sockaddr *)&ctl_addr; 69188840Sscottl#endif 70188840Sscottl /* must be initiating a talk */ 71188840Sscottl if (!look_for_invite(rp)) 72188840Sscottl return (0); 73188840Sscottl /* 74188840Sscottl * There was an invitation waiting for us, 75188840Sscottl * so connect with the other (hopefully waiting) party 76188840Sscottl */ 77188840Sscottl current_state = "Waiting to connect with caller"; 78188840Sscottl do { 79188840Sscottl if (rp->addr.sa_family != AF_INET) 80188840Sscottl p_error("Response uses invalid network address"); 81188840Sscottl (void)memcpy(&addr, &rp->addr.sa_family, sizeof(addr)); 82188840Sscottl addr.sa_family = rp->addr.sa_family; 83188840Sscottl addr.sa_len = sizeof(addr); 84188840Sscottl errno = 0; 85188840Sscottl if (connect(sockt, &addr, sizeof(addr)) != -1) 86188840Sscottl return (1); 87188840Sscottl } while (errno == EINTR); 88188840Sscottl if (errno == ECONNREFUSED) { 89188840Sscottl /* 90188840Sscottl * The caller gave up, but his invitation somehow 91188840Sscottl * was not cleared. Clear it and initiate an 92188840Sscottl * invitation. (We know there are no newer invitations, 93188840Sscottl * the talkd works LIFO.) 94188840Sscottl */ 95188840Sscottl ctl_transact(his_machine_addr, msg, DELETE, rp); 96188840Sscottl close(sockt); 97188840Sscottl open_sockt(); 98188840Sscottl return (0); 99188840Sscottl } 100188840Sscottl p_error("Unable to connect with initiator"); 101188840Sscottl /*NOTREACHED*/ 102188840Sscottl return (0); 103188840Sscottl} 104188840Sscottl 105188840Sscottl/* 106188840Sscottl * Look for an invitation on 'machine' 107188840Sscottl */ 108188840Sscottlint 109188840Sscottllook_for_invite(rp) 110188840Sscottl CTL_RESPONSE *rp; 111188840Sscottl{ 112188840Sscottl current_state = "Checking for invitation on caller's machine"; 113188840Sscottl ctl_transact(his_machine_addr, msg, LOOK_UP, rp); 114188840Sscottl /* the switch is for later options, such as multiple invitations */ 115188840Sscottl switch (rp->answer) { 116188840Sscottl 117188840Sscottl case SUCCESS: 118188840Sscottl msg.id_num = htonl(rp->id_num); 119188840Sscottl return (1); 120188840Sscottl 121188840Sscottl default: 122188840Sscottl /* there wasn't an invitation waiting for us */ 123188840Sscottl return (0); 124188840Sscottl } 125188840Sscottl} 126188840Sscottl