summaryrefslogtreecommitdiffstats
path: root/hunt/huntd/get_names.c
blob: 6878c8b3ff363309843bd093083c653146464320 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*	$NetBSD: get_names.c,v 1.16 2014/03/29 20:16:09 dholland Exp $	*/
/*
 * Copyright (c) 1983-2003, Regents of the University of California.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * + Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright 
 *   notice, this list of conditions and the following disclaimer in the 
 *   documentation and/or other materials provided with the distribution.
 * + Neither the name of the University of California, San Francisco nor 
 *   the names of its contributors may be used to endorse or promote 
 *   products derived from this software without specific prior written 
 *   permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: get_names.c,v 1.16 2014/03/29 20:16:09 dholland Exp $");
#endif /* not lint */

#if defined(TALK_43) || defined(TALK_42)

#include <sys/param.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "hunt.h"
#include "talk_ctl.h"

static char hostname[MAXHOSTNAMELEN + 1];
char *my_machine_name;

/*
 * Determine the local user and machine
 */
void
get_local_name(const char *my_name)
{
	struct hostent *hp;
	struct servent *sp;

	/* Load these useful values into the standard message header */
	msg.id_num = 0;
	(void) strncpy(msg.l_name, my_name, NAME_SIZE);
	msg.l_name[NAME_SIZE - 1] = '\0';
	msg.r_tty[0] = '\0';
	msg.pid = getpid();
#ifdef TALK_43
	msg.vers = TALK_VERSION;
	msg.addr.sa_family = htons(AF_INET);
	msg.ctl_addr.sa_family = htons(AF_INET);
#else
	msg.addr.sin_family = htons(AF_INET);
	msg.ctl_addr.sin_family = htons(AF_INET);
#endif

	(void)gethostname(hostname, sizeof (hostname));
	hostname[sizeof(hostname) - 1] = '\0';
	my_machine_name = hostname;
	/* look up the address of the local host */
	hp = gethostbyname(my_machine_name);
	if (hp == (struct hostent *) 0) {
#ifdef LOG
		syslog(LOG_ERR,
		    "This machine doesn't exist. Boy, am I confused!");
#else
		perror("This machine doesn't exist. Boy, am I confused!");
#endif
		exit(1);
	}
	memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
	/* find the daemon portal */
#ifdef TALK_43
	sp = getservbyname("ntalk", "udp");
#else
	sp = getservbyname("talk", "udp");
#endif
	if (sp == 0) {
#ifdef LOG
		syslog(LOG_ERR, "This machine doesn't support talk");
#else
		perror("This machine doesn't support talk");
#endif
		exit(1);
	}
	daemon_port = sp->s_port;
}

/*
 * Determine the remote user and machine
 */
int
get_remote_name(char *his_address)
{
	const char *his_name;
	const char *his_machine_name;
	char *ptr;
	struct addrinfo ai0, *ai;
	struct sockaddr_in *sin;

	/* check for, and strip out, the machine name of the target */
	for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
					&& *ptr != '!' && *ptr != '.'; ptr++)
		continue;
	if (*ptr == '\0') {
		/* this is a local to local talk */
		his_name = his_address;
		his_machine_name = my_machine_name;
		his_machine_addr = my_machine_addr;
	} else {
		if (*ptr == '@') {
			/* user@host */
			his_name = his_address;
			his_machine_name = ptr + 1;
		} else {
			/* host.user or host!user or host:user */
			his_name = ptr + 1;
			his_machine_name = his_address;
		}
		*ptr = '\0';

		/*
		 * Look up the address of the recipient's machine.
		 * Since this is used for sending udp talk packets,
		 * it has to be AF_INET.
		 */
		ai0.ai_flags = 0;
		ai0.ai_family = AF_INET;
		ai0.ai_socktype = SOCK_DGRAM;
		ai0.ai_protocol = IPPROTO_UDP;
		ai0.ai_addrlen = 0;
		ai0.ai_addr = NULL;
		ai0.ai_canonname = NULL;
		ai0.ai_next = NULL;
		if (getaddrinfo(his_machine_name, NULL, &ai0, &ai) != 0) {
			return 0;
		}
		assert(ai->ai_family == AF_INET);
		assert(ai->ai_socktype == SOCK_DGRAM);
		assert(ai->ai_protocol == IPPROTO_UDP);
		assert(ai->ai_addrlen == sizeof(his_machine_addr));
		assert(ai->ai_addr != NULL);
		sin = (struct sockaddr_in *)ai->ai_addr;
		his_machine_addr = sin->sin_addr;
		freeaddrinfo(ai);
	}
	/* Load these useful values into the standard message header */
	(void) strncpy(msg.r_name, his_name, NAME_SIZE);
	msg.r_name[NAME_SIZE - 1] = '\0';

	return 1;
}
#endif