I am working on a Rust project using the libp2p library to create a peer-to-peer network. I have configured my swarm to listen on all interfaces using the following code:
let listen_address_udp = format!("/ip4/0.0.0.0/udp/{}/quic-v1", port);
swarm.listen_on(listen_address_udp.parse()?)?;
let listen_address_tcp = format!("/ip4/0.0.0.0/tcp/{}", port);
swarm.listen_on(listen_address_tcp.parse()?)?;
However, when reading swarm events, I am unable to retrieve the external public address that my node is listening on. The code for reading swarm events is as follows:
loop {
select! {
_ = sig_term_handler.recv() => {
trigger_message = !trigger_message;
},
event = swarm.select_next_some() => match event {
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(gossipsub::Event::Message {
propagation_source: peer_id,
message_id: id,
message,
})) => {
println!(
"Received'{}' with id: {id} from peer: {peer_id}, Size :{}",
String::from_utf8_lossy(&message.data), message.data.len()
)
},
SwarmEvent::NewListenAddr { address, .. } => {
println!("Local node is listening on {address}");
}
_ => {}
}
}
}
The output I receive only shows the local addresses where my node is listening, such as:
Local node is listening on /ip4/127.0.0.1/tcp/8082
Local node is listening on /ip4/172.24.181.240/tcp/8082
I have tried pinging and opening port 8082 for TCP on all nodes, but I still cannot determine if my local node is publicly listening or not. What should be the external public address I expect to see in the SwarmEvent::NewListenAddr event? Any suggestions or insights into resolving this issue would be greatly appreciated.I have also attempted to fetch the public IP address for the node and then tried binding the node to the public IP address.