Advertisement
a3f

libuv TCP Proxy

a3f
May 31st, 2014
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. /*
  2. simple proxy using libuv
  3. */
  4. #include "../uv/uv.h"
  5. #include "../uvhelper.h"
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define CLIENT_PORT 8000
  11. #define SERVER_PORT 7171
  12.  
  13. #define strh(x) #x
  14. #define str(x) strh(x)
  15.  
  16. void on_client_connect(uv_stream_t *server, int status);
  17.  
  18. uv_loop_t *loop;
  19. uv_tcp_t client, proxy, server;
  20.  
  21. int main (void)
  22. {
  23.     loop = uv_default_loop();
  24.  
  25.     uv_tcp_init(loop, &client);
  26.     uv_tcp_init(loop, &server);
  27.     uv_tcp_init(loop, &proxy);
  28.  
  29.     struct sockaddr_in bind_addr;
  30.     uv_ip4_addr("0.0.0.0", CLIENT_PORT, &bind_addr);
  31.     uv_tcp_bind(&proxy, (const struct sockaddr *)&bind_addr, 0);
  32.     uv_listen((uv_stream_t *)&proxy, SOMAXCONN, on_client_connect);
  33.    
  34.     printf(":: Listening on all interfaces @port=" str(SERVER_PORT) "\n");
  35.     uv_run(loop, UV_RUN_DEFAULT);
  36. }
  37. void read_from_client(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
  38. {
  39.     printf("%s = %d bytes\n", __func__, nread);
  40.     if (nread > 0)
  41.     {
  42.         uv_easy_write((uv_stream_t *)&server, -1,
  43.         &(uv_buf_t) {.base = buf->base, .len = nread}
  44.                      );
  45.     }
  46. }
  47. void read_from_server(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
  48. {
  49.     printf("%s = %d bytes\n", __func__, nread);
  50.     if (nread > 0)
  51.     {
  52.     printf(strh(__func__) "= %u bytes\n", nread);
  53.     if (nread > 0)
  54.     {
  55.         uv_easy_write((uv_stream_t *)&client, -1,
  56.         &(uv_buf_t) {.base = buf->base, .len = nread}
  57.                      );
  58.     }
  59. }
  60. void on_server_connect(uv_connect_t *con, int status)
  61. {
  62.     printf("Server connection established.\n");
  63.     uv_read_start((uv_stream_t *) &server, alloc_buffer, read_from_server);
  64.  
  65.     if (uv_accept((uv_stream_t *)&proxy, (uv_stream_t *) &client) == 0)
  66.     {
  67.         uv_read_start((uv_stream_t *) &client, alloc_buffer, read_from_client);
  68.     }
  69.     else
  70.     {
  71.         printf("error in on_servc");
  72.         uv_close((uv_handle_t *) &proxy, NULL);
  73.     }
  74. }
  75.  
  76. void on_client_connect(uv_stream_t *proxyq, int status)
  77. {
  78.     printf("Client connection established\n");
  79.     struct sockaddr_in bind_addr;
  80.     uv_ip4_addr("127.0.0.1", SERVER_PORT, &bind_addr);
  81.     uv_connect_t *connect = malloc(sizeof(uv_connect_t));
  82.     uv_tcp_connect(connect, &server, (const struct sockaddr *)&bind_addr,
  83.                    on_server_connect);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement