• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/lib/iomgr/vsock.h"
19 
20 #include <grpc/support/port_platform.h>
21 
22 #ifdef GRPC_HAVE_VSOCK
23 
24 #include <grpc/support/alloc.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 
29 #include "absl/strings/str_cat.h"
30 #include "src/core/lib/address_utils/parse_address.h"
31 #include "src/core/lib/iomgr/sockaddr.h"
32 #include "src/core/lib/transport/error_utils.h"
33 #include "src/core/util/crash.h"
34 #include "src/core/util/useful.h"
35 
grpc_resolve_vsock_address(absl::string_view name)36 absl::StatusOr<std::vector<grpc_resolved_address>> grpc_resolve_vsock_address(
37     absl::string_view name) {
38   grpc_resolved_address addr;
39   grpc_error_handle error = grpc_core::VSockaddrPopulate(name, &addr);
40   GRPC_RETURN_IF_ERROR(error);
41   return std::vector<grpc_resolved_address>({addr});
42 }
43 
grpc_is_vsock(const grpc_resolved_address * resolved_addr)44 int grpc_is_vsock(const grpc_resolved_address* resolved_addr) {
45   const grpc_sockaddr* addr =
46       reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
47   return addr->sa_family == AF_VSOCK;
48 }
49 #else
grpc_resolve_vsock_address(absl::string_view)50 absl::StatusOr<std::vector<grpc_resolved_address>> grpc_resolve_vsock_address(
51     absl::string_view /*name*/) {
52   return absl::InvalidArgumentError("VSOCK is not supported.");
53 }
54 
grpc_is_vsock(const grpc_resolved_address *)55 int grpc_is_vsock(const grpc_resolved_address* /*resolved_addr*/) { return 0; }
56 #endif
57