Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
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
57namespace sockpp {
58
60
66{
68 sockaddr_un addr_;
69
71 static constexpr size_t SZ = sizeof(sockaddr_un);
72
73public:
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);
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
185std::ostream& operator<<(std::ostream& os, const unix_address& addr);
186
188// end namespace sockpp
189}
190
191#endif // __sockpp_unix_addr_h
192
Definition sock_address.h:65
virtual sockaddr * sockaddr_ptr()=0
Definition unix_address.h:66
const sockaddr * sockaddr_ptr() const override
Definition unix_address.h:147
const sockaddr_un * sockaddr_un_ptr() const
Definition unix_address.h:161
sockaddr_un * sockaddr_un_ptr()
Definition unix_address.h:166
std::string to_string() const
Definition unix_address.h:172
unix_address(const sock_address &addr)
Definition unix_address.h:101
unix_address(const sockaddr_un &addr)
Definition unix_address.h:111
unix_address(const std::string &path)
unix_address()
Definition unix_address.h:84
sockaddr * sockaddr_ptr() override
Definition unix_address.h:154
socklen_t size() const override
Definition unix_address.h:137
unix_address(const unix_address &addr)
Definition unix_address.h:116
static constexpr sa_family_t ADDRESS_FAMILY
Definition unix_address.h:75
std::string path() const
Definition unix_address.h:128
bool is_set() const
Definition unix_address.h:123