xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/talk/ctl.c (revision 84f7a9b9dca4f23b5f50edef0e59d7eb44301114)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * This file handles haggling with the various talk daemons to
44  * get a socket to talk to. sockt is opened and connected in
45  * the progress
46  */
47 
48 #include "talk_ctl.h"
49 #include <libintl.h>
50 
51 struct sockaddr_in daemon_addr = { AF_INET };
52 struct sockaddr_in ctl_addr = { AF_INET };
53 struct sockaddr_in my_addr = { AF_INET };
54 
55 /* inet addresses of the two machines */
56 struct in_addr my_machine_addr;
57 struct in_addr rem_machine_addr;
58 
59 u_short daemon_port;	/* port number of the talk daemon */
60 
61 int ctl_sockt;
62 int sockt;
63 int invitation_waiting = 0;
64 
65 CTL_MSG msg;
66 
67 void
68 open_sockt()
69 {
70 	socklen_t length;
71 
72 	my_addr.sin_addr = my_machine_addr;
73 	my_addr.sin_port = 0;
74 
75 	sockt = socket(AF_INET, SOCK_STREAM, 0);
76 
77 	if (sockt <= 0) {
78 		p_error(gettext("Bad socket"));
79 	}
80 
81 	if (bind(sockt, (struct sockaddr *)&my_addr, sizeof (my_addr)) != 0) {
82 		p_error(gettext("Binding local socket"));
83 	}
84 
85 	length = (socklen_t) sizeof (my_addr);
86 
87 	if (getsockname(sockt, (struct sockaddr *)&my_addr, &length) == -1) {
88 		p_error(gettext("Bad address for socket"));
89 	}
90 }
91 
92 	/* open the ctl socket */
93 
94 void
95 open_ctl()
96 {
97 	socklen_t length;
98 
99 	ctl_addr.sin_port = 0;
100 	ctl_addr.sin_addr = my_machine_addr;
101 
102 	ctl_sockt = socket(AF_INET, SOCK_DGRAM, 0);
103 
104 	if (ctl_sockt <= 0) {
105 		p_error(gettext("Bad socket"));
106 	}
107 
108 	if (bind(ctl_sockt, (struct sockaddr *)&ctl_addr, sizeof (ctl_addr))
109 		!= 0) {
110 		p_error(gettext("Couldn't bind to control socket"));
111 	}
112 
113 	length = (socklen_t) sizeof (ctl_addr);
114 	if (getsockname(ctl_sockt, (struct sockaddr *)&ctl_addr, &length)
115 		== -1) {
116 		p_error(gettext("Bad address for ctl socket"));
117 	}
118 }
119 
120 /* print_addr is a debug print routine */
121 
122 void
123 print_addr(addr)
124 struct sockaddr_in addr;
125 {
126 	int i;
127 
128 	printf("addr = %x, port = %o, family = %o zero = ",
129 		addr.sin_addr, (int)addr.sin_port, addr.sin_family);
130 
131 	for (i = 0; i < 8; i++) {
132 		printf("%o ", (int)addr.sin_zero[i]);
133 	}
134 	putchar('\n');
135 }
136