1
0

blackhole-server.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. typedef struct {
  26. uv_tcp_t handle;
  27. uv_shutdown_t shutdown_req;
  28. } conn_rec;
  29. static uv_tcp_t tcp_server;
  30. static void connection_cb(uv_stream_t* stream, int status);
  31. static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
  32. static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
  33. static void shutdown_cb(uv_shutdown_t* req, int status);
  34. static void close_cb(uv_handle_t* handle);
  35. static void connection_cb(uv_stream_t* stream, int status) {
  36. conn_rec* conn;
  37. int r;
  38. ASSERT(status == 0);
  39. ASSERT(stream == (uv_stream_t*)&tcp_server);
  40. conn = malloc(sizeof *conn);
  41. ASSERT(conn != NULL);
  42. r = uv_tcp_init(stream->loop, &conn->handle);
  43. ASSERT(r == 0);
  44. r = uv_accept(stream, (uv_stream_t*)&conn->handle);
  45. ASSERT(r == 0);
  46. r = uv_read_start((uv_stream_t*)&conn->handle, alloc_cb, read_cb);
  47. ASSERT(r == 0);
  48. }
  49. static void alloc_cb(uv_handle_t* handle,
  50. size_t suggested_size,
  51. uv_buf_t* buf) {
  52. static char slab[65536];
  53. buf->base = slab;
  54. buf->len = sizeof(slab);
  55. }
  56. static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  57. conn_rec* conn;
  58. int r;
  59. if (nread >= 0)
  60. return;
  61. ASSERT(nread == UV_EOF);
  62. conn = container_of(stream, conn_rec, handle);
  63. r = uv_shutdown(&conn->shutdown_req, stream, shutdown_cb);
  64. ASSERT(r == 0);
  65. }
  66. static void shutdown_cb(uv_shutdown_t* req, int status) {
  67. conn_rec* conn = container_of(req, conn_rec, shutdown_req);
  68. uv_close((uv_handle_t*)&conn->handle, close_cb);
  69. }
  70. static void close_cb(uv_handle_t* handle) {
  71. conn_rec* conn = container_of(handle, conn_rec, handle);
  72. free(conn);
  73. }
  74. HELPER_IMPL(tcp4_blackhole_server) {
  75. struct sockaddr_in addr;
  76. uv_loop_t* loop;
  77. int r;
  78. loop = uv_default_loop();
  79. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  80. r = uv_tcp_init(loop, &tcp_server);
  81. ASSERT(r == 0);
  82. r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  83. ASSERT(r == 0);
  84. r = uv_listen((uv_stream_t*)&tcp_server, 128, connection_cb);
  85. ASSERT(r == 0);
  86. r = uv_run(loop, UV_RUN_DEFAULT);
  87. ASSERT(0 && "Blackhole server dropped out of event loop.");
  88. return 0;
  89. }