1 /*
2  *
3  * Copyright 2017 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/port.h"
22 
23 #ifdef GRPC_POSIX_SOCKET_SOCKET_FACTORY
24 
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gpr/useful.h"
27 #include "src/core/lib/iomgr/socket_factory_posix.h"
28 
29 #include <grpc/impl/codegen/grpc_types.h>
30 #include <grpc/support/sync.h>
31 
grpc_socket_factory_init(grpc_socket_factory * factory,const grpc_socket_factory_vtable * vtable)32 void grpc_socket_factory_init(grpc_socket_factory* factory,
33                               const grpc_socket_factory_vtable* vtable) {
34   factory->vtable = vtable;
35   gpr_ref_init(&factory->refcount, 1);
36 }
37 
grpc_socket_factory_socket(grpc_socket_factory * factory,int domain,int type,int protocol)38 int grpc_socket_factory_socket(grpc_socket_factory* factory, int domain,
39                                int type, int protocol) {
40   return factory->vtable->socket(factory, domain, type, protocol);
41 }
42 
grpc_socket_factory_bind(grpc_socket_factory * factory,int sockfd,const grpc_resolved_address * addr)43 int grpc_socket_factory_bind(grpc_socket_factory* factory, int sockfd,
44                              const grpc_resolved_address* addr) {
45   return factory->vtable->bind(factory, sockfd, addr);
46 }
47 
grpc_socket_factory_compare(grpc_socket_factory * a,grpc_socket_factory * b)48 int grpc_socket_factory_compare(grpc_socket_factory* a,
49                                 grpc_socket_factory* b) {
50   int c = GPR_ICMP(a, b);
51   if (c != 0) {
52     grpc_socket_factory* sma = a;
53     grpc_socket_factory* smb = b;
54     c = GPR_ICMP(sma->vtable, smb->vtable);
55     if (c == 0) {
56       c = sma->vtable->compare(sma, smb);
57     }
58   }
59   return c;
60 }
61 
grpc_socket_factory_ref(grpc_socket_factory * factory)62 grpc_socket_factory* grpc_socket_factory_ref(grpc_socket_factory* factory) {
63   gpr_ref(&factory->refcount);
64   return factory;
65 }
66 
grpc_socket_factory_unref(grpc_socket_factory * factory)67 void grpc_socket_factory_unref(grpc_socket_factory* factory) {
68   if (gpr_unref(&factory->refcount)) {
69     factory->vtable->destroy(factory);
70   }
71 }
72 
socket_factory_arg_copy(void * p)73 static void* socket_factory_arg_copy(void* p) {
74   return grpc_socket_factory_ref(static_cast<grpc_socket_factory*>(p));
75 }
76 
socket_factory_arg_destroy(void * p)77 static void socket_factory_arg_destroy(void* p) {
78   grpc_socket_factory_unref(static_cast<grpc_socket_factory*>(p));
79 }
80 
socket_factory_cmp(void * a,void * b)81 static int socket_factory_cmp(void* a, void* b) {
82   return grpc_socket_factory_compare(static_cast<grpc_socket_factory*>(a),
83                                      static_cast<grpc_socket_factory*>(b));
84 }
85 
86 static const grpc_arg_pointer_vtable socket_factory_arg_vtable = {
87     socket_factory_arg_copy, socket_factory_arg_destroy, socket_factory_cmp};
88 
grpc_socket_factory_to_arg(grpc_socket_factory * factory)89 grpc_arg grpc_socket_factory_to_arg(grpc_socket_factory* factory) {
90   return grpc_channel_arg_pointer_create((char*)GRPC_ARG_SOCKET_FACTORY,
91                                          factory, &socket_factory_arg_vtable);
92 }
93 
94 #endif
95