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