| 1 |
#include <dnswire/reader.h> |
| 2 |
#include <dnswire/writer.h> |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <errno.h> |
| 6 |
#include <string.h> |
| 7 |
#include <arpa/inet.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <sys/types.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <netinet/in.h> |
| 12 |
#include <sys/un.h> |
| 13 |
|
| 14 |
/* |
| 15 |
* This is a combination of reader and simpler_sender, comments may |
| 16 |
* be a bit off since they haven't been updated for this. |
| 17 |
*/ |
| 18 |
|
| 19 |
int main(int argc, const char* argv[]) |
| 20 |
{ |
| 21 |
if (argc < 3) { |
| 22 |
fprintf(stderr, "usage: reader_sender <file> [ <IP> <port> | <unix socket path> ]\n"); |
| 23 |
return 1; |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* First we open the given file and read all of the content. |
| 28 |
*/ |
| 29 |
|
| 30 |
FILE* fp = fopen(argv[1], "r"); |
| 31 |
if (!fp) { |
| 32 |
fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno)); |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* We now initialize the reader and check that it can allocate the |
| 38 |
* buffers it needs. |
| 39 |
*/ |
| 40 |
|
| 41 |
struct dnswire_reader reader; |
| 42 |
int done = 0; |
| 43 |
|
| 44 |
if (dnswire_reader_init(&reader) != dnswire_ok) { |
| 45 |
fprintf(stderr, "Unable to initialize dnswire reader\n"); |
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
* We first initialize the writer and check that it can allocate the |
| 51 |
* buffers it needs. |
| 52 |
*/ |
| 53 |
|
| 54 |
struct dnswire_writer writer; |
| 55 |
|
| 56 |
if (dnswire_writer_init(&writer) != dnswire_ok) { |
| 57 |
fprintf(stderr, "Unable to initialize dnswire writer\n"); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Use bidirectional communication over the TCP or UNIX socket. |
| 63 |
*/ |
| 64 |
if (dnswire_writer_set_bidirectional(&writer, true) != dnswire_ok) { |
| 65 |
fprintf(stderr, "Unable to set dnswire writer to bidirectional mode\n"); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
int sockfd; |
| 70 |
|
| 71 |
if (argc == 3) { |
| 72 |
/* |
| 73 |
* We setup and connect to a unix socket at the given path on command line. |
| 74 |
*/ |
| 75 |
|
| 76 |
struct sockaddr_un path; |
| 77 |
|
| 78 |
memset(&path, 0, sizeof(struct sockaddr_un)); |
| 79 |
path.sun_family = AF_UNIX; |
| 80 |
strncpy(path.sun_path, argv[2], sizeof(path.sun_path) - 1); |
| 81 |
|
| 82 |
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 83 |
if (sockfd == -1) { |
| 84 |
fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 85 |
return 1; |
| 86 |
} |
| 87 |
printf("socket\n"); |
| 88 |
|
| 89 |
if (connect(sockfd, (struct sockaddr*)&path, sizeof(struct sockaddr_un))) { |
| 90 |
fprintf(stderr, "connect() failed: %s\n", strerror(errno)); |
| 91 |
close(sockfd); |
| 92 |
return 1; |
| 93 |
} |
| 94 |
printf("connect\n"); |
| 95 |
} else { |
| 96 |
/* |
| 97 |
* We setup and connect to the IP and port given on command line. |
| 98 |
*/ |
| 99 |
|
| 100 |
struct sockaddr_storage addr_store; |
| 101 |
struct sockaddr_in* addr = (struct sockaddr_in*)&addr_store; |
| 102 |
socklen_t addrlen; |
| 103 |
|
| 104 |
if (strchr(argv[2], ':')) { |
| 105 |
addr->sin_family = AF_INET6; |
| 106 |
addrlen = sizeof(struct sockaddr_in6); |
| 107 |
} else { |
| 108 |
addr->sin_family = AF_INET; |
| 109 |
addrlen = sizeof(struct sockaddr_in); |
| 110 |
} |
| 111 |
|
| 112 |
if (inet_pton(addr->sin_family, argv[2], &addr->sin_addr) != 1) { |
| 113 |
fprintf(stderr, "inet_pton(%s) failed: %s\n", argv[2], strerror(errno)); |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
addr->sin_port = ntohs(atoi(argv[3])); |
| 118 |
|
| 119 |
sockfd = socket(addr->sin_family, SOCK_STREAM, IPPROTO_TCP); |
| 120 |
if (sockfd == -1) { |
| 121 |
fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 122 |
return 1; |
| 123 |
} |
| 124 |
printf("socket\n"); |
| 125 |
|
| 126 |
if (connect(sockfd, (struct sockaddr*)addr, addrlen)) { |
| 127 |
fprintf(stderr, "connect() failed: %s\n", strerror(errno)); |
| 128 |
close(sockfd); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
printf("connect\n"); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* We now loop until we have a DNSTAP message, the stream was stopped |
| 136 |
* or we got an error. |
| 137 |
*/ |
| 138 |
|
| 139 |
int done2 = 0; |
| 140 |
|
| 141 |
while (!done) { |
| 142 |
switch (dnswire_reader_fread(&reader, fp)) { |
| 143 |
case dnswire_have_dnstap: |
| 144 |
dnswire_writer_set_dnstap(writer, dnswire_reader_dnstap(reader)); |
| 145 |
|
| 146 |
printf("sending...\n"); |
| 147 |
done2 = 0; |
| 148 |
while (!done2) { |
| 149 |
switch (dnswire_writer_write(&writer, sockfd)) { |
| 150 |
case dnswire_ok: |
| 151 |
/* |
| 152 |
* The DNSTAP message was written successfully, we can now set |
| 153 |
* a new DNSTAP message for the writer or stop the stream. |
| 154 |
*/ |
| 155 |
printf("sent\n"); |
| 156 |
done2 = 1; |
| 157 |
break; |
| 158 |
case dnswire_again: |
| 159 |
break; |
| 160 |
case dnswire_endofdata: |
| 161 |
/* |
| 162 |
* The stream is stopped... |
| 163 |
*/ |
| 164 |
printf("prematurely stopped\n"); |
| 165 |
done = 1; |
| 166 |
done2 = 2; |
| 167 |
break; |
| 168 |
default: |
| 169 |
fprintf(stderr, "dnswire_writer_write() error\n"); |
| 170 |
done = 1; |
| 171 |
done2 = 2; |
| 172 |
} |
| 173 |
} |
| 174 |
break; |
| 175 |
case dnswire_again: |
| 176 |
case dnswire_need_more: |
| 177 |
break; |
| 178 |
case dnswire_endofdata: |
| 179 |
done = 1; |
| 180 |
break; |
| 181 |
default: |
| 182 |
fprintf(stderr, "dnswire_reader_fread() error\n"); |
| 183 |
done = 1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if (done2 != 2) { |
| 188 |
/* |
| 189 |
* This stops the stream, loop again until it's stopped. |
| 190 |
*/ |
| 191 |
printf("stopping...\n"); |
| 192 |
dnswire_writer_stop(&writer); |
| 193 |
done2 = 0; |
| 194 |
while (!done2) { |
| 195 |
switch (dnswire_writer_write(&writer, sockfd)) { |
| 196 |
case dnswire_again: |
| 197 |
break; |
| 198 |
case dnswire_endofdata: |
| 199 |
/* |
| 200 |
* The stream is stopped, we're done! |
| 201 |
*/ |
| 202 |
printf("stopped\n"); |
| 203 |
done2 = 1; |
| 204 |
break; |
| 205 |
default: |
| 206 |
fprintf(stderr, "dnswire_writer_write() error\n"); |
| 207 |
done2 = 1; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
dnswire_writer_destroy(writer); |
| 213 |
|
| 214 |
/* |
| 215 |
* Time to exit, let's shutdown and close the sockets. |
| 216 |
*/ |
| 217 |
|
| 218 |
shutdown(sockfd, SHUT_RDWR); |
| 219 |
close(sockfd); |
| 220 |
|
| 221 |
dnswire_reader_destroy(reader); |
| 222 |
fclose(fp); |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |