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: #include <sys/cdefs.h>
34: #ifndef lint
35: __RCSID("$NetBSD: get_names.c,v 1.7 2003/06/11 12:00:22 wiz Exp $");
36: #endif
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: #ifndef MAXHOSTNAMELEN
52: #define MAXHOSTNAMELEN 256
53: #endif
54:
55: static char hostname[MAXHOSTNAMELEN + 1];
56: char *my_machine_name;
57:
58:
59:
60:
61: void
62: get_local_name(my_name)
63: const char *my_name;
64: {
65: struct hostent *hp;
66: struct servent *sp;
67:
68:
69: msg.id_num = 0;
70: (void) strncpy(msg.l_name, my_name, NAME_SIZE);
71: msg.l_name[NAME_SIZE - 1] = '\0';
72: msg.r_tty[0] = '\0';
73: msg.pid = getpid();
74: # ifdef TALK_43
75: msg.vers = TALK_VERSION;
76: msg.addr.sa_family = htons(AF_INET);
77: msg.ctl_addr.sa_family = htons(AF_INET);
78: # else
79: msg.addr.sin_family = htons(AF_INET);
80: msg.ctl_addr.sin_family = htons(AF_INET);
81: # endif
82:
83: (void)gethostname(hostname, sizeof (hostname));
84: hostname[sizeof(hostname) - 1] = '\0';
85: my_machine_name = hostname;
86:
87: hp = gethostbyname(my_machine_name);
88: if (hp == (struct hostent *) 0) {
89: # ifdef LOG
90: syslog(LOG_ERR,
91: "This machine doesn't exist. Boy, am I confused!");
92: # else
93: perror("This machine doesn't exist. Boy, am I confused!");
94: # endif
95: exit(1);
96: }
97: memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
98:
99: # ifdef TALK_43
100: sp = getservbyname("ntalk", "udp");
101: # else
102: sp = getservbyname("talk", "udp");
103: # endif
104: if (sp == 0) {
105: # ifdef LOG
106: syslog(LOG_ERR, "This machine doesn't support talk");
107: # else
108: perror("This machine doesn't support talk");
109: # endif
110: exit(1);
111: }
112: daemon_port = sp->s_port;
113: }
114:
115:
116:
117:
118: int
119: get_remote_name(his_address)
120: char *his_address;
121: {
122: char *his_name;
123: char *his_machine_name;
124: char *ptr;
125: struct hostent *hp;
126:
127:
128:
129: for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
130: && *ptr != '!' && *ptr != '.'; ptr++)
131: continue;
132: if (*ptr == '\0') {
133:
134: his_name = his_address;
135: his_machine_name = my_machine_name;
136: } else {
137: if (*ptr == '@') {
138:
139: his_name = his_address;
140: his_machine_name = ptr + 1;
141: } else {
142:
143: his_name = ptr + 1;
144: his_machine_name = his_address;
145: }
146: *ptr = '\0';
147: }
148:
149: (void) strncpy(msg.r_name, his_name, NAME_SIZE);
150: msg.r_name[NAME_SIZE - 1] = '\0';
151:
152:
153: if (memcmp((char *) &his_machine_name, (char *) &my_machine_name,
154: sizeof(his_machine_name)) == 0)
155: memcpy(&his_machine_addr, &my_machine_addr,
156: sizeof(his_machine_name));
157: else {
158:
159: hp = gethostbyname(his_machine_name);
160: if (hp == (struct hostent *) 0)
161: return 0;
162: memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
163: }
164: return 1;
165: }
166: #endif