• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2016 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 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_POSIX_SOCKET_TCP
24 
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <grpc/support/alloc.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 
32 #include <string>
33 
34 #include "absl/log/check.h"
35 #include "absl/strings/str_cat.h"
36 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
37 #include "src/core/lib/iomgr/endpoint_pair.h"
38 #include "src/core/lib/iomgr/socket_utils_posix.h"
39 #include "src/core/lib/iomgr/tcp_posix.h"
40 #include "src/core/lib/iomgr/unix_sockets_posix.h"
41 #include "src/core/lib/resource_quota/api.h"
42 #include "src/core/util/crash.h"
43 #include "src/core/util/string.h"
44 
create_sockets(int sv[2])45 static void create_sockets(int sv[2]) {
46   int flags;
47   grpc_create_socketpair_if_unix(sv);
48   flags = fcntl(sv[0], F_GETFL, 0);
49   CHECK_EQ(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK), 0);
50   flags = fcntl(sv[1], F_GETFL, 0);
51   CHECK_EQ(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK), 0);
52   CHECK(grpc_set_socket_no_sigpipe_if_possible(sv[0]) == absl::OkStatus());
53   CHECK(grpc_set_socket_no_sigpipe_if_possible(sv[1]) == absl::OkStatus());
54 }
55 
grpc_iomgr_create_endpoint_pair(const char * name,const grpc_channel_args * args)56 grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(
57     const char* name, const grpc_channel_args* args) {
58   int sv[2];
59   grpc_endpoint_pair p;
60   create_sockets(sv);
61   grpc_core::ExecCtx exec_ctx;
62   std::string final_name = absl::StrCat(name, ":client");
63   auto new_args = grpc_core::CoreConfiguration::Get()
64                       .channel_args_preconditioning()
65                       .PreconditionChannelArgs(args);
66   p.client = grpc_tcp_create(
67       grpc_fd_create(sv[1], final_name.c_str(), false),
68       TcpOptionsFromEndpointConfig(
69           grpc_event_engine::experimental::ChannelArgsEndpointConfig(new_args)),
70       "socketpair-server");
71   final_name = absl::StrCat(name, ":server");
72   p.server = grpc_tcp_create(
73       grpc_fd_create(sv[0], final_name.c_str(), false),
74       TcpOptionsFromEndpointConfig(
75           grpc_event_engine::experimental::ChannelArgsEndpointConfig(new_args)),
76       "socketpair-client");
77   return p;
78 }
79 
80 #endif
81