1 /*
2 * Copyright (c) 1983, 1993
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 8.1 (Berkeley) 06/06/93";
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 <sys/types.h>
19 #include <sys/socket.h>
20 #include <protocols/talkd.h>
21 #include <netinet/in.h>
22 #include "talk.h"
23 #include "talk_ctl.h"
24
25 struct sockaddr_in daemon_addr = { sizeof(daemon_addr), AF_INET };
26 struct sockaddr_in ctl_addr = { sizeof(ctl_addr), AF_INET };
27 struct sockaddr_in my_addr = { sizeof(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
open_sockt()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, (struct sockaddr *)&my_addr, sizeof(my_addr)) != 0)
51 p_error("Binding local socket");
52 length = sizeof(my_addr);
53 if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1)
54 p_error("Bad address for socket");
55 }
56
57 /* open the ctl socket */
open_ctl()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,
68 (struct sockaddr *)&ctl_addr, sizeof(ctl_addr)) != 0)
69 p_error("Couldn't bind to control socket");
70 length = sizeof(ctl_addr);
71 if (getsockname(ctl_sockt,
72 (struct sockaddr *)&ctl_addr, &length) == -1)
73 p_error("Bad address for ctl socket");
74 }
75
76 /* print_addr is a debug print routine */
77 print_addr(addr)
78 struct sockaddr_in addr;
79 {
80 int i;
81
82 printf("addr = %x, port = %o, family = %o zero = ",
83 addr.sin_addr, addr.sin_port, addr.sin_family);
84 for (i = 0; i<8;i++)
85 printf("%o ", (int)addr.sin_zero[i]);
86 putchar('\n');
87 }
88