@@ -257,6 +257,34 @@ impl<'a> TcpSocket<'a> {
257
257
Ok ( ( ) )
258
258
}
259
259
260
+ /// Close the transmit half of the full-duplex connection.
261
+ ///
262
+ /// Note that there is no corresponding function for the receive half of the full-duplex
263
+ /// connection; only the remote end can close it. If you no longer wish to receive any
264
+ /// data and would like to reuse the socket right away, use [abort](#method.abort).
265
+ pub fn close ( & mut self ) {
266
+ match self . state {
267
+ // In the LISTEN state there is no established connection.
268
+ State :: Listen =>
269
+ self . set_state ( State :: Closed ) ,
270
+ // In the SYN_SENT state the remote endpoint is not yet synchronized and, upon
271
+ // receiving an RST, will abort the connection.
272
+ State :: SynSent =>
273
+ self . set_state ( State :: Closed ) ,
274
+ // In the SYN_RECEIVED, ESTABLISHED and CLOSE_WAIT states the transmit half
275
+ // of the connection is open, and needs to be explicitly closed with a FIN.
276
+ State :: SynReceived | State :: Established =>
277
+ self . set_state ( State :: FinWait1 ) ,
278
+ State :: CloseWait =>
279
+ self . set_state ( State :: LastAck ) ,
280
+ // In the FIN_WAIT_1, FIN_WAIT_2, CLOSING, LAST_ACK, TIME_WAIT and CLOSED states,
281
+ // the transmit half of the connection is already closed, and no further
282
+ // action is needed.
283
+ State :: FinWait1 | State :: FinWait2 | State :: Closing |
284
+ State :: TimeWait | State :: LastAck | State :: Closed => ( )
285
+ }
286
+ }
287
+
260
288
/// Return whether the socket is open.
261
289
///
262
290
/// This function returns true if the socket will process incoming or dispatch outgoing
@@ -854,14 +882,21 @@ mod test {
854
882
#[ test]
855
883
fn test_closed ( ) {
856
884
let mut s = socket ( ) ;
857
- assert_eq ! ( s. state( ) , State :: Closed ) ;
885
+ assert_eq ! ( s. state, State :: Closed ) ;
858
886
859
887
send ! ( s, TcpRepr {
860
888
control: TcpControl :: Syn ,
861
889
..SEND_TEMPL
862
890
} , Err ( Error :: Rejected ) ) ;
863
891
}
864
892
893
+ #[ test]
894
+ fn test_closed_close ( ) {
895
+ let mut s = socket ( ) ;
896
+ s. close ( ) ;
897
+ assert_eq ! ( s. state, State :: Closed ) ;
898
+ }
899
+
865
900
// =========================================================================================//
866
901
// Tests for the LISTEN state.
867
902
// =========================================================================================//
@@ -895,6 +930,13 @@ mod test {
895
930
} ] ) ;
896
931
}
897
932
933
+ #[ test]
934
+ fn test_listen_close ( ) {
935
+ let mut s = socket_listen ( ) ;
936
+ s. close ( ) ;
937
+ assert_eq ! ( s. state, State :: Closed ) ;
938
+ }
939
+
898
940
// =========================================================================================//
899
941
// Tests for the SYN_RECEIVED state.
900
942
// =========================================================================================//
@@ -922,6 +964,13 @@ mod test {
922
964
assert_eq ! ( s. remote_endpoint, IpEndpoint :: default ( ) ) ;
923
965
}
924
966
967
+ #[ test]
968
+ fn test_syn_received_close ( ) {
969
+ let mut s = socket_syn_received ( ) ;
970
+ s. close ( ) ;
971
+ assert_eq ! ( s. state, State :: FinWait1 ) ;
972
+ }
973
+
925
974
// =========================================================================================//
926
975
// Tests for the SYN_SENT state.
927
976
// =========================================================================================//
@@ -970,6 +1019,13 @@ mod test {
970
1019
assert_eq ! ( s. state, State :: SynSent ) ;
971
1020
}
972
1021
1022
+ #[ test]
1023
+ fn test_syn_sent_close ( ) {
1024
+ let mut s = socket ( ) ;
1025
+ s. close ( ) ;
1026
+ assert_eq ! ( s. state, State :: Closed ) ;
1027
+ }
1028
+
973
1029
// =========================================================================================//
974
1030
// Tests for the ESTABLISHED state.
975
1031
// =========================================================================================//
@@ -1170,6 +1226,64 @@ mod test {
1170
1226
assert_eq ! ( s. state, State :: Closed ) ;
1171
1227
}
1172
1228
1229
+ #[ test]
1230
+ fn test_established_close ( ) {
1231
+ let mut s = socket_established ( ) ;
1232
+ s. close ( ) ;
1233
+ assert_eq ! ( s. state, State :: FinWait1 ) ;
1234
+ }
1235
+
1236
+ // =========================================================================================//
1237
+ // Tests for the FIN_WAIT_1 state.
1238
+ // =========================================================================================//
1239
+ fn socket_fin_wait_1 ( ) -> TcpSocket < ' static > {
1240
+ let mut s = socket_established ( ) ;
1241
+ s. state = State :: FinWait1 ;
1242
+ s
1243
+ }
1244
+
1245
+ #[ test]
1246
+ fn test_fin_wait_1_close ( ) {
1247
+ let mut s = socket_fin_wait_1 ( ) ;
1248
+ s. close ( ) ;
1249
+ assert_eq ! ( s. state, State :: FinWait1 ) ;
1250
+ }
1251
+
1252
+ // =========================================================================================//
1253
+ // Tests for the FIN_WAIT_2 state.
1254
+ // =========================================================================================//
1255
+ fn socket_fin_wait_2 ( ) -> TcpSocket < ' static > {
1256
+ let mut s = socket_fin_wait_1 ( ) ;
1257
+ s. state = State :: FinWait2 ;
1258
+ s. local_seq_no = LOCAL_SEQ + 1 + 1 ;
1259
+ s
1260
+ }
1261
+
1262
+ #[ test]
1263
+ fn test_fin_wait_2_close ( ) {
1264
+ let mut s = socket_fin_wait_2 ( ) ;
1265
+ s. close ( ) ;
1266
+ assert_eq ! ( s. state, State :: FinWait2 ) ;
1267
+ }
1268
+
1269
+ // =========================================================================================//
1270
+ // Tests for the CLOSING state.
1271
+ // =========================================================================================//
1272
+ fn socket_closing ( ) -> TcpSocket < ' static > {
1273
+ let mut s = socket_fin_wait_1 ( ) ;
1274
+ s. state = State :: Closing ;
1275
+ s. remote_seq_no = REMOTE_SEQ + 1 + 1 ;
1276
+ s. remote_last_ack = REMOTE_SEQ + 1 + 1 ;
1277
+ s
1278
+ }
1279
+
1280
+ #[ test]
1281
+ fn test_closing_close ( ) {
1282
+ let mut s = socket_closing ( ) ;
1283
+ s. close ( ) ;
1284
+ assert_eq ! ( s. state, State :: Closing ) ;
1285
+ }
1286
+
1173
1287
// =========================================================================================//
1174
1288
// Tests for the CLOSE_WAIT state.
1175
1289
// =========================================================================================//
@@ -1198,6 +1312,47 @@ mod test {
1198
1312
} ] ) ;
1199
1313
}
1200
1314
1315
+ #[ test]
1316
+ fn test_close_wait_close ( ) {
1317
+ let mut s = socket_close_wait ( ) ;
1318
+ s. close ( ) ;
1319
+ assert_eq ! ( s. state, State :: LastAck ) ;
1320
+ }
1321
+
1322
+ // =========================================================================================//
1323
+ // Tests for the LAST_ACK state.
1324
+ // =========================================================================================//
1325
+ fn socket_last_ack ( ) -> TcpSocket < ' static > {
1326
+ let mut s = socket_close_wait ( ) ;
1327
+ s. state = State :: LastAck ;
1328
+ s
1329
+ }
1330
+
1331
+ #[ test]
1332
+ fn test_last_ack_close ( ) {
1333
+ let mut s = socket_last_ack ( ) ;
1334
+ s. close ( ) ;
1335
+ assert_eq ! ( s. state, State :: LastAck ) ;
1336
+ }
1337
+
1338
+ // =========================================================================================//
1339
+ // Tests for the TIME_WAIT state.
1340
+ // =========================================================================================//
1341
+ fn socket_time_wait ( ) -> TcpSocket < ' static > {
1342
+ let mut s = socket_fin_wait_2 ( ) ;
1343
+ s. state = State :: TimeWait ;
1344
+ s. remote_seq_no = REMOTE_SEQ + 1 + 1 ;
1345
+ s. remote_last_ack = REMOTE_SEQ + 1 + 1 ;
1346
+ s
1347
+ }
1348
+
1349
+ #[ test]
1350
+ fn test_time_wait_close ( ) {
1351
+ let mut s = socket_time_wait ( ) ;
1352
+ s. close ( ) ;
1353
+ assert_eq ! ( s. state, State :: TimeWait ) ;
1354
+ }
1355
+
1201
1356
// =========================================================================================//
1202
1357
// Tests for transitioning through multiple states.
1203
1358
// =========================================================================================//
0 commit comments