Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 1 | #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 | |
| 15 | namespace aos { |
| 16 | namespace { |
| 17 | |
| 18 | struct FDsToCopy { |
| 19 | const int input; |
| 20 | const int output; |
Brian Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 21 | |
| 22 | const struct sockaddr_in *const interface_address; |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | void *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 Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 34 | 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 Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 65 | 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 Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 71 | } else if (read_bytes == 0 && to_copy->interface_address == NULL) { |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 72 | // read(2) says that this means EOF |
| 73 | return NULL; |
| 74 | } |
Brian Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 75 | if (good_data) { |
| 76 | position += read_bytes; |
| 77 | } |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 78 | } |
| 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 | |
| 97 | int NetconsoleMain(int argc, char **argv) { |
| 98 | logging::Init(); |
| 99 | |
| 100 | int output; |
| 101 | if (argc > 1) { |
| 102 | output = open(argv[1], O_APPEND | O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 103 | 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]); |
| 114 | } else { |
| 115 | output = STDOUT_FILENO; |
| 116 | fprintf(stderr, "Writing output to stdout.\n"); |
| 117 | } |
| 118 | |
| 119 | const int input = STDIN_FILENO; |
| 120 | |
| 121 | int on = 1; |
| 122 | |
| 123 | int from_crio = socket(AF_INET, SOCK_DGRAM, 0); |
| 124 | if (from_crio == -1) { |
| 125 | LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n", |
| 126 | errno, strerror(errno)); |
| 127 | } |
| 128 | if (setsockopt(from_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { |
| 129 | LOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed with %d: %s\n", |
| 130 | on, from_crio, errno, strerror(errno)); |
| 131 | } |
| 132 | if (setsockopt(from_crio, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) { |
| 133 | LOG(FATAL, "SOL_SOCKET::SO_BROADCAST=%d(%d) failed with %d: %s\n", |
| 134 | on, from_crio, errno, strerror(errno)); |
| 135 | } |
Brian Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 136 | if (setsockopt(from_crio, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) == -1) { |
| 137 | LOG(FATAL, "IPROTO_IP::IP_PKTINFO=%d(%d) failed with %d: %s\n", |
| 138 | on, from_crio, errno, strerror(errno)); |
| 139 | } |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 140 | union { |
| 141 | struct sockaddr_in in; |
| 142 | struct sockaddr addr; |
| 143 | } address; |
| 144 | address.in.sin_family = AF_INET; |
| 145 | address.in.sin_port = htons(6666); |
| 146 | address.in.sin_addr.s_addr = INADDR_ANY; |
| 147 | if (bind(from_crio, &address.addr, sizeof(address)) == -1) { |
| 148 | LOG(FATAL, "bind(%d, %p, %zu) failed with %d: %s\n", |
| 149 | from_crio, &address.addr, sizeof(address), errno, strerror(errno)); |
| 150 | } |
| 151 | |
| 152 | int to_crio = socket(AF_INET, SOCK_DGRAM, 0); |
| 153 | if (to_crio == -1) { |
| 154 | LOG(FATAL, "socket(AF_INET, SOCK_DGRAM, 0) failed with %d: %s\n", |
| 155 | errno, strerror(errno)); |
| 156 | } |
| 157 | if (setsockopt(to_crio, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { |
| 158 | LOG(FATAL, "SOL_SOCKET::SO_REUSEADDR=%d(%d) failed with %d: %s\n", |
| 159 | on, to_crio, errno, strerror(errno)); |
| 160 | } |
| 161 | address.in.sin_port = htons(6668); |
| 162 | if (inet_aton( |
| 163 | configuration::GetIPAddress(configuration::NetworkDevice::kCRIO), |
| 164 | &address.in.sin_addr) == 0) { |
| 165 | LOG(FATAL, "inet_aton(%s, %p) failed with %d: %s\n", |
| 166 | configuration::GetIPAddress(configuration::NetworkDevice::kCRIO), |
| 167 | &address.in.sin_addr, errno, strerror(errno)); |
| 168 | } |
| 169 | if (connect(to_crio, &address.addr, sizeof(address)) == -1) { |
| 170 | LOG(FATAL, "connect(%d, %p, %zu) failed with %d: %s\n", |
| 171 | to_crio, &address.addr, sizeof(address), errno, strerror(errno)); |
| 172 | } |
| 173 | |
| 174 | fprintf(stderr, "Using cRIO IP %s.\n", |
| 175 | configuration::GetIPAddress(configuration::NetworkDevice::kCRIO)); |
| 176 | |
Brian Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 177 | if (inet_aton( |
| 178 | configuration::GetIPAddress(configuration::NetworkDevice::kSelf), |
| 179 | &address.in.sin_addr) == 0) { |
| 180 | LOG(FATAL, "inet_aton(%s, %p) failed with %d: %s\n", |
| 181 | configuration::GetIPAddress(configuration::NetworkDevice::kSelf), |
| 182 | &address.in.sin_addr, errno, strerror(errno)); |
| 183 | } |
| 184 | FDsToCopy output_fds{from_crio, output, &address.in}; |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 185 | pthread_t output_thread; |
| 186 | if (pthread_create(&output_thread, NULL, FDCopyThread, &output_fds) == -1) { |
| 187 | LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n", |
| 188 | &output_thread, FDCopyThread, &output_fds, errno, strerror(errno)); |
| 189 | } |
Brian Silverman | e1514fc | 2013-04-13 14:57:35 -0700 | [diff] [blame^] | 190 | FDsToCopy input_fds{input, to_crio, NULL}; |
Brian Silverman | 771b4b8 | 2013-04-12 16:36:56 -0700 | [diff] [blame] | 191 | pthread_t input_thread; |
| 192 | if (pthread_create(&input_thread, NULL, FDCopyThread, &input_fds) == -1) { |
| 193 | LOG(FATAL, "pthread_create(%p, NULL, %p, %p) failed with %d: %s\n", |
| 194 | &input_thread, FDCopyThread, &input_fds, errno, strerror(errno)); |
| 195 | } |
| 196 | |
| 197 | // input_thread will finish when stdin gets an EOF |
| 198 | if (pthread_join(input_thread, NULL) == -1) { |
| 199 | LOG(FATAL, "pthread_join(input_thread, NULL) failed with %d: %s\n", |
| 200 | errno, strerror(errno)); |
| 201 | } |
| 202 | exit(EXIT_SUCCESS); |
| 203 | } |
| 204 | |
| 205 | } // namespace |
| 206 | } // namespace aos |
| 207 | |
| 208 | int main(int argc, char **argv) { |
| 209 | return ::aos::NetconsoleMain(argc, argv); |
| 210 | } |