blob: b9ff969e8100c858616db042e2c0742a8113f442 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
ahedberg8afb88d2016-03-22 13:57:47 -04003 * Copyright 2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
murgatroid9954070892016-08-08 17:01:18 -070034#include "src/core/lib/iomgr/port.h"
Craig Tillerd14a1a52015-01-21 15:26:29 -080035
murgatroid99623dd4f2016-08-08 17:31:27 -070036#ifdef GRPC_POSIX_SOCKET
Craig Tillerd14a1a52015-01-21 15:26:29 -080037
Craig Tiller9533d042016-03-25 17:11:06 -070038#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 Nobleb7ebd3b2014-11-26 16:33:03 -080041
ctiller18b49ab2014-12-09 14:39:16 -080042#include <errno.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <fcntl.h>
ctiller18b49ab2014-12-09 14:39:16 -080044#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include <sys/socket.h>
Craig Tillerf40df232016-03-25 13:38:14 -070046#include <sys/types.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tillerfa275a92015-06-01 13:55:54 -070048#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070050#include <grpc/support/string_util.h>
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/iomgr/tcp_posix.h"
52#include "src/core/lib/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
ctiller18b49ab2014-12-09 14:39:16 -080054static void create_sockets(int sv[2]) {
55 int flags;
ahedberg80d6b122016-03-17 17:37:35 -040056 grpc_create_socketpair_if_unix(sv);
ctiller18b49ab2014-12-09 14:39:16 -080057 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 Tiller71d28ad2016-05-07 21:48:44 -070061 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 Nobleb7ebd3b2014-11-26 16:33:03 -080063}
64
Craig Tillere34c2852016-09-23 09:43:32 -070065grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(
Craig Tillerafcc8752016-10-18 16:10:06 -070066 const char *name, grpc_resource_quota *resource_quota,
67 size_t read_slice_size) {
ctiller18b49ab2014-12-09 14:39:16 -080068 int sv[2];
69 grpc_endpoint_pair p;
Craig Tillerfa275a92015-06-01 13:55:54 -070070 char *final_name;
ctiller18b49ab2014-12-09 14:39:16 -080071 create_sockets(sv);
Craig Tillerfa275a92015-06-01 13:55:54 -070072
73 gpr_asprintf(&final_name, "%s:client", name);
Craig Tiller20afa3d2016-10-17 14:52:14 -070074 p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), resource_quota,
Craig Tillere34c2852016-09-23 09:43:32 -070075 read_slice_size, "socketpair-server");
Craig Tillerfa275a92015-06-01 13:55:54 -070076 gpr_free(final_name);
77 gpr_asprintf(&final_name, "%s:server", name);
Craig Tiller20afa3d2016-10-17 14:52:14 -070078 p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), resource_quota,
Craig Tillere34c2852016-09-23 09:43:32 -070079 read_slice_size, "socketpair-client");
Craig Tillerfa275a92015-06-01 13:55:54 -070080 gpr_free(final_name);
ctiller18b49ab2014-12-09 14:39:16 -080081 return p;
82}
Craig Tillerd14a1a52015-01-21 15:26:29 -080083
Craig Tiller190d3602015-02-18 09:23:38 -080084#endif