gethostbyname.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. /* bsd extensions */
  10. #include <sys/uio.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. #include "priv.h"
  15. int h_errno;
  16. enum
  17. {
  18. Nname= 6,
  19. };
  20. /*
  21. * for inet addresses only
  22. */
  23. struct hostent*
  24. gethostbyname(char *name)
  25. {
  26. int i, t, fd, m;
  27. char *p, *bp;
  28. int nn, na;
  29. unsigned long x;
  30. static struct hostent h;
  31. static char buf[1024];
  32. static char *nptr[Nname+1];
  33. static char *aptr[Nname+1];
  34. static char addr[Nname][4];
  35. h.h_name = 0;
  36. t = _sock_ipattr(name);
  37. /* connect to server */
  38. fd = open("/net/cs", O_RDWR);
  39. if(fd < 0){
  40. _syserrno();
  41. h_errno = NO_RECOVERY;
  42. return 0;
  43. }
  44. /* construct the query, always expect an ip# back */
  45. switch(t){
  46. case Tsys:
  47. snprintf(buf, sizeof buf, "!sys=%s ip=*", name);
  48. break;
  49. case Tdom:
  50. snprintf(buf, sizeof buf, "!dom=%s ip=*", name);
  51. break;
  52. case Tip:
  53. snprintf(buf, sizeof buf, "!ip=%s", name);
  54. break;
  55. }
  56. /* query the server */
  57. if(write(fd, buf, strlen(buf)) < 0){
  58. _syserrno();
  59. h_errno = TRY_AGAIN;
  60. return 0;
  61. }
  62. lseek(fd, 0, 0);
  63. for(i = 0; i < sizeof(buf)-1; i += m){
  64. m = read(fd, buf+i, sizeof(buf) - 1 - i);
  65. if(m <= 0)
  66. break;
  67. buf[i+m++] = ' ';
  68. }
  69. close(fd);
  70. buf[i] = 0;
  71. /* parse the reply */
  72. nn = na = 0;
  73. for(bp = buf;;){
  74. p = strchr(bp, '=');
  75. if(p == 0)
  76. break;
  77. *p++ = 0;
  78. if(strcmp(bp, "dom") == 0){
  79. if(h.h_name == 0)
  80. h.h_name = p;
  81. if(nn < Nname)
  82. nptr[nn++] = p;
  83. } else if(strcmp(bp, "sys") == 0){
  84. if(nn < Nname)
  85. nptr[nn++] = p;
  86. } else if(strcmp(bp, "ip") == 0){
  87. x = inet_addr(p);
  88. x = ntohl(x);
  89. if(na < Nname){
  90. addr[na][0] = x>>24;
  91. addr[na][1] = x>>16;
  92. addr[na][2] = x>>8;
  93. addr[na][3] = x;
  94. aptr[na] = addr[na];
  95. na++;
  96. }
  97. }
  98. while(*p && *p != ' ')
  99. p++;
  100. if(*p)
  101. *p++ = 0;
  102. bp = p;
  103. }
  104. if(nn+na == 0){
  105. h_errno = HOST_NOT_FOUND;
  106. return 0;
  107. }
  108. nptr[nn] = 0;
  109. aptr[na] = 0;
  110. h.h_aliases = nptr;
  111. h.h_addr_list = aptr;
  112. h.h_length = 4;
  113. h.h_addrtype = AF_INET;
  114. if(h.h_name == 0)
  115. h.h_name = nptr[0];
  116. if(h.h_name == 0)
  117. h.h_name = aptr[0];
  118. return &h;
  119. }