xref: /original-bsd/usr.bin/talk/ctl.c (revision 72b8f354)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)ctl.c	5.6 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 /*
13  * This file handles haggling with the various talk daemons to
14  * get a socket to talk to. sockt is opened and connected in
15  * the progress
16  */
17 
18 #include "talk_ctl.h"
19 
20 struct	sockaddr_in daemon_addr = { sizeof(daemon_addr), AF_INET };
21 struct	sockaddr_in ctl_addr = { sizeof(ctl_addr), AF_INET };
22 struct	sockaddr_in my_addr = { sizeof(my_addr), AF_INET };
23 
24 	/* inet addresses of the two machines */
25 struct	in_addr my_machine_addr;
26 struct	in_addr his_machine_addr;
27 
28 u_short daemon_port;	/* port number of the talk daemon */
29 
30 int	ctl_sockt;
31 int	sockt;
32 int	invitation_waiting = 0;
33 
34 CTL_MSG msg;
35 
36 open_sockt()
37 {
38 	int length;
39 
40 	my_addr.sin_addr = my_machine_addr;
41 	my_addr.sin_port = 0;
42 	sockt = socket(AF_INET, SOCK_STREAM, 0);
43 	if (sockt <= 0)
44 		p_error("Bad socket");
45 	if (bind(sockt, &my_addr, sizeof(my_addr)) != 0)
46 		p_error("Binding local socket");
47 	length = sizeof(my_addr);
48 	if (getsockname(sockt, &my_addr, &length) == -1)
49 		p_error("Bad address for socket");
50 }
51 
52 /* open the ctl socket */
53 open_ctl()
54 {
55 	int length;
56 
57 	ctl_addr.sin_port = 0;
58 	ctl_addr.sin_addr = my_machine_addr;
59 	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
60 	if (ctl_sockt <= 0)
61 		p_error("Bad socket");
62 	if (bind(ctl_sockt, &ctl_addr, sizeof(ctl_addr), 0) != 0)
63 		p_error("Couldn't bind to control socket");
64 	length = sizeof(ctl_addr);
65 	if (getsockname(ctl_sockt, &ctl_addr, &length) == -1)
66 		p_error("Bad address for ctl socket");
67 }
68 
69 /* print_addr is a debug print routine */
70 print_addr(addr)
71 	struct sockaddr_in addr;
72 {
73 	int i;
74 
75 	printf("addr = %x, port = %o, family = %o zero = ",
76 		addr.sin_addr, addr.sin_port, addr.sin_family);
77 	for (i = 0; i<8;i++)
78 	printf("%o ", (int)addr.sin_zero[i]);
79 	putchar('\n');
80 }
81