forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closewait.go
196 lines (153 loc) · 4.63 KB
/
closewait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nat
/*
client/server for testing CLOSE_WAIT timeout condition in iptables NAT.
client server
| |
|<--tcp handshake-->|
|<-------fin--------| half-close from server
| | client is in CLOSE_WAIT
*/
import (
"errors"
"io"
"log"
"net"
"time"
"k8s.io/kubernetes/test/images/net/common"
)
// leakedConnection is a global variable that should leak the active
// connection assigned here.
var leakedConnection *net.TCPConn
// Server JSON options.
type CloseWaitServerOptions struct {
// Address to bind for the test
LocalAddr string
// Timeout to wait after sending the FIN.
PostFinTimeoutSeconds int
}
type closeWaitServer struct {
options *CloseWaitServerOptions
}
// NewCloseWaitServer returns a new Runner.
func NewCloseWaitServer() common.Runner {
return &closeWaitServer{}
}
// NewOptions allocates new options structure.
func (server *closeWaitServer) NewOptions() interface{} {
return &CloseWaitServerOptions{}
}
// Run the server-side of the test.
func (server *closeWaitServer) Run(logger *log.Logger, rawOptions interface{}) error {
if options, ok := rawOptions.(*CloseWaitServerOptions); ok {
server.options = options
} else {
return errors.New("invalid type")
}
logger.Printf("Run %v", server.options)
addr, err := net.ResolveTCPAddr("tcp", server.options.LocalAddr)
if err != nil {
return err
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}
defer listener.Close()
logger.Printf("Server listening on %v", addr)
conn, err := listener.AcceptTCP()
if err != nil {
return err
}
defer conn.Close()
logger.Printf("Client connected")
// Send client half-close FIN so client is now in CLOSE_WAIT. We keep
// the client -> server pipe open to verify whether or not the NAT
// dropped our connection.
if err := conn.CloseWrite(); err != nil {
return err
}
logger.Printf("Server sent FIN, waiting %v seconds",
server.options.PostFinTimeoutSeconds)
<-time.After(time.Duration(server.options.PostFinTimeoutSeconds) * time.Second)
logger.Printf("Done")
return nil
}
// Client JSON options
type CloseWaitClientOptions struct {
// RemoteAddr of the server to connect to.
RemoteAddr string
// TimeoutSeconds on I/O with the server.
TimeoutSeconds int
// Half-close timeout (to give the test time to check the status of the
// conntrack table entry.
PostFinTimeoutSeconds int
// Leak connection (assign to global variable so connection persists
// as long as the process remains.
LeakConnection bool
}
type closeWaitClient struct {
options *CloseWaitClientOptions
}
// NewCloseWaitClient creates a new runner
func NewCloseWaitClient() common.Runner {
return &closeWaitClient{}
}
// NewOptions allocates new options structure.
func (client *closeWaitClient) NewOptions() interface{} {
return &CloseWaitClientOptions{}
}
// Run the client.m
func (client *closeWaitClient) Run(logger *log.Logger, rawOptions interface{}) error {
if options, ok := rawOptions.(*CloseWaitClientOptions); ok {
client.options = options
} else {
return errors.New("invalid type")
}
logger.Printf("Run %v", client.options)
addr, err := net.ResolveTCPAddr("tcp", client.options.RemoteAddr)
if err != nil {
return err
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return err
}
if !client.options.LeakConnection {
defer conn.Close()
}
logger.Printf("Connected to server")
if client.options.TimeoutSeconds > 0 {
delay := time.Duration(client.options.TimeoutSeconds) * time.Second
conn.SetReadDeadline(time.Now().Add(delay))
}
buf := make([]byte, 1, 1)
size, err := conn.Read(buf)
if err != nil && err != io.EOF {
return err
}
if size != 0 {
return errors.New("Got data but expected EOF")
}
logger.Printf("Server has half-closed the connection, waiting %v seconds",
client.options.PostFinTimeoutSeconds)
if client.options.LeakConnection {
logger.Printf("Leaking client connection (assigning to global variable)")
leakedConnection = conn
}
<-time.After(
time.Duration(client.options.PostFinTimeoutSeconds) * time.Second)
logger.Printf("Done")
return nil
}