blob: 4d32afece7979c709ab9cc7dd4c46de0cdcd54db [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 Silvermanf02c3982014-04-21 22:04:41 -070017#include "aos/common/byteorder.h"
Brian Silverman771b4b82013-04-12 16:36:56 -070018
19namespace aos {
20namespace {
21
22struct FDsToCopy {
23 const int input;
24 const int output;
Brian Silvermane1514fc2013-04-13 14:57:35 -070025
26 const struct sockaddr_in *const interface_address;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070027 const struct sockaddr_in *const source_address;
Brian Silverman771b4b82013-04-12 16:36:56 -070028};
29
30void *FDCopyThread(void *to_copy_in) {
31 FDsToCopy *to_copy = static_cast<FDsToCopy *>(to_copy_in);
32
33 char buffer[32768];
34 ssize_t position = 0;
35 while (true) {
36 assert(position >= 0);
37 assert(position <= static_cast<ssize_t>(sizeof(buffer)));
38 if (position != sizeof(buffer)) {
Brian Silvermane1514fc2013-04-13 14:57:35 -070039 ssize_t read_bytes;
40 bool good_data = true;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070041 if (to_copy->interface_address != nullptr ||
42 to_copy->source_address != nullptr) {
Brian Silvermane1514fc2013-04-13 14:57:35 -070043 char control_buffer[0x100];
44 struct msghdr header;
45 memset(static_cast<void *>(&header), 0, sizeof(header));
46 header.msg_control = control_buffer;
47 header.msg_controllen = sizeof(control_buffer);
48 struct iovec iovecs[1];
49 iovecs[0].iov_base = buffer + position;
Brian Silvermanfe1ef172014-04-12 17:12:45 -070050 iovecs[0].iov_len = sizeof(buffer) - position;
Brian Silvermane1514fc2013-04-13 14:57:35 -070051 header.msg_iov = iovecs;
52 header.msg_iovlen = sizeof(iovecs) / sizeof(iovecs[0]);
Brian Silvermanfe1ef172014-04-12 17:12:45 -070053 struct sockaddr_in sender_address;
54 header.msg_name = &sender_address;
55 header.msg_namelen = sizeof(sender_address);
56
Brian Silvermane1514fc2013-04-13 14:57:35 -070057 read_bytes = recvmsg(to_copy->input, &header, 0);
58 if (read_bytes != -1) {
Brian Silvermanfe1ef172014-04-12 17:12:45 -070059 if (to_copy->interface_address != nullptr) {
60 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&header);
61 cmsg != NULL;
62 cmsg = CMSG_NXTHDR(&header, cmsg)) {
63 if (cmsg->cmsg_level == IPPROTO_IP &&
64 cmsg->cmsg_type == IP_PKTINFO) {
65 unsigned char *data = CMSG_DATA(cmsg);
66 struct in_pktinfo *pktinfo;
67 memcpy(&pktinfo, &data, sizeof(void *));
68 if (pktinfo->ipi_spec_dst.s_addr !=
69 to_copy->interface_address->sin_addr.s_addr) {
70 good_data = false;
71 }
72 }
73 }
74 }
75 if (to_copy->source_address != nullptr) {
76 assert(header.msg_namelen >= sizeof(struct sockaddr_in));
Brian Silvermanf02c3982014-04-21 22:04:41 -070077 if (to_copy->source_address->sin_port != hton<uint16_t>(0)) {
Brian Silvermanfe1ef172014-04-12 17:12:45 -070078 if (sender_address.sin_port !=
79 to_copy->source_address->sin_port) {
80 good_data = false;
81 }
82 }
83 if (sender_address.sin_addr.s_addr !=
84 to_copy->source_address->sin_addr.s_addr) {
85 good_data = false;
Brian Silvermane1514fc2013-04-13 14:57:35 -070086 }
87 }
88 }
89 } else {
90 read_bytes = read(to_copy->input,
Brian Silvermanfe1ef172014-04-12 17:12:45 -070091 buffer + position, sizeof(buffer) - position);
Brian Silvermane1514fc2013-04-13 14:57:35 -070092 }
Brian Silverman771b4b82013-04-12 16:36:56 -070093 if (read_bytes == -1) {
94 if (errno != EINTR) {
Brian Silverman01be0002014-05-10 15:44:38 -070095 PLOG(FATAL, "read(%d, %p, %zd) failed",
96 to_copy->input, buffer + position, position - sizeof(buffer));
Brian Silverman771b4b82013-04-12 16:36:56 -070097 }
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) {
Brian Silverman01be0002014-05-10 15:44:38 -0700113 PLOG(FATAL, "write(%d, %p, %zd) failed",
114 to_copy->output, buffer, position);
Brian Silverman771b4b82013-04-12 16:36:56 -0700115 }
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();
Brian Silverman01be0002014-05-10 15:44:38 -0700131 logging::AddImplementation(new logging::StreamLogImplementation(stdout));
Brian Silverman771b4b82013-04-12 16:36:56 -0700132
Brian Silvermancb9637c2013-04-13 22:19:43 -0700133 int input, output;
Brian Silverman771b4b82013-04-12 16:36:56 -0700134 if (argc > 1) {
Brian Silvermancb9637c2013-04-13 22:19:43 -0700135 output = open(argv[1], O_APPEND | O_CREAT | O_WRONLY | O_TRUNC, 0666);
Brian Silverman771b4b82013-04-12 16:36:56 -0700136 if (output == -1) {
137 if (errno == EACCES || errno == ELOOP || errno == ENOSPC ||
138 errno == ENOTDIR || errno == EROFS || errno == ETXTBSY) {
Brian Silverman01be0002014-05-10 15:44:38 -0700139 PLOG(FATAL, "opening output file '%s' failed", argv[1]);
Brian Silverman771b4b82013-04-12 16:36:56 -0700140 }
Brian Silverman01be0002014-05-10 15:44:38 -0700141 PLOG(FATAL, "open('%s', stuff, 0644) failed", argv[1]);
Brian Silverman771b4b82013-04-12 16:36:56 -0700142 }
143 fprintf(stderr, "Writing output to '%s'.\n", argv[1]);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700144 input = -1;
145 fprintf(stderr, "Not taking any input.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700146 } else {
147 output = STDOUT_FILENO;
148 fprintf(stderr, "Writing output to stdout.\n");
Brian Silvermancb9637c2013-04-13 22:19:43 -0700149 input = STDIN_FILENO;
150 fprintf(stderr, "Reading stdin.\n");
Brian Silverman771b4b82013-04-12 16:36:56 -0700151 }
152
Brian Silverman771b4b82013-04-12 16:36:56 -0700153 int on = 1;
154
155 int from_crio = socket(AF_INET, SOCK_DGRAM, 0);
156 if (from_crio == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700157 PLOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed");
Brian Silverman771b4b82013-04-12 16:36:56 -0700158 }
159 if (setsockopt(from_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700160 PLOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed", on, from_crio);
Brian Silverman771b4b82013-04-12 16:36:56 -0700161 }
162 if (setsockopt(from_crio, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700163 PLOG(FATAL, "SOL_SOCKET::SO_BROADCAST=%d(%d) failed", on, from_crio);
Brian Silverman771b4b82013-04-12 16:36:56 -0700164 }
Brian Silvermane1514fc2013-04-13 14:57:35 -0700165 if (setsockopt(from_crio, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700166 PLOG(FATAL, "IPROTO_IP::IP_PKTINFO=%d(%d) failed", on, from_crio);
Brian Silvermane1514fc2013-04-13 14:57:35 -0700167 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700168 union {
169 struct sockaddr_in in;
170 struct sockaddr addr;
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700171 } address, crio_address;
172
Brian Silverman771b4b82013-04-12 16:36:56 -0700173 address.in.sin_family = AF_INET;
Brian Silvermanf02c3982014-04-21 22:04:41 -0700174 address.in.sin_port = hton<uint16_t>(6666);
Brian Silverman771b4b82013-04-12 16:36:56 -0700175 address.in.sin_addr.s_addr = INADDR_ANY;
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700176
177 crio_address.in.sin_family = AF_INET;
Brian Silvermanf02c3982014-04-21 22:04:41 -0700178 crio_address.in.sin_port = hton<uint16_t>(0);
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700179 crio_address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
180 ::aos::util::SetLastSegment(&crio_address.in.sin_addr,
181 ::aos::NetworkAddress::kCRIO);
182
Brian Silverman771b4b82013-04-12 16:36:56 -0700183 if (bind(from_crio, &address.addr, sizeof(address)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700184 PLOG(FATAL, "bind(%d, %p, %zu) failed",
185 from_crio, &address.addr, sizeof(address));
Brian Silverman771b4b82013-04-12 16:36:56 -0700186 }
187
Brian Silvermancb9637c2013-04-13 22:19:43 -0700188 pthread_t input_thread, output_thread;
189
Brian Silverman66f079a2013-08-26 16:24:30 -0700190 address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
191 ::aos::util::SetLastSegment(&address.in.sin_addr, NetworkAddress::kCRIO);
192 fprintf(stderr, "Using cRIO IP %s.\n",
193 inet_ntoa(address.in.sin_addr));
194
Brian Silvermancb9637c2013-04-13 22:19:43 -0700195 if (input != -1) {
196 int to_crio = socket(AF_INET, SOCK_DGRAM, 0);
197 if (to_crio == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700198 PLOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed");
Brian Silvermancb9637c2013-04-13 22:19:43 -0700199 }
200 if (setsockopt(to_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700201 PLOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed", on, to_crio);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700202 }
Brian Silvermanf02c3982014-04-21 22:04:41 -0700203 address.in.sin_port = hton<uint16_t>(6668);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700204 if (connect(to_crio, &address.addr, sizeof(address)) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700205 PLOG(FATAL, "connect(%d, %p, %zu) failed",
206 to_crio, &address.addr, sizeof(address));
Brian Silvermancb9637c2013-04-13 22:19:43 -0700207 }
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700208 FDsToCopy input_fds{input, to_crio, nullptr, nullptr};
Brian Silvermancb9637c2013-04-13 22:19:43 -0700209 if (pthread_create(&input_thread, NULL, FDCopyThread, &input_fds) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700210 PLOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed",
211 &input_thread, FDCopyThread, &input_fds);
Brian Silvermancb9637c2013-04-13 22:19:43 -0700212 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700213 }
214
Brian Silverman66f079a2013-08-26 16:24:30 -0700215 address.in.sin_addr = ::aos::configuration::GetOwnIPAddress();
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700216 FDsToCopy output_fds{from_crio, output, &address.in, &crio_address.in};
Brian Silverman771b4b82013-04-12 16:36:56 -0700217 if (pthread_create(&output_thread, NULL, FDCopyThread, &output_fds) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700218 PLOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed",
219 &output_thread, FDCopyThread, &output_fds);
Brian Silverman771b4b82013-04-12 16:36:56 -0700220 }
Brian Silverman771b4b82013-04-12 16:36:56 -0700221
222 // input_thread will finish when stdin gets an EOF
Brian Silvermancb9637c2013-04-13 22:19:43 -0700223 if (pthread_join((input == -1) ? output_thread : input_thread, NULL) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700224 PLOG(FATAL, "pthread_join(a_thread, NULL) failed");
Brian Silverman771b4b82013-04-12 16:36:56 -0700225 }
226 exit(EXIT_SUCCESS);
227}
228
229} // namespace
230} // namespace aos
231
232int main(int argc, char **argv) {
233 return ::aos::NetconsoleMain(argc, argv);
234}