ctl.c revision 37453
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 * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3532503Scharnier#if 0
361590Srgrimesstatic char sccsid[] = "@(#)ctl.c	8.1 (Berkeley) 6/6/93";
3732503Scharnier#endif
3832503Scharnierstatic const char rcsid[] =
3937453Sbde	"$Id: ctl.c,v 1.4 1998/01/14 07:20:59 charnier Exp $";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes/*
431590Srgrimes * This file handles haggling with the various talk daemons to
441590Srgrimes * get a socket to talk to. sockt is opened and connected in
451590Srgrimes * the progress
461590Srgrimes */
471590Srgrimes
481590Srgrimes#include <sys/types.h>
491590Srgrimes#include <sys/socket.h>
501590Srgrimes#include "talk.h"
511590Srgrimes
521590Srgrimesstruct	sockaddr_in daemon_addr = { sizeof(daemon_addr), AF_INET };
531590Srgrimesstruct	sockaddr_in ctl_addr = { sizeof(ctl_addr), AF_INET };
541590Srgrimesstruct	sockaddr_in my_addr = { sizeof(my_addr), AF_INET };
551590Srgrimes
561590Srgrimes	/* inet addresses of the two machines */
571590Srgrimesstruct	in_addr my_machine_addr;
581590Srgrimesstruct	in_addr his_machine_addr;
591590Srgrimes
601590Srgrimesu_short daemon_port;	/* port number of the talk daemon */
611590Srgrimes
621590Srgrimesint	ctl_sockt;
631590Srgrimesint	sockt;
641590Srgrimesint	invitation_waiting = 0;
651590Srgrimes
661590SrgrimesCTL_MSG msg;
671590Srgrimes
6814443Sjoergvoid
691590Srgrimesopen_sockt()
701590Srgrimes{
711590Srgrimes	int length;
721590Srgrimes
731590Srgrimes	my_addr.sin_addr = my_machine_addr;
741590Srgrimes	my_addr.sin_port = 0;
751590Srgrimes	sockt = socket(AF_INET, SOCK_STREAM, 0);
761590Srgrimes	if (sockt <= 0)
771590Srgrimes		p_error("Bad socket");
781590Srgrimes	if (bind(sockt, (struct sockaddr *)&my_addr, sizeof(my_addr)) != 0)
791590Srgrimes		p_error("Binding local socket");
801590Srgrimes	length = sizeof(my_addr);
811590Srgrimes	if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1)
821590Srgrimes		p_error("Bad address for socket");
831590Srgrimes}
841590Srgrimes
851590Srgrimes/* open the ctl socket */
8614443Sjoergvoid
878874Srgrimesopen_ctl()
881590Srgrimes{
891590Srgrimes	int length;
901590Srgrimes
911590Srgrimes	ctl_addr.sin_port = 0;
921590Srgrimes	ctl_addr.sin_addr = my_machine_addr;
931590Srgrimes	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
941590Srgrimes	if (ctl_sockt <= 0)
951590Srgrimes		p_error("Bad socket");
961590Srgrimes	if (bind(ctl_sockt,
971590Srgrimes	    (struct sockaddr *)&ctl_addr, sizeof(ctl_addr)) != 0)
981590Srgrimes		p_error("Couldn't bind to control socket");
991590Srgrimes	length = sizeof(ctl_addr);
1001590Srgrimes	if (getsockname(ctl_sockt,
1011590Srgrimes	    (struct sockaddr *)&ctl_addr, &length) == -1)
1021590Srgrimes		p_error("Bad address for ctl socket");
1031590Srgrimes}
1041590Srgrimes
1051590Srgrimes/* print_addr is a debug print routine */
10614443Sjoergvoid
1071590Srgrimesprint_addr(addr)
1081590Srgrimes	struct sockaddr_in addr;
1091590Srgrimes{
1101590Srgrimes	int i;
1111590Srgrimes
11214443Sjoerg	printf("addr = %lx, port = %o, family = %o zero = ",
11337453Sbde		(u_long)addr.sin_addr.s_addr, addr.sin_port, addr.sin_family);
1141590Srgrimes	for (i = 0; i<8;i++)
1151590Srgrimes	printf("%o ", (int)addr.sin_zero[i]);
1161590Srgrimes	putchar('\n');
1171590Srgrimes}
118