Debian Codegraph @

examples/sender.c

raw
1 #include <dnswire/writer.h>
2
3 #include <arpa/inet.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10
11 #include "create_dnstap.c"
12
13 int main(int argc, const char* argv[])
14 {
15 if (argc < 2) {
16 fprintf(stderr, "usage: sender [ <IP> <port> | <unix socket path> ]\n");
17 return 1;
18 }
19
20 /*
21 * We first initialize the writer and check that it can allocate the
22 * buffers it needs.
23 */
24
25 struct dnswire_writer writer;
26
27 if (dnswire_writer_init(&writer) != dnswire_ok) {
28 fprintf(stderr, "Unable to initialize dnswire writer\n");
29 return 1;
30 }
31
32 /*
33 * Use bidirectional communication over the TCP or UNIX socket.
34 */
35 if (dnswire_writer_set_bidirectional(&writer, true) != dnswire_ok) {
36 fprintf(stderr, "Unable to set dnswire writer to bidirectional mode\n");
37 return 1;
38 }
39
40 int sockfd;
41
42 if (argc == 2) {
43 /*
44 * We setup and connect to 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 (connect(sockfd, (struct sockaddr*)&path, sizeof(struct sockaddr_un))) {
61 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
62 close(sockfd);
63 return 1;
64 }
65 printf("connect\n");
66 } else {
67 /*
68 * We setup and connect to 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 (connect(sockfd, (struct sockaddr*)addr, addrlen)) {
98 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
99 close(sockfd);
100 return 1;
101 }
102 printf("connect\n");
103 }
104
105 /*
106 * We are now connected!
107 *
108 * Now we create a DNSTAP message.
109 */
110
111 struct dnstap d = create_dnstap("sender");
112
113 /*
114 * We set the DNSTAP message the writer should write.
115 */
116
117 dnswire_writer_set_dnstap(writer, &d);
118
119 /*
120 * We now loop and wait for the DNSTAP message to be written.
121 */
122
123 int done = 0;
124
125 printf("sending...\n");
126 while (!done) {
127 switch (dnswire_writer_write(&writer, sockfd)) {
128 case dnswire_ok:
129 /*
130 * The DNSTAP message was written successfully, we can now set
131 * a new DNSTAP message for the writer or stop the stream.
132 *
133 * This stops the stream, loop again until it's stopped.
134 */
135 if (dnswire_writer_stop(&writer) != dnswire_ok) {
136 printf("stopping failed!\n");
137 done = 1;
138 }
139 break;
140 case dnswire_again:
141 /*
142 * This indicates that we need to call the writer again as it
143 * will only do one pass in a non-blocking fasion.
144 */
145 break;
146 case dnswire_endofdata:
147 /*
148 * The stream is stopped, we're done!
149 */
150 printf("stopped\n");
151 done = 1;
152 break;
153 default:
154 fprintf(stderr, "dnswire_writer_write() error\n");
155 done = 1;
156 }
157 }
158
159 dnswire_writer_destroy(writer);
160
161 /*
162 * Time to exit, let's shutdown and close the sockets.
163 */
164
165 shutdown(sockfd, SHUT_RDWR);
166 close(sockfd);
167
168 return 0;
169 }