vmci_address.cpp 4.49 KB
Newer Older
Ilya Kulakov's avatar
Ilya Kulakov committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Ilya Kulakov's avatar
Ilya Kulakov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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

    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
    (at your option) any later version.

    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.

    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 31
#include "precompiled.hpp"

Ilya Kulakov's avatar
Ilya Kulakov committed
32 33 34 35 36 37 38 39 40 41
#include "vmci_address.hpp"

#if defined(ZMQ_HAVE_VMCI)

#include <climits>
#include <string>
#include <sstream>

#include "err.hpp"

42
zmq::vmci_address_t::vmci_address_t (ctx_t *parent_) : parent (parent_)
Ilya Kulakov's avatar
Ilya Kulakov committed
43 44 45 46
{
    memset (&address, 0, sizeof address);
}

47 48 49 50
zmq::vmci_address_t::vmci_address_t (const sockaddr *sa,
                                     socklen_t sa_len,
                                     ctx_t *parent_) :
    parent (parent_)
Ilya Kulakov's avatar
Ilya Kulakov committed
51 52 53 54
{
    zmq_assert (sa && sa_len > 0);

    memset (&address, 0, sizeof address);
55 56
    if (sa->sa_family == parent->get_vmci_socket_family ())
        memcpy (&address, sa, sa_len);
Ilya Kulakov's avatar
Ilya Kulakov committed
57 58 59 60 61 62
}

zmq::vmci_address_t::~vmci_address_t ()
{
}

63
int zmq::vmci_address_t::resolve (const char *path_)
Ilya Kulakov's avatar
Ilya Kulakov committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
{
    //  Find the ':' at end that separates address from the port number.
    const char *delimiter = strrchr (path_, ':');
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    //  Separate the address/port.
    std::string addr_str (path_, delimiter - path_);
    std::string port_str (delimiter + 1);

    unsigned int cid = VMADDR_CID_ANY;
    unsigned int port = VMADDR_PORT_ANY;

79
    if (!addr_str.length ()) {
Ilya Kulakov's avatar
Ilya Kulakov committed
80 81
        errno = EINVAL;
        return -1;
82 83
    } else if (addr_str == "@") {
        cid = VMCISock_GetLocalCID ();
84 85 86 87 88

        if (cid == VMADDR_CID_ANY) {
            errno = ENODEV;
            return -1;
        }
89 90
    } else if (addr_str != "*" && addr_str != "-1") {
        const char *begin = addr_str.c_str ();
Ilya Kulakov's avatar
Ilya Kulakov committed
91
        char *end = NULL;
92
        unsigned long l = strtoul (begin, &end, 10);
Ilya Kulakov's avatar
Ilya Kulakov committed
93

94 95
        if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE)
            || l > UINT_MAX) {
Ilya Kulakov's avatar
Ilya Kulakov committed
96 97 98 99 100 101 102
            errno = EINVAL;
            return -1;
        }

        cid = static_cast<unsigned int> (l);
    }

103
    if (!port_str.length ()) {
Ilya Kulakov's avatar
Ilya Kulakov committed
104 105
        errno = EINVAL;
        return -1;
106 107
    } else if (port_str != "*" && port_str != "-1") {
        const char *begin = port_str.c_str ();
Ilya Kulakov's avatar
Ilya Kulakov committed
108
        char *end = NULL;
109
        unsigned long l = strtoul (begin, &end, 10);
Ilya Kulakov's avatar
Ilya Kulakov committed
110

111 112
        if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE)
            || l > UINT_MAX) {
Ilya Kulakov's avatar
Ilya Kulakov committed
113 114 115 116 117 118 119
            errno = EINVAL;
            return -1;
        }

        port = static_cast<unsigned int> (l);
    }

120 121
    address.svm_family =
      static_cast<sa_family_t> (parent->get_vmci_socket_family ());
Ilya Kulakov's avatar
Ilya Kulakov committed
122 123 124 125 126 127 128 129
    address.svm_cid = cid;
    address.svm_port = port;

    return 0;
}

int zmq::vmci_address_t::to_string (std::string &addr_)
{
130
    if (address.svm_family != parent->get_vmci_socket_family ()) {
Ilya Kulakov's avatar
Ilya Kulakov committed
131 132 133 134 135 136 137 138 139 140
        addr_.clear ();
        return -1;
    }

    std::stringstream s;

    s << "vmci://";

    if (address.svm_cid == VMADDR_CID_ANY) {
        s << "*";
141
    } else {
Ilya Kulakov's avatar
Ilya Kulakov committed
142 143 144 145 146 147 148
        s << address.svm_cid;
    }

    s << ":";

    if (address.svm_port == VMADDR_PORT_ANY) {
        s << "*";
149
    } else {
Ilya Kulakov's avatar
Ilya Kulakov committed
150 151 152 153 154 155 156 157 158
        s << address.svm_port;
    }

    addr_ = s.str ();
    return 0;
}

const sockaddr *zmq::vmci_address_t::addr () const
{
159
    return reinterpret_cast<const sockaddr *> (&address);
Ilya Kulakov's avatar
Ilya Kulakov committed
160 161 162 163 164 165 166 167
}

socklen_t zmq::vmci_address_t::addrlen () const
{
    return static_cast<socklen_t> (sizeof address);
}

#endif