xref: /original-bsd/usr.bin/talk/ctl.c (revision 2e271f8d)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)ctl.c	5.3 (Berkeley) 05/20/88";
15 #endif /* not lint */
16 
17 /*
18  * This file handles haggling with the various talk daemons to
19  * get a socket to talk to. sockt is opened and connected in
20  * the progress
21  */
22 
23 #include "talk_ctl.h"
24 
25 struct	sockaddr_in daemon_addr = { AF_INET };
26 struct	sockaddr_in ctl_addr = { AF_INET };
27 struct	sockaddr_in my_addr = { AF_INET };
28 
29 	/* inet addresses of the two machines */
30 struct	in_addr my_machine_addr;
31 struct	in_addr his_machine_addr;
32 
33 u_short daemon_port;	/* port number of the talk daemon */
34 
35 int	ctl_sockt;
36 int	sockt;
37 int	invitation_waiting = 0;
38 
39 CTL_MSG msg;
40 
41 open_sockt()
42 {
43 	int length;
44 
45 	my_addr.sin_addr = my_machine_addr;
46 	my_addr.sin_port = 0;
47 	sockt = socket(AF_INET, SOCK_STREAM, 0);
48 	if (sockt <= 0)
49 		p_error("Bad socket");
50 	if (bind(sockt, &my_addr, sizeof(my_addr)) != 0)
51 		p_error("Binding local socket");
52 	length = sizeof(my_addr);
53 	if (getsockname(sockt, &my_addr, &length) == -1)
54 		p_error("Bad address for socket");
55 }
56 
57 /* open the ctl socket */
58 open_ctl()
59 {
60 	int length;
61 
62 	ctl_addr.sin_port = 0;
63 	ctl_addr.sin_addr = my_machine_addr;
64 	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
65 	if (ctl_sockt <= 0)
66 		p_error("Bad socket");
67 	if (bind(ctl_sockt, &ctl_addr, sizeof(ctl_addr), 0) != 0)
68 		p_error("Couldn't bind to control socket");
69 	length = sizeof(ctl_addr);
70 	if (getsockname(ctl_sockt, &ctl_addr, &length) == -1)
71 		p_error("Bad address for ctl socket");
72 }
73 
74 /* print_addr is a debug print routine */
75 print_addr(addr)
76 	struct sockaddr_in addr;
77 {
78 	int i;
79 
80 	printf("addr = %x, port = %o, family = %o zero = ",
81 		addr.sin_addr, addr.sin_port, addr.sin_family);
82 	for (i = 0; i<8;i++)
83 	printf("%o ", (int)addr.sin_zero[i]);
84 	putchar('\n');
85 }
86