• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_LISTENER_UTILS_H
15 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_LISTENER_UTILS_H
16 
17 #include <grpc/event_engine/event_engine.h>
18 #include <grpc/support/port_platform.h>
19 
20 #include "absl/status/statusor.h"
21 #include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h"
22 
23 namespace grpc_event_engine {
24 namespace experimental {
25 
26 // This interface exists to allow different EventEngines to implement different
27 // custom interception operations while a socket is Appended. The
28 // listener util functions are defined over this interface and thus can be
29 // shared across multiple EventEngines.
30 class ListenerSocketsContainer {
31  public:
32   struct ListenerSocket {
33     // Listener socket fd
34     PosixSocketWrapper sock;
35     // Assigned/chosen listening port
36     int port;
37     // Socket configuration
38     bool zero_copy_enabled;
39     // Address at which the socket is listening for connections
40     grpc_event_engine::experimental::EventEngine::ResolvedAddress addr;
41     // Dual stack mode.
42     PosixSocketWrapper::DSMode dsmode;
43   };
44   // Adds a socket to the internal db of sockets associated with a listener.
45   virtual void Append(ListenerSocket socket) = 0;
46 
47   // Returns a non-OK status if the socket cannot be found. Otherwise, returns
48   // the socket.
49   virtual absl::StatusOr<ListenerSocket> Find(
50       const grpc_event_engine::experimental::EventEngine::ResolvedAddress&
51           addr) = 0;
52 
53   virtual ~ListenerSocketsContainer() = default;
54 };
55 
56 // Creates and configures a socket to be used by the EventEngine Listener. The
57 // type of the socket to create is determined by the by the passed address. The
58 // socket configuration is specified by passed tcp options. If successful, it
59 // returns a ListenerSocketsContainer::ListenerSocket type which holds the
60 // socket fd and its dsmode. If unsuccessful, it returns a Not-OK status.
61 absl::StatusOr<ListenerSocketsContainer::ListenerSocket>
62 CreateAndPrepareListenerSocket(
63     const PosixTcpOptions& options,
64     const grpc_event_engine::experimental::EventEngine::ResolvedAddress& addr);
65 
66 // Instead of creating and adding a socket bound to specific address, this
67 // function creates and adds a socket bound to the wildcard address on the
68 // server. The newly created socket is configured according to the passed
69 // options and added to the passed ListenerSocketsContainer object. The function
70 // returns the port at which the created socket listens for incoming
71 // connections.
72 absl::StatusOr<int> ListenerContainerAddWildcardAddresses(
73     ListenerSocketsContainer& listener_sockets, const PosixTcpOptions& options,
74     int requested_port);
75 
76 // Get all addresses assigned to network interfaces on the machine and create
77 // and add a socket for each local address. Each newly created socket is
78 // configured according to the passed options and added to the passed
79 // ListenerSocketsContainer object. The requested_port is the port to use for
80 // every socket. If set to 0, a random port will be used for every socket.
81 // The function returns the chosen port number for all created sockets.
82 absl::StatusOr<int> ListenerContainerAddAllLocalAddresses(
83     ListenerSocketsContainer& listener_sockets, const PosixTcpOptions& options,
84     int requested_port);
85 
86 }  // namespace experimental
87 }  // namespace grpc_event_engine
88 
89 #endif  // GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_LISTENER_UTILS_H
90