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 #include "compat.hpp"
32 #include "ipc_address.hpp"
33 
34 #if defined ZMQ_HAVE_IPC
35 
36 #include "err.hpp"
37 
38 #include <string>
39 
ipc_address_t()40 zmq::ipc_address_t::ipc_address_t ()
41 {
42     memset (&_address, 0, sizeof _address);
43 }
44 
ipc_address_t(const sockaddr * sa_,socklen_t sa_len_)45 zmq::ipc_address_t::ipc_address_t (const sockaddr *sa_, socklen_t sa_len_) :
46     _addrlen (sa_len_)
47 {
48     zmq_assert (sa_ && sa_len_ > 0);
49 
50     memset (&_address, 0, sizeof _address);
51     if (sa_->sa_family == AF_UNIX)
52         memcpy (&_address, sa_, sa_len_);
53 }
54 
~ipc_address_t()55 zmq::ipc_address_t::~ipc_address_t ()
56 {
57 }
58 
resolve(const char * path_)59 int zmq::ipc_address_t::resolve (const char *path_)
60 {
61     const size_t path_len = strlen (path_);
62     if (path_len >= sizeof _address.sun_path) {
63         errno = ENAMETOOLONG;
64         return -1;
65     }
66     if (path_[0] == '@' && !path_[1]) {
67         errno = EINVAL;
68         return -1;
69     }
70 
71     _address.sun_family = AF_UNIX;
72     memcpy (_address.sun_path, path_, path_len + 1);
73     /* Abstract sockets start with '\0' */
74     if (path_[0] == '@')
75         *_address.sun_path = '\0';
76 
77     _addrlen =
78       static_cast<socklen_t> (offsetof (sockaddr_un, sun_path) + path_len);
79     return 0;
80 }
81 
to_string(std::string & addr_) const82 int zmq::ipc_address_t::to_string (std::string &addr_) const
83 {
84     if (_address.sun_family != AF_UNIX) {
85         addr_.clear ();
86         return -1;
87     }
88 
89     const char prefix[] = "ipc://";
90     char buf[sizeof prefix + sizeof _address.sun_path];
91     char *pos = buf;
92     memcpy (pos, prefix, sizeof prefix - 1);
93     pos += sizeof prefix - 1;
94     const char *src_pos = _address.sun_path;
95     if (!_address.sun_path[0] && _address.sun_path[1]) {
96         *pos++ = '@';
97         src_pos++;
98     }
99     // according to http://man7.org/linux/man-pages/man7/unix.7.html, NOTES
100     // section, address.sun_path might not always be null-terminated; therefore,
101     // we calculate the length based of addrlen
102     const size_t src_len =
103       strnlen (src_pos, _addrlen - offsetof (sockaddr_un, sun_path)
104                           - (src_pos - _address.sun_path));
105     memcpy (pos, src_pos, src_len);
106     addr_.assign (buf, pos - buf + src_len);
107     return 0;
108 }
109 
addr() const110 const sockaddr *zmq::ipc_address_t::addr () const
111 {
112     return reinterpret_cast<const sockaddr *> (&_address);
113 }
114 
addrlen() const115 socklen_t zmq::ipc_address_t::addrlen () const
116 {
117     return _addrlen;
118 }
119 
120 #endif
121