1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#include <dnswire/writer.h>
#include <dnswire/reader.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include "create_dnstap.c"
int main(int argc, const char* argv[])
{
if (argc < 2) {
return 1;
}
struct sockaddr_un path;
memset(&path, 0, sizeof(struct sockaddr_un));
path.sun_family = AF_UNIX;
strncpy(path.sun_path, argv[1], sizeof(path.sun_path) - 1);
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
return 1;
}
int ret = 1, attempts = 5;
while (attempts--) {
if (!(ret = connect(fd, (struct sockaddr*)&path, sizeof(struct sockaddr_un)))) {
break;
} else {
fprintf(stderr, "sleep\n");
sleep(1);
}
}
if (ret) {
close(fd);
return ret;
}
struct dnswire_writer writer;
if (dnswire_writer_init(&writer) != dnswire_ok) {
return 1;
}
if (dnswire_writer_set_bidirectional(&writer, true) != dnswire_ok) {
return 1;
}
// force small buffers to trigger buf resizing
writer.read_size = 4;
writer.read_inc = 4;
if (dnswire_writer_set_bufsize(&writer, 4) != dnswire_ok) {
return 1;
}
if (dnswire_writer_set_bufinc(&writer, 4) != dnswire_ok) {
return 1;
}
struct dnstap d = DNSTAP_INITIALIZER;
create_dnstap(&d, "writer_reader_unixsock-1");
dnswire_writer_set_dnstap(writer, &d);
while (1) {
enum dnswire_result res = dnswire_writer_write(&writer, fd);
switch (res) {
case dnswire_ok:
break;
case dnswire_again:
case dnswire_need_more:
continue;
default:
fprintf(stderr, "dnswire_writer_write() error\n");
return 1;
}
break;
}
create_dnstap(&d, "writer_reader_unixsock-2");
while (1) {
enum dnswire_result res = dnswire_writer_write(&writer, fd);
switch (res) {
case dnswire_ok:
break;
case dnswire_again:
case dnswire_need_more:
continue;
default:
fprintf(stderr, "dnswire_writer_write() error\n");
return 1;
}
break;
}
if (dnswire_writer_stop(&writer) != dnswire_ok) {
fprintf(stderr, "dnswire_writer_stop() failed\n");
return 1;
}
while (1) {
enum dnswire_result res = dnswire_writer_write(&writer, fd);
switch (res) {
case dnswire_ok:
case dnswire_endofdata:
break;
case dnswire_again:
case dnswire_need_more:
continue;
default:
fprintf(stderr, "dnswire_writer_write() error\n");
return 1;
}
break;
}
dnswire_writer_destroy(writer);
shutdown(fd, SHUT_RDWR);
close(fd);
return ret;
}
|