@@ -17,6 +17,7 @@ use libp2p::tcp::TokioTcpConfig;
17
17
use libp2p:: websocket:: WsConfig ;
18
18
use libp2p:: yamux:: { WindowUpdateMode , YamuxConfig } ;
19
19
use libp2p:: { core, identity, noise, Multiaddr , PeerId , Transport , TransportError } ;
20
+ use log:: info;
20
21
use std:: sync:: Arc ;
21
22
use std:: time:: Duration ;
22
23
use std:: { fmt, io} ;
@@ -36,6 +37,8 @@ pub struct Config {
36
37
pub bootstrap_nodes : Vec < Multiaddr > ,
37
38
/// List of [`Multiaddr`] on which to listen for incoming connections.
38
39
pub listen_on : Vec < Multiaddr > ,
40
+ /// Fallback to random port if specified (or default) port is already occupied.
41
+ pub listen_on_fallback_to_random_port : bool ,
39
42
/// Adds a timeout to the setup and protocol upgrade process for all inbound and outbound
40
43
/// connections established through the transport.
41
44
pub timeout : Duration ,
@@ -98,6 +101,7 @@ impl Config {
98
101
keypair,
99
102
bootstrap_nodes : vec ! [ ] ,
100
103
listen_on : vec ! [ ] ,
104
+ listen_on_fallback_to_random_port : true ,
101
105
timeout : Duration :: from_secs ( 10 ) ,
102
106
identify,
103
107
kademlia,
@@ -129,6 +133,7 @@ pub async fn create(
129
133
Config {
130
134
keypair,
131
135
listen_on,
136
+ listen_on_fallback_to_random_port,
132
137
timeout,
133
138
identify,
134
139
kademlia,
@@ -198,8 +203,22 @@ pub async fn create(
198
203
} ) )
199
204
. build ( ) ;
200
205
201
- for addr in listen_on {
202
- swarm. listen_on ( addr) ?;
206
+ for mut addr in listen_on {
207
+ if let Err ( error) = swarm. listen_on ( addr. clone ( ) ) {
208
+ if !listen_on_fallback_to_random_port {
209
+ return Err ( error. into ( ) ) ;
210
+ }
211
+
212
+ let addr_string = addr. to_string ( ) ;
213
+ // Listen on random port if specified is already occupied
214
+ if let Some ( Protocol :: Tcp ( _port) ) = addr. pop ( ) {
215
+ info ! (
216
+ "Failed to listen on {addr_string} ({error}), falling back to random port"
217
+ ) ;
218
+ addr. push ( Protocol :: Tcp ( 0 ) ) ;
219
+ swarm. listen_on ( addr) ?;
220
+ }
221
+ }
203
222
}
204
223
205
224
Ok :: < _ , CreationError > ( swarm)
0 commit comments