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[] = "@(#)ctl.c	8.1 (Berkeley) 6/6/93";
3632503Scharnier#endif
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * This file handles haggling with the various talk daemons to
401590Srgrimes * get a socket to talk to. sockt is opened and connected in
411590Srgrimes * the progress
421590Srgrimes */
431590Srgrimes
441590Srgrimes#include <sys/types.h>
451590Srgrimes#include <sys/socket.h>
46141563Sstefanf
47141563Sstefanf#include <string.h>
48141563Sstefanf
491590Srgrimes#include "talk.h"
501590Srgrimes
51178642Sdelphijstruct	sockaddr_in daemon_addr = { .sin_len = sizeof(daemon_addr), .sin_family = AF_INET };
52178642Sdelphijstruct	sockaddr_in ctl_addr = { .sin_len = sizeof(ctl_addr), .sin_family = AF_INET };
53178642Sdelphijstruct	sockaddr_in my_addr = { .sin_len = sizeof(my_addr), .sin_family = AF_INET };
541590Srgrimes
551590Srgrimes	/* inet addresses of the two machines */
561590Srgrimesstruct	in_addr my_machine_addr;
571590Srgrimesstruct	in_addr his_machine_addr;
581590Srgrimes
591590Srgrimesu_short daemon_port;	/* port number of the talk daemon */
601590Srgrimes
611590Srgrimesint	ctl_sockt;
621590Srgrimesint	sockt;
631590Srgrimesint	invitation_waiting = 0;
641590Srgrimes
651590SrgrimesCTL_MSG msg;
661590Srgrimes
6714443Sjoergvoid
68178642Sdelphijopen_sockt(void)
691590Srgrimes{
70127263Scperciva	socklen_t length;
711590Srgrimes
72120550Stjr	(void)memset(&my_addr, 0, sizeof(my_addr));
73120550Stjr	my_addr.sin_family = AF_INET;
74120550Stjr	my_addr.sin_len = sizeof(my_addr);
751590Srgrimes	my_addr.sin_addr = my_machine_addr;
761590Srgrimes	my_addr.sin_port = 0;
771590Srgrimes	sockt = socket(AF_INET, SOCK_STREAM, 0);
78127263Scperciva	if (sockt == -1)
791590Srgrimes		p_error("Bad socket");
801590Srgrimes	if (bind(sockt, (struct sockaddr *)&my_addr, sizeof(my_addr)) != 0)
811590Srgrimes		p_error("Binding local socket");
821590Srgrimes	length = sizeof(my_addr);
831590Srgrimes	if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1)
841590Srgrimes		p_error("Bad address for socket");
851590Srgrimes}
861590Srgrimes
871590Srgrimes/* open the ctl socket */
8814443Sjoergvoid
89178642Sdelphijopen_ctl(void)
901590Srgrimes{
91127263Scperciva	socklen_t length;
921590Srgrimes
93120550Stjr	(void)memset(&ctl_addr, 0, sizeof(ctl_addr));
94120550Stjr	ctl_addr.sin_family = AF_INET;
95120550Stjr	ctl_addr.sin_len = sizeof(my_addr);
961590Srgrimes	ctl_addr.sin_port = 0;
971590Srgrimes	ctl_addr.sin_addr = my_machine_addr;
981590Srgrimes	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
99127263Scperciva	if (ctl_sockt == -1)
1001590Srgrimes		p_error("Bad socket");
1011590Srgrimes	if (bind(ctl_sockt,
1021590Srgrimes	    (struct sockaddr *)&ctl_addr, sizeof(ctl_addr)) != 0)
1031590Srgrimes		p_error("Couldn't bind to control socket");
1041590Srgrimes	length = sizeof(ctl_addr);
1051590Srgrimes	if (getsockname(ctl_sockt,
1061590Srgrimes	    (struct sockaddr *)&ctl_addr, &length) == -1)
1071590Srgrimes		p_error("Bad address for ctl socket");
1081590Srgrimes}
1091590Srgrimes
1101590Srgrimes/* print_addr is a debug print routine */
11114443Sjoergvoid
112178642Sdelphijprint_addr(struct sockaddr_in addr)
1131590Srgrimes{
1141590Srgrimes	int i;
1151590Srgrimes
11614443Sjoerg	printf("addr = %lx, port = %o, family = %o zero = ",
11737453Sbde		(u_long)addr.sin_addr.s_addr, addr.sin_port, addr.sin_family);
1181590Srgrimes	for (i = 0; i<8;i++)
1191590Srgrimes	printf("%o ", (int)addr.sin_zero[i]);
1201590Srgrimes	putchar('\n');
1211590Srgrimes}
122