TestDNSDaemon.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/un.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #if defined(AIX) || defined(__linux)
  12. #include <sys/select.h> // for fd_set
  13. #endif
  14. #if defined(__linux)
  15. // Didn't find gettdtablehi() or gettdtablesize() on linux. Using FD_SETSIZE
  16. #define getdtablehi() FD_SETSIZE
  17. #else
  18. #define getdtablehi() getdtablesize()
  19. // If you find a system doesn't have getdtablesize try #define getdtablesize
  20. // to FD_SETSIZE. And if you encounter a system that doesn't even have
  21. // FD_SETSIZE, just grab your ankles and use 255.
  22. #endif
  23. #include "nspr.h"
  24. #include "nsCRT.h"
  25. #include "unix_dns.h"
  26. struct sockaddr_un unix_addr;
  27. int async_dns_lookup(char* hostName)
  28. {
  29. fprintf(stderr, "start async_dns_lookup\n");
  30. int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  31. if (socket_fd == -1) {
  32. fprintf(stderr, "socket returned error.\n");
  33. return -1;
  34. }
  35. unix_addr.sun_family = AF_UNIX;
  36. strcpy(unix_addr.sun_path, DNS_SOCK_NAME);
  37. int err = connect(socket_fd,(struct sockaddr*)&unix_addr, sizeof(unix_addr));
  38. if (err == -1) {
  39. fprintf(stderr, "connect failed (errno = %d).\n",errno);
  40. close(socket_fd);
  41. return -1;
  42. }
  43. char buf[256];
  44. strcpy(buf, "lookup: ");
  45. strcpy(&buf[8], hostName);
  46. err = send(socket_fd, buf, strlen(buf)+1, 0);
  47. if (err < 0)
  48. fprintf(stderr, "send(%s) returned error (errno=%d).\n",buf, errno);
  49. // receive 4 byte ID
  50. err = recv(socket_fd, buf, 256, 0);
  51. if (err < 0)
  52. fprintf(stderr, "recv() returned error (errno=%d).\n", errno);
  53. else
  54. {
  55. // printf("recv() returned %d bytes.");
  56. int id = *(int *)buf;
  57. fprintf(stderr, "id: %d\n", id);
  58. }
  59. return socket_fd;
  60. }
  61. static char *
  62. string_trim(char *s)
  63. {
  64. char *s2;
  65. if (!s) return 0;
  66. s2 = s + strlen(s) - 1;
  67. while (s2 > s && (*s2 == '\n' || *s2 == '\r' || *s2 == ' ' || *s2 == '\t'))
  68. *s2-- = 0;
  69. while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
  70. s++;
  71. return s;
  72. }
  73. hostent *
  74. bytesToHostent(char *buf)
  75. {
  76. int i;
  77. // int size = 0;
  78. int len, aliasCount, addressCount;
  79. int addrtype, addrlength;
  80. char* p = buf;
  81. char s[1024];
  82. len = *(int *)p; // length of name
  83. p += sizeof(int); // advance past name length
  84. memcpy(s, p, len); s[len] = 0;
  85. fprintf(stderr, "hostname: %s\n", s);
  86. p += len; // advance past name
  87. aliasCount = *(int *)p; // number of aliases
  88. p += sizeof(int); // advance past alias count
  89. for (i=0; i<aliasCount; i++) {
  90. len = *(int *)p; // length of alias name
  91. p += sizeof(int); // advance past alias name length
  92. memcpy(s, p, len); s[len] = 0;
  93. fprintf(stderr, "alias: %s\n", s);
  94. p += len; // advance past alias name
  95. }
  96. addrtype = *(int *)p;
  97. fprintf(stderr, "addrtype: %d\n", addrtype);
  98. p += sizeof(int);
  99. addrlength = *(int *)p;
  100. fprintf(stderr, "addrlength: %d\n", addrlength);
  101. p += sizeof(int);
  102. addressCount = *(int *)p;
  103. p += sizeof(int);
  104. for (i=0; i<addressCount; i++) {
  105. len = *(int *)p;
  106. p += sizeof(int);
  107. fprintf(stderr, "addr len: %d\n", len);
  108. fprintf(stderr, "addr : %x\n", *(int *)p);
  109. p += len;
  110. }
  111. // size = p - buf;
  112. // size += 1 + aliasCount;
  113. return 0;
  114. }
  115. int
  116. main(int argc, char* argv[])
  117. {
  118. PRStatus status;
  119. // launch daemon
  120. printf("### launch daemon...\n");
  121. PRProcessAttr *attributes = PR_NewProcessAttr();
  122. if (attributes == nullptr) {
  123. printf("PR_NewProcessAttr() failed.\n");
  124. return -1;
  125. }
  126. PRProcess *daemon = PR_CreateProcess("nsDnsAsyncLookup", nullptr, nullptr, attributes);
  127. if (daemon == nullptr) {
  128. printf("PR_CreateProcess failed.\n");
  129. } else {
  130. // status = PR_DetachProcess(daemon);
  131. //if (status != 0)
  132. // printf("PR_DetachProcess returned %d\n", status);
  133. //daemon = nullptr;
  134. }
  135. PR_DestroyProcessAttr(attributes);
  136. // create socket and connect to daemon
  137. int socket_fd = 0;
  138. bool notDone = true;
  139. char buf[1024];
  140. while(notDone) {
  141. int status = 0;
  142. fd_set fdset;
  143. FD_ZERO(&fdset);
  144. FD_SET(fileno(stdin), &fdset);
  145. if (socket_fd > 0)
  146. FD_SET(socket_fd, &fdset);
  147. status = select(getdtablehi(), &fdset, 0, 0, 0);
  148. if (status <= 0)
  149. {
  150. fprintf(stderr, "%s: select() returned %d\n", argv[0], status);
  151. exit(-1);
  152. }
  153. // which fd is set?
  154. if (FD_ISSET(fileno(stdin), &fdset))
  155. {
  156. char *line = fgets(buf, sizeof(buf)-1, stdin);
  157. line = string_trim(line);
  158. if(!strcmp(line, "quit") || !strcmp(line, "exit"))
  159. {
  160. fprintf(stderr, "bye now.\n");
  161. notDone = false;
  162. }
  163. else if (!strncmp(line, "abort ", 6))
  164. {
  165. // abort id
  166. }
  167. else if (strchr(line, ' ') || strchr(line, '\t'))
  168. {
  169. fprintf(stderr, "%s: unrecognized command %s.\n", argv[0], line);
  170. }
  171. else
  172. {
  173. fprintf(stderr, "%s: looking up %s...\n", argv[0], line);
  174. // initiate dns lookup
  175. socket_fd = async_dns_lookup(line);
  176. }
  177. }
  178. if (socket_fd && FD_ISSET(socket_fd, &fdset))
  179. {
  180. // read from socket, parse results
  181. int size = read(socket_fd, buf, 1024);
  182. if (size > 0)
  183. {
  184. // parse buffer into hostent
  185. char *p = buf;
  186. fprintf(stderr, "bytes read: %d\n", size);
  187. fprintf(stderr, "response code: %d\n", *(int *)p);
  188. p += sizeof(int);
  189. for (int i=0; i < size; i++) {
  190. if (!(i%8))
  191. fprintf(stderr, "\n");
  192. fprintf(stderr, "%2.2x ",(unsigned char)buf[i]);
  193. }
  194. fprintf(stderr, "\n");
  195. hostent *h;
  196. h = bytesToHostent(p);
  197. }
  198. close(socket_fd);
  199. socket_fd = 0;
  200. }
  201. }
  202. return 0;
  203. }
  204. /*
  205. buffer
  206. int nameLen;
  207. if (nameLen > 0)
  208. char [nameLen+1] name
  209. int aliasCount
  210. for each alias
  211. int aliasNameLen
  212. char [aliasNameLen+1] aliasName
  213. int h_addrtype
  214. int h_length
  215. int addrCount
  216. for each addr
  217. char[h_length] addr
  218. */