blob: fa4cd522c10f97173a7aab7913a0c82001954e49 [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"
Brianc1dc7d22014-04-02 12:21:08 -070013#include "aos/common/util/inet_addr.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080014#include "aos/linux_code/configuration.h"
Brian Silvermanfe1ef172014-04-12 17:12:45 -070015#include "aos/common/network_port.h"
16#include "aos/linux_code/init.h"
Brian Silverman771b4b82013-04-12 16:36:56 -070017
18namespace aos {
19namespace {
20
21struct FDsToCopy {
22 const int input;
23 const int output;
Brian Silvermane1514fc2013-04-13 14:57:35 -070024
25 const struct sockaddr_in *const interface_address;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070026 const struct sockaddr_in *const source_address;
Brian Silverman771b4b82013-04-12 16:36:56 -070027};
28
29void *FDCopyThread(void *to_copy_in) {
30 FDsToCopy *to_copy = static_cast<FDsToCopy *>(to_copy_in);
31
32 char buffer[32768];
33 ssize_t position = 0;
34 while (true) {
35 assert(position >= 0);
36 assert(position <= static_cast<ssize_t>(sizeof(buffer)));
37 if (position != sizeof(buffer)) {
Brian Silvermane1514fc2013-04-13 14:57:35 -070038 ssize_t read_bytes;
39 bool good_data = true;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070040 if (to_copy->interface_address != nullptr ||
41 to_copy->source_address != nullptr) {
Brian Silvermane1514fc2013-04-13 14:57:35 -070042 char control_buffer[0x100];
43 struct msghdr header;
44 memset(static_cast<void *>(&header), 0, sizeof(header));
45 header.msg_control = control_buffer;
46 header.msg_controllen = sizeof(control_buffer);
47 struct iovec iovecs[1];
48 iovecs[0].iov_base = buffer + position;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070049 iovecs[0].iov_len = sizeof(buffer) - position;
Brian Silvermane1514fc2013-04-13 14:57:35 -070050 header.msg_iov = iovecs;
51 header.msg_iovlen = sizeof(iovecs) / sizeof(iovecs[0]);
Brian Silvermanfe1ef172014-04-12 17:12:45 -070052 struct sockaddr_in sender_address;
53 header.msg_name = &sender_address;
54 header.msg_namelen = sizeof(sender_address);
55
Brian Silvermane1514fc2013-04-13 14:57:35 -070056 read_bytes = recvmsg(to_copy->input, &header, 0);
57 if (read_bytes != -1) {
Brian Silvermanfe1ef172014-04-12 17:12:45 -070058 if (to_copy->interface_address != nullptr) {
59 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&header);
60 cmsg != NULL;
61 cmsg = CMSG_NXTHDR(&header, cmsg)) {
62 if (cmsg->cmsg_level == IPPROTO_IP &&
63 cmsg->cmsg_type == IP_PKTINFO) {
64 unsigned char *data = CMSG_DATA(cmsg);
65 struct in_pktinfo *pktinfo;
66 memcpy(&pktinfo, &data, sizeof(void *));
67 if (pktinfo->ipi_spec_dst.s_addr !=
68 to_copy->interface_address->sin_addr.s_addr) {
69 good_data = false;
70 }
71 }
72 }
73 }
74 if (to_copy->source_address != nullptr) {
75 assert(header.msg_namelen >= sizeof(struct sockaddr_in));
76 if (to_copy->source_address->sin_port != htons(0)) {
77 if (sender_address.sin_port !=
78 to_copy->source_address->sin_port) {
79 good_data = false;
80 }
81 }
82 if (sender_address.sin_addr.s_addr !=
83 to_copy->source_address->sin_addr.s_addr) {
84 good_data = false;
Brian Silvermane1514fc2013-04-13 14:57:35 -070085 }
86 }
87 }
88 } else {
89 read_bytes = read(to_copy->input,
Brian Silvermanfe1ef172014-04-12 17:12:45 -070090 buffer + position, sizeof(buffer) - position);
Brian Silvermane1514fc2013-04-13 14:57:35 -070091 }
Brian Silverman771b4b82013-04-12 16:36:56 -070092 if (read_bytes == -1) {
93 if (errno != EINTR) {
94 LOG(FATAL, "read(%d, %p, %zd) failed with %d: %s\n",
95 to_copy->input, buffer + position, position - sizeof(buffer),
96 errno, strerror(errno));
97 }
Brian Silvermane1514fc2013-04-13 14:57:35 -070098 } else if (read_bytes == 0 && to_copy->interface_address == NULL) {
Brian Silverman771b4b82013-04-12 16:36:56 -070099 // read(2) says that this means EOF
100 return NULL;
101 }
Brian Silvermane1514fc2013-04-13 14:57:35 -0700102 if (good_data) {
103 position += read_bytes;
104 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700105 }
106
107 assert(position >= 0);
108 assert(position <= static_cast<ssize_t>(sizeof(buffer)));
109 if (position > 0) {
110 ssize_t sent_bytes = write(to_copy->output, buffer, position);
111 if (sent_bytes == -1) {
112 if (errno != EINTR) {
113 LOG(FATAL, "write(%d, %p, %zd) failed with %d: %s\n",
114 to_copy->output, buffer, position, errno, strerror(errno));
115 }
116 } else if (sent_bytes != 0) {
Brian Silverman1c8670d2013-04-13 18:59:13 -0700117 if (sent_bytes == position) {
118 position = 0;
119 } else {
120 memmove(buffer, buffer + sent_bytes, position - sent_bytes);
121 position -= sent_bytes;
122 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700123 }
124 }
125 }
126}
127
128int NetconsoleMain(int argc, char **argv) {
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700129 WriteCoreDumps();
Brian Silverman771b4b82013-04-12 16:36:56 -0700130 logging::Init();
131
Brian Silvermancb9637c2013-04-13 22:19:43 -0700132 int input, output;
Brian Silverman771b4b82013-04-12 16:36:56 -0700133 if (argc > 1) {
Brian Silvermancb9637c2013-04-13 22:19:43 -0700134 output = open(argv[1], O_APPEND | O_CREAT | O_WRONLY | O_TRUNC, 0666);
Brian Silverman771b4b82013-04-12 16:36:56 -0700135 if (output == -1) {
136 if (errno == EACCES || errno == ELOOP || errno == ENOSPC ||
137 errno == ENOTDIR || errno == EROFS || errno == ETXTBSY) {
138 fprintf(stderr, "Opening output file '%s' failed because of %s.\n",
139 argv[1], strerror(errno));
140 exit(EXIT_FAILURE);
141 }
142 LOG(FATAL, "open('%s', stuff, 0644) failed with %d: %s\n", argv[1],
143 errno, strerror(errno));
144 }
145 fprintf(stderr, "Writing output to '%s'.\n", argv[1]);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700146 input = -1;
147 fprintf(stderr, "Not taking any input.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700148 } else {
149 output = STDOUT_FILENO;
150 fprintf(stderr, "Writing output to stdout.\n");
Brian Silvermancb9637c2013-04-13 22:19:43 -0700151 input = STDIN_FILENO;
152 fprintf(stderr, "Reading stdin.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700153 }
154
Brian Silverman771b4b82013-04-12 16:36:56 -0700155 int on = 1;
156
157 int from_crio = socket(AF_INET, SOCK_DGRAM, 0);
158 if (from_crio == -1) {
159 LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n",
160 errno, strerror(errno));
161 }
162 if (setsockopt(from_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, from_crio, errno, strerror(errno));
165 }
166 if (setsockopt(from_crio, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
167 LOG(FATAL, "SOL_SOCKET::SO_BROADCAST=%d(%d) failed with %d: %s\n",
168 on, from_crio, errno, strerror(errno));
169 }
Brian Silvermane1514fc2013-04-13 14:57:35 -0700170 if (setsockopt(from_crio, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) == -1) {
171 LOG(FATAL, "IPROTO_IP::IP_PKTINFO=%d(%d) failed with %d: %s\n",
172 on, from_crio, errno, strerror(errno));
173 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700174 union {
175 struct sockaddr_in in;
176 struct sockaddr addr;
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700177 } address, crio_address;
178
Brian Silverman771b4b82013-04-12 16:36:56 -0700179 address.in.sin_family = AF_INET;
180 address.in.sin_port = htons(6666);
181 address.in.sin_addr.s_addr = INADDR_ANY;
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700182
183 crio_address.in.sin_family = AF_INET;
184 crio_address.in.sin_port = htons(0);
185 crio_address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
186 ::aos::util::SetLastSegment(&crio_address.in.sin_addr,
187 ::aos::NetworkAddress::kCRIO);
188
Brian Silverman771b4b82013-04-12 16:36:56 -0700189 if (bind(from_crio, &address.addr, sizeof(address)) == -1) {
190 LOG(FATAL, "bind(%d, %p, %zu) failed with %d: %s\n",
191 from_crio, &address.addr, sizeof(address), errno, strerror(errno));
192 }
193
Brian Silvermancb9637c2013-04-13 22:19:43 -0700194 pthread_t input_thread, output_thread;
195
Brian Silverman66f079a2013-08-26 16:24:30 -0700196 address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
197 ::aos::util::SetLastSegment(&address.in.sin_addr, NetworkAddress::kCRIO);
198 fprintf(stderr, "Using cRIO IP %s.\n",
199 inet_ntoa(address.in.sin_addr));
200
Brian Silvermancb9637c2013-04-13 22:19:43 -0700201 if (input != -1) {
202 int to_crio = socket(AF_INET, SOCK_DGRAM, 0);
203 if (to_crio == -1) {
204 LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n",
205 errno, strerror(errno));
206 }
207 if (setsockopt(to_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
208 LOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed with %d: %s\n",
209 on, to_crio, errno, strerror(errno));
210 }
211 address.in.sin_port = htons(6668);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700212 if (connect(to_crio, &address.addr, sizeof(address)) == -1) {
213 LOG(FATAL, "connect(%d, %p, %zu) failed with %d: %s\n",
214 to_crio, &address.addr, sizeof(address), errno, strerror(errno));
215 }
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700216 FDsToCopy input_fds{input, to_crio, nullptr, nullptr};
Brian Silvermancb9637c2013-04-13 22:19:43 -0700217 if (pthread_create(&input_thread, NULL, FDCopyThread, &input_fds) == -1) {
218 LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n",
219 &input_thread, FDCopyThread, &input_fds, errno, strerror(errno));
220 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700221 }
222
Brian Silverman66f079a2013-08-26 16:24:30 -0700223 address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700224 FDsToCopy output_fds{from_crio, output, &address.in, &crio_address.in};
Brian Silverman771b4b82013-04-12 16:36:56 -0700225 if (pthread_create(&output_thread, NULL, FDCopyThread, &output_fds) == -1) {
226 LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n",
227 &output_thread, FDCopyThread, &output_fds, errno, strerror(errno));
228 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700229
230 // input_thread will finish when stdin gets an EOF
Brian Silvermancb9637c2013-04-13 22:19:43 -0700231 if (pthread_join((input == -1) ? output_thread : input_thread, NULL) == -1) {
232 LOG(FATAL, "pthread_join(a_thread, NULL) failed with %d: %s\n",
Brian Silverman771b4b82013-04-12 16:36:56 -0700233 errno, strerror(errno));
234 }
235 exit(EXIT_SUCCESS);
236}
237
238} // namespace
239} // namespace aos
240
241int main(int argc, char **argv) {
242 return ::aos::NetconsoleMain(argc, argv);
243}