1 /*-
2 * Copyright (c) 1983, 1985
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)ctl.c 5.1 (Berkeley) 6/6/85";
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 = { AF_INET };
21 struct sockaddr_in ctl_addr = { AF_INET };
22 struct sockaddr_in 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 void
open_sockt()37 open_sockt()
38 {
39 int length;
40
41 my_addr.sin_addr = my_machine_addr;
42 my_addr.sin_port = 0;
43 sockt = socket(AF_INET, SOCK_STREAM, 0);
44 if (sockt <= 0)
45 p_error("Bad socket");
46 if (bind(sockt, (struct sockaddr *)&my_addr, sizeof(my_addr)) != 0)
47 p_error("Binding local socket");
48 length = sizeof(my_addr);
49 if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1)
50 p_error("Bad address for socket");
51 }
52
53 /* open the ctl socket */
54 void
open_ctl()55 open_ctl()
56 {
57 int length;
58
59 ctl_addr.sin_port = 0;
60 ctl_addr.sin_addr = my_machine_addr;
61 ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
62 if (ctl_sockt <= 0)
63 p_error("Bad socket");
64 if (bind(ctl_sockt, (struct sockaddr *)&ctl_addr, sizeof(ctl_addr)) != 0)
65 p_error("Couldn't bind to control socket");
66 length = sizeof(ctl_addr);
67 if (getsockname(ctl_sockt, (struct sockaddr *)&ctl_addr, &length) == -1)
68 p_error("Bad address for ctl socket");
69 }
70
71 /* print_addr is a debug print routine */
72 void
print_addr(addr)73 print_addr(addr)
74 struct sockaddr_in addr;
75 {
76 int i;
77
78 printf("addr = %lx, port = %o, family = %o zero = ",
79 (long)addr.sin_addr.s_addr, addr.sin_port, addr.sin_family);
80 for (i = 0; i<8;i++)
81 printf("%o ", (int)addr.sin_zero[i]);
82 putchar('\n');
83 }
84