1
0

dns-server.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. typedef struct {
  27. uv_write_t req;
  28. uv_buf_t buf;
  29. } write_req_t;
  30. /* used to track multiple DNS requests received */
  31. typedef struct {
  32. char* prevbuf_ptr;
  33. int prevbuf_pos;
  34. int prevbuf_rem;
  35. } dnsstate;
  36. /* modify handle to append dnsstate */
  37. typedef struct {
  38. uv_tcp_t handle;
  39. dnsstate state;
  40. } dnshandle;
  41. static uv_loop_t* loop;
  42. static uv_tcp_t server;
  43. static void after_write(uv_write_t* req, int status);
  44. static void after_read(uv_stream_t*, ssize_t nread, const uv_buf_t* buf);
  45. static void on_close(uv_handle_t* peer);
  46. static void on_connection(uv_stream_t*, int status);
  47. #define WRITE_BUF_LEN (64*1024)
  48. #define DNSREC_LEN (4)
  49. #define LEN_OFFSET 0
  50. #define QUERYID_OFFSET 2
  51. static unsigned char DNSRsp[] = {
  52. 0, 43, 0, 0, 0x81, 0x80, 0, 1, 0, 1, 0, 0, 0, 0
  53. };
  54. static unsigned char qrecord[] = {
  55. 5, 'e', 'c', 'h', 'o', 's', 3, 's', 'r', 'v', 0, 0, 1, 0, 1
  56. };
  57. static unsigned char arecord[] = {
  58. 0xc0, 0x0c, 0, 1, 0, 1, 0, 0, 5, 0xbd, 0, 4, 10, 0, 1, 1
  59. };
  60. static void after_write(uv_write_t* req, int status) {
  61. write_req_t* wr;
  62. if (status) {
  63. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  64. ASSERT(0);
  65. }
  66. wr = (write_req_t*) req;
  67. /* Free the read/write buffer and the request */
  68. free(wr->buf.base);
  69. free(wr);
  70. }
  71. static void after_shutdown(uv_shutdown_t* req, int status) {
  72. uv_close((uv_handle_t*) req->handle, on_close);
  73. free(req);
  74. }
  75. static void addrsp(write_req_t* wr, char* hdr) {
  76. char * dnsrsp;
  77. short int rsplen;
  78. short int* reclen;
  79. rsplen = sizeof(DNSRsp) + sizeof(qrecord) + sizeof(arecord);
  80. ASSERT (rsplen + wr->buf.len < WRITE_BUF_LEN);
  81. dnsrsp = wr->buf.base + wr->buf.len;
  82. /* copy stock response */
  83. memcpy(dnsrsp, DNSRsp, sizeof(DNSRsp));
  84. memcpy(dnsrsp + sizeof(DNSRsp), qrecord, sizeof(qrecord));
  85. memcpy(dnsrsp + sizeof(DNSRsp) + sizeof(qrecord), arecord, sizeof(arecord));
  86. /* overwrite with network order length and id from request header */
  87. reclen = (short int*)dnsrsp;
  88. *reclen = htons(rsplen-2);
  89. dnsrsp[QUERYID_OFFSET] = hdr[QUERYID_OFFSET];
  90. dnsrsp[QUERYID_OFFSET+1] = hdr[QUERYID_OFFSET+1];
  91. wr->buf.len += rsplen;
  92. }
  93. static void process_req(uv_stream_t* handle,
  94. ssize_t nread,
  95. const uv_buf_t* buf) {
  96. write_req_t* wr;
  97. dnshandle* dns = (dnshandle*)handle;
  98. char hdrbuf[DNSREC_LEN];
  99. int hdrbuf_remaining = DNSREC_LEN;
  100. int rec_remaining = 0;
  101. int readbuf_remaining;
  102. char* dnsreq;
  103. char* hdrstart;
  104. int usingprev = 0;
  105. wr = (write_req_t*) malloc(sizeof *wr);
  106. wr->buf.base = (char*)malloc(WRITE_BUF_LEN);
  107. wr->buf.len = 0;
  108. if (dns->state.prevbuf_ptr != NULL) {
  109. dnsreq = dns->state.prevbuf_ptr + dns->state.prevbuf_pos;
  110. readbuf_remaining = dns->state.prevbuf_rem;
  111. usingprev = 1;
  112. } else {
  113. dnsreq = buf->base;
  114. readbuf_remaining = nread;
  115. }
  116. hdrstart = dnsreq;
  117. while (dnsreq != NULL) {
  118. /* something to process */
  119. while (readbuf_remaining > 0) {
  120. /* something to process in current buffer */
  121. if (hdrbuf_remaining > 0) {
  122. /* process len and id */
  123. if (readbuf_remaining < hdrbuf_remaining) {
  124. /* too little to get request header. save for next buffer */
  125. memcpy(&hdrbuf[DNSREC_LEN - hdrbuf_remaining],
  126. dnsreq,
  127. readbuf_remaining);
  128. hdrbuf_remaining = DNSREC_LEN - readbuf_remaining;
  129. break;
  130. } else {
  131. /* save header */
  132. memcpy(&hdrbuf[DNSREC_LEN - hdrbuf_remaining],
  133. dnsreq,
  134. hdrbuf_remaining);
  135. dnsreq += hdrbuf_remaining;
  136. readbuf_remaining -= hdrbuf_remaining;
  137. hdrbuf_remaining = 0;
  138. /* get record length */
  139. rec_remaining = (unsigned) hdrbuf[0] * 256 + (unsigned) hdrbuf[1];
  140. rec_remaining -= (DNSREC_LEN - 2);
  141. }
  142. }
  143. if (rec_remaining <= readbuf_remaining) {
  144. /* prepare reply */
  145. addrsp(wr, hdrbuf);
  146. /* move to next record */
  147. dnsreq += rec_remaining;
  148. hdrstart = dnsreq;
  149. readbuf_remaining -= rec_remaining;
  150. rec_remaining = 0;
  151. hdrbuf_remaining = DNSREC_LEN;
  152. } else {
  153. /* otherwise this buffer is done. */
  154. rec_remaining -= readbuf_remaining;
  155. break;
  156. }
  157. }
  158. /* If we had to use bytes from prev buffer, start processing the current
  159. * one.
  160. */
  161. if (usingprev == 1) {
  162. /* free previous buffer */
  163. free(dns->state.prevbuf_ptr);
  164. dnsreq = buf->base;
  165. readbuf_remaining = nread;
  166. usingprev = 0;
  167. } else {
  168. dnsreq = NULL;
  169. }
  170. }
  171. /* send write buffer */
  172. if (wr->buf.len > 0) {
  173. if (uv_write((uv_write_t*) &wr->req, handle, &wr->buf, 1, after_write)) {
  174. FATAL("uv_write failed");
  175. }
  176. }
  177. if (readbuf_remaining > 0) {
  178. /* save start of record position, so we can continue on next read */
  179. dns->state.prevbuf_ptr = buf->base;
  180. dns->state.prevbuf_pos = hdrstart - buf->base;
  181. dns->state.prevbuf_rem = nread - dns->state.prevbuf_pos;
  182. } else {
  183. /* nothing left in this buffer */
  184. dns->state.prevbuf_ptr = NULL;
  185. dns->state.prevbuf_pos = 0;
  186. dns->state.prevbuf_rem = 0;
  187. free(buf->base);
  188. }
  189. }
  190. static void after_read(uv_stream_t* handle,
  191. ssize_t nread,
  192. const uv_buf_t* buf) {
  193. uv_shutdown_t* req;
  194. if (nread < 0) {
  195. /* Error or EOF */
  196. ASSERT(nread == UV_EOF);
  197. if (buf->base) {
  198. free(buf->base);
  199. }
  200. req = malloc(sizeof *req);
  201. uv_shutdown(req, handle, after_shutdown);
  202. return;
  203. }
  204. if (nread == 0) {
  205. /* Everything OK, but nothing read. */
  206. free(buf->base);
  207. return;
  208. }
  209. /* process requests and send responses */
  210. process_req(handle, nread, buf);
  211. }
  212. static void on_close(uv_handle_t* peer) {
  213. free(peer);
  214. }
  215. static void buf_alloc(uv_handle_t* handle,
  216. size_t suggested_size,
  217. uv_buf_t* buf) {
  218. buf->base = malloc(suggested_size);
  219. buf->len = suggested_size;
  220. }
  221. static void on_connection(uv_stream_t* server, int status) {
  222. dnshandle* handle;
  223. int r;
  224. ASSERT(status == 0);
  225. handle = (dnshandle*) malloc(sizeof *handle);
  226. ASSERT(handle != NULL);
  227. /* initialize read buffer state */
  228. handle->state.prevbuf_ptr = 0;
  229. handle->state.prevbuf_pos = 0;
  230. handle->state.prevbuf_rem = 0;
  231. r = uv_tcp_init(loop, (uv_tcp_t*)handle);
  232. ASSERT(r == 0);
  233. r = uv_accept(server, (uv_stream_t*)handle);
  234. ASSERT(r == 0);
  235. r = uv_read_start((uv_stream_t*)handle, buf_alloc, after_read);
  236. ASSERT(r == 0);
  237. }
  238. static int dns_start(int port) {
  239. struct sockaddr_in addr;
  240. int r;
  241. ASSERT(0 == uv_ip4_addr("0.0.0.0", port, &addr));
  242. r = uv_tcp_init(loop, &server);
  243. if (r) {
  244. /* TODO: Error codes */
  245. fprintf(stderr, "Socket creation error\n");
  246. return 1;
  247. }
  248. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  249. if (r) {
  250. /* TODO: Error codes */
  251. fprintf(stderr, "Bind error\n");
  252. return 1;
  253. }
  254. r = uv_listen((uv_stream_t*)&server, 128, on_connection);
  255. if (r) {
  256. /* TODO: Error codes */
  257. fprintf(stderr, "Listen error\n");
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. HELPER_IMPL(dns_server) {
  263. loop = uv_default_loop();
  264. if (dns_start(TEST_PORT_2))
  265. return 1;
  266. uv_run(loop, UV_RUN_DEFAULT);
  267. return 0;
  268. }