blob: 83089d74af32a90bede562ae2b20d9e857a797e4 [file] [log] [blame]
Brian Silverman771b4b82013-04-12 16:36:56 -07001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <netinet/in.h>
5#include <arpa/inet.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <pthread.h>
9#include <sys/stat.h>
10#include <assert.h>
11
12#include "aos/common/logging/logging_impl.h"
13#include "aos/common/Configuration.h"
14
15namespace aos {
16namespace {
17
18struct FDsToCopy {
19 const int input;
20 const int output;
Brian Silvermane1514fc2013-04-13 14:57:35 -070021
22 const struct sockaddr_in *const interface_address;
Brian Silverman771b4b82013-04-12 16:36:56 -070023};
24
25void *FDCopyThread(void *to_copy_in) {
26 FDsToCopy *to_copy = static_cast<FDsToCopy *>(to_copy_in);
27
28 char buffer[32768];
29 ssize_t position = 0;
30 while (true) {
31 assert(position >= 0);
32 assert(position <= static_cast<ssize_t>(sizeof(buffer)));
33 if (position != sizeof(buffer)) {
Brian Silvermane1514fc2013-04-13 14:57:35 -070034 ssize_t read_bytes;
35 bool good_data = true;
36 if (to_copy->interface_address != NULL) {
37 char control_buffer[0x100];
38 struct msghdr header;
39 memset(static_cast<void *>(&header), 0, sizeof(header));
40 header.msg_control = control_buffer;
41 header.msg_controllen = sizeof(control_buffer);
42 struct iovec iovecs[1];
43 iovecs[0].iov_base = buffer + position;
44 iovecs[0].iov_len = position - sizeof(buffer);
45 header.msg_iov = iovecs;
46 header.msg_iovlen = sizeof(iovecs) / sizeof(iovecs[0]);
47 read_bytes = recvmsg(to_copy->input, &header, 0);
48 if (read_bytes != -1) {
49 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&header);
50 cmsg != NULL;
51 cmsg = CMSG_NXTHDR(&header, cmsg)) {
52 if (cmsg->cmsg_level == IPPROTO_IP &&
53 cmsg->cmsg_type == IP_PKTINFO) {
54 struct in_pktinfo *pktinfo =
55 reinterpret_cast<struct in_pktinfo *>(CMSG_DATA(cmsg));
56 good_data = pktinfo->ipi_spec_dst.s_addr ==
57 to_copy->interface_address->sin_addr.s_addr;
58 }
59 }
60 }
61 } else {
62 read_bytes = read(to_copy->input,
63 buffer + position, position - sizeof(buffer));
64 }
Brian Silverman771b4b82013-04-12 16:36:56 -070065 if (read_bytes == -1) {
66 if (errno != EINTR) {
67 LOG(FATAL, "read(%d, %p, %zd) failed with %d: %s\n",
68 to_copy->input, buffer + position, position - sizeof(buffer),
69 errno, strerror(errno));
70 }
Brian Silvermane1514fc2013-04-13 14:57:35 -070071 } else if (read_bytes == 0 && to_copy->interface_address == NULL) {
Brian Silverman771b4b82013-04-12 16:36:56 -070072 // read(2) says that this means EOF
73 return NULL;
74 }
Brian Silvermane1514fc2013-04-13 14:57:35 -070075 if (good_data) {
76 position += read_bytes;
77 }
Brian Silverman771b4b82013-04-12 16:36:56 -070078 }
79
80 assert(position >= 0);
81 assert(position <= static_cast<ssize_t>(sizeof(buffer)));
82 if (position > 0) {
83 ssize_t sent_bytes = write(to_copy->output, buffer, position);
84 if (sent_bytes == -1) {
85 if (errno != EINTR) {
86 LOG(FATAL, "write(%d, %p, %zd) failed with %d: %s\n",
87 to_copy->output, buffer, position, errno, strerror(errno));
88 }
89 } else if (sent_bytes != 0) {
90 memmove(buffer, buffer + sent_bytes, position - sent_bytes);
91 position -= sent_bytes;
92 }
93 }
94 }
95}
96
97int NetconsoleMain(int argc, char **argv) {
98 logging::Init();
99
Brian Silvermancb9637c2013-04-13 22:19:43 -0700100 int input, output;
Brian Silverman771b4b82013-04-12 16:36:56 -0700101 if (argc > 1) {
Brian Silvermancb9637c2013-04-13 22:19:43 -0700102 output = open(argv[1], O_APPEND | O_CREAT | O_WRONLY | O_TRUNC, 0666);
Brian Silverman771b4b82013-04-12 16:36:56 -0700103 if (output == -1) {
104 if (errno == EACCES || errno == ELOOP || errno == ENOSPC ||
105 errno == ENOTDIR || errno == EROFS || errno == ETXTBSY) {
106 fprintf(stderr, "Opening output file '%s' failed because of %s.\n",
107 argv[1], strerror(errno));
108 exit(EXIT_FAILURE);
109 }
110 LOG(FATAL, "open('%s', stuff, 0644) failed with %d: %s\n", argv[1],
111 errno, strerror(errno));
112 }
113 fprintf(stderr, "Writing output to '%s'.\n", argv[1]);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700114 input = -1;
115 fprintf(stderr, "Not taking any input.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700116 } else {
117 output = STDOUT_FILENO;
118 fprintf(stderr, "Writing output to stdout.\n");
Brian Silvermancb9637c2013-04-13 22:19:43 -0700119 input = STDIN_FILENO;
120 fprintf(stderr, "Reading stdin.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700121 }
122
Brian Silverman771b4b82013-04-12 16:36:56 -0700123 int on = 1;
124
125 int from_crio = socket(AF_INET, SOCK_DGRAM, 0);
126 if (from_crio == -1) {
127 LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n",
128 errno, strerror(errno));
129 }
130 if (setsockopt(from_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
131 LOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed with %d: %s\n",
132 on, from_crio, errno, strerror(errno));
133 }
134 if (setsockopt(from_crio, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
135 LOG(FATAL, "SOL_SOCKET::SO_BROADCAST=%d(%d) failed with %d: %s\n",
136 on, from_crio, errno, strerror(errno));
137 }
Brian Silvermane1514fc2013-04-13 14:57:35 -0700138 if (setsockopt(from_crio, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) == -1) {
139 LOG(FATAL, "IPROTO_IP::IP_PKTINFO=%d(%d) failed with %d: %s\n",
140 on, from_crio, errno, strerror(errno));
141 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700142 union {
143 struct sockaddr_in in;
144 struct sockaddr addr;
145 } address;
146 address.in.sin_family = AF_INET;
147 address.in.sin_port = htons(6666);
148 address.in.sin_addr.s_addr = INADDR_ANY;
149 if (bind(from_crio, &address.addr, sizeof(address)) == -1) {
150 LOG(FATAL, "bind(%d, %p, %zu) failed with %d: %s\n",
151 from_crio, &address.addr, sizeof(address), errno, strerror(errno));
152 }
153
Brian Silvermancb9637c2013-04-13 22:19:43 -0700154 pthread_t input_thread, output_thread;
155
156 if (input != -1) {
157 int to_crio = socket(AF_INET, SOCK_DGRAM, 0);
158 if (to_crio == -1) {
159 LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n",
160 errno, strerror(errno));
161 }
162 if (setsockopt(to_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
163 LOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed with %d: %s\n",
164 on, to_crio, errno, strerror(errno));
165 }
166 address.in.sin_port = htons(6668);
167 if (inet_aton(
168 configuration::GetIPAddress(configuration::NetworkDevice::kCRIO),
169 &address.in.sin_addr) == 0) {
170 LOG(FATAL, "inet_aton(%s, %p) failed with %d: %s\n",
Brian Silverman771b4b82013-04-12 16:36:56 -0700171 configuration::GetIPAddress(configuration::NetworkDevice::kCRIO),
Brian Silvermancb9637c2013-04-13 22:19:43 -0700172 &address.in.sin_addr, errno, strerror(errno));
173 }
174 if (connect(to_crio, &address.addr, sizeof(address)) == -1) {
175 LOG(FATAL, "connect(%d, %p, %zu) failed with %d: %s\n",
176 to_crio, &address.addr, sizeof(address), errno, strerror(errno));
177 }
178 FDsToCopy input_fds{input, to_crio, NULL};
179 if (pthread_create(&input_thread, NULL, FDCopyThread, &input_fds) == -1) {
180 LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n",
181 &input_thread, FDCopyThread, &input_fds, errno, strerror(errno));
182 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700183 }
184
185 fprintf(stderr, "Using cRIO IP %s.\n",
186 configuration::GetIPAddress(configuration::NetworkDevice::kCRIO));
187
Brian Silvermane1514fc2013-04-13 14:57:35 -0700188 if (inet_aton(
189 configuration::GetIPAddress(configuration::NetworkDevice::kSelf),
190 &address.in.sin_addr) == 0) {
191 LOG(FATAL, "inet_aton(%s, %p) failed with %d: %s\n",
192 configuration::GetIPAddress(configuration::NetworkDevice::kSelf),
193 &address.in.sin_addr, errno, strerror(errno));
194 }
195 FDsToCopy output_fds{from_crio, output, &address.in};
Brian Silverman771b4b82013-04-12 16:36:56 -0700196 if (pthread_create(&output_thread, NULL, FDCopyThread, &output_fds) == -1) {
197 LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n",
198 &output_thread, FDCopyThread, &output_fds, errno, strerror(errno));
199 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700200
201 // input_thread will finish when stdin gets an EOF
Brian Silvermancb9637c2013-04-13 22:19:43 -0700202 if (pthread_join((input == -1) ? output_thread : input_thread, NULL) == -1) {
203 LOG(FATAL, "pthread_join(a_thread, NULL) failed with %d: %s\n",
Brian Silverman771b4b82013-04-12 16:36:56 -0700204 errno, strerror(errno));
205 }
206 exit(EXIT_SUCCESS);
207}
208
209} // namespace
210} // namespace aos
211
212int main(int argc, char **argv) {
213 return ::aos::NetconsoleMain(argc, argv);
214}