1 // 2 // 3 // Copyright 2015 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 #ifndef GRPC_SRC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H 20 #define GRPC_SRC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H 21 22 #include <grpc/impl/grpc_types.h> 23 #include <grpc/support/port_platform.h> 24 #include <grpc/support/sync.h> 25 #include <stdbool.h> 26 27 /// How is an fd to be used? 28 typedef enum { 29 /// Used for client connection 30 GRPC_FD_CLIENT_CONNECTION_USAGE, 31 /// Used for server listening 32 GRPC_FD_SERVER_LISTENER_USAGE, 33 /// Used for server connection 34 GRPC_FD_SERVER_CONNECTION_USAGE, 35 } grpc_fd_usage; 36 37 /// Information about an fd to mutate 38 typedef struct { 39 /// File descriptor to mutate 40 int fd; 41 /// How the fd will be used 42 grpc_fd_usage usage; 43 } grpc_mutate_socket_info; 44 45 /// The virtual table of grpc_socket_mutator 46 struct grpc_socket_mutator_vtable { 47 /// Mutates the socket options of \a fd -- deprecated, prefer mutate_fd_2 48 bool (*mutate_fd)(int fd, grpc_socket_mutator* mutator); 49 /// Compare socket mutator \a a and \a b 50 int (*compare)(grpc_socket_mutator* a, grpc_socket_mutator* b); 51 /// Destroys the socket mutator instance 52 void (*destroy)(grpc_socket_mutator* mutator); 53 /// Mutates the socket options of the fd in \a info - if set takes preference 54 /// to mutate_fd 55 bool (*mutate_fd_2)(const grpc_mutate_socket_info* info, 56 grpc_socket_mutator* mutator); 57 }; 58 59 /// The Socket Mutator interface allows changes on socket options 60 struct grpc_socket_mutator { 61 const grpc_socket_mutator_vtable* vtable; 62 gpr_refcount refcount; 63 }; 64 65 /// called by concrete implementations to initialize the base struct 66 void grpc_socket_mutator_init(grpc_socket_mutator* mutator, 67 const grpc_socket_mutator_vtable* vtable); 68 69 /// Wrap \a mutator as a grpc_arg 70 grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator* mutator); 71 72 /// Perform the file descriptor mutation operation of \a mutator on \a fd 73 bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator* mutator, int fd, 74 grpc_fd_usage usage); 75 76 /// Compare if \a a and \a b are the same mutator or have same settings 77 int grpc_socket_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b); 78 79 grpc_socket_mutator* grpc_socket_mutator_ref(grpc_socket_mutator* mutator); 80 void grpc_socket_mutator_unref(grpc_socket_mutator* mutator); 81 82 #endif // GRPC_SRC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H 83