|
|
1.1 ! root 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 "talk_ctl.h" ! 13: # include <sys/param.h> ! 14: # include <netdb.h> ! 15: ! 16: extern char *rindex(), *strncpy(); ! 17: ! 18: extern CTL_MSG msg; ! 19: ! 20: struct hostent *gethostbyname(); ! 21: struct servent *getservbyname(); ! 22: ! 23: static char hostname[MAXHOSTNAMELEN]; ! 24: char *my_machine_name; ! 25: ! 26: /* ! 27: * Determine the local user and machine ! 28: */ ! 29: get_local_name(my_name) ! 30: char *my_name; ! 31: { ! 32: struct hostent *hp; ! 33: struct servent *sp; ! 34: ! 35: /* Load these useful values into the standard message header */ ! 36: msg.id_num = 0; ! 37: (void) strncpy(msg.l_name, my_name, NAME_SIZE); ! 38: msg.l_name[NAME_SIZE - 1] = '\0'; ! 39: msg.r_tty[0] = '\0'; ! 40: msg.pid = getpid(); ! 41: # ifdef TALK_43 ! 42: msg.vers = TALK_VERSION; ! 43: msg.addr.sa_family = htons(AF_INET); ! 44: msg.ctl_addr.sa_family = htons(AF_INET); ! 45: # else ! 46: msg.addr.sin_family = htons(AF_INET); ! 47: msg.ctl_addr.sin_family = htons(AF_INET); ! 48: # endif ! 49: ! 50: (void) gethostname(hostname, sizeof (hostname)); ! 51: my_machine_name = hostname; ! 52: /* look up the address of the local host */ ! 53: hp = gethostbyname(my_machine_name); ! 54: if (hp == (struct hostent *) 0) { ! 55: printf("This machine doesn't exist. Boy, am I confused!\n"); ! 56: exit(-1); ! 57: } ! 58: bcopy(hp->h_addr, (char *) &my_machine_addr, hp->h_length); ! 59: /* find the daemon portal */ ! 60: # ifdef TALK_43 ! 61: sp = getservbyname("ntalk", "udp"); ! 62: # else ! 63: sp = getservbyname("talk", "udp"); ! 64: # endif ! 65: if (sp == 0) { ! 66: # ifdef LOG ! 67: syslog(LOG_ERR, "This machine doesn't support talk"); ! 68: # else ! 69: perror("This machine doesn't support talk"); ! 70: # endif ! 71: exit(-1); ! 72: } ! 73: daemon_port = sp->s_port; ! 74: } ! 75: ! 76: /* ! 77: * Determine the remote user and machine ! 78: */ ! 79: get_remote_name(his_address) ! 80: char *his_address; ! 81: { ! 82: char *his_name; ! 83: char *his_machine_name; ! 84: char *ptr; ! 85: struct hostent *hp; ! 86: ! 87: ! 88: /* check for, and strip out, the machine name of the target */ ! 89: for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':' ! 90: && *ptr != '!' && *ptr != '.'; ptr++) ! 91: continue; ! 92: if (*ptr == '\0') { ! 93: /* this is a local to local talk */ ! 94: his_name = his_address; ! 95: his_machine_name = my_machine_name; ! 96: } else { ! 97: if (*ptr == '@') { ! 98: /* user@host */ ! 99: his_name = his_address; ! 100: his_machine_name = ptr + 1; ! 101: } else { ! 102: /* host.user or host!user or host:user */ ! 103: his_name = ptr + 1; ! 104: his_machine_name = his_address; ! 105: } ! 106: *ptr = '\0'; ! 107: } ! 108: /* Load these useful values into the standard message header */ ! 109: (void) strncpy(msg.r_name, his_name, NAME_SIZE); ! 110: msg.r_name[NAME_SIZE - 1] = '\0'; ! 111: ! 112: /* if he is on the same machine, then simply copy */ ! 113: if (bcmp((char *) &his_machine_name, (char *) &my_machine_name, ! 114: sizeof(his_machine_name)) == 0) ! 115: bcopy((char *) &my_machine_addr, (char *) &his_machine_addr, ! 116: sizeof(his_machine_name)); ! 117: else { ! 118: /* look up the address of the recipient's machine */ ! 119: hp = gethostbyname(his_machine_name); ! 120: if (hp == (struct hostent *) 0) ! 121: return 0; /* unknown host */ ! 122: bcopy(hp->h_addr, (char *) &his_machine_addr, hp->h_length); ! 123: } ! 124: return 1; ! 125: } ! 126: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.