• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_WINSOCK_SOCKET
24 #include "src/core/lib/iomgr/endpoint_pair.h"
25 #include "src/core/lib/iomgr/sockaddr.h"
26 #include "src/core/lib/iomgr/sockaddr_utils.h"
27 
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <string.h>
31 
32 #include <grpc/support/log.h>
33 #include "src/core/lib/iomgr/socket_windows.h"
34 #include "src/core/lib/iomgr/tcp_windows.h"
35 
create_sockets(SOCKET sv[2])36 static void create_sockets(SOCKET sv[2]) {
37   SOCKET svr_sock = INVALID_SOCKET;
38   SOCKET lst_sock = INVALID_SOCKET;
39   SOCKET cli_sock = INVALID_SOCKET;
40   SOCKADDR_IN addr;
41   int addr_len = sizeof(addr);
42 
43   lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
44                        WSA_FLAG_OVERLAPPED);
45   GPR_ASSERT(lst_sock != INVALID_SOCKET);
46 
47   memset(&addr, 0, sizeof(addr));
48   addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
49   addr.sin_family = AF_INET;
50   GPR_ASSERT(bind(lst_sock, (grpc_sockaddr*)&addr, sizeof(addr)) !=
51              SOCKET_ERROR);
52   GPR_ASSERT(listen(lst_sock, SOMAXCONN) != SOCKET_ERROR);
53   GPR_ASSERT(getsockname(lst_sock, (grpc_sockaddr*)&addr, &addr_len) !=
54              SOCKET_ERROR);
55 
56   cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
57                        WSA_FLAG_OVERLAPPED);
58   GPR_ASSERT(cli_sock != INVALID_SOCKET);
59 
60   GPR_ASSERT(WSAConnect(cli_sock, (grpc_sockaddr*)&addr, addr_len, NULL, NULL,
61                         NULL, NULL) == 0);
62   svr_sock = accept(lst_sock, (grpc_sockaddr*)&addr, &addr_len);
63   GPR_ASSERT(svr_sock != INVALID_SOCKET);
64 
65   closesocket(lst_sock);
66   grpc_tcp_prepare_socket(cli_sock);
67   grpc_tcp_prepare_socket(svr_sock);
68 
69   sv[1] = cli_sock;
70   sv[0] = svr_sock;
71 }
72 
grpc_iomgr_create_endpoint_pair(const char * name,grpc_channel_args * channel_args)73 grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(
74     const char* name, grpc_channel_args* channel_args) {
75   SOCKET sv[2];
76   grpc_endpoint_pair p;
77   create_sockets(sv);
78   grpc_core::ExecCtx exec_ctx;
79   p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client"),
80                              channel_args, "endpoint:server");
81   p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server"),
82                              channel_args, "endpoint:client");
83 
84   return p;
85 }
86 
87 #endif
88