blob: 7d81470a7873c9cd5e064705a1a67cf430bdb8a9 [file] [log] [blame]
Craig Tiller532fa362015-04-08 10:53:42 -07001/*
2 *
3 * Copyright 2015, Google Inc.
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
34#include <grpc/support/port_platform.h>
35
Craig Tiller1861dca2015-04-08 11:25:26 -070036#ifdef GPR_WINSOCK_SOCKET
Craig Tillera1814362015-04-08 11:54:08 -070037#include "src/core/iomgr/sockaddr_utils.h"
Craig Tiller532fa362015-04-08 10:53:42 -070038#include "src/core/iomgr/endpoint_pair.h"
39
40#include <errno.h>
41#include <fcntl.h>
42#include <string.h>
Craig Tiller532fa362015-04-08 10:53:42 -070043
Craig Tillera1814362015-04-08 11:54:08 -070044#include "src/core/iomgr/tcp_windows.h"
45#include "src/core/iomgr/socket_windows.h"
Craig Tiller532fa362015-04-08 10:53:42 -070046#include <grpc/support/log.h>
47
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
55 lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
56 GPR_ASSERT(lst_sock != INVALID_SOCKET);
57
Craig Tillera1814362015-04-08 11:54:08 -070058 memset(&addr, 0, sizeof(addr));
Nicolas Noblee1445362015-05-11 17:40:26 -070059 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
60 addr.sin_family = AF_INET;
Craig Tillera1814362015-04-08 11:54:08 -070061 GPR_ASSERT(bind(lst_sock, (struct sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR);
Craig Tiller532fa362015-04-08 10:53:42 -070062 GPR_ASSERT(listen(lst_sock, SOMAXCONN) != SOCKET_ERROR);
Craig Tillera1814362015-04-08 11:54:08 -070063 GPR_ASSERT(getsockname(lst_sock, (struct sockaddr*)&addr, &addr_len) != SOCKET_ERROR);
Craig Tiller532fa362015-04-08 10:53:42 -070064
65 cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
66 GPR_ASSERT(cli_sock != INVALID_SOCKET);
67
Craig Tillera1814362015-04-08 11:54:08 -070068 GPR_ASSERT(WSAConnect(cli_sock, (struct sockaddr*)&addr, addr_len, NULL, NULL, NULL, NULL) == 0);
69 svr_sock = accept(lst_sock, (struct sockaddr*)&addr, &addr_len);
Craig Tiller532fa362015-04-08 10:53:42 -070070 GPR_ASSERT(svr_sock != INVALID_SOCKET);
71
Craig Tillera1814362015-04-08 11:54:08 -070072 closesocket(lst_sock);
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020073 grpc_tcp_prepare_socket(cli_sock);
74 grpc_tcp_prepare_socket(svr_sock);
Craig Tiller532fa362015-04-08 10:53:42 -070075
76 sv[1] = cli_sock;
77 sv[0] = svr_sock;
78}
79
Craig Tiller33da3322015-06-02 10:29:40 -070080grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name, size_t read_slice_size) {
Craig Tiller532fa362015-04-08 10:53:42 -070081 SOCKET sv[2];
82 grpc_endpoint_pair p;
83 create_sockets(sv);
Craig Tiller81bcc4c2015-07-20 14:04:18 -070084 p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client"), "endpoint:server");
85 p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server"), "endpoint:client");
Craig Tiller532fa362015-04-08 10:53:42 -070086 return p;
87}
88
89#endif