Verified Commit 2d770910 authored by SIMONIN Matthieu's avatar SIMONIN Matthieu
Browse files

[fake-vm] VsgAddress -> libc::in_addr_t

parent 8767472a
......@@ -2,7 +2,7 @@
#[macro_use(local_vsg_address_str, local_vsg_address, remote_vsg_address)]
extern crate fake_vm;
use fake_vm::{Context, VsgAddress, Error, Result};
use fake_vm::{Context, Error, Result};
use libc::{self, uintptr_t};
#[allow(unused_imports)]
use log::{debug, error};
......@@ -125,7 +125,7 @@ pub unsafe extern fn vsg_gettimeofday(context: *const Context, timeval: *mut lib
///
/// * Fails with `libc::ENOMEM` whenever there is no more buffers to hold the message to send.
#[no_mangle]
pub unsafe extern fn vsg_send(context: *const Context, dst: VsgAddress, msglen: u32, msg: *const u8) -> c_int {
pub unsafe extern fn vsg_send(context: *const Context, dst: libc::in_addr_t, msglen: u32, msg: *const u8) -> c_int {
if let Some(context) = context.as_ref() {
// We can tolerate msg.is_null() if msglen == 0 but std::slice::from_raw_parts() requires
// non null pointers.
......@@ -175,7 +175,7 @@ pub unsafe extern fn vsg_send(context: *const Context, dst: VsgAddress, msglen:
/// * Fails with `libc::EMSGSIZE` whenever the next message in the queue has a payload bigger than
/// the provided buffer. The message is lost.
#[no_mangle]
pub unsafe extern fn vsg_recv(context: *const Context, psrc: *mut VsgAddress, pdst: *mut VsgAddress, msglen: *mut u32, msg: *mut u8) -> c_int {
pub unsafe extern fn vsg_recv(context: *const Context, psrc: *mut libc::in_addr_t, pdst: *mut libc::in_addr_t, msglen: *mut u32, msg: *mut u8) -> c_int {
const_assert!(fake_vm::MAX_PACKET_SIZE <= std::u32::MAX as usize);
if let Some(context) = context.as_ref() {
......
use chrono::naive::NaiveDateTime;
use crate::VsgAddress;
use libc;
use std::num::NonZeroUsize;
use std::str::FromStr;
use structopt::StructOpt;
......@@ -13,7 +13,7 @@ pub(crate) struct Config {
/// Name (address) of this application in the network
#[structopt(short = "n", long = "name", parse(try_from_str = "crate::vsg_address_from_str"))]
pub address: VsgAddress,
pub address: libc::in_addr_t,
/// Initial time in the VM, formatted as %Y-%m-%dT%H:%M:%S%.f (%.f part is optional)
#[structopt(short = "t", long = "initial_time", parse(try_from_str = "chrono::naive::NaiveDateTime::from_str"))]
......
......@@ -42,9 +42,7 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type RecvCallback = Box<dyn Fn() -> () + Send + Sync>;
pub type VsgAddress = u32;
fn vsg_address_from_str(ip: &str) -> std::result::Result<VsgAddress, std::net::AddrParseError> {
fn vsg_address_from_str(ip: &str) -> std::result::Result<libc::in_addr_t , std::net::AddrParseError> {
use std::str::FromStr;
let ipv4 = std::net::Ipv4Addr::from_str(ip)?;
Ok(Into::<u32>::into(ipv4).to_be())
......@@ -52,13 +50,13 @@ fn vsg_address_from_str(ip: &str) -> std::result::Result<VsgAddress, std::net::A
#[derive(Debug)]
struct Packet {
src: VsgAddress,
dst: VsgAddress,
src: libc::in_addr_t,
dst: libc::in_addr_t,
payload: Buffer,
}
impl Packet {
fn new(src: VsgAddress, dst: VsgAddress, payload: Buffer) -> Packet {
fn new(src: libc::in_addr_t, dst: libc::in_addr_t, payload: Buffer) -> Packet {
Packet {
src: src,
dst: dst,
......@@ -72,7 +70,7 @@ impl Packet {
// interior mutability.
struct InnerContext {
// Read-only
address: VsgAddress,
address: libc::in_addr_t,
// No concurrency: (mut) accessed only by the deadline handler
// Mutex is used to show interior mutability despite sharing.
connector: Mutex<ConnectorImpl>,
......@@ -137,7 +135,7 @@ impl InnerContext {
}
}
fn send(&self, dst: VsgAddress, msg: &[u8]) -> Result<()> {
fn send(&self, dst: libc::in_addr_t, msg: &[u8]) -> Result<()> {
let mut buffer = self.output_buffer_pool.allocate_buffer(msg.len())?;
buffer.copy_from_slice(msg);
......@@ -151,7 +149,7 @@ impl InnerContext {
Ok(())
}
fn recv<'a, 'b>(&'a self, msg: &'b mut [u8]) -> Result<(VsgAddress, VsgAddress, &'b mut [u8])> {
fn recv<'a, 'b>(&'a self, msg: &'b mut [u8]) -> Result<(libc::in_addr_t, libc::in_addr_t, &'b mut [u8])> {
match self.input_queue.pop() {
Some(Packet { src, dst, payload, }) => {
if msg.len() >= payload.len() {
......@@ -290,11 +288,11 @@ impl Context {
self.0.gettimeofday()
}
pub fn send(&self, dst: VsgAddress, msg: &[u8]) -> Result<()> {
pub fn send(&self, dst: libc::in_addr_t, msg: &[u8]) -> Result<()> {
self.0.send(dst, msg)
}
pub fn recv<'a, 'b>(&'a self, msg: &'b mut [u8]) -> Result<(VsgAddress, VsgAddress, &'b mut [u8])> {
pub fn recv<'a, 'b>(&'a self, msg: &'b mut [u8]) -> Result<(libc::in_addr_t, libc::in_addr_t, &'b mut [u8])> {
self.0.recv(msg)
}
......
use crate::buffer_pool::Buffer;
use libc;
use std::cell::UnsafeCell;
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use super::{VsgAddress};
#[derive(Debug)]
pub enum Error {
......@@ -28,8 +28,8 @@ impl std::error::Error for Error {}
#[derive(Debug)]
struct OutputMsg {
send_time: Duration,
src: VsgAddress,
dst: VsgAddress,
src: libc::in_addr_t,
dst: libc::in_addr_t,
payload: Buffer,
}
......@@ -46,9 +46,9 @@ pub struct OutputMsgDrain<'a> {
}
impl<'a> Iterator for OutputMsgDrain<'a> {
type Item = (Duration, VsgAddress, VsgAddress, Buffer);
type Item = (Duration, libc::in_addr_t, libc::in_addr_t, Buffer);
fn next<'b>(&'b mut self) -> Option<(Duration, VsgAddress, VsgAddress, Buffer)> {
fn next<'b>(&'b mut self) -> Option<(Duration, libc::in_addr_t, libc::in_addr_t, Buffer)> {
let msg_set = self.msg_set;
let num_slots = msg_set.slots.len();
let next_index = self.index;
......@@ -81,7 +81,7 @@ impl OutputMsgSet {
}
}
pub fn insert(&self, send_time: Duration, src: VsgAddress, dst: VsgAddress, payload: Buffer) -> Result<()> {
pub fn insert(&self, send_time: Duration, src: libc::in_addr_t, dst: libc::in_addr_t, payload: Buffer) -> Result<()> {
for (idx, slot) in self.slot_busy.iter().enumerate() {
if !slot.swap(true, Ordering::AcqRel) {
let output_msg = OutputMsg {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment