|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, OARC, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is part of dnsjit. |
| 6 | + * |
| 7 | + * dnsjit is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * dnsjit is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with dnsjit. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "config.h" |
| 22 | + |
| 23 | +#include "output/tcpcli.h" |
| 24 | +#include "core/object/dns.h" |
| 25 | +#include "core/object/udp.h" |
| 26 | +#include "core/object/tcp.h" |
| 27 | + |
| 28 | +#include <sys/types.h> |
| 29 | +#include <sys/socket.h> |
| 30 | +#include <netdb.h> |
| 31 | +#include <unistd.h> |
| 32 | +#include <fcntl.h> |
| 33 | +#include <string.h> |
| 34 | +#include <errno.h> |
| 35 | +#include <arpa/inet.h> |
| 36 | + |
| 37 | +static core_log_t _log = LOG_T_INIT("output.tcpcli"); |
| 38 | +static output_tcpcli_t _defaults = { |
| 39 | + LOG_T_INIT_OBJ("output.tcpcli"), |
| 40 | + 0, 0, -1, |
| 41 | +}; |
| 42 | + |
| 43 | +core_log_t* output_tcpcli_log() |
| 44 | +{ |
| 45 | + return &_log; |
| 46 | +} |
| 47 | + |
| 48 | +int output_tcpcli_init(output_tcpcli_t* self, const char* host, const char* port) |
| 49 | +{ |
| 50 | + struct addrinfo* addr; |
| 51 | + int err; |
| 52 | + |
| 53 | + if (!self || !host || !port) { |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + *self = _defaults; |
| 58 | + |
| 59 | + ldebug("init %s %s", host, port); |
| 60 | + |
| 61 | + if ((err = getaddrinfo(host, port, 0, &addr))) { |
| 62 | + lcritical("getaddrinfo() %d", err); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + if (!addr) { |
| 66 | + lcritical("getaddrinfo failed"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + ldebug("getaddrinfo() flags: 0x%x family: 0x%x socktype: 0x%x protocol: 0x%x addrlen: %d", |
| 70 | + addr->ai_flags, |
| 71 | + addr->ai_family, |
| 72 | + addr->ai_socktype, |
| 73 | + addr->ai_protocol, |
| 74 | + addr->ai_addrlen); |
| 75 | + |
| 76 | + if ((self->fd = socket(addr->ai_addr->sa_family, SOCK_STREAM, 0)) < 0) { |
| 77 | + lcritical("socket failed"); |
| 78 | + freeaddrinfo(addr); |
| 79 | + return 1; |
| 80 | + } |
| 81 | + |
| 82 | + if (connect(self->fd, addr->ai_addr, addr->ai_addrlen)) { |
| 83 | + lcritical("connect failed"); |
| 84 | + freeaddrinfo(addr); |
| 85 | + close(self->fd); |
| 86 | + self->fd = -1; |
| 87 | + return 1; |
| 88 | + } |
| 89 | + |
| 90 | + freeaddrinfo(addr); |
| 91 | + |
| 92 | + if ((err = fcntl(self->fd, F_GETFL)) == -1 |
| 93 | + || fcntl(self->fd, F_SETFL, err | O_NONBLOCK)) { |
| 94 | + lcritical("fcntl failed"); |
| 95 | + } |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +int output_tcpcli_destroy(output_tcpcli_t* self) |
| 101 | +{ |
| 102 | + if (!self) { |
| 103 | + return 1; |
| 104 | + } |
| 105 | + |
| 106 | + ldebug("destroy"); |
| 107 | + |
| 108 | + if (self->fd > -1) { |
| 109 | + shutdown(self->fd, SHUT_RDWR); |
| 110 | + close(self->fd); |
| 111 | + } |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +static int _receive(void* ctx, const core_object_t* obj) |
| 117 | +{ |
| 118 | + output_tcpcli_t* self = (output_tcpcli_t*)ctx; |
| 119 | + const uint8_t* payload; |
| 120 | + size_t len, sent; |
| 121 | + uint16_t dnslen; |
| 122 | + |
| 123 | + if (!self) { |
| 124 | + return 1; |
| 125 | + } |
| 126 | + |
| 127 | + for (; obj;) { |
| 128 | + switch (obj->obj_type) { |
| 129 | + case CORE_OBJECT_DNS: |
| 130 | + obj = obj->obj_prev; |
| 131 | + continue; |
| 132 | + case CORE_OBJECT_UDP: |
| 133 | + payload = ((core_object_udp_t*)obj)->payload; |
| 134 | + len = ((core_object_udp_t*)obj)->len; |
| 135 | + break; |
| 136 | + case CORE_OBJECT_TCP: |
| 137 | + payload = ((core_object_tcp_t*)obj)->payload; |
| 138 | + len = ((core_object_tcp_t*)obj)->len; |
| 139 | + break; |
| 140 | + default: |
| 141 | + return 1; |
| 142 | + } |
| 143 | + |
| 144 | + if (len < 3 || payload[2] & 0x80) { |
| 145 | + return 0; |
| 146 | + } |
| 147 | + |
| 148 | + sent = 0; |
| 149 | + self->pkts++; |
| 150 | + |
| 151 | + dnslen = htons(len); |
| 152 | + |
| 153 | + for (;;) { |
| 154 | + ssize_t ret = sendto(self->fd, ((uint8_t*)&dnslen) + sent, sizeof(dnslen) - sent, 0, 0, 0); |
| 155 | + if (ret > -1) { |
| 156 | + sent += ret; |
| 157 | + if (sent < sizeof(dnslen)) |
| 158 | + continue; |
| 159 | + |
| 160 | + sent = 0; |
| 161 | + for (;;) { |
| 162 | + ssize_t ret = sendto(self->fd, payload + sent, len - sent, 0, 0, 0); |
| 163 | + if (ret > -1) { |
| 164 | + sent += ret; |
| 165 | + if (sent < len) |
| 166 | + continue; |
| 167 | + return 0; |
| 168 | + } |
| 169 | + switch (errno) { |
| 170 | + case EAGAIN: |
| 171 | +#if EAGAIN != EWOULDBLOCK |
| 172 | + case EWOULDBLOCK: |
| 173 | +#endif |
| 174 | + continue; |
| 175 | + default: |
| 176 | + break; |
| 177 | + } |
| 178 | + self->errs++; |
| 179 | + break; |
| 180 | + } |
| 181 | + break; |
| 182 | + } |
| 183 | + switch (errno) { |
| 184 | + case EAGAIN: |
| 185 | +#if EAGAIN != EWOULDBLOCK |
| 186 | + case EWOULDBLOCK: |
| 187 | +#endif |
| 188 | + continue; |
| 189 | + default: |
| 190 | + break; |
| 191 | + } |
| 192 | + self->errs++; |
| 193 | + break; |
| 194 | + } |
| 195 | + break; |
| 196 | + } |
| 197 | + |
| 198 | + return 1; |
| 199 | +} |
| 200 | + |
| 201 | +core_receiver_t output_tcpcli_receiver() |
| 202 | +{ |
| 203 | + return _receive; |
| 204 | +} |
0 commit comments