blob: d3a45f83e032848f9b0d81dc0dad46d410942a34 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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
Craig Tillerd14a1a52015-01-21 15:26:29 -080034#include <grpc/support/port_platform.h>
35
36#ifdef GPR_POSIX_SOCKET
37
ctiller18b49ab2014-12-09 14:39:16 -080038#include "src/core/iomgr/endpoint_pair.h"
Craig Tiller535701c2015-12-07 10:09:44 -080039#include "src/core/iomgr/socket_utils_posix.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040
ctiller18b49ab2014-12-09 14:39:16 -080041#include <errno.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <fcntl.h>
ctiller18b49ab2014-12-09 14:39:16 -080043#include <string.h>
44#include <sys/types.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include <sys/socket.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
ctiller18b49ab2014-12-09 14:39:16 -080047#include "src/core/iomgr/tcp_posix.h"
Craig Tillerfa275a92015-06-01 13:55:54 -070048#include "src/core/support/string.h"
49#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070051#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
ctiller18b49ab2014-12-09 14:39:16 -080053static void create_sockets(int sv[2]) {
54 int flags;
ahedberg803931d2016-03-11 17:24:12 -050055#ifdef GPR_HAVE_UNIX_SOCKET
ctiller18b49ab2014-12-09 14:39:16 -080056 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
ahedberg803931d2016-03-11 17:24:12 -050057#endif
ctiller18b49ab2014-12-09 14:39:16 -080058 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 Tiller535701c2015-12-07 10:09:44 -080062 GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]));
63 GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tillerfa275a92015-06-01 13:55:54 -070066grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
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 Tiller1b22b9d2015-07-20 13:42:22 -070074 p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size,
75 "socketpair-server");
Craig Tillerfa275a92015-06-01 13:55:54 -070076 gpr_free(final_name);
77 gpr_asprintf(&final_name, "%s:server", name);
Craig Tiller1b22b9d2015-07-20 13:42:22 -070078 p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size,
79 "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