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