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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
#![warn(missing_docs)] //! `Mles utils` library is provided for Mles client and server implementations for easy handling of //! proper header and message structures. /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright (C) 2017-2018 Mles developers * */ #[macro_use] extern crate serde_derive; extern crate serde_bytes; extern crate serde_cbor; mod frame; mod local_db; mod peer; mod server; use siphasher::sip::SipHasher; use std::hash::{Hash, Hasher}; use std::io::Cursor; use std::io::Write; use std::io::{Error, Read}; use std::net::TcpStream; use bytes::{Buf, BufMut, BytesMut}; use std::net::{IpAddr, SocketAddr}; /// HDRL defines the size of the header including version, length and timestamp pub(crate) const HDRL: usize = 8; /// CIDL defines the size of the connection id pub(crate) const CIDL: usize = 4; /// KEYL defines the size of the key pub(crate) const KEYL: usize = 8; /// HDRKEYL defines the size of the header + key pub(crate) const HDRKEYL: usize = HDRL + KEYL; /// Max message size pub(crate) const MSGMAXSIZE: usize = 0xffffff; const KEEPALIVE: u64 = 5; /// MsgHdr structure /// /// This structure defines the header of the Mles message including first 'M' byte, /// length of the encoded data, connection id and SipHash key. /// Encoded message will always be in network byte order. /// pub struct MsgHdr { thlen: u32, cid: u32, key: u64, } impl MsgHdr { /// Create a new MsgHdr object with length, cid and key. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let msghdr = MsgHdr::new(len, cid, key); /// ``` pub fn new(len: u32, cid: u32, key: u64) -> MsgHdr { MsgHdr { thlen: hdr_set_len(len), cid, key, } } /// Get type of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.get_type(); /// assert_eq!('M' as u8, msghdr.get_type()); /// ``` pub fn get_type(&self) -> u8 { hdr_get_type(self.thlen) } /// Get MsgHdr length on the line. /// pub fn get_hdrkey_len() -> usize { HDRKEYL } /// Set length of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_len(515); /// ``` pub fn set_len(&mut self, len: u32) { self.thlen = hdr_set_len(len); } /// Get length of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_len(515); /// assert_eq!(515, msghdr.get_len()); /// ``` pub fn get_len(&self) -> u32 { hdr_get_len(self.thlen) } /// Set cid of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_cid(515); /// ``` pub fn set_cid(&mut self, cid: u32) { self.cid = cid; } /// Get cid of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_cid(515); /// assert_eq!(515, msghdr.get_cid()); /// ``` pub fn get_cid(&self) -> u32 { self.cid } /// Set key of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_key(515); /// ``` pub fn set_key(&mut self, key: u64) { self.key = key; } /// Get key of MsgHdr. /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// msghdr.set_key(515); /// assert_eq!(515, msghdr.get_key()); /// ``` pub fn get_key(&self) -> u64 { self.key } /// Encode MsgHdr to line format. /// /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 0; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// let msgv: Vec<u8> = msghdr.encode(); /// ``` pub fn encode(&self) -> Vec<u8> { let mut msgv = write_hdr(self.get_len() as usize, self.get_cid()).to_vec(); msgv.extend(write_key(self.get_key()).to_vec()); msgv } /// Decode MsgHdr from line format. /// /// /// # Example /// ``` /// use mles_utils::{MsgHdr}; /// /// let key = 0xf00f; /// let cid = MsgHdr::select_cid(key); /// let len = 16; /// /// let mut msghdr = MsgHdr::new(len, cid, key); /// let msgv: Vec<u8> = msghdr.encode(); /// let msgh = MsgHdr::decode(msgv); /// assert_eq!(key, msgh.get_key()); /// assert_eq!(cid, msgh.get_cid()); /// assert_eq!(len, msgh.get_len()); /// ``` pub fn decode(buf: Vec<u8>) -> MsgHdr { MsgHdr::new( read_hdr_len(&buf) as u32, read_cid_from_hdr(&buf), read_key_from_hdr(&buf), ) } /// Do a valid hash for Mles over provided UTF-8 String list. /// /// # Example /// ``` /// use mles_utils::MsgHdr; /// /// let hashstr1 = "A string".to_string(); /// let hashstr2 = "Another string".to_string(); /// let hashable = vec![hashstr1, hashstr2]; /// let key: u64 = MsgHdr::do_hash(&hashable); /// ``` #[inline] pub fn do_hash(t: &[String]) -> u64 { let mut s = SipHasher::new(); for item in t { item.hash(&mut s); } s.finish() } /// Return a connection id from key. /// /// # Example /// ``` /// use mles_utils::MsgHdr; /// /// let cid = MsgHdr::select_cid(0x1000000100000001); /// assert_eq!(cid, 0x00000001); /// ``` #[inline] pub fn select_cid(key: u64) -> u32 { key as u32 } /// Do a valid UTF-8 string from a `SocketAddr`. /// /// For IPv4 the format is "x.x.x.x:y", where x is u8 and y is u16 /// For IPv6 the format is "[z:z:z:z:z:z:z:z]:y", where z is u16 in hexadecimal format and y is u16 /// /// # Example /// ``` /// /// use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr}; /// use mles_utils::MsgHdr; /// /// let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// let addrstr = MsgHdr::addr2str(&addr); /// /// assert_eq!("127.0.0.1:8080", addrstr); /// /// let addr = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0xff03, 0, 0, 0, 0, 0, 0, 1)), 8077); /// let addrstr = MsgHdr::addr2str(&addr); /// /// assert_eq!("[ff03:0:0:0:0:0:0:1]:8077", addrstr); /// ``` #[inline] pub fn addr2str(addr: &SocketAddr) -> String { let ipaddr = addr.ip(); match ipaddr { IpAddr::V4(v4) => { let v4oct = v4.octets(); let v4str = format!( "{}.{}.{}.{}:{}", v4oct[0], v4oct[1], v4oct[2], v4oct[3], addr.port() ); v4str } IpAddr::V6(v6) => { let v6seg = v6.segments(); let v6str = format!( "[{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}]:{}", v6seg[0], v6seg[1], v6seg[2], v6seg[3], v6seg[4], v6seg[5], v6seg[6], v6seg[7], addr.port() ); v6str } } } } fn hdr_set_len(len: u32) -> u32 { 77 << 24 | len & 0xffffff } fn hdr_get_len(thlen: u32) -> u32 { thlen & 0xffffff } fn hdr_get_type(thlen: u32) -> u8 { (thlen >> 24) as u8 } /// Msg structure /// /// This structure defines the Mles interface value triplet (uid, channel, message). /// It is eventually serialized and deserialized by CBOR. /// #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Msg { uid: String, channel: String, #[serde(with = "serde_bytes")] message: Vec<u8>, } impl Msg { /// Create a new Msg object with value triplet. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// ``` #[inline] pub fn new(uid: String, channel: String, message: Vec<u8>) -> Msg { Msg { uid, channel, message, } } /// Set uid for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.set_uid("New uid".to_string()); /// /// assert_eq!("New uid".to_string(), *msg.get_uid()); /// ``` #[inline] pub fn set_uid(mut self, uid: String) -> Msg { self.uid = uid; self } /// Set channel for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.set_channel("New channel".to_string()); /// /// assert_eq!("New channel".to_string(), *msg.get_channel()); /// ``` #[inline] pub fn set_channel(mut self, channel: String) -> Msg { self.channel = channel; self } /// Set message for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let new_message: Vec<u8> = "New message".to_string().into_bytes(); /// let msg = msg.set_message(new_message); /// ``` #[inline] pub fn set_message(mut self, message: Vec<u8>) -> Msg { self.message = message; self } /// Get uid for Msg object. See example for set uid. #[inline] pub fn get_uid(&self) -> &String { &self.uid } /// Get channel for Msg object. See example for set channel. #[inline] pub fn get_channel(&self) -> &String { &self.channel } /// Get message for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg: &Vec<u8> = msg.get_message(); /// ``` #[inline] pub fn get_message(&self) -> &Vec<u8> { &self.message } /// Get message len for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg_len: usize = msg.get_message_len(); /// ``` #[inline] pub fn get_message_len(&self) -> usize { self.message.len() } /// Get mutable message reference for Msg object. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let mut msg = Msg::new("My uid".to_string(), "My channel".to_string(), "My /// message".to_string().into_bytes()); /// let message = msg.get_mut_message(); /// message.extend_from_slice(&" is mutable".to_string().into_bytes()); /// ``` #[inline] pub fn get_mut_message(&mut self) -> &mut Vec<u8> { &mut self.message } /// Encode Msg object to CBOR. /// /// # Errors /// If message cannot be encoded, an empty vector is returned. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let encoded_msg: Vec<u8> = msg.encode(); /// ``` #[inline] pub fn encode(&self) -> Vec<u8> { let encoded = serde_cbor::to_vec(self); match encoded { Ok(encoded) => encoded, Err(err) => { println!("Error on encode: {}", err); Vec::new() } } } /// Decode CBOR byte string to Msg object. /// /// # Errors /// If message cannot be decoded, a Msg structure with empty items is returned. /// /// # Example /// ``` /// use mles_utils::Msg; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let encoded_msg: Vec<u8> = msg.encode(); /// let decoded_msg: Msg = Msg::decode(&encoded_msg); /// ``` #[inline] pub fn decode(slice: &[u8]) -> Msg { let value = serde_cbor::from_slice(slice); match value { Ok(value) => value, Err(err) => { println!("Error on decode: {}", err); Msg { uid: "".to_string(), channel: "".to_string(), message: Vec::new(), } // return empty vec in case of error } } } } #[derive(Serialize, Deserialize, Debug, Clone)] struct MsgVec { #[serde(with = "serde_bytes")] encoded_msg: Vec<u8>, // this is encoded Msg } impl MsgVec { pub fn new(encoded_msg: &Vec<u8>) -> MsgVec { MsgVec { encoded_msg: encoded_msg.clone(), } } pub fn get(&self) -> &Vec<u8> { &self.encoded_msg } } /// ResyncMsg structure /// /// This structure defines resynchronization Msg structure that can be used /// to resynchronize history state to root server from peers. The resynchronization /// message can be sent only during initial connection message and packs the /// history into one message that can be taken into account by Mles root server. /// #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ResyncMsg { resync_message: Vec<MsgVec>, } impl ResyncMsg { /// Create a new ResyncMsg object with encoded message vector. /// /// # Example /// ``` /// use mles_utils::{Msg, ResyncMsg}; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.encode(); /// let vec = vec![msg]; /// let rmsg = ResyncMsg::new(&vec); /// ``` #[inline] pub fn new(messages: &Vec<Vec<u8>>) -> ResyncMsg { let mut rmsg = ResyncMsg { resync_message: Vec::new(), }; //transform to correct format for msg in messages { rmsg.resync_message.push(MsgVec::new(&msg)); } rmsg } /// Get the length of the resync message vector /// /// # Example /// ``` /// use mles_utils::{Msg, ResyncMsg}; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.encode(); /// let vec = vec![msg]; /// let rmsg = ResyncMsg::new(&vec); /// assert_eq!(1, rmsg.len()); /// ``` #[inline] pub fn len(&self) -> usize { self.resync_message.len() } /// Get all items of the resync message vector /// /// # Example /// ``` /// use mles_utils::{Msg, ResyncMsg}; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.encode(); /// let vec = vec![msg]; /// let rmsg = ResyncMsg::new(&vec); /// let rvec = rmsg.get_messages(); /// assert_eq!(vec[0], rvec[0]); /// ``` #[inline] pub fn get_messages(&self) -> Vec<Vec<u8>> { //transform to correct format let mut messages = Vec::new(); for msg in self.resync_message.iter() { let msg = msg.get(); messages.push(msg.clone()); } messages } /// Encode ResyncMsg object to CBOR. /// /// # Errors /// If resync message cannot be encoded, an empty vector is returned. /// /// # Example /// ``` /// use mles_utils::{ResyncMsg, Msg}; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.encode(); /// let vec = vec![msg]; /// let rmsg = ResyncMsg::new(&vec); /// let encoded_msg: Vec<u8> = rmsg.encode(); /// ``` #[inline] pub fn encode(&self) -> Vec<u8> { let encoded = serde_cbor::to_vec(self); match encoded { Ok(encoded) => encoded, Err(err) => { println!("Error on resync encode: {}", err); Vec::new() } } } /// Decode CBOR byte string to ResyncMsg object. /// /// # Errors /// If message cannot be decoded, a ResyncMsg structure with empty items is returned. /// /// # Example /// ``` /// use mles_utils::{ResyncMsg, Msg}; /// /// let msg = Msg::new("My uid".to_string(), "My channel".to_string(), Vec::new()); /// let msg = msg.encode(); /// let vec = vec![msg]; /// let rmsg = ResyncMsg::new(&vec); /// let encoded_msg: Vec<u8> = rmsg.encode(); /// let decoded_msg: ResyncMsg = ResyncMsg::decode(&encoded_msg); /// assert_eq!(1, decoded_msg.len()); /// ``` #[inline] pub fn decode(slice: &[u8]) -> ResyncMsg { let value = serde_cbor::from_slice(slice); match value { Ok(value) => value, Err(_) => { ResyncMsg { resync_message: Vec::new(), } // return empty vec in case of error } } } } /// Msg connection structure /// /// This structure defines the Mles connection for simple synchronous connections. /// pub struct MsgConn { uid: String, channel: String, key: Option<u64>, stream: Option<TcpStream>, } impl MsgConn { /// Create a new MsgConn object for a connection. /// /// # Example /// ``` /// use mles_utils::MsgConn; /// /// let conn = MsgConn::new("My uid".to_string(), "My channel".to_string()); /// ``` #[inline] pub fn new(uid: String, channel: String) -> MsgConn { MsgConn { uid, channel, key: None, stream: None, } } /// Gets the defined uid. /// /// # Example /// ``` /// use mles_utils::MsgConn; /// /// let conn = MsgConn::new("My uid".to_string(), "My channel".to_string()); /// assert_eq!("My uid".to_string(), conn.get_uid()); /// ``` #[inline] pub fn get_uid(&self) -> String { self.uid.clone() } /// Gets the defined channel. /// /// # Example /// ``` /// use mles_utils::MsgConn; /// /// let conn = MsgConn::new("My uid".to_string(), "My channel".to_string()); /// assert_eq!("My channel".to_string(), conn.get_channel()); /// ``` #[inline] pub fn get_channel(&self) -> String { self.channel.clone() } /// Gets the defined key. /// /// # Example /// ``` /// use mles_utils::MsgConn; /// /// //key is set only when connection is initiated.. /// let conn = MsgConn::new("My uid".to_string(), "My channel".to_string()); /// assert_eq!(true, conn.get_key().is_none()); /// ``` #[inline] pub fn get_key(&self) -> Option<u64> { self.key } /// Connects to the defined address with a message. /// #[inline] pub fn connect_with_message(mut self, raddr: SocketAddr, msg: Vec<u8>) -> MsgConn { let msg = Msg::new(self.get_uid(), self.get_channel(), msg); match TcpStream::connect(raddr) { Ok(mut stream) => { let _val = stream.set_nodelay(true); if self.get_key().is_none() { let mut keys = Vec::new(); let laddr = match stream.local_addr() { Ok(laddr) => laddr, Err(_) => { let addr = "0.0.0.0:0"; addr.parse::<SocketAddr>().unwrap() } }; keys.push(MsgHdr::addr2str(&laddr)); keys.push(self.get_uid()); keys.push(self.get_channel()); let key = MsgHdr::do_hash(&keys); self.key = Some(key); } let encoded_msg = msg.encode(); let key = self.get_key().unwrap(); let keyv = write_key(key); let mut msgv = write_hdr_with_capacity( encoded_msg.len(), MsgHdr::select_cid(key), HDRKEYL + encoded_msg.len(), ); msgv.extend(keyv); msgv.extend(encoded_msg); let msgv = msgv.freeze(); match stream.write_all(msgv.as_ref()) { Ok(_) => self.stream = Some(stream), Err(err) => { println!("Send error {}", err); self.stream = None; } } self } Err(_) => { println!("Could not connect to server {}", raddr); self } } } /// Connects to the defined address (without a message). /// #[inline] pub fn connect(self, raddr: SocketAddr) -> MsgConn { self.connect_with_message(raddr, Vec::new()) } /// Send a message. Blocks until a message is sent. /// /// # Errors /// If a message cannot be sent, stream is set to None. /// #[inline] pub fn send_message(mut self, msg: Vec<u8>) -> MsgConn { let message = Msg::new(self.get_uid(), self.get_channel(), msg); let encoded_msg = message.encode(); let key = self.get_key().unwrap(); let keyv = write_key(key); let mut msgv = write_hdr_with_capacity( encoded_msg.len(), MsgHdr::select_cid(key), HDRKEYL + encoded_msg.len(), ); msgv.extend(keyv); msgv.extend(encoded_msg); let msgv = msgv.freeze(); let mut stream = self.stream.unwrap(); match stream.write_all(msgv.as_ref()) { Ok(_) => self.stream = Some(stream), Err(err) => { println!("Send error {}", err); self.stream = None; } } self } /// Reads a message with non-zero message content. Blocks until a message is received. /// /// # Errors /// If message cannot be read, an empty message is returned. /// #[inline] pub fn read_message(mut self) -> (MsgConn, Vec<u8>) { let stream = self.stream.unwrap(); loop { let tuple = read_n(&stream, HDRKEYL); let status = tuple.0; if let Ok(0) = status { println!("Read failed: eof"); self.stream = None; return (self, Vec::new()); } let buf = tuple.1; if buf.is_empty() { continue; } if read_hdr_type(buf.as_slice()) != 'M' as u32 { continue; } let hdr_len = read_hdr_len(buf.as_slice()); if 0 == hdr_len { continue; } let tuple = read_n(&stream, hdr_len); let status = tuple.0; if let Ok(0) = status { continue; }; let payload = tuple.1; if payload.len() != (hdr_len as usize) { continue; } let decoded_message = Msg::decode(payload.as_slice()); if 0 == decoded_message.get_message_len() { continue; } self.stream = Some(stream); return (self, decoded_message.get_message().to_owned()); } } /// Closes the connection. /// #[inline] pub fn close(mut self) -> MsgConn { if self.stream.is_some() { drop(self.stream.unwrap()); } self.stream = None; self } } #[inline] pub(crate) fn read_hdr_type(hdr: &[u8]) -> u32 { if hdr.len() < HDRL { return 0; } let mut buf = Cursor::new(&hdr[..]); let num = buf.get_u32_be(); num >> 24 } fn read_hdr_len(hdr: &[u8]) -> usize { if hdr.len() < HDRL { return 0; } let mut buf = Cursor::new(&hdr[..]); let num = buf.get_u32_be(); (num & 0xffffff) as usize } fn write_hdr(len: usize, cid: u32) -> BytesMut { let hdr = (('M' as u32) << 24) | len as u32; let mut msgv = BytesMut::with_capacity(HDRKEYL); msgv.put_u32_be(hdr); msgv.put_u32_be(cid); msgv } fn write_hdr_with_capacity(len: usize, cid: u32, cap: usize) -> BytesMut { let hdr = (('M' as u32) << 24) | len as u32; let mut msgv = BytesMut::with_capacity(cap); msgv.put_u32_be(hdr); msgv.put_u32_be(cid); msgv } fn write_hdr_without_cid(len: usize) -> BytesMut { let hdr = (('M' as u32) << 24) | len as u32; let mut msgv = BytesMut::with_capacity(HDRL); msgv.put_u32_be(hdr); msgv } #[inline] pub(crate) fn write_len_to_hdr(len: usize, mut hdrv: BytesMut) -> BytesMut { if hdrv.len() < HDRL { return BytesMut::new(); } let tail = hdrv.split_off(HDRL - CIDL); let mut nhdrv = write_hdr_without_cid(len); nhdrv.extend(tail); nhdrv } fn write_key(val: u64) -> BytesMut { let key = val; let mut msgv = BytesMut::with_capacity(KEYL); msgv.put_u64_be(key); msgv } fn write_hdr_with_key(len: usize, key: u64) -> BytesMut { let mut hdrv = write_hdr(len, MsgHdr::select_cid(key)); hdrv.extend(write_key(key)); hdrv } fn read_key_from_hdr(keyv: &[u8]) -> u64 { if keyv.len() < HDRKEYL { return 0; } let mut buf = Cursor::new(&keyv[HDRL..]); buf.get_u64_be() } fn read_cid_from_hdr(hdrv: &[u8]) -> u32 { if hdrv.len() < HDRL { return 0; } let mut buf = Cursor::new(&hdrv[(HDRL - CIDL)..]); buf.get_u32_be() } /// Check if a peer is defined /// /// # Example /// ``` /// use mles_utils::has_peer; /// /// let sockaddr = None; /// assert_eq!(false, has_peer(&sockaddr)); /// ``` #[inline] pub fn has_peer(peer: &Option<SocketAddr>) -> bool { peer::has_peer(peer) } fn read_n<R>(reader: R, bytes_to_read: usize) -> (Result<usize, Error>, Vec<u8>) where R: Read, { let mut buf = Vec::with_capacity(bytes_to_read); let mut chunk = reader.take(bytes_to_read as u64); let status = chunk.read_to_end(&mut buf); (status, buf) } /// Run an Mles server /// /// # Example /// ``` /// use std::thread; /// use std::net::{IpAddr, Ipv4Addr}; /// use std::net::{SocketAddr, ToSocketAddrs}; /// use mles_utils::server_run; /// /// let uid = "User".to_string(); /// let channel = "Channel".to_string(); /// let message = "Hello World!".to_string(); /// let address = "127.0.0.1:8077".to_string(); /// let address = address.parse().unwrap(); /// let child = thread::spawn(move || server_run(address, None, "".to_string(), "".to_string(), 100, 0)); /// drop(child); /// ``` #[inline] pub fn server_run( address: SocketAddr, peer: Option<SocketAddr>, keyval: String, keyaddr: String, hist_limit: usize, debug_flags: u64, ) { server::run(address, peer, keyval, keyaddr, hist_limit, debug_flags); } #[cfg(test)] mod tests { use super::*; use std::net::SocketAddr; use std::thread; use std::time::Duration; #[test] fn test_read_hdr_len_one() { let orig_len = 1; let hdrv = write_hdr(orig_len, 0x1); let len = read_hdr_len(hdrv.as_ref()); assert_eq!(len, orig_len); } #[test] fn test_read_hdr_len_16k() { let orig_len = 16000; let hdrv = write_hdr_with_capacity(orig_len, 0x1, HDRKEYL + orig_len); let len = read_hdr_len(hdrv.as_ref()); assert_eq!(len, orig_len); } #[test] fn test_read_hdr_len_16_7m() { let orig_len = 16777215; let hdrv = write_hdr(orig_len, 0x1); let len = read_hdr_len(hdrv.as_ref()); assert_eq!(len, orig_len); } #[test] fn test_encode_decode_msg() { let uid = "User".to_string(); let channel = "Channel".to_string(); let msg = "a test msg".to_string().into_bytes(); let orig_msg = Msg::new(uid, channel, msg); let encoded_msg = orig_msg.encode(); let decoded_msg = Msg::decode(&encoded_msg); assert_eq!(decoded_msg.uid, orig_msg.uid); assert_eq!(decoded_msg.channel, orig_msg.channel); assert_eq!(decoded_msg.message, orig_msg.message); } #[test] fn test_encode_decode_resync_msg() { let uid = "User".to_string(); let channel = "Channel".to_string(); let msg = "a test msg".to_string().into_bytes(); let orig_msg = Msg::new(uid, channel, msg); let encoded_msg = orig_msg.encode(); let uid2 = "User two".to_string(); let channel2 = "Channel two".to_string(); let msg2 = "a test msg two".to_string().into_bytes(); let orig_msg2 = Msg::new(uid2, channel2, msg2); let encoded_msg2 = orig_msg2.encode(); let vec = vec![encoded_msg, encoded_msg2]; let rmsg = ResyncMsg::new(&vec); let encoded_resync_msg: Vec<u8> = rmsg.encode(); let decoded_resync_msg: ResyncMsg = ResyncMsg::decode(&encoded_resync_msg); let mut cnt = 0; for msg in decoded_resync_msg.get_messages() { let decoded_msg = Msg::decode(&msg); if 0 == cnt { assert_eq!(decoded_msg.uid, orig_msg.uid); assert_eq!(decoded_msg.channel, orig_msg.channel); assert_eq!(decoded_msg.message, orig_msg.message); } else { assert_eq!(decoded_msg.uid, orig_msg2.uid); assert_eq!(decoded_msg.channel, orig_msg2.channel); assert_eq!(decoded_msg.message, orig_msg2.message); } cnt += 1; } } #[test] fn test_set_get_msg() { let uid = "User".to_string(); let channel = "Channel".to_string(); let msg = "a test msg".to_string().into_bytes(); let orig_msg = Msg::new("".to_string(), channel.to_string(), Vec::new()); let orig_msg = orig_msg.set_uid(uid.clone()); let orig_msg = orig_msg.set_channel(channel.clone()); let orig_msg = orig_msg.set_message(msg.clone()); assert_eq!(&uid, orig_msg.get_uid()); assert_eq!(&channel, orig_msg.get_channel()); assert_eq!(&msg, orig_msg.get_message()); } #[test] fn test_set_get_mut_msg() { let uid = "User".to_string(); let channel = "Channel".to_string(); let omsg = "a test ".to_string().into_bytes(); let nmsg = "a test mut msg".to_string().into_bytes(); let orig_msg = Msg::new("".to_string(), channel.to_string(), omsg); let orig_msg = orig_msg.set_uid(uid.clone()); let mut orig_msg = orig_msg.set_channel(channel.clone()); let mut_msg = orig_msg.get_mut_message(); mut_msg.extend_from_slice(&"mut msg".to_string().into_bytes()); assert_eq!(&uid, orig_msg.get_uid()); assert_eq!(&channel, orig_msg.get_channel()); assert_eq!(&nmsg, orig_msg.get_message()); } #[test] fn test_cid() { let orig_key = 0xffeffe; let hdrv = write_hdr_with_key(64, orig_key); let orig_len = hdrv.len(); let key = read_key_from_hdr(&hdrv); assert_eq!(orig_key, key); let read_cid = read_cid_from_hdr(&hdrv); assert_eq!(orig_key as u32, read_cid); let key = read_key_from_hdr(&hdrv); assert_eq!(orig_key, key); let len = hdrv.len(); assert_eq!(orig_len, len); } #[test] fn test_msgconn_send_read() { let sec = Duration::new(1, 0); let addr = "127.0.0.1:8078"; let addr = addr.parse::<SocketAddr>().unwrap(); let raddr = addr.clone(); let uid = "User".to_string(); let uid2 = "User two".to_string(); let channel = "Channel".to_string(); let message = "Hello World!".to_string(); //create server let child = thread::spawn(move || server_run(addr, None, "".to_string(), "".to_string(), 100, 0)); thread::sleep(sec); //send hello world let mut conn = MsgConn::new(uid2.clone(), channel.clone()); conn = conn.connect_with_message(raddr, message.into_bytes()); conn.close(); //read hello world let mut conn = MsgConn::new(uid.clone(), channel.clone()); conn = conn.connect(raddr); let (conn, msg) = conn.read_message(); let msg = String::from_utf8_lossy(msg.as_slice()); assert_eq!("Hello World!", msg); //close connection conn.close(); //drop server drop(child); } #[test] fn test_msgconn_read_send() { let sec = Duration::new(1, 0); let addr = "127.0.0.1:8076"; let addr = addr.parse::<SocketAddr>().unwrap(); let raddr = addr.clone(); let uid = "User".to_string(); let uid2 = "User two".to_string(); let channel = "Channel".to_string(); let message = "Hello World!".to_string(); //create server let child = thread::spawn(move || server_run(addr, None, "".to_string(), "".to_string(), 100, 0)); thread::sleep(sec); //read connect let mut conn = MsgConn::new(uid.clone(), channel.clone()); conn = conn.connect(raddr); //send hello world let mut sconn = MsgConn::new(uid2.clone(), channel.clone()); sconn = sconn.connect_with_message(raddr, message.into_bytes()); sconn.close(); //read hello world let (conn, msg) = conn.read_message(); let msg = String::from_utf8_lossy(msg.as_slice()); assert_eq!("Hello World!", msg); //close connection conn.close(); //drop server drop(child); } #[test] fn test_msgconn_peer_send_read() { let sec = Duration::new(1, 0); let addr = "127.0.0.1:8075"; let addr = addr.parse::<SocketAddr>().unwrap(); let paddr = "127.0.0.1:8074"; let paddr = paddr.parse::<SocketAddr>().unwrap(); let praddr = paddr.clone(); let uid = "User".to_string(); let uid2 = "User two".to_string(); let channel = "Channel".to_string(); let message = "Hello World!".to_string(); //create server let child = thread::spawn(move || server_run(addr, None, "".to_string(), "".to_string(), 100, 0)); thread::sleep(sec); //create peer server let pchild = thread::spawn(move || { server_run(paddr, Some(addr), "".to_string(), "".to_string(), 100, 0) }); thread::sleep(sec); //send hello world let mut conn = MsgConn::new(uid.clone(), channel.clone()); conn = conn.connect_with_message(praddr, message.into_bytes()); conn.close(); //read hello world let mut conn = MsgConn::new(uid2.clone(), channel.clone()); conn = conn.connect(praddr); let (conn, msg) = conn.read_message(); let msg = String::from_utf8_lossy(msg.as_slice()); assert_eq!("Hello World!", msg); //close connection conn.close(); //drop peer server drop(pchild); //drop server drop(child); } #[test] fn test_msgconn_peer_read_send() { let sec = Duration::new(1, 0); let addr = "127.0.0.1:8073"; let addr = addr.parse::<SocketAddr>().unwrap(); let paddr = "127.0.0.1:8072"; let paddr = paddr.parse::<SocketAddr>().unwrap(); let praddr = paddr.clone(); let uid = "User".to_string(); let uid2 = "User two".to_string(); let channel = "Channel".to_string(); let message = "Hello World!".to_string(); //create server let child = thread::spawn(move || server_run(addr, None, "".to_string(), "".to_string(), 100, 0)); thread::sleep(sec); //create peer server let pchild = thread::spawn(move || { server_run(paddr, Some(addr), "".to_string(), "".to_string(), 100, 0) }); thread::sleep(sec); //read connect let mut conn = MsgConn::new(uid.clone(), channel.clone()); conn = conn.connect(praddr); //send hello world let mut sconn = MsgConn::new(uid2.clone(), channel.clone()); sconn = sconn.connect_with_message(praddr, message.into_bytes()); sconn.close(); //read hello world let (conn, msg) = conn.read_message(); let msg = String::from_utf8_lossy(msg.as_slice()); assert_eq!("Hello World!", msg); //close connection conn.close(); //drop peer server drop(pchild); //drop server drop(child); } #[test] fn test_msgconn_basic_read_send() { let sec = Duration::new(1, 0); //set server address to connect let addr = "127.0.0.1:8071".parse::<SocketAddr>().unwrap(); //create server let serv = thread::spawn(move || server_run(addr, None, "".to_string(), "".to_string(), 0, 0)); thread::sleep(sec); let child = thread::spawn(|| { let uid = "User two".to_string(); let channel = "Channel".to_string(); let addr = "127.0.0.1:8071".parse::<SocketAddr>().unwrap(); //connect client to server let mut conn = MsgConn::new(uid, channel); conn = conn.connect(addr); //blocking read for hello world let (conn, msg) = conn.read_message(); let msg = String::from_utf8_lossy(msg.as_slice()); assert_eq!("Hello World!", msg); conn.close(); }); thread::sleep(sec); let addr = "127.0.0.1:8071".parse::<SocketAddr>().unwrap(); let uid = "User".to_string(); let channel = "Channel".to_string(); let message = "Hello World!".to_string(); //send hello world to awaiting client let mut conn = MsgConn::new(uid, channel); conn = conn.connect_with_message(addr, message.into_bytes()); conn.close(); let _res = child.join(); drop(serv); } }