xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/iomgr/vsock.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 #include <grpc/support/port_platform.h>
19 
20 #include "src/core/lib/iomgr/vsock.h"
21 
22 #ifdef GRPC_HAVE_VSOCK
23 
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 
28 #include "absl/strings/str_cat.h"
29 
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 
33 #include "src/core/lib/address_utils/parse_address.h"
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/gprpp/crash.h"
36 #include "src/core/lib/iomgr/sockaddr.h"
37 #include "src/core/lib/transport/error_utils.h"
38 
grpc_resolve_vsock_address(absl::string_view name)39 absl::StatusOr<std::vector<grpc_resolved_address>> grpc_resolve_vsock_address(
40     absl::string_view name) {
41   grpc_resolved_address addr;
42   grpc_error_handle error = grpc_core::VSockaddrPopulate(name, &addr);
43   GRPC_RETURN_IF_ERROR(error);
44   return std::vector<grpc_resolved_address>({addr});
45 }
46 
grpc_is_vsock(const grpc_resolved_address * resolved_addr)47 int grpc_is_vsock(const grpc_resolved_address* resolved_addr) {
48   const grpc_sockaddr* addr =
49       reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
50   return addr->sa_family == AF_VSOCK;
51 }
52 #else
grpc_resolve_vsock_address(absl::string_view)53 absl::StatusOr<std::vector<grpc_resolved_address>> grpc_resolve_vsock_address(
54     absl::string_view /*name*/) {
55   return absl::InvalidArgumentError("VSOCK is not supported.");
56 }
57 
grpc_is_vsock(const grpc_resolved_address *)58 int grpc_is_vsock(const grpc_resolved_address* /*resolved_addr*/) { return 0; }
59 #endif
60