]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/net/unix_address.hh
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / include / seastar / net / unix_address.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2019 Red Hat, Inc.
20 */
21 #pragma once
22
23 #include <iosfwd>
24 #include <sys/types.h>
25 #include <sys/un.h>
26 #include <string>
27
28 namespace seastar {
29
30 /*!
31 A helper struct for creating/manipulating UNIX-domain sockets.
32
33 A UNIX-domain socket is either named or unnamed. If named, the name is either
34 a path in the filesystem namespace, or an abstract-domain identifier. Abstract-domain
35 names start with a null byte, and may contain non-printable characters.
36
37 std::string() can hold a sequence of arbitrary bytes, and has a length() attribute
38 that does not rely on using strlen(). Thus it is used here to hold the address.
39 */
40 struct unix_domain_addr {
41 const std::string name;
42 const int path_count; // either name.length() or name.length()+1. See path_length_aux() below.
43
44 explicit unix_domain_addr(const std::string& fn) : name{fn}, path_count{path_length_aux()} {}
45
46 explicit unix_domain_addr(const char* fn) : name{fn}, path_count{path_length_aux()} {}
47
48 int path_length() const { return path_count; }
49
50 // the following holds:
51 // for abstract name: name.length() == number of meaningful bytes, including the null in name[0].
52 // for filesystem path: name.length() does not count the implicit terminating null.
53 // Here we tweak the outside-visible length of the address.
54 int path_length_aux() const {
55 auto pl = (int)name.length();
56 if (!pl || (name[0] == '\0')) {
57 // unnamed, or abstract-namespace
58 return pl;
59 }
60 return 1 + pl;
61 }
62
63 const char* path_bytes() const { return name.c_str(); }
64
65 bool operator==(const unix_domain_addr& a) const {
66 return name == a.name;
67 }
68 bool operator!=(const unix_domain_addr& a) const {
69 return !(*this == a);
70 }
71 };
72
73 std::ostream& operator<<(std::ostream&, const unix_domain_addr&);
74
75 } // namespace seastar