1 /* $NetBSD: get_names.c,v 1.9 2009/07/04 02:37:20 dholland Exp $ */
3 * Copyright (c) 1983-2003, Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * + Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * + Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * + Neither the name of the University of California, San Francisco nor
16 * the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: get_names.c,v 1.9 2009/07/04 02:37:20 dholland Exp $");
40 #if defined(TALK_43) || defined(TALK_42)
42 #include <sys/param.h>
51 static char hostname
[MAXHOSTNAMELEN
+ 1];
52 char *my_machine_name
;
55 * Determine the local user and machine
58 get_local_name(char *my_name
)
63 /* Load these useful values into the standard message header */
65 (void) strncpy(msg
.l_name
, my_name
, NAME_SIZE
);
66 msg
.l_name
[NAME_SIZE
- 1] = '\0';
70 msg
.vers
= TALK_VERSION
;
71 msg
.addr
.sa_family
= htons(AF_INET
);
72 msg
.ctl_addr
.sa_family
= htons(AF_INET
);
74 msg
.addr
.sin_family
= htons(AF_INET
);
75 msg
.ctl_addr
.sin_family
= htons(AF_INET
);
78 (void)gethostname(hostname
, sizeof (hostname
));
79 hostname
[sizeof(hostname
) - 1] = '\0';
80 my_machine_name
= hostname
;
81 /* look up the address of the local host */
82 hp
= gethostbyname(my_machine_name
);
83 if (hp
== (struct hostent
*) 0) {
86 "This machine doesn't exist. Boy, am I confused!");
88 perror("This machine doesn't exist. Boy, am I confused!");
92 memcpy(&my_machine_addr
, hp
->h_addr
, hp
->h_length
);
93 /* find the daemon portal */
95 sp
= getservbyname("ntalk", "udp");
97 sp
= getservbyname("talk", "udp");
101 syslog(LOG_ERR
, "This machine doesn't support talk");
103 perror("This machine doesn't support talk");
107 daemon_port
= sp
->s_port
;
111 * Determine the remote user and machine
114 get_remote_name(char *his_address
)
117 char *his_machine_name
;
121 /* check for, and strip out, the machine name of the target */
122 for (ptr
= his_address
; *ptr
!= '\0' && *ptr
!= '@' && *ptr
!= ':'
123 && *ptr
!= '!' && *ptr
!= '.'; ptr
++)
126 /* this is a local to local talk */
127 his_name
= his_address
;
128 his_machine_name
= my_machine_name
;
132 his_name
= his_address
;
133 his_machine_name
= ptr
+ 1;
135 /* host.user or host!user or host:user */
137 his_machine_name
= his_address
;
141 /* Load these useful values into the standard message header */
142 (void) strncpy(msg
.r_name
, his_name
, NAME_SIZE
);
143 msg
.r_name
[NAME_SIZE
- 1] = '\0';
145 /* if he is on the same machine, then simply copy */
146 if (memcmp(&his_machine_name
, &my_machine_name
,
147 sizeof(his_machine_name
)) == 0)
148 memcpy(&his_machine_addr
, &my_machine_addr
,
149 sizeof(his_machine_name
));
151 /* look up the address of the recipient's machine */
152 hp
= gethostbyname(his_machine_name
);
153 if (hp
== (struct hostent
*) 0)
154 return 0; /* unknown host */
155 memcpy(&his_machine_addr
, hp
->h_addr
, hp
->h_length
);