• 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 <stdio.h>
20 #include <string.h>
21 
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
25 #include "test/cpp/naming/dns_test_util.h"
26 
27 #ifdef GPR_WINDOWS
28 #include "src/core/lib/iomgr/sockaddr_windows.h"
29 #include "src/core/lib/iomgr/socket_windows.h"
30 #define BAD_SOCKET_RETURN_VAL INVALID_SOCKET
31 #else
32 #include "src/core/lib/iomgr/sockaddr_posix.h"
33 #define BAD_SOCKET_RETURN_VAL -1
34 #endif
35 
36 namespace grpc {
37 namespace testing {
38 
FakeNonResponsiveDNSServer(int port)39 FakeNonResponsiveDNSServer::FakeNonResponsiveDNSServer(int port) {
40   udp_socket_ = socket(AF_INET6, SOCK_DGRAM, 0);
41   tcp_socket_ = socket(AF_INET6, SOCK_STREAM, 0);
42   if (udp_socket_ == BAD_SOCKET_RETURN_VAL) {
43     gpr_log(GPR_DEBUG, "Failed to create UDP ipv6 socket");
44     abort();
45   }
46   if (tcp_socket_ == BAD_SOCKET_RETURN_VAL) {
47     gpr_log(GPR_DEBUG, "Failed to create TCP ipv6 socket");
48     abort();
49   }
50   sockaddr_in6 addr;
51   memset(&addr, 0, sizeof(addr));
52   addr.sin6_family = AF_INET6;
53   addr.sin6_port = htons(port);
54   ((char*)&addr.sin6_addr)[15] = 1;
55   if (bind(udp_socket_, (const sockaddr*)&addr, sizeof(addr)) != 0) {
56     gpr_log(GPR_DEBUG, "Failed to bind UDP ipv6 socket to [::1]:%d", port);
57     abort();
58   }
59 #ifdef GPR_WINDOWS
60   char val = 1;
61   if (setsockopt(tcp_socket_, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) ==
62       SOCKET_ERROR) {
63     gpr_log(GPR_DEBUG,
64             "Failed to set SO_REUSEADDR on TCP ipv6 socket to [::1]:%d", port);
65     abort();
66   }
67 #else
68   int val = 1;
69   if (setsockopt(tcp_socket_, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) !=
70       0) {
71     gpr_log(GPR_DEBUG,
72             "Failed to set SO_REUSEADDR on TCP ipv6 socket to [::1]:%d", port);
73     abort();
74   }
75 #endif
76   if (bind(tcp_socket_, (const sockaddr*)&addr, sizeof(addr)) != 0) {
77     gpr_log(GPR_DEBUG, "Failed to bind TCP ipv6 socket to [::1]:%d", port);
78     abort();
79   }
80   if (listen(tcp_socket_, 100)) {
81     gpr_log(GPR_DEBUG, "Failed to listen on TCP ipv6 socket to [::1]:%d", port);
82     abort();
83   }
84 }
85 
~FakeNonResponsiveDNSServer()86 FakeNonResponsiveDNSServer::~FakeNonResponsiveDNSServer() {
87 #ifdef GPR_WINDOWS
88   closesocket(udp_socket_);
89   closesocket(tcp_socket_);
90 #else
91   close(udp_socket_);
92   close(tcp_socket_);
93 #endif
94 }
95 
96 }  // namespace testing
97 }  // namespace grpc
98