get_names.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <sys/cdefs.h>
  33. #ifndef lint
  34. __RCSID("$NetBSD: get_names.c,v 1.7 2003/06/11 12:00:22 wiz Exp $");
  35. #endif /* not lint */
  36. #include "bsd.h"
  37. #if defined(TALK_43) || defined(TALK_42)
  38. # include <sys/param.h>
  39. # include <netdb.h>
  40. # include <stdio.h>
  41. # include <stdlib.h>
  42. # include <string.h>
  43. # include <unistd.h>
  44. # include "hunt.h"
  45. # include "talk_ctl.h"
  46. #ifndef MAXHOSTNAMELEN
  47. #define MAXHOSTNAMELEN 256
  48. #endif
  49. static char hostname[MAXHOSTNAMELEN + 1];
  50. char *my_machine_name;
  51. /*
  52. * Determine the local user and machine
  53. */
  54. void
  55. get_local_name(my_name)
  56. const char *my_name;
  57. {
  58. struct hostent *hp;
  59. struct servent *sp;
  60. /* Load these useful values into the standard message header */
  61. msg.id_num = 0;
  62. (void) strncpy(msg.l_name, my_name, NAME_SIZE);
  63. msg.l_name[NAME_SIZE - 1] = '\0';
  64. msg.r_tty[0] = '\0';
  65. msg.pid = getpid();
  66. # ifdef TALK_43
  67. msg.vers = TALK_VERSION;
  68. msg.addr.sa_family = htons(AF_INET);
  69. msg.ctl_addr.sa_family = htons(AF_INET);
  70. # else
  71. msg.addr.sin_family = htons(AF_INET);
  72. msg.ctl_addr.sin_family = htons(AF_INET);
  73. # endif
  74. (void)gethostname(hostname, sizeof (hostname));
  75. hostname[sizeof(hostname) - 1] = '\0';
  76. my_machine_name = hostname;
  77. /* look up the address of the local host */
  78. hp = gethostbyname(my_machine_name);
  79. if (hp == (struct hostent *) 0) {
  80. # ifdef LOG
  81. syslog(LOG_ERR,
  82. "This machine doesn't exist. Boy, am I confused!");
  83. # else
  84. perror("This machine doesn't exist. Boy, am I confused!");
  85. # endif
  86. exit(1);
  87. }
  88. memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
  89. /* find the daemon portal */
  90. # ifdef TALK_43
  91. sp = getservbyname("ntalk", "udp");
  92. # else
  93. sp = getservbyname("talk", "udp");
  94. # endif
  95. if (sp == 0) {
  96. # ifdef LOG
  97. syslog(LOG_ERR, "This machine doesn't support talk");
  98. # else
  99. perror("This machine doesn't support talk");
  100. # endif
  101. exit(1);
  102. }
  103. daemon_port = sp->s_port;
  104. }
  105. /*
  106. * Determine the remote user and machine
  107. */
  108. int
  109. get_remote_name(his_address)
  110. char *his_address;
  111. {
  112. char *his_name;
  113. char *his_machine_name;
  114. char *ptr;
  115. struct hostent *hp;
  116. /* check for, and strip out, the machine name of the target */
  117. for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
  118. && *ptr != '!' && *ptr != '.'; ptr++)
  119. continue;
  120. if (*ptr == '\0') {
  121. /* this is a local to local talk */
  122. his_name = his_address;
  123. his_machine_name = my_machine_name;
  124. } else {
  125. if (*ptr == '@') {
  126. /* user@host */
  127. his_name = his_address;
  128. his_machine_name = ptr + 1;
  129. } else {
  130. /* host.user or host!user or host:user */
  131. his_name = ptr + 1;
  132. his_machine_name = his_address;
  133. }
  134. *ptr = '\0';
  135. }
  136. /* Load these useful values into the standard message header */
  137. (void) strncpy(msg.r_name, his_name, NAME_SIZE);
  138. msg.r_name[NAME_SIZE - 1] = '\0';
  139. /* if he is on the same machine, then simply copy */
  140. if (memcmp((char *) &his_machine_name, (char *) &my_machine_name,
  141. sizeof(his_machine_name)) == 0)
  142. memcpy(&his_machine_addr, &my_machine_addr,
  143. sizeof(his_machine_name));
  144. else {
  145. /* look up the address of the recipient's machine */
  146. hp = gethostbyname(his_machine_name);
  147. if (hp == (struct hostent *) 0)
  148. return 0; /* unknown host */
  149. memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
  150. }
  151. return 1;
  152. }
  153. #endif