Debian Codegraph @

examples/receiver.c

raw
1 #include <dnswire/reader.h>
2
3 #include <stdio.h>
4 #include <errno.h>
5 #include <arpa/inet.h>
6 #include <stdlib.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <sys/un.h>
10
11 #include "print_dnstap.c"
12
13 int main(int argc, const char* argv[])
14 {
15 if (argc < 2) {
16 fprintf(stderr, "usage: receiver [ <IP> <port> | <unix socket path> ]\n");
17 return 1;
18 }
19
20 /*
21 * We first initialize the reader and check that it can allocate the
22 * buffers it needs.
23 */
24
25 struct dnswire_reader reader;
26
27 if (dnswire_reader_init(&reader) != dnswire_ok) {
28 fprintf(stderr, "Unable to initialize dnswire reader\n");
29 return 1;
30 }
31
32 /*
33 * Allow bidirectional communication over the TCP or UNIX socket.
34 */
35 if (dnswire_reader_allow_bidirectional(&reader, true) != dnswire_ok) {
36 fprintf(stderr, "Unable to set dnswire reader to bidirectional mode\n");
37 return 1;
38 }
39
40 int sockfd;
41
42 if (argc == 2) {
43 /*
44 * We setup a unix socket at the given path on command line.
45 */
46
47 struct sockaddr_un path;
48
49 memset(&path, 0, sizeof(struct sockaddr_un));
50 path.sun_family = AF_UNIX;
51 strncpy(path.sun_path, argv[1], sizeof(path.sun_path) - 1);
52
53 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
54 if (sockfd == -1) {
55 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
56 return 1;
57 }
58 printf("socket\n");
59
60 if (bind(sockfd, (struct sockaddr*)&path, sizeof(struct sockaddr_un))) {
61 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
62 close(sockfd);
63 return 1;
64 }
65 printf("bind\n");
66 } else {
67 /*
68 * We setup and listen on the IP and port given on command line.
69 */
70
71 struct sockaddr_storage addr_store;
72 struct sockaddr_in* addr = (struct sockaddr_in*)&addr_store;
73 socklen_t addrlen;
74
75 if (strchr(argv[1], ':')) {
76 addr->sin_family = AF_INET6;
77 addrlen = sizeof(struct sockaddr_in6);
78 } else {
79 addr->sin_family = AF_INET;
80 addrlen = sizeof(struct sockaddr_in);
81 }
82
83 if (inet_pton(addr->sin_family, argv[1], &addr->sin_addr) != 1) {
84 fprintf(stderr, "inet_pton(%s) failed: %s\n", argv[1], strerror(errno));
85 return 1;
86 }
87
88 addr->sin_port = ntohs(atoi(argv[2]));
89
90 sockfd = socket(addr->sin_family, SOCK_STREAM, IPPROTO_TCP);
91 if (sockfd == -1) {
92 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
93 return 1;
94 }
95 printf("socket\n");
96
97 if (bind(sockfd, (struct sockaddr*)addr, addrlen)) {
98 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
99 close(sockfd);
100 return 1;
101 }
102 printf("bind\n");
103 }
104
105 if (listen(sockfd, 1)) {
106 fprintf(stderr, "listen() failed: %s\n", strerror(errno));
107 close(sockfd);
108 return 1;
109 }
110 printf("listen\n");
111
112 int clifd = accept(sockfd, 0, 0);
113 if (clifd < 0) {
114 fprintf(stderr, "accept() failed: %s\n", strerror(errno));
115 close(sockfd);
116 return 1;
117 }
118 printf("accept\n");
119
120 /*
121 * We have accepted connection from the sender!
122 *
123 * We now loop until we have a DNSTAP message, the stream was stopped
124 * or we got an error.
125 */
126
127 int done = 0;
128
129 printf("receiving...\n");
130 while (!done) {
131 switch (dnswire_reader_read(&reader, clifd)) {
132 case dnswire_have_dnstap:
133 /*
134 * We received a DNSTAP message, let's print it.
135 */
136 print_dnstap(dnswire_reader_dnstap(reader));
137 break;
138 case dnswire_again:
139 case dnswire_need_more:
140 /*
141 * This indicates that we need to call the reader again as it
142 * will only do one pass in a non-blocking fasion.
143 */
144 break;
145 case dnswire_endofdata:
146 /*
147 * The stream was stopped from the sender side, we're done!
148 */
149 printf("stopped\n");
150 done = 1;
151 break;
152 default:
153 fprintf(stderr, "dnswire_reader_read() error\n");
154 done = 1;
155 }
156 }
157
158 dnswire_reader_destroy(reader);
159
160 /*
161 * Time to exit, let's shutdown and close the sockets.
162 */
163
164 shutdown(clifd, SHUT_RDWR);
165 close(clifd);
166 close(sockfd);
167
168 return 0;
169 }