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