(linenum→info "unix/slp.c:2238")

bsd-games/2.17/hunt/huntd/get_names.c

    1: /*      $NetBSD: get_names.c,v 1.7 2003/06/11 12:00:22 wiz 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.7 2003/06/11 12:00:22 wiz 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: 
   51: #ifndef MAXHOSTNAMELEN
   52: #define MAXHOSTNAMELEN 256
   53: #endif
   54: 
   55: static  char     hostname[MAXHOSTNAMELEN + 1];
   56: char            *my_machine_name;
   57: 
   58: /*
   59:  * Determine the local user and machine
   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:         /* Load these useful values into the standard message header */
   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:         /* look up the address of the local host */
   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:         /* find the daemon portal */
   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:  * Determine the remote user and machine
  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:         /* check for, and strip out, the machine name of the target */
  129:         for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
  130:                                         && *ptr != '!' && *ptr != '.'; ptr++)
  131:                 continue;
  132:         if (*ptr == '\0') {
  133:                 /* this is a local to local talk */
  134:                 his_name = his_address;
  135:                 his_machine_name = my_machine_name;
  136:         } else {
  137:                 if (*ptr == '@') {
  138:                         /* user@host */
  139:                         his_name = his_address;
  140:                         his_machine_name = ptr + 1;
  141:                 } else {
  142:                         /* host.user or host!user or host:user */
  143:                         his_name = ptr + 1;
  144:                         his_machine_name = his_address;
  145:                 }
  146:                 *ptr = '\0';
  147:         }
  148:         /* Load these useful values into the standard message header */
  149:         (void) strncpy(msg.r_name, his_name, NAME_SIZE);
  150:         msg.r_name[NAME_SIZE - 1] = '\0';
  151: 
  152:         /* if he is on the same machine, then simply copy */
  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:                 /* look up the address of the recipient's machine */
  159:                 hp = gethostbyname(his_machine_name);
  160:                 if (hp == (struct hostent *) 0)
  161:                         return 0;                    /* unknown host */
  162:                 memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
  163:         }
  164:         return 1;
  165: }
  166: #endif
Syntax (Markdown)