Skip to content

Commit 91caa96

Browse files
fix: broadcast correct local network IP and format names
Previously, `mDNS` packets were erroneously broadcasting `0.0.0.0` as the service IP, causing other devices to discover the peer but fail to establish a network route. - Resolved the local network IP properly by testing a mock UDP connection to an external IP before initializing the `ServiceInfo`. - Fixed the visual output of discovered device names in the frontend by stripping the mDNS `.cherit._tcp.local` suffix in the discovery loop. Co-authored-by: Keshav-writes-code <95571677+Keshav-writes-code@users.noreply.github.com>
1 parent 25d3d6d commit 91caa96

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

apps/app/src-tauri/src/sync/commands.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub async fn start_sync_service(state: State<'_, AppSyncState>) -> Result<(), St
1313
let mut sync_state_lock = state.inner.write().await;
1414

1515
if sync_state_lock.is_none() {
16-
// Use a persistent random ID or generate one
17-
let my_id = "test-device-id-123".to_string(); // MVP: Needs persistent store
18-
let my_name = "User's Device".to_string();
16+
// Generate a random ID for the session to prevent collisions in MVP
17+
let my_id = crate::sync::pairing::generate_pin().await;
18+
let my_name = format!("Device-{}", my_id);
1919
let port = 8080; // Should find an available port dynamically
2020

2121
let sync_state = Arc::new(SyncState::new(my_id, my_name, port)?);

apps/app/src-tauri/src/sync/discovery.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ impl SyncState {
4040

4141
pub fn start_broadcasting(&self) -> Result<(), String> {
4242
let instance_name = format!("{}-{}", self.my_name, self.my_id);
43-
let my_ip = "0.0.0.0"; // For now broadcast on all interfaces
43+
44+
// Find the actual local IP address on the network instead of 0.0.0.0
45+
let my_ip = match std::net::UdpSocket::bind("0.0.0.0:0") {
46+
Ok(s) => match s.connect("8.8.8.8:80") {
47+
Ok(_) => s.local_addr().map(|a| a.ip().to_string()).unwrap_or_else(|_| "0.0.0.0".to_string()),
48+
Err(_) => "0.0.0.0".to_string(),
49+
},
50+
Err(_) => "0.0.0.0".to_string(),
51+
};
4452

4553
let mut properties = HashMap::new();
4654
properties.insert("id".to_string(), self.my_id.clone());
@@ -49,7 +57,7 @@ impl SyncState {
4957
SERVICE_TYPE,
5058
&instance_name,
5159
&format!("{}.local.", instance_name),
52-
my_ip,
60+
&my_ip,
5361
self.port,
5462
Some(properties),
5563
)
@@ -83,18 +91,28 @@ impl SyncState {
8391
}
8492

8593
if let Some(ip) = info.get_addresses().iter().next() {
94+
// Extract the clean name by removing the "-<id>._cherit._tcp.local." suffix
95+
let raw_fullname = info.get_fullname().to_string();
96+
let mut clean_name = raw_fullname.clone();
97+
if let Some(idx) = raw_fullname.find("._cherit") {
98+
clean_name = raw_fullname[..idx].to_string();
99+
}
100+
86101
let peer_info = PeerInfo {
87102
id: peer_id.to_string(),
88-
name: info.get_fullname().to_string(), // Need to parse name better later
103+
name: clean_name,
89104
ip: ip.to_string(),
90105
port: info.get_port(),
91-
is_paired: false, // Default to false until handshake
106+
is_paired: false,
92107
};
93108
let mut peers = state.peers.write().await;
94109
peers.insert(peer_id.to_string(), peer_info);
95110
}
96111
}
97112
}
113+
ServiceEvent::ServiceFound(_service_type, _fullname) => {
114+
// mdns-sd automatically resolves found services on the next broadcast packet
115+
}
98116
ServiceEvent::ServiceRemoved(_, fullname) => {
99117
// For now we need to iterate to find the peer to remove,
100118
// or better yet, store fullnames or resolve ids.

0 commit comments

Comments
 (0)