]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/get_names.c
const
[bsdgames-darwin.git] / hunt / huntd / get_names.c
1 /* $NetBSD: get_names.c,v 1.13 2014/03/29 19:02:12 dholland Exp $ */
2 /*
3 * Copyright (c) 1983-2003, Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
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
18 * permission.
19 *
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.
31 */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: get_names.c,v 1.13 2014/03/29 19:02:12 dholland Exp $");
36 #endif /* not lint */
37
38 #include "bsd.h"
39
40 #if defined(TALK_43) || defined(TALK_42)
41
42 #include <sys/param.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include "hunt.h"
49 #include "talk_ctl.h"
50
51 static char hostname[MAXHOSTNAMELEN + 1];
52 char *my_machine_name;
53
54 /*
55 * Determine the local user and machine
56 */
57 void
58 get_local_name(char *my_name)
59 {
60 struct hostent *hp;
61 struct servent *sp;
62
63 /* Load these useful values into the standard message header */
64 msg.id_num = 0;
65 (void) strncpy(msg.l_name, my_name, NAME_SIZE);
66 msg.l_name[NAME_SIZE - 1] = '\0';
67 msg.r_tty[0] = '\0';
68 msg.pid = getpid();
69 #ifdef TALK_43
70 msg.vers = TALK_VERSION;
71 msg.addr.sa_family = htons(AF_INET);
72 msg.ctl_addr.sa_family = htons(AF_INET);
73 #else
74 msg.addr.sin_family = htons(AF_INET);
75 msg.ctl_addr.sin_family = htons(AF_INET);
76 #endif
77
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) {
84 #ifdef LOG
85 syslog(LOG_ERR,
86 "This machine doesn't exist. Boy, am I confused!");
87 #else
88 perror("This machine doesn't exist. Boy, am I confused!");
89 #endif
90 exit(1);
91 }
92 memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
93 /* find the daemon portal */
94 #ifdef TALK_43
95 sp = getservbyname("ntalk", "udp");
96 #else
97 sp = getservbyname("talk", "udp");
98 #endif
99 if (sp == 0) {
100 #ifdef LOG
101 syslog(LOG_ERR, "This machine doesn't support talk");
102 #else
103 perror("This machine doesn't support talk");
104 #endif
105 exit(1);
106 }
107 daemon_port = sp->s_port;
108 }
109
110 /*
111 * Determine the remote user and machine
112 */
113 int
114 get_remote_name(char *his_address)
115 {
116 const char *his_name;
117 const char *his_machine_name;
118 char *ptr;
119 struct hostent *hp;
120
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++)
124 continue;
125 if (*ptr == '\0') {
126 /* this is a local to local talk */
127 his_name = his_address;
128 his_machine_name = my_machine_name;
129 his_machine_addr = my_machine_addr;
130 } else {
131 if (*ptr == '@') {
132 /* user@host */
133 his_name = his_address;
134 his_machine_name = ptr + 1;
135 } else {
136 /* host.user or host!user or host:user */
137 his_name = ptr + 1;
138 his_machine_name = his_address;
139 }
140 *ptr = '\0';
141
142 /* look up the address of the recipient's machine */
143 hp = gethostbyname(his_machine_name);
144 if (hp == NULL) {
145 /* unknown host */
146 return 0;
147 }
148 memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
149 }
150 /* Load these useful values into the standard message header */
151 (void) strncpy(msg.r_name, his_name, NAME_SIZE);
152 msg.r_name[NAME_SIZE - 1] = '\0';
153
154 return 1;
155 }
156 #endif