|
| 1 | +// Copyright (c) 2022-present INESC-ID. |
| 2 | +// Distributed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +use crate::{AsPointer, ByteRepr, Ptr, Value}; |
| 5 | +use std::cell::RefCell; |
| 6 | +use std::rc::Rc; |
| 7 | + |
| 8 | +#[derive(Default)] |
| 9 | +pub struct InAddr { |
| 10 | + pub s_addr: Value<u32>, |
| 11 | +} |
| 12 | + |
| 13 | +pub struct In6Addr { |
| 14 | + pub s6_addr: Value<Box<[u8]>>, |
| 15 | +} |
| 16 | + |
| 17 | +impl In6Addr { |
| 18 | + pub fn s6_addr(&self) -> Ptr<u8> { |
| 19 | + self.s6_addr.as_pointer() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for In6Addr { |
| 24 | + fn default() -> Self { |
| 25 | + Self { |
| 26 | + s6_addr: Rc::new(RefCell::new(vec![0u8; 16].into_boxed_slice())), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Clone for InAddr { |
| 32 | + fn clone(&self) -> Self { |
| 33 | + Self { |
| 34 | + s_addr: Rc::new(RefCell::new(*self.s_addr.borrow())), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl Clone for In6Addr { |
| 40 | + fn clone(&self) -> Self { |
| 41 | + Self { |
| 42 | + s6_addr: Rc::new(RefCell::new(self.s6_addr.borrow().clone())), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl ByteRepr for InAddr { |
| 48 | + fn byte_size() -> usize { |
| 49 | + 4 |
| 50 | + } |
| 51 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 52 | + (*self.s_addr.borrow()).to_bytes(&mut buf[0..4]); |
| 53 | + } |
| 54 | + fn from_bytes(buf: &[u8]) -> Self { |
| 55 | + Self { |
| 56 | + s_addr: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))), |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl ByteRepr for In6Addr { |
| 62 | + fn byte_size() -> usize { |
| 63 | + 16 |
| 64 | + } |
| 65 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 66 | + buf[0..16].copy_from_slice(&self.s6_addr.borrow()); |
| 67 | + } |
| 68 | + fn from_bytes(buf: &[u8]) -> Self { |
| 69 | + Self { |
| 70 | + s6_addr: Rc::new(RefCell::new(buf[0..16].to_vec().into_boxed_slice())), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl ByteRepr for ::libc::in_addr {} |
| 76 | +impl ByteRepr for ::libc::in6_addr {} |
0 commit comments