xref: /titanic_51/usr/src/cmd/cmd-inet/usr.bin/talk/get_addrs.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
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 #include "talk_ctl.h"
43 #include <locale.h>
44 
45 #ifdef SYSV
46 #define	bcmp(a, b, c)	memcmp((a), (b), (c))
47 #define	bcopy(a, b, c)	memcpy((b), (a), (c))
48 #endif /* SYSV */
49 
50 struct hostent *gethostbyname();
51 struct servent *getservbyname();
52 
53 void
54 get_addrs(my_machine_name, rem_machine_name)
55 char *my_machine_name;
56 char *rem_machine_name;
57 {
58 	struct hostent *hp;
59 	struct servent *sp;
60 
61 	msg.pid = getpid();
62 
63 	/* look up the address of the local host */
64 
65 	hp = gethostbyname(my_machine_name);
66 
67 	if (hp == (struct hostent *) 0) {
68 		fprintf(stderr,
69 		gettext("This machine doesn't exist. Boy, am I confused!\n"));
70 		exit(1);
71 	}
72 
73 	if (hp->h_addrtype != AF_INET) {
74 		fprintf(stderr,
75 		gettext("Protocol mix up with local machine address\n"));
76 		exit(1);
77 	}
78 
79 	bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length);
80 
81 	/* if on the same machine, then simply copy */
82 
83 	if (bcmp((char *)&rem_machine_name, (char *)&my_machine_name,
84 		sizeof (rem_machine_name)) == 0) {
85 	bcopy((char *)&my_machine_addr, (char *)&rem_machine_addr,
86 		sizeof (rem_machine_name));
87 	} else {
88 
89 		if ((rem_machine_addr.s_addr =
90 			(unsigned long)inet_addr(rem_machine_name)) == -1) {
91 
92 		/* look up the address of the recipient's machine */
93 
94 		hp = gethostbyname(rem_machine_name);
95 
96 		if (hp == (struct hostent *) 0) {
97 			fprintf(stderr,
98 			gettext("%s is an unknown host\n"), rem_machine_name);
99 			exit(1);
100 		}
101 
102 		if (hp->h_addrtype != AF_INET) {
103 			fprintf(stderr,
104 		gettext("Protocol mix up with remote machine address\n"));
105 			exit(1);
106 		}
107 
108 		bcopy(hp->h_addr, (char *) &rem_machine_addr, hp->h_length);
109 	}
110 	}
111 
112 
113 	/* find the daemon portal */
114 
115 #ifdef NTALK
116 	sp = getservbyname("ntalk", "udp");
117 #else
118 	sp = getservbyname("talk", "udp");
119 #endif
120 
121 	if (strcmp(sp->s_proto, "udp") != 0) {
122 	fprintf(stderr, gettext("Protocol mix up with talk daemon\n"));
123 	exit(1);
124 	}
125 
126 	if (sp == 0) {
127 		p_error(
128 		gettext("This machine doesn't support a tcp talk daemon"));
129 		exit(1);
130 	}
131 
132 	daemon_port = sp->s_port;
133 }
134