Skip to content

Commit 9cb41cc

Browse files
cursoragentlovasoa
andcommitted
Refactor: Improve SSRP error handling and logging
Co-authored-by: contact <[email protected]>
1 parent df7bfdf commit 9cb41cc

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

sqlx-core/src/mssql/connection/ssrp.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ pub(crate) async fn resolve_instance_port(server: &str, instance: &str) -> Resul
2828
request.extend_from_slice(instance.as_bytes());
2929
request.push(0);
3030

31-
let socket = UdpSocket::bind("0.0.0.0:0").await.map_err(|e| {
32-
err_protocol!("failed to bind UDP socket for SSRP: {}", e)
33-
})?;
31+
let socket = UdpSocket::bind("0.0.0.0:0")
32+
.await
33+
.map_err(|e| err_protocol!("failed to bind UDP socket for SSRP: {}", e))?;
3434

3535
log::debug!(
3636
"sending SSRP CLNT_UCAST_INST request to {}:{} for instance '{}'",
@@ -43,7 +43,12 @@ pub(crate) async fn resolve_instance_port(server: &str, instance: &str) -> Resul
4343
.send_to(&request, (server, SSRP_PORT))
4444
.await
4545
.map_err(|e| {
46-
err_protocol!("failed to send SSRP request to {}:{}: {}", server, SSRP_PORT, e)
46+
err_protocol!(
47+
"failed to send SSRP request to {}:{}: {}",
48+
server,
49+
SSRP_PORT,
50+
e
51+
)
4752
})?;
4853

4954
let mut buffer = [0u8; 1024];
@@ -97,7 +102,7 @@ pub(crate) async fn resolve_instance_port(server: &str, instance: &str) -> Resul
97102

98103
let response_bytes = &buffer[3..(3 + response_size)];
99104
let (response_str, _encoding_used, had_errors) = WINDOWS_1252.decode(response_bytes);
100-
105+
101106
if had_errors {
102107
log::debug!("SSRP response had MBCS decoding errors, continuing anyway");
103108
}
@@ -114,17 +119,17 @@ fn parse_ssrp_response(data: &str, instance_name: &str) -> Result<u16, Error> {
114119
}
115120

116121
let info = parse_instance_info(instance_data);
117-
122+
118123
if let Some(name) = info.instance_name {
119124
log::debug!("found instance '{}' in SSRP response", name);
120-
125+
121126
if name.eq_ignore_ascii_case(instance_name) {
122127
log::debug!(
123128
"instance '{}' matches requested instance '{}'",
124129
name,
125130
instance_name
126131
);
127-
132+
128133
if let Some(tcp_port_str) = info.tcp_port {
129134
let port = tcp_port_str.parse::<u16>().map_err(|e| {
130135
err_protocol!(
@@ -133,13 +138,9 @@ fn parse_ssrp_response(data: &str, instance_name: &str) -> Result<u16, Error> {
133138
e
134139
)
135140
})?;
136-
137-
log::debug!(
138-
"resolved instance '{}' to port {}",
139-
instance_name,
140-
port
141-
);
142-
141+
142+
log::debug!("resolved instance '{}' to port {}", instance_name, port);
143+
143144
return Ok(port);
144145
} else {
145146
return Err(err_protocol!(
@@ -169,7 +170,7 @@ fn parse_instance_info<'a>(data: &'a str) -> InstanceInfo<'a> {
169170
let mut tokens = data.split(';');
170171
while let Some(key) = tokens.next() {
171172
let value = tokens.next();
172-
173+
173174
match key {
174175
"ServerName" => info.server_name = value,
175176
"InstanceName" => info.instance_name = value,
@@ -217,7 +218,8 @@ mod tests {
217218

218219
#[test]
219220
fn test_parse_ssrp_response_no_tcp_port() {
220-
let data = "ServerName;MYSERVER;InstanceName;SQLEXPRESS;IsClustered;No;Version;15.0.2000.5;;";
221+
let data =
222+
"ServerName;MYSERVER;InstanceName;SQLEXPRESS;IsClustered;No;Version;15.0.2000.5;;";
221223
let result = parse_ssrp_response(data, "SQLEXPRESS");
222224
assert!(result.is_err());
223225
}

sqlx-core/src/mssql/connection/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl MssqlStream {
6060
log::debug!("establishing TCP connection to {}:{}", options.host, port);
6161
let tcp_stream = TcpStream::connect((&*options.host, port)).await?;
6262
log::debug!("TCP connection established to {}:{}", options.host, port);
63-
63+
6464
let wrapped_stream = TlsPreloginWrapper::new(tcp_stream);
6565
let inner = BufStream::new(MaybeTlsStream::Raw(wrapped_stream));
6666

0 commit comments

Comments
 (0)