xref: /netbsd-src/usr.bin/talk/ctl.c (revision d34c2845b874ea4ec9cbafb52393b636b607ecbb)
1*d34c2845Smatt /*	$NetBSD: ctl.c,v 1.10 2012/03/20 20:34:59 matt Exp $	*/
251207773Sjtc 
361f28255Scgd /*
451207773Sjtc  * Copyright (c) 1983, 1993
551207773Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)ctl.c	8.1 (Berkeley) 6/6/93";
3651207773Sjtc #endif
37*d34c2845Smatt __RCSID("$NetBSD: ctl.c,v 1.10 2012/03/20 20:34:59 matt Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd /*
4161f28255Scgd  * This file handles haggling with the various talk daemons to
4261f28255Scgd  * get a socket to talk to. sockt is opened and connected in
4361f28255Scgd  * the progress
4461f28255Scgd  */
4561f28255Scgd 
4661f28255Scgd #include "talk.h"
4732a9e65fSlukem #include <arpa/inet.h>
4861f28255Scgd #include "talk_ctl.h"
4961f28255Scgd 
50c2293a6fSlukem struct	sockaddr_in daemon_addr = { sizeof(daemon_addr), AF_INET, 0, {0}, {0} };
51c2293a6fSlukem struct	sockaddr_in ctl_addr = { sizeof(ctl_addr), AF_INET, 0, {0}, {0} };
52c2293a6fSlukem struct	sockaddr_in my_addr = { sizeof(my_addr), AF_INET, 0, {0}, {0} };
5361f28255Scgd 
5461f28255Scgd 	/* inet addresses of the two machines */
5561f28255Scgd struct	in_addr my_machine_addr;
5661f28255Scgd struct	in_addr his_machine_addr;
5761f28255Scgd 
5861f28255Scgd u_short daemon_port;	/* port number of the talk daemon */
5961f28255Scgd 
6061f28255Scgd int	ctl_sockt;
6161f28255Scgd int	sockt;
6261f28255Scgd int	invitation_waiting = 0;
6361f28255Scgd 
6461f28255Scgd CTL_MSG msg;
6561f28255Scgd 
6632a9e65fSlukem void
open_sockt(void)6768168df6Sjoerg open_sockt(void)
6861f28255Scgd {
694d4a8466Schristos 	socklen_t length;
7061f28255Scgd 
717ccd2afaSchristos 
727ccd2afaSchristos 	(void)memset(&my_addr, 0, sizeof(my_addr));
737ccd2afaSchristos 	my_addr.sin_family = AF_INET;
747ccd2afaSchristos #ifdef BSD4_4
757ccd2afaSchristos 	my_addr.sin_len = sizeof(my_addr);
767ccd2afaSchristos #endif
7761f28255Scgd 	my_addr.sin_addr = my_machine_addr;
7861f28255Scgd 	my_addr.sin_port = 0;
7961f28255Scgd 	sockt = socket(AF_INET, SOCK_STREAM, 0);
804d4a8466Schristos 	if (sockt == -1)
8161f28255Scgd 		p_error("Bad socket");
8261f28255Scgd 	if (bind(sockt, (struct sockaddr *)&my_addr, sizeof(my_addr)) != 0)
8361f28255Scgd 		p_error("Binding local socket");
8461f28255Scgd 	length = sizeof(my_addr);
8561f28255Scgd 	if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1)
8661f28255Scgd 		p_error("Bad address for socket");
8761f28255Scgd }
8861f28255Scgd 
8961f28255Scgd /* open the ctl socket */
9032a9e65fSlukem void
open_ctl(void)91*d34c2845Smatt open_ctl(void)
9261f28255Scgd {
934d4a8466Schristos 	socklen_t length;
9461f28255Scgd 
957ccd2afaSchristos 	(void)memset(&ctl_addr, 0, sizeof(ctl_addr));
967ccd2afaSchristos 	ctl_addr.sin_family = AF_INET;
977ccd2afaSchristos #ifdef BSD4_4
987ccd2afaSchristos 	ctl_addr.sin_len = sizeof(my_addr);
997ccd2afaSchristos #endif
10061f28255Scgd 	ctl_addr.sin_port = 0;
10161f28255Scgd 	ctl_addr.sin_addr = my_machine_addr;
10261f28255Scgd 	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
1034d4a8466Schristos 	if (ctl_sockt == -1)
10461f28255Scgd 		p_error("Bad socket");
10561f28255Scgd 	if (bind(ctl_sockt,
10661f28255Scgd 	    (struct sockaddr *)&ctl_addr, sizeof(ctl_addr)) != 0)
10761f28255Scgd 		p_error("Couldn't bind to control socket");
10861f28255Scgd 	length = sizeof(ctl_addr);
10961f28255Scgd 	if (getsockname(ctl_sockt,
11061f28255Scgd 	    (struct sockaddr *)&ctl_addr, &length) == -1)
11161f28255Scgd 		p_error("Bad address for ctl socket");
11261f28255Scgd }
11361f28255Scgd 
11461f28255Scgd /* print_addr is a debug print routine */
11532a9e65fSlukem void
print_addr(struct sockaddr_in addr)11668168df6Sjoerg print_addr(struct sockaddr_in addr)
11761f28255Scgd {
11861f28255Scgd 	int i;
11961f28255Scgd 
12032a9e65fSlukem 	printf("addr = %s, port = %o, family = %o zero = ",
12132a9e65fSlukem 		inet_ntoa(addr.sin_addr), addr.sin_port, addr.sin_family);
12261f28255Scgd 	for (i = 0; i<8;i++)
12361f28255Scgd 	printf("%o ", (int)addr.sin_zero[i]);
12461f28255Scgd 	putchar('\n');
12561f28255Scgd }
126