]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/get_names.c
More include additions for exit, abs, strcmp, etc.
[bsdgames-darwin.git] / hunt / huntd / get_names.c
1 /* $NetBSD: get_names.c,v 1.4 2000/07/03 03:57:41 matt 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.4 2000/07/03 03:57:41 matt 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 extern CTL_MSG msg;
27
28 static char hostname[MAXHOSTNAMELEN + 1];
29 char *my_machine_name;
30
31 /*
32 * Determine the local user and machine
33 */
34 void
35 get_local_name(my_name)
36 char *my_name;
37 {
38 struct hostent *hp;
39 struct servent *sp;
40
41 /* Load these useful values into the standard message header */
42 msg.id_num = 0;
43 (void) strncpy(msg.l_name, my_name, NAME_SIZE);
44 msg.l_name[NAME_SIZE - 1] = '\0';
45 msg.r_tty[0] = '\0';
46 msg.pid = getpid();
47 # ifdef TALK_43
48 msg.vers = TALK_VERSION;
49 msg.addr.sa_family = htons(AF_INET);
50 msg.ctl_addr.sa_family = htons(AF_INET);
51 # else
52 msg.addr.sin_family = htons(AF_INET);
53 msg.ctl_addr.sin_family = htons(AF_INET);
54 # endif
55
56 (void)gethostname(hostname, sizeof (hostname));
57 hostname[sizeof(hostname) - 1] = '\0';
58 my_machine_name = hostname;
59 /* look up the address of the local host */
60 hp = gethostbyname(my_machine_name);
61 if (hp == (struct hostent *) 0) {
62 printf("This machine doesn't exist. Boy, am I confused!\n");
63 exit(-1);
64 }
65 memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
66 /* find the daemon portal */
67 # ifdef TALK_43
68 sp = getservbyname("ntalk", "udp");
69 # else
70 sp = getservbyname("talk", "udp");
71 # endif
72 if (sp == 0) {
73 # ifdef LOG
74 syslog(LOG_ERR, "This machine doesn't support talk");
75 # else
76 perror("This machine doesn't support talk");
77 # endif
78 exit(-1);
79 }
80 daemon_port = sp->s_port;
81 }
82
83 /*
84 * Determine the remote user and machine
85 */
86 int
87 get_remote_name(his_address)
88 char *his_address;
89 {
90 char *his_name;
91 char *his_machine_name;
92 char *ptr;
93 struct hostent *hp;
94
95
96 /* check for, and strip out, the machine name of the target */
97 for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
98 && *ptr != '!' && *ptr != '.'; ptr++)
99 continue;
100 if (*ptr == '\0') {
101 /* this is a local to local talk */
102 his_name = his_address;
103 his_machine_name = my_machine_name;
104 } else {
105 if (*ptr == '@') {
106 /* user@host */
107 his_name = his_address;
108 his_machine_name = ptr + 1;
109 } else {
110 /* host.user or host!user or host:user */
111 his_name = ptr + 1;
112 his_machine_name = his_address;
113 }
114 *ptr = '\0';
115 }
116 /* Load these useful values into the standard message header */
117 (void) strncpy(msg.r_name, his_name, NAME_SIZE);
118 msg.r_name[NAME_SIZE - 1] = '\0';
119
120 /* if he is on the same machine, then simply copy */
121 if (memcmp((char *) &his_machine_name, (char *) &my_machine_name,
122 sizeof(his_machine_name)) == 0)
123 memcpy(&his_machine_addr, &my_machine_addr,
124 sizeof(his_machine_name));
125 else {
126 /* look up the address of the recipient's machine */
127 hp = gethostbyname(his_machine_name);
128 if (hp == (struct hostent *) 0)
129 return 0; /* unknown host */
130 memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
131 }
132 return 1;
133 }
134 #endif