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 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/socket_mutator.h"
22 
23 #include <grpc/impl/codegen/grpc_types.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gpr/useful.h"
29 
grpc_socket_mutator_init(grpc_socket_mutator * mutator,const grpc_socket_mutator_vtable * vtable)30 void grpc_socket_mutator_init(grpc_socket_mutator* mutator,
31                               const grpc_socket_mutator_vtable* vtable) {
32   mutator->vtable = vtable;
33   gpr_ref_init(&mutator->refcount, 1);
34 }
35 
grpc_socket_mutator_ref(grpc_socket_mutator * mutator)36 grpc_socket_mutator* grpc_socket_mutator_ref(grpc_socket_mutator* mutator) {
37   gpr_ref(&mutator->refcount);
38   return mutator;
39 }
40 
grpc_socket_mutator_mutate_fd(grpc_socket_mutator * mutator,int fd,grpc_fd_usage usage)41 bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator* mutator, int fd,
42                                    grpc_fd_usage usage) {
43   if (mutator->vtable->mutate_fd_2 != nullptr) {
44     grpc_mutate_socket_info info{fd, usage};
45     return mutator->vtable->mutate_fd_2(&info, mutator);
46   }
47   switch (usage) {
48     case GRPC_FD_SERVER_CONNECTION_USAGE:
49       return true;
50     case GRPC_FD_CLIENT_CONNECTION_USAGE:
51     case GRPC_FD_SERVER_LISTENER_USAGE:
52       return mutator->vtable->mutate_fd(fd, mutator);
53   }
54   GPR_UNREACHABLE_CODE(return false);
55 }
56 
grpc_socket_mutator_compare(grpc_socket_mutator * a,grpc_socket_mutator * b)57 int grpc_socket_mutator_compare(grpc_socket_mutator* a,
58                                 grpc_socket_mutator* b) {
59   int c = grpc_core::QsortCompare(a, b);
60   if (c != 0) {
61     grpc_socket_mutator* sma = a;
62     grpc_socket_mutator* smb = b;
63     c = grpc_core::QsortCompare(sma->vtable, smb->vtable);
64     if (c == 0) {
65       c = sma->vtable->compare(sma, smb);
66     }
67   }
68   return c;
69 }
70 
grpc_socket_mutator_unref(grpc_socket_mutator * mutator)71 void grpc_socket_mutator_unref(grpc_socket_mutator* mutator) {
72   if (gpr_unref(&mutator->refcount)) {
73     mutator->vtable->destroy(mutator);
74   }
75 }
76 
socket_mutator_arg_copy(void * p)77 static void* socket_mutator_arg_copy(void* p) {
78   return grpc_socket_mutator_ref(static_cast<grpc_socket_mutator*>(p));
79 }
80 
socket_mutator_arg_destroy(void * p)81 static void socket_mutator_arg_destroy(void* p) {
82   grpc_socket_mutator_unref(static_cast<grpc_socket_mutator*>(p));
83 }
84 
socket_mutator_cmp(void * a,void * b)85 static int socket_mutator_cmp(void* a, void* b) {
86   return grpc_socket_mutator_compare(static_cast<grpc_socket_mutator*>(a),
87                                      static_cast<grpc_socket_mutator*>(b));
88 }
89 
90 static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = {
91     socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp};
92 
grpc_socket_mutator_to_arg(grpc_socket_mutator * mutator)93 grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator* mutator) {
94   return grpc_channel_arg_pointer_create(
95       const_cast<char*>(GRPC_ARG_SOCKET_MUTATOR), mutator,
96       &socket_mutator_arg_vtable);
97 }
98