socks.cpp 7.72 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31 32 33 34 35 36
#include <sys/types.h>

#include "err.hpp"
#include "socks.hpp"
#include "tcp.hpp"

Richard Newton's avatar
Richard Newton committed
37 38
#ifndef ZMQ_HAVE_WINDOWS
#include <sys/socket.h>
39
#include <netinet/in.h>
Richard Newton's avatar
Richard Newton committed
40 41 42
#include <netdb.h>
#endif

43
zmq::socks_greeting_t::socks_greeting_t (uint8_t method_) : num_methods (1)
44
{
45
    methods[0] = method_;
46 47
}

48 49 50
zmq::socks_greeting_t::socks_greeting_t (uint8_t *methods_,
                                         uint8_t num_methods_) :
    num_methods (num_methods_)
51
{
52
    for (uint8_t i = 0; i < num_methods_; i++)
53
        methods[i] = methods_[i];
54 55
}

56 57 58 59 60
zmq::socks_greeting_encoder_t::socks_greeting_encoder_t () :
    bytes_encoded (0),
    bytes_written (0)
{
}
61 62 63 64 65 66

void zmq::socks_greeting_encoder_t::encode (const socks_greeting_t &greeting_)
{
    uint8_t *ptr = buf;

    *ptr++ = 0x05;
67
    *ptr++ = static_cast<uint8_t> (greeting_.num_methods);
68
    for (uint8_t i = 0; i < greeting_.num_methods; i++)
69
        *ptr++ = greeting_.methods[i];
70 71 72 73 74 75 76

    bytes_encoded = 2 + greeting_.num_methods;
    bytes_written = 0;
}

int zmq::socks_greeting_encoder_t::output (fd_t fd_)
{
77 78
    const int rc =
      tcp_write (fd_, buf + bytes_written, bytes_encoded - bytes_written);
79
    if (rc > 0)
80
        bytes_written += static_cast<size_t> (rc);
81 82 83 84 85 86 87 88 89 90 91 92 93
    return rc;
}

bool zmq::socks_greeting_encoder_t::has_pending_data () const
{
    return bytes_written < bytes_encoded;
}

void zmq::socks_greeting_encoder_t::reset ()
{
    bytes_encoded = bytes_written = 0;
}

94 95 96
zmq::socks_choice_t::socks_choice_t (unsigned char method_) : method (method_)
{
}
97

98 99 100
zmq::socks_choice_decoder_t::socks_choice_decoder_t () : bytes_read (0)
{
}
101 102 103 104 105 106

int zmq::socks_choice_decoder_t::input (fd_t fd_)
{
    zmq_assert (bytes_read < 2);
    const int rc = tcp_read (fd_, buf + bytes_read, 2 - bytes_read);
    if (rc > 0) {
107 108
        bytes_read += static_cast<size_t> (rc);
        if (buf[0] != 0x05)
109 110 111 112 113 114 115 116 117 118 119 120 121
            return -1;
    }
    return rc;
}

bool zmq::socks_choice_decoder_t::message_ready () const
{
    return bytes_read == 2;
}

zmq::socks_choice_t zmq::socks_choice_decoder_t::decode ()
{
    zmq_assert (message_ready ());
122
    return socks_choice_t (buf[1]);
123 124 125 126 127 128 129
}

void zmq::socks_choice_decoder_t::reset ()
{
    bytes_read = 0;
}

130

131 132 133 134 135 136
zmq::socks_request_t::socks_request_t (uint8_t command_,
                                       std::string hostname_,
                                       uint16_t port_) :
    command (command_),
    hostname (hostname_),
    port (port_)
137 138 139
{
    zmq_assert (hostname_.size () <= UINT8_MAX);
}
140

141 142 143 144 145
zmq::socks_request_encoder_t::socks_request_encoder_t () :
    bytes_encoded (0),
    bytes_written (0)
{
}
146 147 148

void zmq::socks_request_encoder_t::encode (const socks_request_t &req)
{
149
    zmq_assert (req.hostname.size () <= UINT8_MAX);
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    unsigned char *ptr = buf;
    *ptr++ = 0x05;
    *ptr++ = req.command;
    *ptr++ = 0x00;

#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
    __addrinfo64 hints, *res = NULL;
#else
    addrinfo hints, *res = NULL;
#endif

    memset (&hints, 0, sizeof hints);

    //  Suppress potential DNS lookups.
    hints.ai_flags = AI_NUMERICHOST;

    const int rc = getaddrinfo (req.hostname.c_str (), NULL, &hints, &res);
    if (rc == 0 && res->ai_family == AF_INET) {
169 170
        const struct sockaddr_in *sockaddr_in =
          reinterpret_cast<const struct sockaddr_in *> (res->ai_addr);
171 172 173
        *ptr++ = 0x01;
        memcpy (ptr, &sockaddr_in->sin_addr, 4);
        ptr += 4;
174
    } else if (rc == 0 && res->ai_family == AF_INET6) {
175 176
        const struct sockaddr_in6 *sockaddr_in6 =
          reinterpret_cast<const struct sockaddr_in6 *> (res->ai_addr);
177 178 179
        *ptr++ = 0x04;
        memcpy (ptr, &sockaddr_in6->sin6_addr, 16);
        ptr += 16;
180
    } else {
181
        *ptr++ = 0x03;
182
        *ptr++ = static_cast<unsigned char> (req.hostname.size ());
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        memcpy (ptr, req.hostname.c_str (), req.hostname.size ());
        ptr += req.hostname.size ();
    }

    if (rc == 0)
        freeaddrinfo (res);

    *ptr++ = req.port / 256;
    *ptr++ = req.port % 256;

    bytes_encoded = ptr - buf;
    bytes_written = 0;
}

int zmq::socks_request_encoder_t::output (fd_t fd_)
{
199 200
    const int rc =
      tcp_write (fd_, buf + bytes_written, bytes_encoded - bytes_written);
201
    if (rc > 0)
202
        bytes_written += static_cast<size_t> (rc);
203 204 205 206 207 208 209 210 211 212 213 214 215
    return rc;
}

bool zmq::socks_request_encoder_t::has_pending_data () const
{
    return bytes_written < bytes_encoded;
}

void zmq::socks_request_encoder_t::reset ()
{
    bytes_encoded = bytes_written = 0;
}

216 217 218 219 220 221 222 223
zmq::socks_response_t::socks_response_t (uint8_t response_code_,
                                         std::string address_,
                                         uint16_t port_) :
    response_code (response_code_),
    address (address_),
    port (port_)
{
}
224

225 226 227
zmq::socks_response_decoder_t::socks_response_decoder_t () : bytes_read (0)
{
}
228 229 230 231 232 233 234 235

int zmq::socks_response_decoder_t::input (fd_t fd_)
{
    size_t n = 0;

    if (bytes_read < 5)
        n = 5 - bytes_read;
    else {
236
        const uint8_t atyp = buf[3];
237 238 239
        zmq_assert (atyp == 0x01 || atyp == 0x03 || atyp == 0x04);
        if (atyp == 0x01)
            n = 3 + 2;
240 241 242
        else if (atyp == 0x03)
            n = buf[4] + 2;
        else if (atyp == 0x04)
243 244 245 246
            n = 15 + 2;
    }
    const int rc = tcp_read (fd_, buf + bytes_read, n);
    if (rc > 0) {
247 248
        bytes_read += static_cast<size_t> (rc);
        if (buf[0] != 0x05)
249 250
            return -1;
        if (bytes_read >= 2)
251
            if (buf[1] > 0x08)
252 253
                return -1;
        if (bytes_read >= 3)
254
            if (buf[2] != 0x00)
255 256
                return -1;
        if (bytes_read >= 4) {
257
            const uint8_t atyp = buf[3];
258 259 260 261 262 263 264 265 266 267 268 269
            if (atyp != 0x01 && atyp != 0x03 && atyp != 0x04)
                return -1;
        }
    }
    return rc;
}

bool zmq::socks_response_decoder_t::message_ready () const
{
    if (bytes_read < 4)
        return false;
    else {
270
        const uint8_t atyp = buf[3];
271 272 273
        zmq_assert (atyp == 0x01 || atyp == 0x03 || atyp == 0x04);
        if (atyp == 0x01)
            return bytes_read == 10;
274 275
        else if (atyp == 0x03)
            return bytes_read > 4 && bytes_read == 4 + 1 + buf[4] + 2u;
276 277 278 279 280 281 282 283
        else
            return bytes_read == 22;
    }
}

zmq::socks_response_t zmq::socks_response_decoder_t::decode ()
{
    zmq_assert (message_ready ());
284
    return socks_response_t (buf[1], "", 0);
285 286 287 288 289 290
}

void zmq::socks_response_decoder_t::reset ()
{
    bytes_read = 0;
}