11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 3087710Smarkm#include <sys/cdefs.h> 3187710Smarkm 3287710Smarkm__FBSDID("$FreeBSD$"); 3387710Smarkm 341590Srgrimes#ifndef lint 3587710Smarkmstatic const char sccsid[] = "@(#)look_up.c 8.1 (Berkeley) 6/6/93"; 3632503Scharnier#endif 371590Srgrimes 381590Srgrimes#include <sys/types.h> 391590Srgrimes#include <sys/socket.h> 401590Srgrimes#include <protocols/talkd.h> 4187710Smarkm 4287710Smarkm#include <errno.h> 43141563Sstefanf#include <string.h> 44200418Sdelphij#include <unistd.h> 4587710Smarkm 461590Srgrimes#include "talk_ctl.h" 471590Srgrimes#include "talk.h" 481590Srgrimes 491590Srgrimes/* 501590Srgrimes * See if the local daemon has an invitation for us. 511590Srgrimes */ 5214443Sjoergint 53178642Sdelphijcheck_local(void) 541590Srgrimes{ 551590Srgrimes CTL_RESPONSE response; 5687710Smarkm CTL_RESPONSE *rp = &response; 57120550Stjr struct sockaddr addr; 581590Srgrimes 591590Srgrimes /* the rest of msg was set up in get_names */ 601590Srgrimes#ifdef MSG_EOR 611590Srgrimes /* copy new style sockaddr to old, swap family (short in old) */ 621590Srgrimes msg.ctl_addr = *(struct osockaddr *)&ctl_addr; 631590Srgrimes msg.ctl_addr.sa_family = htons(ctl_addr.sin_family); 641590Srgrimes#else 651590Srgrimes msg.ctl_addr = *(struct sockaddr *)&ctl_addr; 661590Srgrimes#endif 671590Srgrimes /* must be initiating a talk */ 681590Srgrimes if (!look_for_invite(rp)) 691590Srgrimes return (0); 701590Srgrimes /* 718874Srgrimes * There was an invitation waiting for us, 728874Srgrimes * so connect with the other (hopefully waiting) party 731590Srgrimes */ 741590Srgrimes current_state = "Waiting to connect with caller"; 751590Srgrimes do { 761590Srgrimes if (rp->addr.sa_family != AF_INET) 771590Srgrimes p_error("Response uses invalid network address"); 78120550Stjr (void)memcpy(&addr, &rp->addr.sa_family, sizeof(addr)); 79120550Stjr addr.sa_family = rp->addr.sa_family; 80120550Stjr addr.sa_len = sizeof(addr); 811590Srgrimes errno = 0; 82120550Stjr if (connect(sockt, &addr, sizeof(addr)) != -1) 831590Srgrimes return (1); 841590Srgrimes } while (errno == EINTR); 851590Srgrimes if (errno == ECONNREFUSED) { 861590Srgrimes /* 871590Srgrimes * The caller gave up, but his invitation somehow 888874Srgrimes * was not cleared. Clear it and initiate an 891590Srgrimes * invitation. (We know there are no newer invitations, 901590Srgrimes * the talkd works LIFO.) 911590Srgrimes */ 921590Srgrimes ctl_transact(his_machine_addr, msg, DELETE, rp); 931590Srgrimes close(sockt); 941590Srgrimes open_sockt(); 951590Srgrimes return (0); 961590Srgrimes } 971590Srgrimes p_error("Unable to connect with initiator"); 981590Srgrimes /*NOTREACHED*/ 9914443Sjoerg return (0); 1001590Srgrimes} 1011590Srgrimes 1021590Srgrimes/* 1031590Srgrimes * Look for an invitation on 'machine' 1041590Srgrimes */ 10514443Sjoergint 106178642Sdelphijlook_for_invite(CTL_RESPONSE *rp) 1071590Srgrimes{ 1081590Srgrimes current_state = "Checking for invitation on caller's machine"; 1091590Srgrimes ctl_transact(his_machine_addr, msg, LOOK_UP, rp); 1101590Srgrimes /* the switch is for later options, such as multiple invitations */ 1111590Srgrimes switch (rp->answer) { 1121590Srgrimes 1131590Srgrimes case SUCCESS: 1141590Srgrimes msg.id_num = htonl(rp->id_num); 1151590Srgrimes return (1); 1161590Srgrimes 1171590Srgrimes default: 1181590Srgrimes /* there wasn't an invitation waiting for us */ 1191590Srgrimes return (0); 1201590Srgrimes } 1211590Srgrimes} 122