1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
use crate::{types::*, Error};
use byteorder::{BigEndian as BE, ReadBytesExt};
use std::io::{Cursor, Read};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
impl<'a> Message<'a> {
/// Parse a message from a byte slice.
pub fn from_slice(input: &'a [u8]) -> Result<Message<'a>, Error> {
match Header::from_slice(input) {
Ok(Header {
message_id:
MessageId {
service_id: 0xFFFF,
method_id: 0x0000,
},
length: 8,
request_id:
RequestId {
client_id: 0xDEAD,
session_id: 0xBEEF,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::RequestNoReturn,
return_code: ReturnCode::Ok,
}) => Ok(Message::CookieClient),
Ok(Header {
message_id:
MessageId {
service_id: 0xFFFF,
method_id: 0x8000,
},
length: 8,
request_id:
RequestId {
client_id: 0xDEAD,
session_id: 0xBEEF,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
}) => Ok(Message::CookieServer),
Ok(header) => {
let payload_len = header.payload_len();
if input.len() < Header::LENGTH + payload_len {
return Err(Error::NotEnoughData {
min: Header::LENGTH + payload_len,
actual: input.len(),
});
}
let payload = &input[Header::LENGTH..Header::LENGTH + payload_len];
if header.is_sd() {
let mut buffer = Cursor::new(payload);
Ok(Message::Sd(header, SdPayload::from_reader(&mut buffer)?))
} else {
Ok(Message::Rpc(header, payload))
}
}
Err(e) => Err(e),
}
}
}
impl Header {
/// Parse a header from a byte slice.
pub fn from_slice(input: &[u8]) -> Result<Header, Error> {
if input.len() < Header::LENGTH {
return Err(Error::NotEnoughData {
min: Header::LENGTH,
actual: input.len(),
});
}
let mut buffer = Cursor::new(input);
Header::from_reader(&mut buffer)
}
/// Parse a header from a `Read`
pub fn from_reader<R: Read>(reader: &mut R) -> Result<Header, Error> {
let service_id = reader.read_u16::<BE>()?;
let method_id = reader.read_u16::<BE>()?;
let length = reader.read_u32::<BE>()?;
let client_id = reader.read_u16::<BE>()?;
let session_id = reader.read_u16::<BE>()?;
let protocol_version = reader.read_u8()?;
let interface_version = reader.read_u8()?;
let message_type = reader.read_u8()?.try_into()?;
let return_code = reader.read_u8()?.try_into()?;
let header = Header {
message_id: MessageId {
service_id,
method_id,
},
length,
request_id: RequestId {
client_id,
session_id,
},
protocol_version,
interface_version,
message_type,
return_code,
};
if (header.is_sd() && length < 20) || (!header.is_sd() && length < 8) {
return Err(Error::InvalidLengthField(length));
}
Ok(header)
}
}
impl SdPayload {
/// Parse SD payload from a `Read`
pub fn from_reader<R: Read>(reader: &mut R) -> Result<SdPayload, Error> {
let flags = reader.read_u8()?;
reader.read_u24::<BE>()?; // reserved
let entries_len: usize = reader.read_u32::<BE>()? as usize;
let num_entries = entries_len / SdEntry::LENGTH;
let mut entries: Vec<SdEntry> = Vec::with_capacity(num_entries);
for _ in 0..num_entries {
entries.push(SdEntry::from_reader(reader)?);
}
let options_len: usize = reader.read_u32::<BE>()? as usize;
let mut options: Vec<SdOption> = Vec::new();
let mut read_len: usize = 0;
while read_len < options_len {
let (option_len, option) = SdOption::from_reader(reader)?;
if let Some(value) = option {
options.push(value);
}
read_len += option_len;
}
Ok(SdPayload {
flags,
entries,
options,
})
}
}
impl SdEntry {
fn from_reader<R: Read>(reader: &mut R) -> Result<SdEntry, Error> {
let entry_type = reader.read_u8()?;
let index1 = reader.read_u8()?;
let index2 = reader.read_u8()?;
let num = reader.read_u8()?;
let num1 = (num >> 4) & 0x0F;
let num2 = num & 0x0F;
let service_id = reader.read_u16::<BE>()?;
let instance_id = reader.read_u16::<BE>()?;
let major_version = reader.read_u8()?;
let ttl = reader.read_u24::<BE>()?;
let options = SdOptionRef {
index1,
index2,
num1,
num2,
};
if SdEntry::is_service(entry_type) {
let minor_version = reader.read_u32::<BE>()?;
Ok(SdEntry::try_from((
entry_type,
SdServiceEntry {
service_id,
instance_id,
major_version,
minor_version,
ttl,
options,
},
))?)
} else {
reader.read_u16::<BE>()?; // reserved
let eventgroup_id = reader.read_u16::<BE>()?;
Ok(SdEntry::try_from((
entry_type,
SdEventgroupEntry {
service_id,
eventgroup_id,
instance_id,
major_version,
ttl,
options,
},
))?)
}
}
}
impl SdOption {
fn from_reader<R: Read>(reader: &mut R) -> Result<(usize, Option<SdOption>), Error> {
let option_len: usize = reader.read_u16::<BE>()? as usize;
let option_type = reader.read_u8()?;
if !SdOption::is_ip_option(option_type) {
let mut buffer: Vec<u8> = vec![0; option_len];
reader.read_exact(&mut buffer)?;
return Ok((3 + option_len, None)); // drop option
}
reader.read_u8()?; // reserved
let ip = if SdOption::is_ip4_option(option_type) {
let mut buffer = [0u8; 4];
reader.read_exact(&mut buffer)?;
IpAddr::V4(Ipv4Addr::from(buffer))
} else {
let mut buffer = [0u8; 16];
reader.read_exact(&mut buffer)?;
IpAddr::V6(Ipv6Addr::from(buffer))
};
reader.read_u8()?; // reserved
let proto = IpProto::try_from(reader.read_u8()?)?;
let port = reader.read_u16::<BE>()?;
let option = SdOption::from(option_type, SdEndpointOption { ip, port, proto })?;
Ok((option.len(), Some(option)))
}
}
#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;
#[test]
fn parse_cookie_client() {
let header: &[u8] = &[
0xFF, 0xFF, 0x00, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x08, // length(u32)
0xDE, 0xAD, 0xBE, 0xEF, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x01, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
assert_eq!(Message::from_slice(header).unwrap(), Message::CookieClient);
}
#[test]
fn parse_cookie_server() {
let header: &[u8] = &[
0xFF, 0xFF, 0x80, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x08, // length(u32)
0xDE, 0xAD, 0xBE, 0xEF, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
assert_eq!(Message::from_slice(header).unwrap(), Message::CookieServer);
}
#[test]
fn parse_empty_rpc_message() {
let header: &[u8] = &[
0x01, 0x03, 0x80, 0x05, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x08, // length(u32)
0x00, 0x01, 0x00, 0x02, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
assert_eq!(
Message::from_slice(header).unwrap(),
Message::Rpc(
Header {
message_id: MessageId {
service_id: 0x0103,
method_id: 0x8005,
},
length: 8,
request_id: RequestId {
client_id: 0x0001,
session_id: 0x0002,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
&[],
)
);
}
#[test]
fn parse_rpc_message() {
let header: &[u8] = &[
0x01, 0x03, 0x80, 0x05, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x0D, // length(u32)
0x00, 0x01, 0x00, 0x02, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
let payload: &[u8] = &[0x01, 0x02, 0x03, 0x04, 0x05];
let message: &[u8] = &[header, payload].concat();
assert_eq!(
Message::from_slice(message).unwrap(),
Message::Rpc(
Header {
message_id: MessageId {
service_id: 0x0103,
method_id: 0x8005,
},
length: 13,
request_id: RequestId {
client_id: 0x0001,
session_id: 0x0002,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
payload,
)
);
}
#[test]
fn parse_empty_sd_message() {
let header: &[u8] = &[
0xFF, 0xFF, 0x81, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x14, // length(u32)
0x00, 0x00, 0x00, 0x00, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
let payload: &[u8] = &[
0xC0, 0x00, 0x00, 0x00, // sdFlags(08), reserved(u24)
0x00, 0x00, 0x00, 0x00, // entries-length(u32)
0x00, 0x00, 0x00, 0x00, // options-length(u32)
];
let message: &[u8] = &[header, payload].concat();
assert_eq!(
Message::from_slice(message).unwrap(),
Message::Sd(
Header {
message_id: MessageId {
service_id: 0xFFFF,
method_id: 0x8100,
},
length: 20,
request_id: RequestId {
client_id: 0x0000,
session_id: 0x0000,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
SdPayload {
flags: 0xC0,
entries: vec![],
options: vec![],
},
)
);
}
#[test]
fn parse_find_service_sd_message() {
let header: &[u8] = &[
0xFF, 0xFF, 0x81, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x2C, // length(u32)
0x00, 0x00, 0x00, 0x00, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
let payload: &[u8] = &[
0xC0, 0x00, 0x00, 0x00, // sdFlags(08), reserved(u24)
// entries
0x00, 0x00, 0x00, 0x10, // entries-length(u32)
// find-service
0x00, 0x00, 0x00, 0x00, // entryType(u8), index1(u8), index2,(u8) num1|2(u8)
0x01, 0x03, 0x00, 0x01, // serviceId(u16), instanceId(u16)
0x02, 0x00, 0x00, 0x03, // majorVersion(u8), ttl(u24)
0x00, 0x00, 0x00, 0x0A, // minorVersion(u32)
// options
0x00, 0x00, 0x00, 0x08, // options-length(u32)
// unrelated option (dropped)
0x00, 0x05, 0x01, 0x00, // length(u16), optionType(u8), reserved(u8)
0x66, 0x6F, 0x6F, 0x00, // config(String)
];
let message: &[u8] = &[header, payload].concat();
assert_eq!(
Message::from_slice(message).unwrap(),
Message::Sd(
Header {
message_id: MessageId {
service_id: 0xFFFF,
method_id: 0x8100,
},
length: 44,
request_id: RequestId {
client_id: 0x0000,
session_id: 0x0000,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
SdPayload {
flags: 0xC0,
entries: vec![SdEntry::FindService(SdServiceEntry {
service_id: 0x0103,
instance_id: 0x0001,
major_version: 0x02,
minor_version: 0x0000000A,
ttl: 0x00000003,
options: SdOptionRef {
index1: 0,
index2: 0,
num1: 0,
num2: 0,
},
})],
options: vec![],
},
)
);
}
#[test]
fn parse_offer_service_sd_message() {
let header: &[u8] = &[
0xFF, 0xFF, 0x81, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x48, // length(u32)
0x00, 0x00, 0x00, 0x00, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
let payload: &[u8] = &[
0xC0, 0x00, 0x00, 0x00, // sdFlags(08), reserved(u24)
// entries
0x00, 0x00, 0x00, 0x10, // entries-length(u32)
// offer-service
0x01, 0x00, 0x00, 0x20, // entryType(u8), index1(u8), index2,(u8) num1|2(u8)
0x01, 0x03, 0x00, 0x01, // serviceId(u16), instanceId(u16)
0x02, 0x00, 0x00, 0x03, // majorVersion(u8), ttl(u24)
0x00, 0x00, 0x00, 0x0A, // minorVersion(u32)
// options
0x00, 0x00, 0x00, 0x24, // options-length(u32)
// ip-4 endpoint
0x00, 0x09, 0x04, 0x00, // length(u16), optionType(u8), reserved(u8)
0x7F, 0x00, 0x00, 0x01, // ip4(u32)
0x00, 0x11, 0x75, 0x30, // reserved(u8), proto(u8), port(u16)
// ip-6 endpoint
0x00, 0x15, 0x06, 0x00, // length(u16), optionType(u8), reserved(u8)
0xFF, 0x0E, 0x00, 0x00, // ip6(u32)
0x00, 0x00, 0x00, 0x00, // ip6(u32)
0x00, 0x00, 0xFF, 0xFF, // ip6(u32)
0xEF, 0xC0, 0xFF, 0xFB, // ip6(u32)
0x00, 0x06, 0x75, 0x30, // reserved(u8), proto(u8), port(u16)
];
let message: &[u8] = &[header, payload].concat();
assert_eq!(
Message::from_slice(message).unwrap(),
Message::Sd(
Header {
message_id: MessageId {
service_id: 0xFFFF,
method_id: 0x8100,
},
length: 72,
request_id: RequestId {
client_id: 0x0000,
session_id: 0x0000,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
SdPayload {
flags: 0xC0,
entries: vec![SdEntry::OfferService(SdServiceEntry {
service_id: 0x0103,
instance_id: 0x0001,
major_version: 0x02,
minor_version: 0x0000000A,
ttl: 0x00000003,
options: SdOptionRef {
index1: 0,
index2: 0,
num1: 2,
num2: 0,
},
})],
options: vec![
SdOption::Ip4Unicast(SdEndpointOption {
ip: IpAddr::V4(Ipv4Addr::from_str("127.0.0.1").unwrap()),
port: 30000,
proto: IpProto::UDP,
}),
SdOption::Ip6Unicast(SdEndpointOption {
ip: IpAddr::V6(
Ipv6Addr::from_str("FF0E:0000:0000:0000:0000:FFFF:EFC0:FFFB")
.unwrap()
),
port: 30000,
proto: IpProto::TCP,
}),
],
},
)
);
}
#[test]
fn parse_subscribe_eventgroup_and_ack_sd_message() {
let header: &[u8] = &[
0xFF, 0xFF, 0x81, 0x00, // serviceId(u16), methodId(u16)
0x00, 0x00, 0x00, 0x48, // length(u32)
0x00, 0x00, 0x00, 0x00, // clientId(u16), sessionId(u16)
0x01, 0x01, 0x02, 0x00, // proto(u8), version(u8), messageType,(u8) returnCode(u8)
];
let payload: &[u8] = &[
0xC0, 0x00, 0x00, 0x00, // sdFlags(08), reserved(u24)
// entries
0x00, 0x00, 0x00, 0x20, // entries-length(u32)
// subscribe-eventgroup
0x06, 0x01, 0x00, 0x10, // entryType(u8), index1(u8), index2,(u8) num1|2(u8)
0x01, 0x03, 0x00, 0x01, // serviceId(u16), instanceId(u16)
0x02, 0x00, 0x00, 0x03, // majorVersion(u8), ttl(u24)
0x00, 0x00, 0x01, 0xC8, // reserved(u16), eventgroupId(u16)
// subscribe-eventgroup-ack
0x07, 0x00, 0x01, 0x01, // entryType(u8), index1(u8), index2,(u8) num1|2(u8)
0x01, 0x03, 0x00, 0x01, // serviceId(u16), instanceId(u16)
0x02, 0x00, 0x00, 0x03, // majorVersion(u8), ttl(u24)
0x00, 0x00, 0x01, 0xC8, // reserved(u16), eventgroupId(u16)
// options
0x00, 0x00, 0x00, 0x14, // options-length(u32)
// unrelated option (dropped)
0x00, 0x05, 0x01, 0x00, // length(u16), optionType(u8), reserved(u8)
0x66, 0x6F, 0x6F, 0x00, // config(String)
// ip-4 endpoint
0x00, 0x09, 0x04, 0x00, // length(u16), optionType(u8), reserved(u8)
0x7F, 0x00, 0x00, 0x01, // ip4(u32)
0x00, 0x11, 0x75, 0x30, // reserved(u8), proto(u8), port(u16)
];
let message: &[u8] = &[header, payload].concat();
assert_eq!(
Message::from_slice(message).unwrap(),
Message::Sd(
Header {
message_id: MessageId {
service_id: 0xFFFF,
method_id: 0x8100,
},
length: 72,
request_id: RequestId {
client_id: 0x0000,
session_id: 0x0000,
},
protocol_version: 0x01,
interface_version: 0x01,
message_type: MessageType::Notification,
return_code: ReturnCode::Ok,
},
SdPayload {
flags: 0xC0,
entries: vec![
SdEntry::SubscribeEventgroup(SdEventgroupEntry {
service_id: 0x0103,
eventgroup_id: 0x01C8,
instance_id: 0x0001,
major_version: 0x02,
ttl: 0x00000003,
options: SdOptionRef {
index1: 1,
index2: 0,
num1: 1,
num2: 0,
},
}),
SdEntry::SubscribeEventgroupAck(SdEventgroupEntry {
service_id: 0x0103,
eventgroup_id: 0x01C8,
instance_id: 0x0001,
major_version: 0x02,
ttl: 0x00000003,
options: SdOptionRef {
index1: 0,
index2: 1,
num1: 0,
num2: 1,
},
})
],
options: vec![SdOption::Ip4Unicast(SdEndpointOption {
ip: IpAddr::V4(Ipv4Addr::from_str("127.0.0.1").unwrap()),
port: 30000,
proto: IpProto::UDP,
})],
},
)
);
}
}