blob: 2024f58143072004d1fb748deafd53b777e35e23 [file] [log] [blame]
Craig Tiller532fa362015-04-08 10:53:42 -07001/*
2 *
Craig Tiller2e190362016-03-25 14:33:26 -07003 * Copyright 2015-2016, Google Inc.
Craig Tiller532fa362015-04-08 10:53:42 -07004 * 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
Craig Tiller1861dca2015-04-08 11:25:26 -070036#ifdef GPR_WINSOCK_SOCKET
Craig Tiller532fa362015-04-08 10:53:42 -070037#include "src/core/iomgr/endpoint_pair.h"
Craig Tillerf40df232016-03-25 13:38:14 -070038#include "src/core/iomgr/sockaddr_utils.h"
Craig Tiller532fa362015-04-08 10:53:42 -070039
40#include <errno.h>
41#include <fcntl.h>
42#include <string.h>
Craig Tiller532fa362015-04-08 10:53:42 -070043
Craig Tiller532fa362015-04-08 10:53:42 -070044#include <grpc/support/log.h>
Craig Tillerf40df232016-03-25 13:38:14 -070045#include "src/core/iomgr/socket_windows.h"
46#include "src/core/iomgr/tcp_windows.h"
Craig Tiller532fa362015-04-08 10:53:42 -070047
48static void create_sockets(SOCKET sv[2]) {
49 SOCKET svr_sock = INVALID_SOCKET;
50 SOCKET lst_sock = INVALID_SOCKET;
51 SOCKET cli_sock = INVALID_SOCKET;
Craig Tillera1814362015-04-08 11:54:08 -070052 SOCKADDR_IN addr;
zeliardc4e77132015-05-04 15:49:03 +090053 int addr_len = sizeof(addr);
Craig Tiller532fa362015-04-08 10:53:42 -070054
Craig Tillerd6c98df2015-08-18 09:33:44 -070055 lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
56 WSA_FLAG_OVERLAPPED);
Craig Tiller532fa362015-04-08 10:53:42 -070057 GPR_ASSERT(lst_sock != INVALID_SOCKET);
58
Craig Tillera1814362015-04-08 11:54:08 -070059 memset(&addr, 0, sizeof(addr));
Nicolas Noblee1445362015-05-11 17:40:26 -070060 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
61 addr.sin_family = AF_INET;
Craig Tillerd6c98df2015-08-18 09:33:44 -070062 GPR_ASSERT(bind(lst_sock, (struct sockaddr *)&addr, sizeof(addr)) !=
63 SOCKET_ERROR);
Craig Tiller532fa362015-04-08 10:53:42 -070064 GPR_ASSERT(listen(lst_sock, SOMAXCONN) != SOCKET_ERROR);
Craig Tillerd6c98df2015-08-18 09:33:44 -070065 GPR_ASSERT(getsockname(lst_sock, (struct sockaddr *)&addr, &addr_len) !=
66 SOCKET_ERROR);
Craig Tiller532fa362015-04-08 10:53:42 -070067
Craig Tillerd6c98df2015-08-18 09:33:44 -070068 cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
69 WSA_FLAG_OVERLAPPED);
Craig Tiller532fa362015-04-08 10:53:42 -070070 GPR_ASSERT(cli_sock != INVALID_SOCKET);
71
Craig Tillerd6c98df2015-08-18 09:33:44 -070072 GPR_ASSERT(WSAConnect(cli_sock, (struct sockaddr *)&addr, addr_len, NULL,
73 NULL, NULL, NULL) == 0);
74 svr_sock = accept(lst_sock, (struct sockaddr *)&addr, &addr_len);
Craig Tiller532fa362015-04-08 10:53:42 -070075 GPR_ASSERT(svr_sock != INVALID_SOCKET);
76
Craig Tillera1814362015-04-08 11:54:08 -070077 closesocket(lst_sock);
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020078 grpc_tcp_prepare_socket(cli_sock);
79 grpc_tcp_prepare_socket(svr_sock);
Craig Tiller532fa362015-04-08 10:53:42 -070080
81 sv[1] = cli_sock;
82 sv[0] = svr_sock;
83}
84
Craig Tillerd6c98df2015-08-18 09:33:44 -070085grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
86 size_t read_slice_size) {
Craig Tiller532fa362015-04-08 10:53:42 -070087 SOCKET sv[2];
88 grpc_endpoint_pair p;
89 create_sockets(sv);
Craig Tillerb256faa2015-07-23 11:28:16 -070090 p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client"),
91 "endpoint:server");
92 p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server"),
93 "endpoint:client");
Craig Tiller532fa362015-04-08 10:53:42 -070094 return p;
95}
96
97#endif