/contrib/ntp/arlib/sample.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 143 lines · 128 code · 15 blank · 0 comment · 21 complexity · 3dab0d421119b76d5afeafa72aa32d95 MD5 · raw file

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include "arlib.h"
  9. #ifndef lint
  10. static char sccsid[] = "@(#)sample.c 1.1 12/21/92 (C)1992 Darren Reed. ASYNC DNS";
  11. #endif
  12. char line[512];
  13. int lookup = 0, seq = 0;
  14. long expire = 0;
  15. main()
  16. {
  17. struct in_addr adr;
  18. struct timeval tv2;
  19. fd_set rd;
  20. long now;
  21. char *s;
  22. int afd, nfd, pid = getpid(), del;
  23. afd = ar_init(ARES_INITLIST|ARES_CALLINIT|ARES_INITSOCK);
  24. (void)printf("afd = %d pid = %d\n",afd, pid);
  25. while (1)
  26. {
  27. (void)printf("Host =>");
  28. (void)fflush(stdout);
  29. *line = '\0';
  30. FD_ZERO(&rd);
  31. FD_SET(0,&rd);
  32. FD_SET(afd,&rd);
  33. now = time(NULL);
  34. if (expire >= now)
  35. {
  36. tv2.tv_usec = 0;
  37. tv2.tv_sec = expire - now;
  38. nfd = select(FD_SETSIZE, &rd, NULL, NULL, &tv2);
  39. }
  40. else
  41. nfd = select(FD_SETSIZE, &rd, NULL, NULL, NULL);
  42. if (FD_ISSET(0, &rd))
  43. {
  44. if (!fgets(line, sizeof(line) - 1, stdin))
  45. exit(0);
  46. if (s = index(line, '\n'))
  47. *s = '\0';
  48. }
  49. if (isalpha(*line))
  50. {
  51. (void)printf("Asking about [%s] #%d.\n",line, ++seq);
  52. (void)ar_gethostbyname(line, (char *)&seq,
  53. sizeof(seq));
  54. lookup++;
  55. }
  56. else if (isdigit(*line))
  57. {
  58. (void)printf("Asking about IP#[%s] #%d.\n",
  59. line, ++seq);
  60. adr.s_addr = inet_addr(line);
  61. (void)ar_gethostbyaddr(&adr, (char *)&seq,
  62. sizeof(seq));
  63. lookup++;
  64. }
  65. if (lookup)
  66. (void)printf("Waiting for answer:\n");
  67. if (FD_ISSET(afd, &rd))
  68. (void)waitonlookup(afd);
  69. del = 0;
  70. expire = ar_timeout(time(NULL), &del, sizeof(del));
  71. if (del)
  72. {
  73. (void)fprintf(stderr,"#%d failed\n", del);
  74. lookup--;
  75. }
  76. }
  77. }
  78. printhostent(hp)
  79. struct hostent *hp;
  80. {
  81. struct in_addr ip;
  82. int i;
  83. (void)printf("hname = %s\n", hp->h_name);
  84. for (i = 0; hp->h_aliases[i]; i++)
  85. (void)printf("alias %d = %s\n", i+1, hp->h_aliases[i]);
  86. for (i = 0; hp->h_addr_list[i]; i++)
  87. {
  88. bcopy(hp->h_addr_list[i], (char *)&ip, sizeof(ip));
  89. (void)printf("IP# %d = %s\n", i+1, inet_ntoa(ip));
  90. }
  91. }
  92. int waitonlookup(afd)
  93. int afd;
  94. {
  95. struct timeval delay;
  96. struct hostent *hp;
  97. fd_set rd;
  98. long now;
  99. int nfd, del;
  100. waitloop:
  101. FD_ZERO(&rd);
  102. now = time(NULL);
  103. if (expire >= now)
  104. delay.tv_sec = expire - now;
  105. else
  106. delay.tv_sec = 1;
  107. delay.tv_usec = 0;
  108. FD_SET(afd, &rd);
  109. FD_SET(0, &rd);
  110. nfd = select(FD_SETSIZE, &rd, 0, 0, &delay);
  111. if (nfd == 0)
  112. return 0;
  113. else if (FD_ISSET(afd, &rd))
  114. {
  115. del = 0;
  116. hp = ar_answer(&del, sizeof(del));
  117. (void)printf("hp=%x seq=%d\n",hp,del);
  118. if (hp)
  119. {
  120. (void)printhostent(hp);
  121. if (!--lookup)
  122. return 1;
  123. }
  124. }
  125. if (FD_ISSET(0, &rd))
  126. return 2;
  127. return 0;
  128. }