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     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 
32 #include "tipc_address.hpp"
33 
34 #if defined ZMQ_HAVE_TIPC
35 
36 #include "err.hpp"
37 
38 #include <string>
39 #include <sstream>
40 
tipc_address_t()41 zmq::tipc_address_t::tipc_address_t ()
42 {
43     memset (&address, 0, sizeof address);
44     _random = false;
45 }
46 
tipc_address_t(const sockaddr * sa_,socklen_t sa_len_)47 zmq::tipc_address_t::tipc_address_t (const sockaddr *sa_, socklen_t sa_len_)
48 {
49     zmq_assert (sa_ && sa_len_ > 0);
50 
51     memset (&address, 0, sizeof address);
52     if (sa_->sa_family == AF_TIPC)
53         memcpy (&address, sa_, sa_len_);
54 
55     _random = false;
56 }
57 
set_random()58 void zmq::tipc_address_t::set_random ()
59 {
60     _random = true;
61 }
is_random() const62 bool zmq::tipc_address_t::is_random () const
63 {
64     return _random;
65 }
is_service() const66 bool zmq::tipc_address_t::is_service () const
67 {
68     if (address.addrtype == TIPC_ADDR_ID)
69         return false;
70 
71     return true;
72 }
resolve(const char * name_)73 int zmq::tipc_address_t::resolve (const char *name_)
74 {
75     unsigned int type = 0;
76     unsigned int lower = 0;
77     unsigned int upper = 0;
78     unsigned int ref = 0;
79     unsigned int z = 1, c = 0, n = 0;
80     char eof;
81     const char *domain;
82     int res;
83 
84 
85     if (strncmp (name_, "<*>", 3) == 0) {
86         set_random ();
87         address.family = AF_TIPC;
88         address.addrtype = TIPC_ADDR_ID;
89         address.addr.id.node = 0;
90         address.addr.id.ref = 0;
91         address.scope = 0;
92         return 0;
93     }
94 
95     res = sscanf (name_, "{%u,%u,%u}", &type, &lower, &upper);
96     /* Fetch optional domain suffix. */
97     if ((domain = strchr (name_, '@'))) {
98         if (sscanf (domain, "@%u.%u.%u%c", &z, &c, &n, &eof) != 3)
99             return EINVAL;
100     }
101     if (res == 3) {
102         if (type < TIPC_RESERVED_TYPES || upper < lower)
103             return EINVAL;
104         address.family = AF_TIPC;
105         address.addrtype = TIPC_ADDR_NAMESEQ;
106         address.addr.nameseq.type = type;
107         address.addr.nameseq.lower = lower;
108         address.addr.nameseq.upper = upper;
109         address.scope = TIPC_ZONE_SCOPE;
110         return 0;
111     }
112     if (res == 2 && type > TIPC_RESERVED_TYPES) {
113         address.family = AF_TIPC;
114         address.addrtype = TIPC_ADDR_NAME;
115         address.addr.name.name.type = type;
116         address.addr.name.name.instance = lower;
117         address.addr.name.domain = tipc_addr (z, c, n);
118         address.scope = 0;
119         return 0;
120     } else if (res == 0) {
121         res = sscanf (name_, "<%u.%u.%u:%u>", &z, &c, &n, &ref);
122         if (res == 4) {
123             address.family = AF_TIPC;
124             address.addrtype = TIPC_ADDR_ID;
125             address.addr.id.node = tipc_addr (z, c, n);
126             address.addr.id.ref = ref;
127             address.scope = 0;
128             return 0;
129         }
130     }
131     return EINVAL;
132 }
133 
to_string(std::string & addr_) const134 int zmq::tipc_address_t::to_string (std::string &addr_) const
135 {
136     if (address.family != AF_TIPC) {
137         addr_.clear ();
138         return -1;
139     }
140     std::stringstream s;
141     if (address.addrtype == TIPC_ADDR_NAMESEQ
142         || address.addrtype == TIPC_ADDR_NAME) {
143         s << "tipc://"
144           << "{" << address.addr.nameseq.type;
145         s << ", " << address.addr.nameseq.lower;
146         s << ", " << address.addr.nameseq.upper << "}";
147         addr_ = s.str ();
148     } else if (address.addrtype == TIPC_ADDR_ID || is_random ()) {
149         s << "tipc://"
150           << "<" << tipc_zone (address.addr.id.node);
151         s << "." << tipc_cluster (address.addr.id.node);
152         s << "." << tipc_node (address.addr.id.node);
153         s << ":" << address.addr.id.ref << ">";
154         addr_ = s.str ();
155     } else {
156         addr_.clear ();
157         return -1;
158     }
159     return 0;
160 }
161 
addr() const162 const sockaddr *zmq::tipc_address_t::addr () const
163 {
164     return (sockaddr *) &address;
165 }
166 
addrlen() const167 socklen_t zmq::tipc_address_t::addrlen () const
168 {
169     return static_cast<socklen_t> (sizeof address);
170 }
171 
172 #endif
173