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/sync.h>
25
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/gpr/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)40 bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator* mutator, int fd) {
41 return mutator->vtable->mutate_fd(fd, mutator);
42 }
43
grpc_socket_mutator_compare(grpc_socket_mutator * a,grpc_socket_mutator * b)44 int grpc_socket_mutator_compare(grpc_socket_mutator* a,
45 grpc_socket_mutator* b) {
46 int c = GPR_ICMP(a, b);
47 if (c != 0) {
48 grpc_socket_mutator* sma = a;
49 grpc_socket_mutator* smb = b;
50 c = GPR_ICMP(sma->vtable, smb->vtable);
51 if (c == 0) {
52 c = sma->vtable->compare(sma, smb);
53 }
54 }
55 return c;
56 }
57
grpc_socket_mutator_unref(grpc_socket_mutator * mutator)58 void grpc_socket_mutator_unref(grpc_socket_mutator* mutator) {
59 if (gpr_unref(&mutator->refcount)) {
60 mutator->vtable->destroy(mutator);
61 }
62 }
63
socket_mutator_arg_copy(void * p)64 static void* socket_mutator_arg_copy(void* p) {
65 return grpc_socket_mutator_ref(static_cast<grpc_socket_mutator*>(p));
66 }
67
socket_mutator_arg_destroy(void * p)68 static void socket_mutator_arg_destroy(void* p) {
69 grpc_socket_mutator_unref(static_cast<grpc_socket_mutator*>(p));
70 }
71
socket_mutator_cmp(void * a,void * b)72 static int socket_mutator_cmp(void* a, void* b) {
73 return grpc_socket_mutator_compare(static_cast<grpc_socket_mutator*>(a),
74 static_cast<grpc_socket_mutator*>(b));
75 }
76
77 static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = {
78 socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp};
79
grpc_socket_mutator_to_arg(grpc_socket_mutator * mutator)80 grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator* mutator) {
81 return grpc_channel_arg_pointer_create((char*)GRPC_ARG_SOCKET_MUTATOR,
82 mutator, &socket_mutator_arg_vtable);
83 }
84