blob: c6a06cf817840b4ebd14613130865bfbb6b38b59 [file] [log] [blame]
Steven Moreland611d15f2021-05-01 01:28:27 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <string>
19
20#include <arpa/inet.h>
21#include <netdb.h>
22#include <netinet/in.h>
23#include <sys/socket.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
27#ifdef __BIONIC__
28#include <linux/vm_sockets.h>
29#endif
30
31namespace android {
32
33class RpcSocketAddress {
34public:
35 virtual ~RpcSocketAddress() {}
36 virtual std::string toString() const = 0;
37 virtual const sockaddr* addr() const = 0;
38 virtual size_t addrSize() const = 0;
39};
40
41class UnixSocketAddress : public RpcSocketAddress {
42public:
43 explicit UnixSocketAddress(const char* path) : mAddr({.sun_family = AF_UNIX}) {
44 unsigned int pathLen = strlen(path) + 1;
45 LOG_ALWAYS_FATAL_IF(pathLen > sizeof(mAddr.sun_path), "Socket path is too long: %u %s",
46 pathLen, path);
47 memcpy(mAddr.sun_path, path, pathLen);
48 }
49 virtual ~UnixSocketAddress() {}
50 std::string toString() const override {
51 return String8::format("path '%.*s'", static_cast<int>(sizeof(mAddr.sun_path)),
52 mAddr.sun_path)
53 .c_str();
54 }
55 const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); }
56 size_t addrSize() const override { return sizeof(mAddr); }
57
58private:
59 sockaddr_un mAddr;
60};
61
62#ifdef __BIONIC__
63
64class VsockSocketAddress : public RpcSocketAddress {
65public:
66 VsockSocketAddress(unsigned int cid, unsigned int port)
67 : mAddr({
68 .svm_family = AF_VSOCK,
69 .svm_port = port,
70 .svm_cid = cid,
71 }) {}
72 virtual ~VsockSocketAddress() {}
73 std::string toString() const override {
74 return String8::format("cid %u port %u", mAddr.svm_cid, mAddr.svm_port).c_str();
75 }
76 const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); }
77 size_t addrSize() const override { return sizeof(mAddr); }
78
79private:
80 sockaddr_vm mAddr;
81};
82
83#endif // __BIONIC__
84
85class InetSocketAddress : public RpcSocketAddress {
86public:
87 InetSocketAddress(const sockaddr* sockAddr, size_t size, const char* addr, unsigned int port)
88 : mSockAddr(sockAddr), mSize(size), mAddr(addr), mPort(port) {}
89 [[nodiscard]] std::string toString() const override {
90 return String8::format("%s:%u", mAddr, mPort).c_str();
91 }
92 [[nodiscard]] const sockaddr* addr() const override { return mSockAddr; }
93 [[nodiscard]] size_t addrSize() const override { return mSize; }
94
95 using AddrInfo = std::unique_ptr<addrinfo, decltype(&freeaddrinfo)>;
96 static AddrInfo getAddrInfo(const char* addr, unsigned int port) {
97 addrinfo hint{
98 .ai_flags = 0,
99 .ai_family = AF_UNSPEC,
100 .ai_socktype = SOCK_STREAM,
101 .ai_protocol = 0,
102 };
103 addrinfo* aiStart = nullptr;
104 if (int rc = getaddrinfo(addr, std::to_string(port).data(), &hint, &aiStart); 0 != rc) {
105 ALOGE("Unable to resolve %s:%u: %s", addr, port, gai_strerror(rc));
106 return AddrInfo(nullptr, nullptr);
107 }
108 if (aiStart == nullptr) {
109 ALOGE("Unable to resolve %s:%u: getaddrinfo returns null", addr, port);
110 return AddrInfo(nullptr, nullptr);
111 }
112 return AddrInfo(aiStart, &freeaddrinfo);
113 }
114
115private:
116 const sockaddr* mSockAddr;
117 size_t mSize;
118 const char* mAddr;
119 unsigned int mPort;
120};
121
122} // namespace android