xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/talk/ctl.c (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*
41  * This file handles haggling with the various talk daemons to
42  * get a socket to talk to. sockt is opened and connected in
43  * the progress
44  */
45 
46 #include "talk_ctl.h"
47 #include <libintl.h>
48 
49 struct sockaddr_in daemon_addr = { AF_INET };
50 struct sockaddr_in ctl_addr = { AF_INET };
51 struct sockaddr_in my_addr = { AF_INET };
52 
53 /* inet addresses of the two machines */
54 struct in_addr my_machine_addr;
55 struct in_addr rem_machine_addr;
56 
57 u_short daemon_port;	/* port number of the talk daemon */
58 
59 int ctl_sockt;
60 int sockt;
61 int invitation_waiting = 0;
62 
63 CTL_MSG msg;
64 
65 void
66 open_sockt()
67 {
68 	socklen_t length;
69 
70 	my_addr.sin_addr = my_machine_addr;
71 	my_addr.sin_port = 0;
72 
73 	sockt = socket(AF_INET, SOCK_STREAM, 0);
74 
75 	if (sockt <= 0) {
76 		p_error(gettext("Bad socket"));
77 	}
78 
79 	if (bind(sockt, (struct sockaddr *)&my_addr, sizeof (my_addr)) != 0) {
80 		p_error(gettext("Binding local socket"));
81 	}
82 
83 	length = (socklen_t) sizeof (my_addr);
84 
85 	if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1) {
86 		p_error(gettext("Bad address for socket"));
87 	}
88 }
89 
90 	/* open the ctl socket */
91 
92 void
93 open_ctl()
94 {
95 	socklen_t length;
96 
97 	ctl_addr.sin_port = 0;
98 	ctl_addr.sin_addr = my_machine_addr;
99 
100 	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
101 
102 	if (ctl_sockt <= 0) {
103 		p_error(gettext("Bad socket"));
104 	}
105 
106 	if (bind(ctl_sockt, (struct sockaddr *)&ctl_addr, sizeof (ctl_addr))
107 		!= 0) {
108 		p_error(gettext("Couldn't bind to control socket"));
109 	}
110 
111 	length = (socklen_t) sizeof (ctl_addr);
112 	if (getsockname(ctl_sockt, (struct sockaddr *)&ctl_addr, &length)
113 		== -1) {
114 		p_error(gettext("Bad address for ctl socket"));
115 	}
116 }
117 
118 /* print_addr is a debug print routine */
119 
120 void
121 print_addr(addr)
122 struct sockaddr_in addr;
123 {
124 	int i;
125 
126 	printf("addr = %x, port = %o, family = %o zero = ",
127 		addr.sin_addr, (int)addr.sin_port, addr.sin_family);
128 
129 	for (i = 0; i < 8; i++) {
130 		printf("%o ", (int)addr.sin_zero[i]);
131 	}
132 	putchar('\n');
133 }
134