@@ -5,7 +5,7 @@ use std::net::{Ipv4Addr, SocketAddrV4};
5
5
use super :: soap;
6
6
use crate :: errors:: { self , AddAnyPortError , AddPortError , GetExternalIpError , RemovePortError , RequestError } ;
7
7
8
- use crate :: common:: { self , parsing :: RequestReponse , messages , parsing} ;
8
+ use crate :: common:: { self , messages , parsing , parsing:: RequestReponse } ;
9
9
use crate :: PortMappingProtocol ;
10
10
11
11
/// This structure represents a gateway found by the search functions.
@@ -26,12 +26,7 @@ impl Gateway {
26
26
}
27
27
}
28
28
29
- async fn perform_request (
30
- & self ,
31
- header : & str ,
32
- body : & str ,
33
- ok : & str ,
34
- ) -> Result < RequestReponse , RequestError > {
29
+ async fn perform_request ( & self , header : & str , body : & str , ok : & str ) -> Result < RequestReponse , RequestError > {
35
30
let url = format ! ( "{}" , self ) ;
36
31
let text = soap:: send_async ( & url, soap:: Action :: new ( header) , body) . await ?;
37
32
parsing:: parse_response ( text, ok)
@@ -44,7 +39,8 @@ impl Gateway {
44
39
messages:: GET_EXTERNAL_IP_HEADER ,
45
40
& messages:: format_get_external_ip_message ( ) ,
46
41
"GetExternalIPAddressResponse" ,
47
- ) . await ;
42
+ )
43
+ . await ;
48
44
parsing:: parse_get_external_ip_response ( result)
49
45
}
50
46
@@ -66,7 +62,9 @@ impl Gateway {
66
62
) -> Result < SocketAddrV4 , AddAnyPortError > {
67
63
let description = description. to_owned ( ) ;
68
64
let ip = self . get_external_ip ( ) . await ?;
69
- let port = self . add_any_port ( protocol, local_addr, lease_duration, & description) . await ?;
65
+ let port = self
66
+ . add_any_port ( protocol, local_addr, lease_duration, & description)
67
+ . await ?;
70
68
Ok ( SocketAddrV4 :: new ( ip, port) )
71
69
}
72
70
@@ -112,13 +110,16 @@ impl Gateway {
112
110
& description,
113
111
) ,
114
112
"AddAnyPortMappingResponse" ,
115
- ) . await ;
113
+ )
114
+ . await ;
116
115
match parsing:: parse_add_any_port_mapping_response ( resp) {
117
116
Ok ( port) => Ok ( port) ,
118
117
Err ( None ) => {
119
118
// The router does not have the AddAnyPortMapping method.
120
119
// Fall back to using AddPortMapping with a random port.
121
- gateway. retry_add_random_port_mapping ( protocol, local_addr, lease_duration, & description) . await
120
+ gateway
121
+ . retry_add_random_port_mapping ( protocol, local_addr, lease_duration, & description)
122
+ . await
122
123
}
123
124
Err ( Some ( err) ) => Err ( err) ,
124
125
}
@@ -132,7 +133,10 @@ impl Gateway {
132
133
description : & str ,
133
134
) -> Result < u16 , AddAnyPortError > {
134
135
for _ in 0u8 ..20u8 {
135
- match self . add_random_port_mapping ( protocol, local_addr, lease_duration, & description) . await {
136
+ match self
137
+ . add_random_port_mapping ( protocol, local_addr, lease_duration, & description)
138
+ . await
139
+ {
136
140
Ok ( port) => return Ok ( port) ,
137
141
Err ( AddAnyPortError :: NoPortsAvailable ) => continue ,
138
142
e => return e,
@@ -152,14 +156,20 @@ impl Gateway {
152
156
let gateway = self . clone ( ) ;
153
157
154
158
let external_port = common:: random_port ( ) ;
155
- let res = self . add_port_mapping ( protocol, external_port, local_addr, lease_duration, & description) . await ;
156
-
159
+ let res = self
160
+ . add_port_mapping ( protocol, external_port, local_addr, lease_duration, & description)
161
+ . await ;
162
+
157
163
match res {
158
164
Ok ( _) => Ok ( external_port) ,
159
165
Err ( err) => match parsing:: convert_add_random_port_mapping_error ( err) {
160
166
Some ( err) => Err ( err) ,
161
- None => gateway. add_same_port_mapping ( protocol, local_addr, lease_duration, & description) . await
162
- }
167
+ None => {
168
+ gateway
169
+ . add_same_port_mapping ( protocol, local_addr, lease_duration, & description)
170
+ . await
171
+ }
172
+ } ,
163
173
}
164
174
}
165
175
@@ -171,10 +181,11 @@ impl Gateway {
171
181
description : & str ,
172
182
) -> Result < u16 , AddAnyPortError > {
173
183
let res = self
174
- . add_port_mapping ( protocol, local_addr. port ( ) , local_addr, lease_duration, description) . await ;
184
+ . add_port_mapping ( protocol, local_addr. port ( ) , local_addr, lease_duration, description)
185
+ . await ;
175
186
match res {
176
187
Ok ( _) => Ok ( local_addr. port ( ) ) ,
177
- Err ( err) => Err ( parsing:: convert_add_same_port_mapping_error ( err) )
188
+ Err ( err) => Err ( parsing:: convert_add_same_port_mapping_error ( err) ) ,
178
189
}
179
190
}
180
191
@@ -186,18 +197,18 @@ impl Gateway {
186
197
lease_duration : u32 ,
187
198
description : & str ,
188
199
) -> Result < ( ) , RequestError > {
189
- self
190
- . perform_request (
191
- messages:: ADD_PORT_MAPPING_HEADER ,
192
- & messages :: format_add_port_mapping_message (
193
- protocol ,
194
- external_port ,
195
- local_addr ,
196
- lease_duration ,
197
- description ,
198
- ) ,
199
- "AddPortMappingResponse" ,
200
- ) . await ?;
200
+ self . perform_request (
201
+ messages :: ADD_PORT_MAPPING_HEADER ,
202
+ & messages:: format_add_port_mapping_message (
203
+ protocol ,
204
+ external_port ,
205
+ local_addr ,
206
+ lease_duration ,
207
+ description ,
208
+ ) ,
209
+ "AddPortMappingResponse" ,
210
+ )
211
+ . await ?;
201
212
Ok ( ( ) )
202
213
}
203
214
@@ -220,39 +231,43 @@ impl Gateway {
220
231
return Err ( AddPortError :: InternalPortZeroInvalid ) ;
221
232
}
222
233
223
- let res = self . add_port_mapping ( protocol, external_port, local_addr, lease_duration, description) . await ;
234
+ let res = self
235
+ . add_port_mapping ( protocol, external_port, local_addr, lease_duration, description)
236
+ . await ;
224
237
if let Err ( err) = res {
225
238
return Err ( parsing:: convert_add_port_error ( err) ) ;
226
239
} ;
227
240
Ok ( ( ) )
228
241
}
229
242
230
243
/// Remove a port mapping.
231
- pub async fn remove_port (
232
- & self ,
233
- protocol : PortMappingProtocol ,
234
- external_port : u16 ,
235
- ) -> Result < ( ) , RemovePortError > {
244
+ pub async fn remove_port ( & self , protocol : PortMappingProtocol , external_port : u16 ) -> Result < ( ) , RemovePortError > {
236
245
let res = self
237
246
. perform_request (
238
247
messages:: DELETE_PORT_MAPPING_HEADER ,
239
248
& messages:: format_delete_port_message ( protocol, external_port) ,
240
249
"DeletePortMappingResponse" ,
241
- ) . await ;
250
+ )
251
+ . await ;
242
252
parsing:: parse_delete_port_mapping_response ( res)
243
253
}
244
254
245
255
/// Get one port mapping entry
246
- ///
256
+ ///
247
257
/// Gets one port mapping entry by its index.
248
258
/// Not all existing port mappings might be visible to this client.
249
259
/// If the index is out of bound, GetGenericPortMappingEntryError::SpecifiedArrayIndexInvalid will be returned
250
- pub async fn get_generic_port_mapping_entry ( & self , index : u32 ) -> Result < parsing:: PortMappingEntry , errors:: GetGenericPortMappingEntryError > {
251
- let result = self . perform_request (
252
- messages:: GET_GENERIC_PORT_MAPPING_ENTRY ,
253
- & messages:: formate_get_generic_port_mapping_entry_message ( index) ,
254
- "GetGenericPortMappingEntryResponse"
255
- ) . await ;
260
+ pub async fn get_generic_port_mapping_entry (
261
+ & self ,
262
+ index : u32 ,
263
+ ) -> Result < parsing:: PortMappingEntry , errors:: GetGenericPortMappingEntryError > {
264
+ let result = self
265
+ . perform_request (
266
+ messages:: GET_GENERIC_PORT_MAPPING_ENTRY ,
267
+ & messages:: formate_get_generic_port_mapping_entry_message ( index) ,
268
+ "GetGenericPortMappingEntryResponse" ,
269
+ )
270
+ . await ;
256
271
parsing:: parse_get_generic_port_mapping_entry ( result)
257
272
}
258
273
}
0 commit comments