Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/endpoint_pair.h" |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/socket_utils_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 41 | #include <errno.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <fcntl.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 43 | #include <string.h> |
| 44 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 47 | #include "src/core/iomgr/tcp_posix.h" |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 48 | #include "src/core/support/string.h" |
| 49 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 51 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 53 | static void create_sockets(int sv[2]) { |
| 54 | int flags; |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 55 | #ifdef GPR_HAVE_UNIX_SOCKET |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 56 | GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 57 | #endif |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 58 | flags = fcntl(sv[0], F_GETFL, 0); |
| 59 | GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); |
| 60 | flags = fcntl(sv[1], F_GETFL, 0); |
| 61 | GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0); |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 62 | GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0])); |
| 63 | GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1])); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 66 | grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name, |
| 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 | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 74 | p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size, |
| 75 | "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 | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 78 | p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size, |
| 79 | "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 |