| 1 |
#include <dnswire/reader.h> |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <uv.h> |
| 7 |
#include <arpa/inet.h> |
| 8 |
#include <time.h> |
| 9 |
#include <errno.h> |
| 10 |
#include <stdbool.h> |
| 11 |
|
| 12 |
#include "print_dnstap.c" |
| 13 |
|
| 14 |
#define BUF_SIZE 4096 |
| 15 |
|
| 16 |
uv_loop_t* loop; |
| 17 |
uv_tcp_t sock; |
| 18 |
uv_connect_t conn; |
| 19 |
char rbuf[BUF_SIZE]; |
| 20 |
|
| 21 |
struct dnswire_reader reader; |
| 22 |
|
| 23 |
void client_alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) |
| 24 |
{ |
| 25 |
buf->base = rbuf; |
| 26 |
buf->len = sizeof(rbuf); |
| 27 |
} |
| 28 |
|
| 29 |
void client_read(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) |
| 30 |
{ |
| 31 |
if (nread > 0) { |
| 32 |
/* |
| 33 |
* We now push all the data we got to the reader. |
| 34 |
*/ |
| 35 |
size_t pushed = 0; |
| 36 |
|
| 37 |
while (pushed < nread) { |
| 38 |
enum dnswire_result res = dnswire_reader_push(&reader, (uint8_t*)&buf->base[pushed], nread - pushed, 0, 0); |
| 39 |
|
| 40 |
pushed += dnswire_reader_pushed(reader); |
| 41 |
|
| 42 |
switch (res) { |
| 43 |
case dnswire_have_dnstap: |
| 44 |
/* |
| 45 |
* We got a DNSTAP message, lets print it! |
| 46 |
*/ |
| 47 |
print_dnstap(dnswire_reader_dnstap(reader)); |
| 48 |
break; |
| 49 |
case dnswire_again: |
| 50 |
case dnswire_need_more: |
| 51 |
break; |
| 52 |
case dnswire_endofdata: |
| 53 |
/* |
| 54 |
* The remote end sent a control stop or finish. |
| 55 |
*/ |
| 56 |
uv_close((uv_handle_t*)handle, 0); |
| 57 |
return; |
| 58 |
default: |
| 59 |
fprintf(stderr, "dnswire_reader_fread() error\n"); |
| 60 |
uv_close((uv_handle_t*)handle, 0); |
| 61 |
return; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
if (nread < 0) { |
| 66 |
if (nread != UV_EOF) { |
| 67 |
fprintf(stderr, "client_read() error: %s\n", uv_err_name(nread)); |
| 68 |
} else { |
| 69 |
printf("disconnected\n"); |
| 70 |
} |
| 71 |
uv_close((uv_handle_t*)handle, 0); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void on_connect(uv_connect_t* req, int status) |
| 76 |
{ |
| 77 |
/* |
| 78 |
* We have connected to the sender, check that there was no errors |
| 79 |
* and start receiving incoming frames. |
| 80 |
*/ |
| 81 |
|
| 82 |
if (status < 0) { |
| 83 |
fprintf(stderr, "on_connect() error: %s\n", uv_strerror(status)); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
uv_read_start((uv_stream_t*)&sock, client_alloc_buffer, client_read); |
| 88 |
} |
| 89 |
|
| 90 |
int main(int argc, const char* argv[]) |
| 91 |
{ |
| 92 |
if (argc < 3) { |
| 93 |
fprintf(stderr, "usage: client_receiver_uv <IP> <port>\n"); |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* We first initialize the reader and check that it can allocate the |
| 99 |
* buffers it needs. |
| 100 |
*/ |
| 101 |
|
| 102 |
if (dnswire_reader_init(&reader) != dnswire_ok) { |
| 103 |
fprintf(stderr, "Unable to initialize dnswire reader\n"); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* We set this reader to reject bidirectional communication. |
| 109 |
*/ |
| 110 |
if (dnswire_reader_allow_bidirectional(&reader, false) != dnswire_ok) { |
| 111 |
fprintf(stderr, "Unable to deny bidirectional communication\n"); |
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* We setup a TCP client using libuv and connect to the given server. |
| 117 |
*/ |
| 118 |
|
| 119 |
struct sockaddr_storage addr; |
| 120 |
int port = atoi(argv[2]); |
| 121 |
|
| 122 |
if (strchr(argv[1], ':')) { |
| 123 |
uv_ip6_addr(argv[1], port, (struct sockaddr_in6*)&addr); |
| 124 |
} else { |
| 125 |
uv_ip4_addr(argv[1], port, (struct sockaddr_in*)&addr); |
| 126 |
} |
| 127 |
|
| 128 |
loop = uv_default_loop(); |
| 129 |
|
| 130 |
uv_tcp_init(loop, &sock); |
| 131 |
uv_tcp_connect(&conn, &sock, (const struct sockaddr*)&addr, on_connect); |
| 132 |
|
| 133 |
return uv_run(loop, UV_RUN_DEFAULT); |
| 134 |
} |