]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/get_names.c
receive, not recieve
[bsdgames-darwin.git] / hunt / huntd / get_names.c
1 /* $NetBSD: get_names.c,v 1.6 2001/02/05 00:42:15 christos Exp $ */
2 /*
3 * Copyright (c) 1983 Regents of the University of California.
4 * All rights reserved. The Berkeley software License Agreement
5 * specifies the terms and conditions for redistribution.
6 */
7
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: get_names.c,v 1.6 2001/02/05 00:42:15 christos Exp $");
11 #endif /* not lint */
12
13 #include "bsd.h"
14
15 #if defined(TALK_43) || defined(TALK_42)
16
17 # include <sys/param.h>
18 # include <netdb.h>
19 # include <stdio.h>
20 # include <stdlib.h>
21 # include <string.h>
22 # include <unistd.h>
23 # include "hunt.h"
24 # include "talk_ctl.h"
25
26 static char hostname[MAXHOSTNAMELEN + 1];
27 char *my_machine_name;
28
29 /*
30 * Determine the local user and machine
31 */
32 void
33 get_local_name(my_name)
34 char *my_name;
35 {
36 struct hostent *hp;
37 struct servent *sp;
38
39 /* Load these useful values into the standard message header */
40 msg.id_num = 0;
41 (void) strncpy(msg.l_name, my_name, NAME_SIZE);
42 msg.l_name[NAME_SIZE - 1] = '\0';
43 msg.r_tty[0] = '\0';
44 msg.pid = getpid();
45 # ifdef TALK_43
46 msg.vers = TALK_VERSION;
47 msg.addr.sa_family = htons(AF_INET);
48 msg.ctl_addr.sa_family = htons(AF_INET);
49 # else
50 msg.addr.sin_family = htons(AF_INET);
51 msg.ctl_addr.sin_family = htons(AF_INET);
52 # endif
53
54 (void)gethostname(hostname, sizeof (hostname));
55 hostname[sizeof(hostname) - 1] = '\0';
56 my_machine_name = hostname;
57 /* look up the address of the local host */
58 hp = gethostbyname(my_machine_name);
59 if (hp == (struct hostent *) 0) {
60 # ifdef LOG
61 syslog(LOG_ERR,
62 "This machine doesn't exist. Boy, am I confused!");
63 # else
64 perror("This machine doesn't exist. Boy, am I confused!");
65 # endif
66 exit(1);
67 }
68 memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
69 /* find the daemon portal */
70 # ifdef TALK_43
71 sp = getservbyname("ntalk", "udp");
72 # else
73 sp = getservbyname("talk", "udp");
74 # endif
75 if (sp == 0) {
76 # ifdef LOG
77 syslog(LOG_ERR, "This machine doesn't support talk");
78 # else
79 perror("This machine doesn't support talk");
80 # endif
81 exit(1);
82 }
83 daemon_port = sp->s_port;
84 }
85
86 /*
87 * Determine the remote user and machine
88 */
89 int
90 get_remote_name(his_address)
91 char *his_address;
92 {
93 char *his_name;
94 char *his_machine_name;
95 char *ptr;
96 struct hostent *hp;
97
98
99 /* check for, and strip out, the machine name of the target */
100 for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
101 && *ptr != '!' && *ptr != '.'; ptr++)
102 continue;
103 if (*ptr == '\0') {
104 /* this is a local to local talk */
105 his_name = his_address;
106 his_machine_name = my_machine_name;
107 } else {
108 if (*ptr == '@') {
109 /* user@host */
110 his_name = his_address;
111 his_machine_name = ptr + 1;
112 } else {
113 /* host.user or host!user or host:user */
114 his_name = ptr + 1;
115 his_machine_name = his_address;
116 }
117 *ptr = '\0';
118 }
119 /* Load these useful values into the standard message header */
120 (void) strncpy(msg.r_name, his_name, NAME_SIZE);
121 msg.r_name[NAME_SIZE - 1] = '\0';
122
123 /* if he is on the same machine, then simply copy */
124 if (memcmp((char *) &his_machine_name, (char *) &my_machine_name,
125 sizeof(his_machine_name)) == 0)
126 memcpy(&his_machine_addr, &my_machine_addr,
127 sizeof(his_machine_name));
128 else {
129 /* look up the address of the recipient's machine */
130 hp = gethostbyname(his_machine_name);
131 if (hp == (struct hostent *) 0)
132 return 0; /* unknown host */
133 memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
134 }
135 return 1;
136 }
137 #endif