Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc/support/port_platform.h>
35 :
36 : #ifdef GPR_POSIX_SOCKET
37 :
38 : #include "src/core/iomgr/endpoint_pair.h"
39 : #include "src/core/iomgr/socket_utils_posix.h"
40 :
41 : #include <errno.h>
42 : #include <fcntl.h>
43 : #include <string.h>
44 : #include <sys/types.h>
45 : #include <sys/socket.h>
46 :
47 : #include "src/core/iomgr/tcp_posix.h"
48 : #include "src/core/support/string.h"
49 : #include <grpc/support/alloc.h>
50 : #include <grpc/support/log.h>
51 : #include <grpc/support/string_util.h>
52 :
53 917 : static void create_sockets(int sv[2]) {
54 : int flags;
55 917 : GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
56 917 : flags = fcntl(sv[0], F_GETFL, 0);
57 917 : GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
58 917 : flags = fcntl(sv[1], F_GETFL, 0);
59 917 : GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
60 917 : GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]));
61 917 : GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]));
62 917 : }
63 :
64 917 : grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
65 : size_t read_slice_size) {
66 : int sv[2];
67 : grpc_endpoint_pair p;
68 : char *final_name;
69 917 : create_sockets(sv);
70 :
71 917 : gpr_asprintf(&final_name, "%s:client", name);
72 917 : p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size,
73 : "socketpair-server");
74 917 : gpr_free(final_name);
75 917 : gpr_asprintf(&final_name, "%s:server", name);
76 917 : p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size,
77 : "socketpair-client");
78 917 : gpr_free(final_name);
79 917 : return p;
80 : }
81 :
82 : #endif
|