1/* $NetBSD: get_names.c,v 1.9 2009/07/04 02:37:20 dholland Exp $ */ 2/* 3 * Copyright (c) 1983-2003, Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * + Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * + Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * + Neither the name of the University of California, San Francisco nor 16 * the names of its contributors may be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34#ifndef lint 35__RCSID("$NetBSD: get_names.c,v 1.9 2009/07/04 02:37:20 dholland Exp $"); 36#endif /* not lint */ 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 51static char hostname[MAXHOSTNAMELEN + 1]; 52char *my_machine_name; 53 54/* 55 * Determine the local user and machine 56 */ 57void 58get_local_name(char *my_name) 59{ 60 struct hostent *hp; 61 struct servent *sp; 62 63 /* Load these useful values into the standard message header */ 64 msg.id_num = 0; 65 (void) strncpy(msg.l_name, my_name, NAME_SIZE); 66 msg.l_name[NAME_SIZE - 1] = '\0'; 67 msg.r_tty[0] = '\0'; 68 msg.pid = getpid(); 69#ifdef TALK_43 70 msg.vers = TALK_VERSION; 71 msg.addr.sa_family = htons(AF_INET); 72 msg.ctl_addr.sa_family = htons(AF_INET); 73#else 74 msg.addr.sin_family = htons(AF_INET); 75 msg.ctl_addr.sin_family = htons(AF_INET); 76#endif 77 78 (void)gethostname(hostname, sizeof (hostname)); 79 hostname[sizeof(hostname) - 1] = '\0'; 80 my_machine_name = hostname; 81 /* look up the address of the local host */ 82 hp = gethostbyname(my_machine_name); 83 if (hp == (struct hostent *) 0) { 84#ifdef LOG 85 syslog(LOG_ERR, 86 "This machine doesn't exist. Boy, am I confused!"); 87#else 88 perror("This machine doesn't exist. Boy, am I confused!"); 89#endif 90 exit(1); 91 } 92 memcpy(&my_machine_addr, hp->h_addr, hp->h_length); 93 /* find the daemon portal */ 94#ifdef TALK_43 95 sp = getservbyname("ntalk", "udp"); 96#else 97 sp = getservbyname("talk", "udp"); 98#endif 99 if (sp == 0) { 100#ifdef LOG 101 syslog(LOG_ERR, "This machine doesn't support talk"); 102#else 103 perror("This machine doesn't support talk"); 104#endif 105 exit(1); 106 } 107 daemon_port = sp->s_port; 108} 109 110/* 111 * Determine the remote user and machine 112 */ 113int 114get_remote_name(char *his_address) 115{ 116 char *his_name; 117 char *his_machine_name; 118 char *ptr; 119 struct hostent *hp; 120 121 /* check for, and strip out, the machine name of the target */ 122 for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':' 123 && *ptr != '!' && *ptr != '.'; ptr++) 124 continue; 125 if (*ptr == '\0') { 126 /* this is a local to local talk */ 127 his_name = his_address; 128 his_machine_name = my_machine_name; 129 } else { 130 if (*ptr == '@') { 131 /* user@host */ 132 his_name = his_address; 133 his_machine_name = ptr + 1; 134 } else { 135 /* host.user or host!user or host:user */ 136 his_name = ptr + 1; 137 his_machine_name = his_address; 138 } 139 *ptr = '\0'; 140 } 141 /* Load these useful values into the standard message header */ 142 (void) strncpy(msg.r_name, his_name, NAME_SIZE); 143 msg.r_name[NAME_SIZE - 1] = '\0'; 144 145 /* if he is on the same machine, then simply copy */ 146 if (memcmp(&his_machine_name, &my_machine_name, 147 sizeof(his_machine_name)) == 0) 148 memcpy(&his_machine_addr, &my_machine_addr, 149 sizeof(his_machine_name)); 150 else { 151 /* look up the address of the recipient's machine */ 152 hp = gethostbyname(his_machine_name); 153 if (hp == (struct hostent *) 0) 154 return 0; /* unknown host */ 155 memcpy(&his_machine_addr, hp->h_addr, hp->h_length); 156 } 157 return 1; 158} 159#endif 160