libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
tipc_address.cpp
Go to the documentation of this file.
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 "tipc_address.hpp"
31 
32 #if defined ZMQ_HAVE_TIPC
33 
34 #include "err.hpp"
35 
36 #include <string>
37 #include <sstream>
38 
39 zmq::tipc_address_t::tipc_address_t ()
40 {
41  memset (&address, 0, sizeof address);
42 }
43 
44 zmq::tipc_address_t::tipc_address_t (const sockaddr *sa, socklen_t sa_len)
45 {
46  zmq_assert (sa && sa_len > 0);
47 
48  memset (&address, 0, sizeof address);
49  if (sa->sa_family == AF_TIPC)
50  memcpy (&address, sa, sa_len);
51 }
52 
53 zmq::tipc_address_t::~tipc_address_t ()
54 {
55 }
56 
57 int zmq::tipc_address_t::resolve (const char *name)
58 {
59  unsigned int type = 0;
60  unsigned int lower = 0;
61  unsigned int upper = 0;
62  unsigned int z = 1, c = 0, n = 0;
63  char eof;
64  const char *domain;
65  const int res = sscanf (name, "{%u,%u,%u}", &type, &lower, &upper);
66 
67  /* Fetch optional domain suffix. */
68  if ((domain = strchr(name, '@'))) {
69  if (sscanf(domain, "@%u.%u.%u%c", &z, &c, &n, &eof) != 3)
70  return EINVAL;
71  }
72  if (res == 3)
73  goto nameseq;
74  else
75  if (res == 2 && type > TIPC_RESERVED_TYPES) {
76  address.family = AF_TIPC;
77  address.addrtype = TIPC_ADDR_NAME;
78  address.addr.name.name.type = type;
79  address.addr.name.name.instance = lower;
80  address.addr.name.domain = tipc_addr (z, c, n);
81  address.scope = 0;
82  return 0;
83  }
84  else
85  return EINVAL;
86 nameseq:
87  if (type < TIPC_RESERVED_TYPES || upper < lower)
88  return EINVAL;
89  address.family = AF_TIPC;
90  address.addrtype = TIPC_ADDR_NAMESEQ;
91  address.addr.nameseq.type = type;
92  address.addr.nameseq.lower = lower;
93  address.addr.nameseq.upper = upper;
94  address.scope = TIPC_ZONE_SCOPE;
95  return 0;
96 }
97 
98 int zmq::tipc_address_t::to_string (std::string &addr_)
99 {
100  if (address.family != AF_TIPC) {
101  addr_.clear ();
102  return -1;
103  }
104  std::stringstream s;
105  s << "tipc://" << "{" << address.addr.nameseq.type;
106  s << ", " << address.addr.nameseq.lower;
107  s << ", " << address.addr.nameseq.upper << "}";
108  addr_ = s.str ();
109  return 0;
110 }
111 
112 const sockaddr *zmq::tipc_address_t::addr () const
113 {
114  return (sockaddr*) &address;
115 }
116 
117 socklen_t zmq::tipc_address_t::addrlen () const
118 {
119  return (socklen_t) sizeof address;
120 }
121 
122 #endif
#define zmq_assert(x)
Definition: err.hpp:119
enum type_t type
const char * address
Definition: test_fork.cpp:32