libBsdSockets
C++ Wrapper classes to the BSD Socket API
LocalAddress.cpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013, Komodo Does Inc
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 
13 */
14 
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <string.h>
18 #include <memory>
19 
20 #include <stdexcept>
21 #include <string>
22 
23 #include "LocalAddress.h"
24 
25 #include "LowLevelAddress.h"
26 
27 namespace BsdSockets {
29 
30  /** \brief The private implementation of LocalAddress.
31  *
32  * This provides the LowLevelAddress.
33  */
35  public:
36  /** Create by encoding thePath
37  *
38  * @param thePath to encode
39  * @param allowBlank true if blank is allowed, which is really only from accept()
40  */
41  LocalAddressPimpl(const std::string& thePath, bool allowBlank)
42  {
43  if(!allowBlank && 0 == thePath.size()) {
44  throw std::invalid_argument("thePath cannot be blank");
45  }
46 
47  if(thePath.size() >= sizeof(lowLevelAddress.sun_path)) {
48  throw std::invalid_argument("thePath is too long for this system");
49  }
50 
51  lowLevelAddress.sun_family = AF_LOCAL;
52  strcpy(lowLevelAddress.sun_path, thePath.c_str());
53  }
54 
55  private:
56  LocalAddressPimpl() = delete;
57  LocalAddressPimpl(const LocalAddressPimpl& rhs) = delete;
58  LocalAddressPimpl(LocalAddressPimpl&& rhs) = delete;
59  LocalAddressPimpl& operator=(const LocalAddressPimpl& rhs) = delete;
60  LocalAddressPimpl& operator=(LocalAddressPimpl&& rhs) = delete;
61  };
62 
63  /*
64  * LocalAddress Implementation
65  */
66 
67  LocalAddress::Ptr LocalAddress::create(const std::string& thePath) {
68  return LocalAddress::Ptr(new LocalAddress(thePath, false));
69  }
70 
72  }
73 
74  LocalAddress::LocalAddress(const std::string& thePath, bool allowBlank)
76  path(thePath), pimpl(new LocalAddressPimpl(thePath, allowBlank))
77  {
78  if(nullptr == pimpl) {
79  throw new std::invalid_argument("Invalid path");
80  }
81  }
82 
83  const std::string& LocalAddress::getPath() const {
84  return path;
85  }
86 
89  }
90 
91  Address::Ptr LocalAddress::create(std::shared_ptr<LowLevelAddress> lowLevelAddress) const {
92  if(AF_LOCAL != lowLevelAddress->getSockAddr().sa_family) {
93  throw std::invalid_argument("LowLevelAddress is of wrong type for LocalAddress to create");
94  }
95 
96  const struct sockaddr_un& ref = *reinterpret_cast<const struct sockaddr_un*>(&lowLevelAddress->getSockAddr());
97  return Address::Ptr(new LocalAddress(ref.sun_path, true));
98  }
99 
101  return *pimpl;
102  }
103 
104 } // namespace BsdSockets
105 
106 
Base class for all addresses.
Definition: Address.h:36
std::shared_ptr< Address > Ptr
Definition: Address.h:38
Local Socket Address (formerly Unix Domain Socket) Address representing a named pipe's name.
Definition: LocalAddress.h:35
const std::string & getPath() const
virtual std::shared_ptr< LowLevelAddress > makeTempLowLevelAddress() const
std::shared_ptr< LocalAddress > Ptr
Definition: LocalAddress.h:37
static LocalAddress::Ptr create(const std::string &thePath)
virtual LowLevelAddress & getLowLevelAddress() const
The private implementation of LocalAddress.
LocalAddressPimpl(const std::string &thePath, bool allowBlank)
Interface for the low-level address implementation.
std::shared_ptr< LowLevelAddress > Ptr
LowLevelAddress template for the various cast-to-sockaddr implementaitons.
Namespace of the BsdSockets library.
Definition: Address.cpp:20
LowLevelAddressType< struct sockaddr_un > LocalLowLevelAddress