[Protocol] Enable the use of Ipv6 addresses (#41)

Replace usages of Ipv4Addr with IpAddr which allows the use of either Ipv4 or Ipv6.

This patch essentially consists of running:
"sed -i 's/Ipv4Addr/IpAddr/g' src/**/*.rs examples/*"
and fixing the errors.
This commit is contained in:
Tom 2023-05-29 08:10:21 +00:00 committed by GitHub
parent e620398615
commit 3f654e0dfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
60 changed files with 155 additions and 162 deletions

View file

@ -9,12 +9,12 @@ use std::{
io::{Read, Write},
net,
};
use std::net::Ipv4Addr;
use std::net::IpAddr;
const DEFAULT_PACKET_SIZE: usize = 1024;
pub trait Socket {
fn new(address: &Ipv4Addr, port: u16) -> GDResult<Self>
fn new(address: &IpAddr, port: u16) -> GDResult<Self>
where Self: Sized;
fn apply_timeout(&self, timeout_settings: Option<TimeoutSettings>) -> GDResult<()>;
@ -28,7 +28,7 @@ pub struct TcpSocket {
}
impl Socket for TcpSocket {
fn new(address: &Ipv4Addr, port: u16) -> GDResult<Self> {
fn new(address: &IpAddr, port: u16) -> GDResult<Self> {
let complete_address = address_and_port_as_string(address, port);
Ok(Self {
@ -65,7 +65,7 @@ pub struct UdpSocket {
}
impl Socket for UdpSocket {
fn new(address: &Ipv4Addr, port: u16) -> GDResult<Self> {
fn new(address: &IpAddr, port: u16) -> GDResult<Self> {
let complete_address = address_and_port_as_string(address, port);
let socket = net::UdpSocket::bind("0.0.0.0:0").map_err(|_| SocketBind)?;
@ -100,7 +100,6 @@ impl Socket for UdpSocket {
#[cfg(test)]
mod tests {
use std::net::IpAddr;
use std::thread;
use super::*;
@ -117,10 +116,7 @@ mod tests {
stream.write(&buf).unwrap();
});
let address = match bound_address.ip() {
IpAddr::V4(a) => a,
_ => panic!("address not ipv4!")
};
let address = bound_address.ip();
// Create a TCP socket and send a message to the server
let mut socket = TcpSocket::new(&address, bound_address.port()).unwrap();
@ -153,10 +149,7 @@ mod tests {
socket.send_to(&buf, src_addr).unwrap();
});
let address = match bound_address.ip() {
IpAddr::V4(a) => a,
_ => panic!("address not ipv4!")
};
let address = bound_address.ip();
// Create a UDP socket and send a message to the server
let mut socket = UdpSocket::new(&address, bound_address.port()).unwrap();