Line data Source code
1 : // Copyright (c) 2012-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libaddr
4 : //
5 : // Permission is hereby granted, free of charge, to any person obtaining a
6 : // copy of this software and associated documentation files (the
7 : // "Software"), to deal in the Software without restriction, including
8 : // without limitation the rights to use, copy, modify, merge, publish,
9 : // distribute, sublicense, and/or sell copies of the Software, and to
10 : // permit persons to whom the Software is furnished to do so, subject to
11 : // the following conditions:
12 : //
13 : // The above copyright notice and this permission notice shall be included
14 : // in all copies or substantial portions of the Software.
15 : //
16 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 : #pragma once
24 :
25 : /** \file
26 : * \brief The Unix libaddr class.
27 : *
28 : * This header defines the Unix address class. This is used to connect to
29 : * Unix sockets.
30 : *
31 : * The library supports all three types of Unix addresses supported by
32 : * Linux:
33 : *
34 : * * File based: a path to a file on disk.
35 : * * Abstract: a abstract path.
36 : * * Unnamed: a completely unnamed socket.
37 : *
38 : * The first one (File) is what most users are expected to use.
39 : *
40 : * The second one (Abstract) is what many X11 tools use to communicate.
41 : * Especially, it is called the "bus" in Gnome.
42 : *
43 : * The third one (Unnamed) is only useful for parent/child communication.
44 : * The child can be a process created by fork() and fork() + exec().
45 : */
46 :
47 : // C++
48 : //
49 : #include <memory>
50 : #include <string>
51 : #include <vector>
52 :
53 :
54 : // C
55 : //
56 : #include <sys/socket.h>
57 : #include <sys/un.h>
58 :
59 :
60 :
61 : namespace addr
62 : {
63 :
64 :
65 :
66 :
67 : /** \brief Initialize a Unix address as such.
68 : *
69 : * This function initializes a sockaddr_un with all zeroes except
70 : * for the sun_family which is set to AF_UNIX.
71 : *
72 : * return The initialized Unix address.
73 : */
74 538 : constexpr struct sockaddr_un init_un()
75 : {
76 538 : struct sockaddr_un un = sockaddr_un();
77 538 : un.sun_family = AF_UNIX;
78 538 : return un;
79 : }
80 :
81 :
82 : constexpr int const DEFAULT_MODE = 0600;
83 :
84 :
85 : class addr_unix
86 : {
87 : public:
88 : typedef std::shared_ptr<addr_unix> pointer_t;
89 : typedef std::vector<addr_unix> vector_t;
90 : typedef int socket_flag_t;
91 :
92 : addr_unix();
93 : addr_unix(sockaddr_un const & un);
94 : addr_unix(std::string const & address, bool abstract = false);
95 :
96 : void set_scheme(std::string const & scheme);
97 : void set_un(sockaddr_un const & un);
98 : void make_unnamed();
99 : void set_file(std::string const & address);
100 : void set_mode(int mode);
101 : void set_group(std::string const & group);
102 : void set_abstract(std::string const & address);
103 : void set_uri(std::string const & address);
104 : bool set_from_socket(int s);
105 :
106 : bool is_file() const;
107 : bool is_abstract() const;
108 : bool is_unnamed() const;
109 : std::string get_scheme() const;
110 : void get_un(sockaddr_un & un) const;
111 : int get_mode() const;
112 : std::string get_group() const;
113 : std::string to_string() const;
114 : std::string to_uri() const;
115 : int unlink();
116 :
117 : bool operator == (addr_unix const & rhs) const;
118 : bool operator != (addr_unix const & rhs) const;
119 : bool operator < (addr_unix const & rhs) const;
120 : bool operator <= (addr_unix const & rhs) const;
121 : bool operator > (addr_unix const & rhs) const;
122 : bool operator >= (addr_unix const & rhs) const;
123 :
124 : private:
125 : std::string verify_path(std::string const & path, bool abstract);
126 :
127 : std::string f_scheme = std::string();
128 : sockaddr_un f_address = init_un();
129 : int f_mode = DEFAULT_MODE;
130 : std::string f_group = std::string();
131 : };
132 :
133 :
134 :
135 :
136 :
137 : }
138 : // namespace addr
139 :
140 :
141 9 : inline bool operator == (sockaddr_un const & a, sockaddr_un const & b)
142 : {
143 9 : return memcmp(&a, &b, sizeof(sockaddr_un)) == 0;
144 : }
145 :
146 :
147 6 : inline bool operator != (sockaddr_un const & a, sockaddr_un const & b)
148 : {
149 6 : return memcmp(&a, &b, sizeof(sockaddr_un)) != 0;
150 : }
151 :
152 :
153 6 : inline bool operator < (sockaddr_un const & a, sockaddr_un const & b)
154 : {
155 6 : return memcmp(&a, &b, sizeof(sockaddr_un)) < 0;
156 : }
157 :
158 :
159 6 : inline bool operator <= (sockaddr_un const & a, sockaddr_un const & b)
160 : {
161 6 : return memcmp(&a, &b, sizeof(sockaddr_un)) <= 0;
162 : }
163 :
164 :
165 6 : inline bool operator > (sockaddr_un const & a, sockaddr_un const & b)
166 : {
167 6 : return memcmp(&a, &b, sizeof(sockaddr_un)) > 0;
168 : }
169 :
170 :
171 6 : inline bool operator >= (sockaddr_un const & a, sockaddr_un const & b)
172 : {
173 6 : return memcmp(&a, &b, sizeof(sockaddr_un)) >= 0;
174 : }
175 :
176 :
177 :
178 : // vim: ts=4 sw=4 et
|