sockpp
Modern C++ socket library wrapper
unix_address.h
Go to the documentation of this file.
1 
13 // --------------------------------------------------------------------------
14 // This file is part of the "sockpp" C++ socket library.
15 //
16 // Copyright (c) 2014-2017 Frank Pagliughi
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are
21 // met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 // --------------------------------------------------------------------------
46 
47 #ifndef __sockpp_unix_addr_h
48 #define __sockpp_unix_addr_h
49 
50 #include "sockpp/platform.h"
51 #include "sockpp/sock_address.h"
52 #include <iostream>
53 #include <string>
54 #include <cstring>
55 #include <sys/un.h>
56 
57 namespace sockpp {
58 
60 
65 class unix_address : public sock_address
66 {
68  sockaddr_un addr_;
69 
71  static constexpr size_t SZ = sizeof(sockaddr_un);
72 
73 public:
75  static constexpr sa_family_t ADDRESS_FAMILY = AF_UNIX;
76 
77  // TODO: This only applies to Linux
78  static constexpr size_t MAX_PATH_NAME = 108;
79 
84  unix_address() : addr_() {}
89  unix_address(const std::string& path);
96  explicit unix_address(const sockaddr& addr);
101  unix_address(const sock_address& addr) {
102  std::memcpy(&addr_, addr.sockaddr_ptr(), SZ);
103  }
111  unix_address(const sockaddr_un& addr) : addr_(addr) {}
116  unix_address(const unix_address& addr) : addr_(addr.addr_) {}
123  bool is_set() const { return addr_.sun_path[0] != '\0'; }
128  std::string path() const { return std::string(addr_.sun_path); }
137  socklen_t size() const override { return socklen_t(SZ); }
138 
139  // TODO: Do we need a:
140  // create(path)
141  // to mimic the inet_address behavior?
142 
147  const sockaddr* sockaddr_ptr() const override {
148  return reinterpret_cast<const sockaddr*>(&addr_);
149  }
154  sockaddr* sockaddr_ptr() override {
155  return reinterpret_cast<sockaddr*>(&addr_);
156  }
161  const sockaddr_un* sockaddr_un_ptr() const { return &addr_; }
166  sockaddr_un* sockaddr_un_ptr() { return &addr_; }
172  std::string to_string() const {
173  return std::string("unix:") + std::string(addr_.sun_path);
174  }
175 };
176 
177 // --------------------------------------------------------------------------
178 
185 std::ostream& operator<<(std::ostream& os, const unix_address& addr);
186 
188 // end namespace sockpp
189 }
190 
191 #endif // __sockpp_unix_addr_h
192 
Generic socket address.
Definition: sock_address.h:64
const sockaddr * sockaddr_ptr() const override
Gets a pointer to this object cast to a const sockaddr.
Definition: unix_address.h:147
unix_address(const unix_address &addr)
Constructs the address by copying the specified address.
Definition: unix_address.h:116
unix_address(const sock_address &addr)
Constructs the address by copying the specified structure.
Definition: unix_address.h:101
virtual sockaddr * sockaddr_ptr()=0
Gets a pointer to this object cast to a sockaddr.
bool is_set() const
Checks if the address is set to some value.
Definition: unix_address.h:123
Platform-specific definitions for the sockpp library.
static constexpr sa_family_t ADDRESS_FAMILY
The address family for this type of address.
Definition: unix_address.h:75
std::ostream & operator<<(std::ostream &os, const inet6_address &addr)
Stream inserter for the address.
std::string path() const
Gets the path to which this address refers.
Definition: unix_address.h:128
Class that represents a UNIX domain address.
Definition: unix_address.h:65
const sockaddr_un * sockaddr_un_ptr() const
Gets a const pointer to this object cast to a sockaddr_un.
Definition: unix_address.h:161
std::string to_string() const
Gets a printable string for the address.
Definition: unix_address.h:172
Definition: acceptor.h:51
sockaddr * sockaddr_ptr() override
Gets a pointer to this object cast to a sockaddr.
Definition: unix_address.h:154
socklen_t size() const override
Gets the size of the address structure.
Definition: unix_address.h:137
sockaddr_un * sockaddr_un_ptr()
Gets a pointer to this object cast to a sockaddr_un.
Definition: unix_address.h:166
unix_address()
Constructs an empty address.
Definition: unix_address.h:84
unix_address(const sockaddr_un &addr)
Constructs the address by copying the specified structure.
Definition: unix_address.h:111
Generic address class for sockpp.