Skip to content

Commit

Permalink
add feature flag to allow duplicate clientid (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnpw authored Feb 14, 2023
1 parent d6f1063 commit 3c611f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions rumqttd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use-rustls = ["dep:tokio-rustls", "dep:rustls-pemfile", "dep:x509-parser"]
use-native-tls = ["dep:tokio-native-tls", "dep:x509-parser"]
websockets = ["dep:tokio-tungstenite", "dep:websocket-codec", "dep:tokio-util", "dep:futures-util"]
validate-tenant-prefix = []
allow-duplicate-clientid = []

[dev-dependencies]
pretty_env_logger = "0.4.0"
Expand Down
7 changes: 5 additions & 2 deletions rumqttd/src/link/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ impl<P: Protocol> RemoteLink<P> {
let client_id = connect.client_id.clone();
let clean_session = connect.clean_session;

// Don't allow empty client_id
if client_id.is_empty() {
if cfg!(feature = "allow-duplicate-clientid") {
if !clean_session && client_id.is_empty() {
return Err(Error::InvalidClientId);
}
} else if client_id.is_empty() {
return Err(Error::InvalidClientId);
}

Expand Down
22 changes: 12 additions & 10 deletions rumqttd/src/router/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,18 @@ impl Router {
let span = tracing::info_span!("incoming_connect", client_id);
let _guard = span.enter();

// Check if same client_id already exists and if so, replace it with this new connection
// ref: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718032

let connection_id = self.connection_map.get(&client_id);
if let Some(connection_id) = connection_id {
error!(
"Duplicate client_id, dropping previous connection with connection_id: {}",
connection_id
);
self.handle_disconnection(*connection_id, true);
if cfg!(not(feature = "allow-duplicate-clientid")) {
// Check if same client_id already exists and if so, replace it with this new connection
// ref: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718032

let connection_id = self.connection_map.get(&client_id);
if let Some(connection_id) = connection_id {
error!(
"Duplicate client_id, dropping previous connection with connection_id: {}",
connection_id
);
self.handle_disconnection(*connection_id, true);
}
}

if self.connections.len() >= self.config.max_connections {
Expand Down

0 comments on commit 3c611f6

Please sign in to comment.