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