Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
ahedberg | 8afb88d | 2016-03-22 13:57:47 -0400 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
murgatroid99 | 5407089 | 2016-08-08 17:01:18 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 35 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 36 | #ifdef GRPC_POSIX_SOCKET |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 37 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/endpoint_pair.h" |
| 39 | #include "src/core/lib/iomgr/socket_utils_posix.h" |
| 40 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 42 | #include <errno.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <fcntl.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 44 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 46 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 48 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 50 | #include <grpc/support/string_util.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 51 | #include "src/core/lib/iomgr/tcp_posix.h" |
| 52 | #include "src/core/lib/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 54 | static void create_sockets(int sv[2]) { |
| 55 | int flags; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 56 | grpc_create_socketpair_if_unix(sv); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 57 | flags = fcntl(sv[0], F_GETFL, 0); |
| 58 | GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); |
| 59 | flags = fcntl(sv[1], F_GETFL, 0); |
| 60 | GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0); |
Craig Tiller | 71d28ad | 2016-05-07 21:48:44 -0700 | [diff] [blame] | 61 | GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]) == GRPC_ERROR_NONE); |
| 62 | GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]) == GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 65 | grpc_endpoint_pair grpc_iomgr_create_endpoint_pair( |
Craig Tiller | afcc875 | 2016-10-18 16:10:06 -0700 | [diff] [blame] | 66 | const char *name, grpc_resource_quota *resource_quota, |
| 67 | size_t read_slice_size) { |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 68 | int sv[2]; |
| 69 | grpc_endpoint_pair p; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 70 | char *final_name; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 71 | create_sockets(sv); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 72 | |
| 73 | gpr_asprintf(&final_name, "%s:client", name); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 74 | p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), resource_quota, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 75 | read_slice_size, "socketpair-server"); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 76 | gpr_free(final_name); |
| 77 | gpr_asprintf(&final_name, "%s:server", name); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 78 | p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), resource_quota, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 79 | read_slice_size, "socketpair-client"); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 80 | gpr_free(final_name); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 81 | return p; |
| 82 | } |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 83 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 84 | #endif |