data_conversion.rs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. #![allow(clippy::unusual_byte_groupings)]
  2. use core::panic;
  3. use std::fmt;
  4. use std::ops::Deref;
  5. use crate::networking::Nat;
  6. use crate::networking::NetworkSettings;
  7. use crate::networking::PortAllocationRule;
  8. use crate::networking::Transport;
  9. use std::error::Error;
  10. use std::net::IpAddr;
  11. use swarm_consensus::BlockID;
  12. use swarm_consensus::Capabilities;
  13. use swarm_consensus::CastData;
  14. use swarm_consensus::CastID;
  15. use swarm_consensus::CastMessage;
  16. use swarm_consensus::Configuration;
  17. use swarm_consensus::GnomeId;
  18. use swarm_consensus::Header;
  19. use swarm_consensus::Message;
  20. use swarm_consensus::NeighborRequest;
  21. use swarm_consensus::NeighborResponse;
  22. use swarm_consensus::Neighborhood;
  23. use swarm_consensus::Payload;
  24. use swarm_consensus::Policy;
  25. use swarm_consensus::Requirement;
  26. use swarm_consensus::Signature;
  27. use swarm_consensus::SwarmID;
  28. use swarm_consensus::SwarmName;
  29. use swarm_consensus::SwarmSyncRequestParams;
  30. use swarm_consensus::SwarmSyncResponse;
  31. use swarm_consensus::SwarmTime;
  32. use swarm_consensus::SwarmType;
  33. use swarm_consensus::SyncData;
  34. // Every received Dgram starts with message identification header
  35. // (and optional second byte for casting messages).
  36. // This byte is used to send local Swarm ID (0-63) and info whether it is
  37. // a regular Message or one of Casts (Uni/Multi/Broad).
  38. // In case it is a Cast, next byte is CastID:
  39. // TTIIIIII[CCCCCCCC] (and later HTPNNNNNSS... but only for Regular Message)
  40. // TT - Datagram Type 00 - Regular Message
  41. // 01 - Unicast
  42. // 10 - Multicast
  43. // 11 - Broadcast
  44. // IIIIII - local Swarm identification in order to support up to 64 different Swarms
  45. // on a single UPD socket
  46. // CCCCCCCC - optional CastID, only when TT is not Regular Message
  47. //
  48. // With above casting messages are independent from Sync mechanism.
  49. //
  50. //
  51. // If we have received a regular message, it starts with following:
  52. //
  53. // 123456789012345678901234567890123456789012345678
  54. // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  55. // HTPNNNNNSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSRRRRRRRR
  56. // HTP = header: 000 Sync, payload: KeepAlive
  57. // 010 Sync, payload: Bye
  58. // 100 Reconf, payload: KeepAlive
  59. // 101 Reconf, payload: Reconfigure
  60. // 110 Block, payload: KeepAlive
  61. // 111 Block, payload: Block
  62. //
  63. // remaining ten possible bit configs are currently UNIMPLEMENTED
  64. //
  65. // NNNNN = Neighborhood value
  66. // SS... = SwarmTime value
  67. // RRR = Reconfigure byte (when payload = Reconfigure)
  68. // ___ rest of Header, Payload with Signature...
  69. // With above we can send Reconf/Block only once per neighbor.
  70. // When we know our neighbor is aware of that data, we save bandwith
  71. // First bit set to 1 indicates we should read more bytes
  72. // to identify Reconf(second bit is 0)/Block(second bit is 1) header.
  73. // Only then we read bytes for Payload.
  74. // pub fn bytes_to_message(bytes: &[u8]) -> Result<Message, ConversionError> {
  75. pub fn bytes_to_message(bytes: Vec<u8>) -> Result<Message, ConversionError> {
  76. // eprintln!("Bytes to message: {:?}", bytes.len());
  77. // println!("decoding: {:#08b} {:?}", bytes[0], bytes);
  78. // let bytes_len = bytes.len();
  79. let swarm_time: SwarmTime = SwarmTime(as_u32_be(&[bytes[1], bytes[2], bytes[3], bytes[4]]));
  80. let neighborhood: Neighborhood = Neighborhood(bytes[0] & 0b0_000_1111);
  81. let mut gnome_id: GnomeId = GnomeId(0);
  82. let (header, mut data_idx) = if bytes[0] & 0b1_000_0000 == 128 {
  83. if bytes[0] & 0b0110_0000 == 96 {
  84. let block_id: u64 = as_u64_be(&[
  85. bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12],
  86. ]);
  87. (Header::Block(BlockID(block_id)), 21)
  88. } else if bytes[0] & 0b0110_0000 == 64 {
  89. let block_id: u64 = as_u64_be(&[
  90. bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12],
  91. ]);
  92. (Header::Block(BlockID(block_id)), 13)
  93. } else if bytes[0] & 0b0110_0000 == 32 {
  94. gnome_id = GnomeId(as_u64_be(&[
  95. bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13],
  96. ]));
  97. // println!("Reconf byte: {}", bytes[5]);
  98. // println!("Decoded GID: {}", gnome_id);
  99. (Header::Reconfigure(bytes[5], gnome_id), 5)
  100. } else {
  101. gnome_id = GnomeId(as_u64_be(&[
  102. bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13],
  103. ]));
  104. // println!("Reconf byte: {}", bytes[5]);
  105. // println!("GnomeId: {}", gnome_id);
  106. (Header::Reconfigure(bytes[5], gnome_id), 14)
  107. }
  108. // } else if bytes[0] & 0b0_111_0000 == 112 {
  109. } else {
  110. // if bytes[5] == 255 {
  111. (Header::Sync, 5)
  112. };
  113. // let payload: Payload = if bytes[0] & 0b0_111_0000 == 112 {
  114. let payload: Payload = if header.is_sync() {
  115. if bytes[0] & 0b0110_0000 == 0 {
  116. let bandwith: u64 = as_u64_be(&[
  117. bytes[data_idx],
  118. bytes[data_idx + 1],
  119. bytes[data_idx + 2],
  120. bytes[data_idx + 3],
  121. bytes[data_idx + 4],
  122. bytes[data_idx + 5],
  123. bytes[data_idx + 6],
  124. bytes[data_idx + 7],
  125. ]);
  126. Payload::KeepAlive(bandwith)
  127. } else if bytes[0] & 0b0110_0000 == 64 {
  128. Payload::Bye
  129. } else {
  130. panic!("Uncovered header value: {}", bytes[0]);
  131. }
  132. } else if header.is_reconfigure() {
  133. // println!("Reconf header");
  134. if bytes[0] & 0b0010_0000 == 0 {
  135. // println!("Reconf KA");
  136. let bandwith: u64 = as_u64_be(&[
  137. bytes[data_idx],
  138. bytes[data_idx + 1],
  139. bytes[data_idx + 2],
  140. bytes[data_idx + 3],
  141. bytes[data_idx + 4],
  142. bytes[data_idx + 5],
  143. bytes[data_idx + 6],
  144. bytes[data_idx + 7],
  145. ]);
  146. Payload::KeepAlive(bandwith)
  147. // } else if bytes[0] & 0b0010_0000 == 32 {
  148. } else {
  149. // println!("Reconf conf {} ({})", bytes[data_idx], bytes.len());
  150. // println!("Configuration! {}", bytes[data_idx]);
  151. // println!("Configuration! {:?}", bytes);
  152. let config = match bytes[data_idx] {
  153. 254 => {
  154. let c_id: CastID = CastID(bytes[data_idx + 9]);
  155. // eprintln!("StartBroadcast config {}, {:?}", gnome_id, c_id);
  156. data_idx += 10;
  157. Configuration::StartBroadcast(gnome_id, c_id)
  158. }
  159. 253 => {
  160. let c_id: CastID = CastID(bytes[data_idx + 9]);
  161. data_idx += 10;
  162. Configuration::ChangeBroadcastOrigin(gnome_id, c_id)
  163. }
  164. 252 => {
  165. let c_id: CastID = CastID(bytes[data_idx + 9]);
  166. data_idx += 10;
  167. Configuration::EndBroadcast(gnome_id, c_id)
  168. }
  169. 251 => {
  170. let c_id: CastID = CastID(bytes[data_idx + 9]);
  171. data_idx += 10;
  172. Configuration::StartMulticast(gnome_id, c_id)
  173. }
  174. 250 => {
  175. let c_id: CastID = CastID(bytes[data_idx + 9]);
  176. data_idx += 10;
  177. Configuration::ChangeMulticastOrigin(gnome_id, c_id)
  178. }
  179. 249 => {
  180. let c_id: CastID = CastID(bytes[data_idx + 9]);
  181. data_idx += 10;
  182. Configuration::EndMulticast(c_id)
  183. }
  184. 248 => {
  185. data_idx += 1;
  186. Configuration::CreateGroup
  187. }
  188. 247 => {
  189. data_idx += 1;
  190. Configuration::DeleteGroup
  191. }
  192. 246 => {
  193. data_idx += 1;
  194. Configuration::ModifyGroup
  195. }
  196. 245 => {
  197. let mut key = vec![];
  198. for i in data_idx + 9..data_idx + 279 {
  199. key.push(bytes[i]);
  200. }
  201. data_idx += 279;
  202. // println!("read: {}, {:?}", gnome_id, key);
  203. Configuration::InsertPubkey(gnome_id, key)
  204. }
  205. // TODO:
  206. other => {
  207. // println!("Custom: {}", other);
  208. // 1 byte for Reconf id
  209. // 8 bytes for GnomeId
  210. // 2 bytes for sync_data len
  211. // println!("Data idx: {}", data_idx);
  212. let s_data_len =
  213. u16::from_be_bytes([bytes[data_idx + 9], bytes[data_idx + 10]]) as usize;
  214. // println!("SData len: {}", s_data_len);
  215. data_idx += 11;
  216. let mut s_data_bytes = Vec::with_capacity(s_data_len);
  217. for i in data_idx..data_idx + s_data_len {
  218. s_data_bytes.push(bytes[i]);
  219. }
  220. data_idx += s_data_len;
  221. Configuration::UserDefined(other, SyncData::new(s_data_bytes).unwrap())
  222. }
  223. };
  224. // println!("Reading sig type at index: {}", data_idx);
  225. let sig_type = bytes[data_idx];
  226. // println!("Sig type: {}, bytes_len: {}", sig_type, bytes.len());
  227. let signature = if sig_type == 0 {
  228. // println!("Regular sig");
  229. let mut sig_bytes = vec![];
  230. for i in data_idx + 1..data_idx + 257 {
  231. sig_bytes.push(bytes[i]);
  232. }
  233. // data_idx += 74;
  234. Signature::Regular(gnome_id, sig_bytes)
  235. } else if sig_type == 255 {
  236. // println!("Extended sig,all bytes len: {}", bytes.len());
  237. // let mut pub_key = Vec::with_capacity(74);
  238. let mut pub_key = Vec::with_capacity(270);
  239. for i in data_idx + 1..data_idx + 271 {
  240. pub_key.push(bytes[i]);
  241. }
  242. let mut sig_bytes = Vec::with_capacity(256);
  243. // for i in data_idx + 75..data_idx + 139 {
  244. for i in data_idx + 271..data_idx + 527 {
  245. sig_bytes.push(bytes[i]);
  246. }
  247. // data_idx += 148;
  248. Signature::Extended(gnome_id, pub_key, sig_bytes)
  249. } else {
  250. panic!("Unexpected value: {}", sig_type);
  251. };
  252. Payload::Reconfigure(signature, config)
  253. }
  254. // } else if bytes[0] & 0b0_111_0000 == 64 {
  255. } else if bytes[0] & 0b0010_0000 == 0 {
  256. // eprintln!("Header Block, Payload KeepAlive");
  257. let bandwith: u64 = as_u64_be(&[
  258. bytes[data_idx],
  259. bytes[data_idx + 1],
  260. bytes[data_idx + 2],
  261. bytes[data_idx + 3],
  262. bytes[data_idx + 4],
  263. bytes[data_idx + 5],
  264. bytes[data_idx + 6],
  265. bytes[data_idx + 7],
  266. ]);
  267. Payload::KeepAlive(bandwith)
  268. // } else if bytes[0] & 0b0010_0000 == 32 {
  269. } else {
  270. let bid: u64 = as_u64_be(&[
  271. bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12],
  272. ]);
  273. eprintln!("Header Block, Payload Block id: {}", bid);
  274. let gnome_id: u64 = as_u64_be(&[
  275. bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18], bytes[19], bytes[20],
  276. ]);
  277. eprintln!("GnomeId: {}", gnome_id);
  278. let sig_type = bytes[data_idx];
  279. let signature = if sig_type == 0 {
  280. // println!("Regular sig");
  281. let mut sig_bytes = Vec::with_capacity(256);
  282. for i in data_idx + 1..data_idx + 257 {
  283. sig_bytes.push(bytes[i]);
  284. }
  285. data_idx += 257;
  286. Signature::Regular(GnomeId(gnome_id), sig_bytes)
  287. } else if sig_type == 255 {
  288. // println!("Extended sig,all bytes len: {}", bytes.len());
  289. // let mut pub_key = Vec::with_capacity(74);
  290. // for i in data_idx + 1..data_idx + 75 {
  291. let mut pub_key = Vec::with_capacity(270);
  292. for i in data_idx + 1..data_idx + 271 {
  293. pub_key.push(bytes[i]);
  294. }
  295. // let mut sig_bytes = Vec::with_capacity(64);
  296. let mut sig_bytes = Vec::with_capacity(256);
  297. for i in data_idx + 271..data_idx + 527 {
  298. sig_bytes.push(bytes[i]);
  299. }
  300. // data_idx += 139;
  301. data_idx += 527;
  302. Signature::Extended(GnomeId(gnome_id), pub_key, sig_bytes)
  303. } else {
  304. panic!("Unexpected value: {}", sig_type);
  305. };
  306. // println!("Data idx: {:?}", &bytes[data_idx..]);
  307. // println!("len: {}", bytes.len());
  308. let data = SyncData::new(Vec::from(&bytes[data_idx..])).unwrap();
  309. Payload::Block(BlockID(bid), signature, data)
  310. };
  311. eprintln!("DCHeader: {}", header);
  312. eprintln!("DCPayload: {}", payload);
  313. eprintln!("DCSwarmTime: {}", swarm_time);
  314. Ok(Message {
  315. swarm_time,
  316. neighborhood,
  317. header,
  318. payload,
  319. })
  320. }
  321. pub fn bytes_to_cast_message(bytes: &[u8]) -> Result<CastMessage, ConversionError> {
  322. let dgram_header = bytes[0];
  323. let c_id = CastID(bytes[1]);
  324. let data = CastData::new(Vec::from(&bytes[2..])).unwrap();
  325. if dgram_header & 0b11000000 == 192 {
  326. // TODO: broadcast
  327. // println!("received a broadcast");
  328. Ok(CastMessage::new_broadcast(c_id, data))
  329. } else if dgram_header & 0b11000000 == 128 {
  330. // TODO: multicast
  331. // eprintln!("received a multicast");
  332. Ok(CastMessage::new_multicast(c_id, data))
  333. } else if dgram_header & 0b11000000 == 64 {
  334. // TODO: unicast
  335. // println!("received a unicast");
  336. Ok(CastMessage::new_unicast(c_id, data))
  337. } else {
  338. Err(ConversionError)
  339. }
  340. }
  341. fn as_u16_be(array: &[u8; 2]) -> u16 {
  342. ((array[0] as u16) << 8) + (array[1] as u16)
  343. }
  344. fn as_u32_be(array: &[u8; 4]) -> u32 {
  345. ((array[0] as u32) << 24)
  346. + ((array[1] as u32) << 16)
  347. + ((array[2] as u32) << 8)
  348. + (array[3] as u32)
  349. }
  350. fn as_u64_be(array: &[u8; 8]) -> u64 {
  351. ((array[0] as u64) << 56)
  352. + ((array[1] as u64) << 48)
  353. + ((array[2] as u64) << 40)
  354. + ((array[3] as u64) << 32)
  355. + ((array[4] as u64) << 24)
  356. + ((array[5] as u64) << 16)
  357. + ((array[6] as u64) << 8)
  358. + (array[7] as u64)
  359. }
  360. #[derive(Debug)]
  361. pub struct ConversionError;
  362. // Failed,
  363. // }
  364. impl fmt::Display for ConversionError {
  365. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  366. // match self {
  367. // ConversionError::Failed => write!(f, "ConversionError: Failed"),
  368. // }
  369. write!(f, "ConversionError")
  370. }
  371. }
  372. impl Error for ConversionError {}
  373. fn put_u16(vec: &mut Vec<u8>, value: u16) {
  374. for a_byte in value.to_be_bytes() {
  375. vec.push(a_byte);
  376. }
  377. }
  378. fn put_u32(vec: &mut Vec<u8>, value: u32) {
  379. for a_byte in value.to_be_bytes() {
  380. vec.push(a_byte);
  381. }
  382. }
  383. fn put_u64(vec: &mut Vec<u8>, value: u64) {
  384. for a_byte in value.to_be_bytes() {
  385. vec.push(a_byte);
  386. }
  387. }
  388. pub fn message_to_bytes(msg: Message) -> Vec<u8> {
  389. // eprintln!("Message to bytes: {:?}", msg);
  390. let mut bytes = Vec::with_capacity(1033);
  391. let nhood = msg.neighborhood.0;
  392. if nhood > 31 {
  393. panic!("Can't handle this!");
  394. }
  395. bytes.push(nhood);
  396. put_u32(&mut bytes, msg.swarm_time.0);
  397. // bytes.append(&mut msg.swarm_time.to_bytes());
  398. let mut block_id_inserted = false;
  399. if let Header::Block(block_id) = msg.header {
  400. if matches!(msg.payload, Payload::Block(_b, ref _s, ref _d)) {
  401. bytes[0] |= 0b111_00000;
  402. } else {
  403. bytes[0] |= 0b110_00000;
  404. }
  405. put_u64(&mut bytes, block_id.0);
  406. block_id_inserted = true;
  407. } else if let Header::Reconfigure(conf_id, gnome_id) = msg.header {
  408. // println!("Header::Refonfigure: {}, {}", conf_id, gnome_id);
  409. if matches!(msg.payload, Payload::Reconfigure(ref _s, ref _d)) {
  410. bytes[0] |= 0b101_00000;
  411. } else {
  412. bytes[0] |= 0b100_00000;
  413. }
  414. bytes.push(conf_id);
  415. put_u64(&mut bytes, gnome_id.0);
  416. } else if matches!(msg.payload, Payload::Bye) {
  417. bytes[0] |= 0b010_00000;
  418. return bytes;
  419. }
  420. match msg.payload {
  421. Payload::KeepAlive(bandwith) => {
  422. put_u64(&mut bytes, bandwith);
  423. }
  424. Payload::Bye => {
  425. panic!("Bye should be short circuiting this fn");
  426. }
  427. Payload::Reconfigure(signature, config) => {
  428. // println!("msg->bytes sign len: {}", signature.len());
  429. if config.is_user_defined() {
  430. let [len_1, len_2] = (config.len() as u16 - 1).to_be_bytes();
  431. bytes.push(len_1);
  432. bytes.push(len_2);
  433. }
  434. for byte in config.content_bytes(false) {
  435. bytes.push(byte);
  436. }
  437. // put_u64(&mut bytes, signature.gnome_id().0);
  438. // print!("Pushing at index {} sig header byte: ", bytes.len());
  439. bytes.push(signature.header_byte());
  440. for sign_byte in signature.bytes() {
  441. bytes.push(sign_byte);
  442. }
  443. }
  444. Payload::Block(block_id, signature, data) => {
  445. // eprintln!("PB: {}", signature.len());
  446. if !block_id_inserted {
  447. put_u64(&mut bytes, block_id.0);
  448. // bytes.put_u32(block_id.0);
  449. };
  450. put_u64(&mut bytes, signature.gnome_id().0);
  451. bytes.push(signature.header_byte());
  452. for sign_byte in signature.bytes() {
  453. bytes.push(sign_byte);
  454. }
  455. // put_u32(&mut bytes, data.0);
  456. for byte in data.bytes() {
  457. bytes.push(byte);
  458. }
  459. }
  460. };
  461. // eprintln!("Message to bytes: {}[{:#b}]", bytes.len(), bytes[0]);
  462. bytes
  463. }
  464. pub fn neighbor_response_to_bytes(n_resp: NeighborResponse, bytes: &mut Vec<u8>) {
  465. match n_resp {
  466. NeighborResponse::BroadcastSync(part_no, total_parts, cast_id_origin_pairs) => {
  467. bytes.push(255);
  468. bytes.push(part_no);
  469. bytes.push(total_parts);
  470. for (c_id, origin) in cast_id_origin_pairs {
  471. bytes.push(c_id.0);
  472. put_u64(bytes, origin.0);
  473. }
  474. }
  475. NeighborResponse::MulticastSync(part_no, total_parts, cast_id_origin_pairs) => {
  476. bytes.push(254);
  477. bytes.push(part_no);
  478. bytes.push(total_parts);
  479. for (c_id, origin) in cast_id_origin_pairs {
  480. bytes.push(c_id.0);
  481. put_u64(bytes, origin.0);
  482. }
  483. }
  484. NeighborResponse::Unicast(swarm_id, cast_id) => {
  485. bytes.push(253);
  486. bytes.push(swarm_id.0);
  487. bytes.push(cast_id.0);
  488. }
  489. NeighborResponse::ForwardConnectResponse(mut network_settings) => {
  490. bytes.push(252);
  491. bytes.append(&mut network_settings);
  492. // insert_network_settings(bytes, network_settings);
  493. }
  494. NeighborResponse::ForwardConnectFailed => {
  495. bytes.push(251);
  496. }
  497. NeighborResponse::AlreadyConnected(id) => {
  498. bytes.push(250);
  499. bytes.push(id);
  500. }
  501. NeighborResponse::ConnectResponse(id, mut network_settings) => {
  502. bytes.push(249);
  503. bytes.push(id);
  504. bytes.append(&mut network_settings);
  505. // insert_network_settings(bytes, network_settings);
  506. }
  507. NeighborResponse::SwarmSync(sync_response) => {
  508. bytes.push(248);
  509. put_u16(bytes, sync_response.chill_phase);
  510. put_u64(bytes, sync_response.founder.0);
  511. put_u32(bytes, sync_response.swarm_time.0);
  512. put_u32(bytes, sync_response.round_start.0);
  513. bytes.push(sync_response.swarm_type.as_byte());
  514. // put_u64(bytes, sync_response.app_root_hash);
  515. bytes.push(sync_response.key_reg_size);
  516. bytes.push(sync_response.capability_size);
  517. bytes.push(sync_response.policy_size);
  518. bytes.push(sync_response.broadcast_size);
  519. bytes.push(sync_response.multicast_size);
  520. bytes.push(sync_response.more_key_reg_messages as u8);
  521. for (g_id, mut pubkey) in sync_response.key_reg_pairs {
  522. put_u64(bytes, g_id.0);
  523. bytes.append(&mut pubkey);
  524. }
  525. }
  526. NeighborResponse::Subscribed(is_bcast, cast_id, origin_id, _source_opt) => {
  527. bytes.push(247);
  528. if is_bcast {
  529. bytes.push(1);
  530. } else {
  531. bytes.push(0);
  532. }
  533. bytes.push(cast_id.0);
  534. put_u64(bytes, origin_id.0);
  535. }
  536. NeighborResponse::KeyRegistrySync(part_no, total_parts, id_pubkey_pairs) => {
  537. bytes.push(246);
  538. bytes.push(part_no);
  539. bytes.push(total_parts);
  540. for (g_id, mut pubkey) in id_pubkey_pairs {
  541. put_u64(bytes, g_id.0);
  542. bytes.append(&mut pubkey);
  543. }
  544. }
  545. NeighborResponse::CapabilitySync(part_no, total_parts, caps_ids_pairs) => {
  546. bytes.push(245);
  547. bytes.push(part_no);
  548. bytes.push(total_parts);
  549. for (c_id, g_ids) in caps_ids_pairs {
  550. bytes.push(c_id.byte());
  551. bytes.push(g_ids.len() as u8);
  552. for g_id in g_ids {
  553. put_u64(bytes, g_id.0);
  554. }
  555. }
  556. }
  557. NeighborResponse::PolicySync(part_no, total_parts, pol_req_pairs) => {
  558. bytes.push(244);
  559. bytes.push(part_no);
  560. bytes.push(total_parts);
  561. for (pol, req) in pol_req_pairs {
  562. pol.append_bytes_to(bytes);
  563. req.append_bytes_to(bytes);
  564. }
  565. }
  566. NeighborResponse::Custom(id, data) => {
  567. bytes.push(id);
  568. for byte in data.bytes() {
  569. bytes.push(byte);
  570. }
  571. }
  572. }
  573. }
  574. pub fn neighbor_request_to_bytes(n_req: NeighborRequest, bytes: &mut Vec<u8>) {
  575. match n_req {
  576. NeighborRequest::SwarmJoinedInfo(swarm_name) => {
  577. bytes.push(255);
  578. for byte in swarm_name.as_bytes() {
  579. bytes.push(byte);
  580. }
  581. }
  582. NeighborRequest::UnicastRequest(swarm_id, cast_ids) => {
  583. bytes.push(254);
  584. bytes.push(swarm_id.0);
  585. for c_id in cast_ids.deref() {
  586. bytes.push(c_id.0);
  587. }
  588. }
  589. NeighborRequest::CreateNeighbor(g_id, swarm_name) => {
  590. eprintln!("CreateNeighbor {} for {} to bytes", g_id, swarm_name);
  591. bytes.push(253);
  592. for byte in g_id.0.to_be_bytes() {
  593. bytes.push(byte);
  594. }
  595. for byte in swarm_name.as_bytes() {
  596. bytes.push(byte);
  597. }
  598. }
  599. NeighborRequest::ForwardConnectRequest(mut network_settings) => {
  600. bytes.push(252);
  601. bytes.append(&mut network_settings);
  602. // insert_network_settings(bytes, network_settings);
  603. }
  604. NeighborRequest::ConnectRequest(id, gnome_id, mut network_settings) => {
  605. bytes.push(251);
  606. bytes.push(id);
  607. put_u64(bytes, gnome_id.0);
  608. bytes.append(&mut network_settings);
  609. // insert_network_settings(bytes, network_settings);
  610. }
  611. NeighborRequest::SwarmSyncRequest(sync_req_params) => {
  612. bytes.push(250);
  613. bytes.push(sync_req_params.sync_key_reg as u8);
  614. bytes.push(sync_req_params.sync_capability as u8);
  615. bytes.push(sync_req_params.sync_policy as u8);
  616. bytes.push(sync_req_params.sync_broadcast as u8);
  617. bytes.push(sync_req_params.sync_multicast as u8);
  618. // put_u64(bytes, sync_req_params.app_root_hash);
  619. }
  620. NeighborRequest::SubscribeRequest(is_bcast, cast_id) => {
  621. bytes.push(249);
  622. if is_bcast {
  623. bytes.push(1);
  624. } else {
  625. bytes.push(0);
  626. }
  627. bytes.push(cast_id.0);
  628. }
  629. NeighborRequest::UnsubscribeRequest(is_bcast, cast_id) => {
  630. bytes.push(248);
  631. if is_bcast {
  632. bytes.push(1);
  633. } else {
  634. bytes.push(0);
  635. }
  636. bytes.push(cast_id.0);
  637. }
  638. NeighborRequest::SourceDrained(is_bcast, cast_id) => {
  639. bytes.push(247);
  640. if is_bcast {
  641. bytes.push(1);
  642. } else {
  643. bytes.push(0);
  644. }
  645. bytes.push(cast_id.0);
  646. }
  647. NeighborRequest::Custom(id, data) => {
  648. bytes.push(id);
  649. for byte in data.bytes() {
  650. bytes.push(byte);
  651. }
  652. }
  653. }
  654. }
  655. pub fn bytes_to_neighbor_request(bytes: Vec<u8>) -> NeighborRequest {
  656. let data_idx = 0;
  657. let bytes_len = bytes.len();
  658. match bytes[0] {
  659. 255 => {
  660. // eprintln!("Reading SwarmNames gnome/data_conversion");
  661. let swarm_name = SwarmName::from(&bytes[data_idx + 1..bytes_len]).unwrap();
  662. NeighborRequest::SwarmJoinedInfo(swarm_name)
  663. }
  664. 254 => {
  665. let swarm_id = SwarmID(bytes[data_idx + 1]);
  666. let mut cast_ids = [CastID(0); 256];
  667. for (inserted, c_id) in bytes[data_idx + 2..bytes_len].iter().enumerate() {
  668. cast_ids[inserted] = CastID(*c_id);
  669. }
  670. NeighborRequest::UnicastRequest(swarm_id, Box::new(cast_ids))
  671. }
  672. 253 => {
  673. let gnome_id = GnomeId(as_u64_be(&[
  674. bytes[data_idx + 1],
  675. bytes[data_idx + 2],
  676. bytes[data_idx + 3],
  677. bytes[data_idx + 4],
  678. bytes[data_idx + 5],
  679. bytes[data_idx + 6],
  680. bytes[data_idx + 7],
  681. bytes[data_idx + 8],
  682. ]));
  683. // let swarm_name = String::from_utf8(bytes[data_idx + 9..bytes_len].to_vec()).unwrap();
  684. let swarm_name = SwarmName::from(&bytes[data_idx + 9..bytes_len]).unwrap();
  685. // eprintln!(
  686. // "Reading SwarmNames gnome/data_conversion (CreateNeighbor for {})",
  687. // swarm_name
  688. // );
  689. NeighborRequest::CreateNeighbor(gnome_id, swarm_name)
  690. }
  691. 252 => {
  692. // let net_set = parse_network_settings(&bytes[data_idx + 1..bytes_len]);
  693. let net_set = bytes[data_idx + 1..bytes_len].to_vec();
  694. NeighborRequest::ForwardConnectRequest(net_set)
  695. }
  696. 251 => {
  697. let id = bytes[data_idx + 1];
  698. let mut g_id: u64 = ((bytes[data_idx + 2]) as u64) << 56;
  699. g_id += ((bytes[data_idx + 3]) as u64) << 48;
  700. g_id += ((bytes[data_idx + 4]) as u64) << 40;
  701. g_id += ((bytes[data_idx + 5]) as u64) << 32;
  702. g_id += ((bytes[data_idx + 6]) as u64) << 24;
  703. g_id += ((bytes[data_idx + 7]) as u64) << 16;
  704. g_id += ((bytes[data_idx + 8]) as u64) << 8;
  705. g_id += (bytes[data_idx + 9]) as u64;
  706. // let net_set = parse_network_settings(&bytes[data_idx + 10..bytes_len]);
  707. let net_set = bytes[data_idx + 10..bytes_len].to_vec();
  708. NeighborRequest::ConnectRequest(id, GnomeId(g_id), net_set)
  709. }
  710. 250 => {
  711. let sync_key_reg = bytes[data_idx + 1] > 0;
  712. let sync_capability = bytes[data_idx + 2] > 0;
  713. let sync_policy = bytes[data_idx + 3] > 0;
  714. let sync_broadcast = bytes[data_idx + 4] > 0;
  715. let sync_multicast = bytes[data_idx + 5] > 0;
  716. // let mut app_root_hash: u64 = ((bytes[data_idx + 6]) as u64) << 56;
  717. // app_root_hash += ((bytes[data_idx + 7]) as u64) << 48;
  718. // app_root_hash += ((bytes[data_idx + 8]) as u64) << 40;
  719. // app_root_hash += ((bytes[data_idx + 9]) as u64) << 32;
  720. // app_root_hash += ((bytes[data_idx + 10]) as u64) << 24;
  721. // app_root_hash += ((bytes[data_idx + 11]) as u64) << 16;
  722. // app_root_hash += ((bytes[data_idx + 12]) as u64) << 8;
  723. // app_root_hash += (bytes[data_idx + 13]) as u64;
  724. let sync_req_params = SwarmSyncRequestParams {
  725. sync_key_reg,
  726. sync_capability,
  727. sync_policy,
  728. sync_broadcast,
  729. sync_multicast,
  730. // app_root_hash,
  731. };
  732. NeighborRequest::SwarmSyncRequest(sync_req_params)
  733. }
  734. 249 => {
  735. let is_bcast = bytes[data_idx + 1] > 0;
  736. let cast_id = CastID(bytes[data_idx + 2]);
  737. NeighborRequest::SubscribeRequest(is_bcast, cast_id)
  738. }
  739. 248 => {
  740. let is_bcast = bytes[data_idx + 1] > 0;
  741. let cast_id = CastID(bytes[data_idx + 2]);
  742. NeighborRequest::UnsubscribeRequest(is_bcast, cast_id)
  743. }
  744. 247 => {
  745. let is_bcast = bytes[data_idx + 1] > 0;
  746. let cast_id = CastID(bytes[data_idx + 2]);
  747. NeighborRequest::SourceDrained(is_bcast, cast_id)
  748. }
  749. other => {
  750. let dta = if bytes.len() > data_idx + 1 {
  751. Vec::from(&bytes[data_idx + 1..])
  752. } else {
  753. vec![]
  754. };
  755. NeighborRequest::Custom(other, CastData::new(dta).unwrap())
  756. }
  757. }
  758. }
  759. pub fn bytes_to_neighbor_response(mut bytes: Vec<u8>) -> NeighborResponse {
  760. let data_idx = 0;
  761. let bytes_len = bytes.len();
  762. let response_type = bytes[data_idx];
  763. // println!("Parsing response type: {}", response_type);
  764. match response_type {
  765. 255 => {
  766. let part_no = bytes[data_idx + 1];
  767. let total_parts = bytes[data_idx + 2];
  768. let mut idx = data_idx + 3;
  769. let mut cid_origin_pairs = vec![];
  770. while idx < bytes_len {
  771. let c_id = bytes[idx];
  772. idx += 1;
  773. let gnome_id: GnomeId = GnomeId(as_u64_be(&[
  774. bytes[idx],
  775. bytes[idx + 1],
  776. bytes[idx + 2],
  777. bytes[idx + 3],
  778. bytes[idx + 4],
  779. bytes[idx + 5],
  780. bytes[idx + 6],
  781. bytes[idx + 7],
  782. ]));
  783. idx += 8;
  784. cid_origin_pairs.push((CastID(c_id), gnome_id));
  785. }
  786. NeighborResponse::BroadcastSync(part_no, total_parts, cid_origin_pairs)
  787. }
  788. 254 => {
  789. let part_no = bytes[data_idx + 1];
  790. let total_parts = bytes[data_idx + 2];
  791. let mut idx = data_idx + 3;
  792. let mut cid_origin_pairs = vec![];
  793. while idx < bytes_len {
  794. let c_id = bytes[idx];
  795. idx += 1;
  796. let gnome_id: GnomeId = GnomeId(as_u64_be(&[
  797. bytes[idx],
  798. bytes[idx + 1],
  799. bytes[idx + 2],
  800. bytes[idx + 3],
  801. bytes[idx + 4],
  802. bytes[idx + 5],
  803. bytes[idx + 6],
  804. bytes[idx + 7],
  805. ]));
  806. idx += 8;
  807. cid_origin_pairs.push((CastID(c_id), gnome_id));
  808. }
  809. NeighborResponse::MulticastSync(part_no, total_parts, cid_origin_pairs)
  810. }
  811. 253 => NeighborResponse::Unicast(SwarmID(bytes[data_idx + 1]), CastID(bytes[data_idx + 2])),
  812. 252 => {
  813. // let net_set = parse_network_settings(&bytes[data_idx + 1..bytes_len]);
  814. let net_set = bytes[data_idx + 1..bytes_len].to_vec();
  815. NeighborResponse::ForwardConnectResponse(net_set)
  816. }
  817. 251 => NeighborResponse::ForwardConnectFailed,
  818. 250 => {
  819. let id = bytes[data_idx + 1];
  820. NeighborResponse::AlreadyConnected(id)
  821. }
  822. 249 => {
  823. let id = bytes[data_idx + 1];
  824. // let net_set = parse_network_settings(&bytes[data_idx + 2..bytes_len]);
  825. let net_set = bytes[data_idx + 2..bytes_len].to_vec();
  826. NeighborResponse::ConnectResponse(id, net_set)
  827. }
  828. 248 => {
  829. let chill_phase = as_u16_be(&[bytes[data_idx + 1], bytes[data_idx + 2]]);
  830. let founder = GnomeId(as_u64_be(&[
  831. bytes[data_idx + 3],
  832. bytes[data_idx + 4],
  833. bytes[data_idx + 5],
  834. bytes[data_idx + 6],
  835. bytes[data_idx + 7],
  836. bytes[data_idx + 8],
  837. bytes[data_idx + 9],
  838. bytes[data_idx + 10],
  839. ]));
  840. let swarm_time: SwarmTime = SwarmTime(as_u32_be(&[
  841. bytes[data_idx + 11],
  842. bytes[data_idx + 12],
  843. bytes[data_idx + 13],
  844. bytes[data_idx + 14],
  845. ]));
  846. let round_start: SwarmTime = SwarmTime(as_u32_be(&[
  847. bytes[data_idx + 15],
  848. bytes[data_idx + 16],
  849. bytes[data_idx + 17],
  850. bytes[data_idx + 18],
  851. ]));
  852. let swarm_type = SwarmType::from(bytes[data_idx + 19]);
  853. // let app_root_hash = as_u64_be(&[
  854. // bytes[data_idx + 20],
  855. // bytes[data_idx + 21],
  856. // bytes[data_idx + 22],
  857. // bytes[data_idx + 23],
  858. // bytes[data_idx + 24],
  859. // bytes[data_idx + 25],
  860. // bytes[data_idx + 26],
  861. // bytes[data_idx + 27],
  862. // ]);
  863. let key_reg_size = bytes[data_idx + 20];
  864. let capability_size = bytes[data_idx + 21];
  865. let policy_size = bytes[data_idx + 22];
  866. let broadcast_size = bytes[data_idx + 23];
  867. let multicast_size = bytes[data_idx + 24];
  868. let more_key_reg_messages = bytes[data_idx + 25] != 0;
  869. let mut key_reg_pairs = vec![];
  870. let mut idx = data_idx + 26;
  871. while idx < bytes_len {
  872. let mut g_vec = [0; 8];
  873. for i in 0..8 {
  874. g_vec[i] = bytes[idx + i];
  875. }
  876. let mut pubkey_vec = Vec::with_capacity(270);
  877. for i in 0..270 {
  878. pubkey_vec.push(bytes[idx + 8 + i]);
  879. }
  880. idx += 278;
  881. key_reg_pairs.push((GnomeId(u64::from_be_bytes(g_vec)), pubkey_vec));
  882. }
  883. let sync_response = SwarmSyncResponse {
  884. chill_phase,
  885. founder,
  886. swarm_time,
  887. round_start,
  888. swarm_type,
  889. // app_root_hash,
  890. key_reg_size,
  891. capability_size,
  892. policy_size,
  893. broadcast_size,
  894. multicast_size,
  895. more_key_reg_messages,
  896. key_reg_pairs,
  897. };
  898. NeighborResponse::SwarmSync(sync_response)
  899. }
  900. 247 => {
  901. let is_bcast = bytes[data_idx + 1] > 0;
  902. let cast_id = CastID(bytes[data_idx + 2]);
  903. let data_idx = data_idx + 3;
  904. let origin_id: GnomeId = GnomeId(as_u64_be(&[
  905. bytes[data_idx],
  906. bytes[data_idx + 1],
  907. bytes[data_idx + 2],
  908. bytes[data_idx + 3],
  909. bytes[data_idx + 4],
  910. bytes[data_idx + 5],
  911. bytes[data_idx + 6],
  912. bytes[data_idx + 7],
  913. ]));
  914. NeighborResponse::Subscribed(is_bcast, cast_id, origin_id, None)
  915. }
  916. 246 => {
  917. let part_no = bytes[data_idx + 1];
  918. let total_parts = bytes[data_idx + 2];
  919. let mut idx = data_idx + 3;
  920. let mut id_pubkey_pairs = vec![];
  921. while idx < bytes_len {
  922. let gnome_id: GnomeId = GnomeId(as_u64_be(&[
  923. bytes[idx],
  924. bytes[idx + 1],
  925. bytes[idx + 2],
  926. bytes[idx + 3],
  927. bytes[idx + 4],
  928. bytes[idx + 5],
  929. bytes[idx + 6],
  930. bytes[idx + 7],
  931. ]));
  932. let mut pubkey = Vec::with_capacity(270);
  933. for i in idx + 8..idx + 278 {
  934. pubkey.push(bytes[i]);
  935. }
  936. id_pubkey_pairs.push((gnome_id, pubkey));
  937. idx += 278;
  938. }
  939. NeighborResponse::KeyRegistrySync(part_no, total_parts, id_pubkey_pairs)
  940. }
  941. 245 => {
  942. let part_no = bytes[data_idx + 1];
  943. let total_parts = bytes[data_idx + 2];
  944. let mut idx = data_idx + 3;
  945. let mut cap_ids_pairs = vec![];
  946. while idx < bytes_len {
  947. let cap_id = bytes[idx];
  948. let ids_len = bytes[idx + 1];
  949. idx += 2;
  950. let mut ids = Vec::with_capacity(ids_len as usize);
  951. for _ix in 0..ids_len {
  952. let gnome_id: GnomeId = GnomeId(as_u64_be(&[
  953. bytes[idx],
  954. bytes[idx + 1],
  955. bytes[idx + 2],
  956. bytes[idx + 3],
  957. bytes[idx + 4],
  958. bytes[idx + 5],
  959. bytes[idx + 6],
  960. bytes[idx + 7],
  961. ]));
  962. ids.push(gnome_id);
  963. idx += 8;
  964. }
  965. cap_ids_pairs.push((Capabilities::from(cap_id), ids));
  966. }
  967. NeighborResponse::CapabilitySync(part_no, total_parts, cap_ids_pairs)
  968. }
  969. 244 => {
  970. let mut b_drain = bytes.drain(0..3);
  971. let _ = b_drain.next();
  972. let part_no = b_drain.next().unwrap();
  973. let total_parts = b_drain.next().unwrap();
  974. let mut pol_req_pairs = vec![];
  975. drop(b_drain);
  976. while !bytes.is_empty() {
  977. let policy = Policy::from(&mut bytes);
  978. let req = Requirement::from(&mut bytes);
  979. pol_req_pairs.push((policy, req));
  980. }
  981. NeighborResponse::PolicySync(part_no, total_parts, pol_req_pairs)
  982. }
  983. _other => {
  984. let dta = if bytes.len() > data_idx + 1 {
  985. Vec::from(&bytes[data_idx + 1..])
  986. } else {
  987. vec![]
  988. };
  989. NeighborResponse::Custom(bytes[data_idx], CastData::new(dta).unwrap())
  990. }
  991. }
  992. }
  993. fn insert_network_settings(bytes: &mut Vec<u8>, network_settings_vec: Vec<NetworkSettings>) {
  994. //TODO: support sending multiple NSs.
  995. // TODO: add transport byte
  996. for network_settings in network_settings_vec {
  997. bytes.append(&mut network_settings.bytes());
  998. // bytes.push(network_settings.nat_type as u8);
  999. // put_u16(bytes, network_settings.pub_port);
  1000. // // bytes.put_u16(network_settings.pub_port);
  1001. // bytes.push(network_settings.port_allocation.0 as u8);
  1002. // // TODO: fix this!
  1003. // // bytes.put_i8(network_settings.port_allocation.1);
  1004. // bytes.push(network_settings.port_allocation.1 as u8);
  1005. // bytes.push(network_settings.transport.byte());
  1006. // let pub_ip = network_settings.pub_ip;
  1007. // match pub_ip {
  1008. // std::net::IpAddr::V4(ip4) => {
  1009. // for b in ip4.octets() {
  1010. // bytes.push(b);
  1011. // }
  1012. // }
  1013. // std::net::IpAddr::V6(ip4) => {
  1014. // for b in ip4.octets() {
  1015. // bytes.push(b);
  1016. // }
  1017. // }
  1018. // }
  1019. }
  1020. }
  1021. fn parse_network_settings(bytes: &[u8]) -> Vec<NetworkSettings> {
  1022. NetworkSettings::from(bytes)
  1023. // let mut bytes_iter = bytes.iter();
  1024. // let mut ns_vec = vec![];
  1025. // //TODO: support multiple NSs
  1026. // while let Some(raw_nat_type) = bytes_iter.next() {
  1027. // let nat_type = match raw_nat_type {
  1028. // 0 => Nat::Unknown,
  1029. // 1 => Nat::None,
  1030. // 2 => Nat::FullCone,
  1031. // 4 => Nat::AddressRestrictedCone,
  1032. // 8 => Nat::PortRestrictedCone,
  1033. // 16 => Nat::SymmetricWithPortControl,
  1034. // 32 => Nat::Symmetric,
  1035. // _ => {
  1036. // eprintln!("Unrecognized NatType while parsing: {}", raw_nat_type);
  1037. // Nat::Unknown
  1038. // }
  1039. // };
  1040. // let mut port_bytes: [u8; 2] = [0, 0];
  1041. // port_bytes[0] = *bytes_iter.next().unwrap();
  1042. // port_bytes[1] = *bytes_iter.next().unwrap();
  1043. // let pub_port: u16 = ((port_bytes[0]) as u16) << 8 | port_bytes[1] as u16;
  1044. // let port_allocation_rule = match bytes_iter.next().unwrap() {
  1045. // 0 => PortAllocationRule::Random,
  1046. // 1 => PortAllocationRule::FullCone,
  1047. // 2 => PortAllocationRule::AddressSensitive,
  1048. // 4 => PortAllocationRule::PortSensitive,
  1049. // _ => PortAllocationRule::Random,
  1050. // };
  1051. // let delta_port = *bytes_iter.next().unwrap() as i8;
  1052. // let transport = Transport::from(*bytes_iter.next().unwrap()).unwrap();
  1053. // let mut ip_bytes: Vec<u8> = vec![];
  1054. // let pub_ip = match transport {
  1055. // Transport::UDPoverIP4 | Transport::TCPoverIP4 => {
  1056. // for _i in 0..4 {
  1057. // ip_bytes.push(*bytes_iter.next().unwrap());
  1058. // }
  1059. // let array: [u8; 4] = ip_bytes.try_into().unwrap();
  1060. // IpAddr::from(array)
  1061. // }
  1062. // Transport::TCPoverIP6 | Transport::UDPoverIP6 => {
  1063. // for _i in 0..16 {
  1064. // ip_bytes.push(*bytes_iter.next().unwrap());
  1065. // }
  1066. // let array: [u8; 16] = ip_bytes.try_into().unwrap();
  1067. // IpAddr::from(array)
  1068. // }
  1069. // };
  1070. // // for a_byte in bytes_iter {
  1071. // // ip_bytes.push(*a_byte);
  1072. // // }
  1073. // // let bytes_len = ip_bytes.len();
  1074. // // let pub_ip = if bytes_len == 4 {
  1075. // // let array: [u8; 4] = ip_bytes.try_into().unwrap();
  1076. // // IpAddr::from(array)
  1077. // // } else if bytes_len == 16 {
  1078. // // let array: [u8; 16] = ip_bytes.try_into().unwrap();
  1079. // // IpAddr::from(array)
  1080. // // } else {
  1081. // // eprintln!("Unable to parse IP addr from: {:?}", ip_bytes);
  1082. // // IpAddr::from([0, 0, 0, 0])
  1083. // // };
  1084. // ns_vec.push(NetworkSettings {
  1085. // pub_ip,
  1086. // pub_port,
  1087. // nat_type,
  1088. // port_allocation: (port_allocation_rule, delta_port),
  1089. // transport,
  1090. // })
  1091. // }
  1092. // ns_vec
  1093. }