Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 2e19036 | 2016-03-25 14:33:26 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [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 | |
| 34 | #include <grpc/support/port_platform.h> |
| 35 | |
Craig Tiller | 1861dca | 2015-04-08 11:25:26 -0700 | [diff] [blame] | 36 | #ifdef GPR_WINSOCK_SOCKET |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 37 | #include "src/core/iomgr/endpoint_pair.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 38 | #include "src/core/iomgr/sockaddr_utils.h" |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <string.h> |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 43 | |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 45 | #include "src/core/iomgr/socket_windows.h" |
| 46 | #include "src/core/iomgr/tcp_windows.h" |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 47 | |
| 48 | static 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 Tiller | a181436 | 2015-04-08 11:54:08 -0700 | [diff] [blame] | 52 | SOCKADDR_IN addr; |
zeliard | c4e7713 | 2015-05-04 15:49:03 +0900 | [diff] [blame] | 53 | int addr_len = sizeof(addr); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 54 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 55 | lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, |
| 56 | WSA_FLAG_OVERLAPPED); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 57 | GPR_ASSERT(lst_sock != INVALID_SOCKET); |
| 58 | |
Craig Tiller | a181436 | 2015-04-08 11:54:08 -0700 | [diff] [blame] | 59 | memset(&addr, 0, sizeof(addr)); |
Nicolas Noble | e144536 | 2015-05-11 17:40:26 -0700 | [diff] [blame] | 60 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 61 | addr.sin_family = AF_INET; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 62 | GPR_ASSERT(bind(lst_sock, (struct sockaddr *)&addr, sizeof(addr)) != |
| 63 | SOCKET_ERROR); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 64 | GPR_ASSERT(listen(lst_sock, SOMAXCONN) != SOCKET_ERROR); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 65 | GPR_ASSERT(getsockname(lst_sock, (struct sockaddr *)&addr, &addr_len) != |
| 66 | SOCKET_ERROR); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 67 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 68 | cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, |
| 69 | WSA_FLAG_OVERLAPPED); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 70 | GPR_ASSERT(cli_sock != INVALID_SOCKET); |
| 71 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 72 | 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 Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 75 | GPR_ASSERT(svr_sock != INVALID_SOCKET); |
| 76 | |
Craig Tiller | a181436 | 2015-04-08 11:54:08 -0700 | [diff] [blame] | 77 | closesocket(lst_sock); |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 78 | grpc_tcp_prepare_socket(cli_sock); |
| 79 | grpc_tcp_prepare_socket(svr_sock); |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 80 | |
| 81 | sv[1] = cli_sock; |
| 82 | sv[0] = svr_sock; |
| 83 | } |
| 84 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 85 | grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name, |
| 86 | size_t read_slice_size) { |
Craig Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 87 | SOCKET sv[2]; |
| 88 | grpc_endpoint_pair p; |
| 89 | create_sockets(sv); |
Craig Tiller | b256faa | 2015-07-23 11:28:16 -0700 | [diff] [blame] | 90 | 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 Tiller | 532fa36 | 2015-04-08 10:53:42 -0700 | [diff] [blame] | 94 | return p; |
| 95 | } |
| 96 | |
| 97 | #endif |