about summary refs log tree commit diff
path: root/src/libstd/rt/io
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-08-04 08:49:55 -0700
committerbors <bors@rust-lang.org>2013-08-04 08:49:55 -0700
commit22f9ce4df682cd4dd1203e5ae3d8c95f5140e209 (patch)
tree049f876da7bc2936c73484b0a95e8e5e3f995ce6 /src/libstd/rt/io
parentf7c4359a2c350521c312bd5dce3ce515878d922a (diff)
parent90465164446ca823cc9e9ecdd40747455f429525 (diff)
downloadrust-22f9ce4df682cd4dd1203e5ae3d8c95f5140e209.tar.gz
rust-22f9ce4df682cd4dd1203e5ae3d8c95f5140e209.zip
auto merge of #8243 : stepancheg/rust/ipv, r=brson
multicast functions now take IpAddr (without port), because they dont't
need port.

Uv* types renamed:
* UvIpAddr -> UvSocketAddr
* UvIpv4 -> UvIpv4SocketAddr
* UvIpv6 -> UvIpv6SocketAddr

"Socket address" is a common name for (ip-address, port) pair (e.g. in
sockaddr_in struct).

P. S. Are there any backward compatibility concerns? What is std::rt module, is it a part of public API?
Diffstat (limited to 'src/libstd/rt/io')
-rw-r--r--src/libstd/rt/io/net/ip.rs40
-rw-r--r--src/libstd/rt/io/net/tcp.rs30
-rw-r--r--src/libstd/rt/io/net/udp.rs20
3 files changed, 53 insertions, 37 deletions
diff --git a/src/libstd/rt/io/net/ip.rs b/src/libstd/rt/io/net/ip.rs
index 2b572574b60..815ec9b5c61 100644
--- a/src/libstd/rt/io/net/ip.rs
+++ b/src/libstd/rt/io/net/ip.rs
@@ -15,19 +15,19 @@ type Port = u16;
 
 #[deriving(Eq, TotalEq)]
 pub enum IpAddr {
-    Ipv4(u8, u8, u8, u8, Port),
-    Ipv6(u16, u16, u16, u16, u16, u16, u16, u16, Port)
+    Ipv4Addr(u8, u8, u8, u8),
+    Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16)
 }
 
 impl ToStr for IpAddr {
     fn to_str(&self) -> ~str {
         match *self {
-            Ipv4(a, b, c, d, p) =>
-                fmt!("%u.%u.%u.%u:%u",
-                    a as uint, b as uint, c as uint, d as uint, p as uint),
+            Ipv4Addr(a, b, c, d) =>
+                fmt!("%u.%u.%u.%u",
+                    a as uint, b as uint, c as uint, d as uint),
 
             // Ipv4 Compatible address
-            Ipv6(0, 0, 0, 0, 0, 0, g, h, p) => {
+            Ipv6Addr(0, 0, 0, 0, 0, 0, g, h) => {
                 let a = fmt!("%04x", g as uint);
                 let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
                 let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
@@ -35,11 +35,11 @@ impl ToStr for IpAddr {
                 let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
                 let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
 
-                fmt!("[::%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
+                fmt!("::%u.%u.%u.%u", a, b, c, d)
             }
 
             // Ipv4-Mapped address
-            Ipv6(0, 0, 0, 0, 0, 1, g, h, p) => {
+            Ipv6Addr(0, 0, 0, 0, 0, 1, g, h) => {
                 let a = fmt!("%04x", g as uint);
                 let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
                 let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
@@ -47,13 +47,29 @@ impl ToStr for IpAddr {
                 let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
                 let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
 
-                fmt!("[::FFFF:%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
+                fmt!("::FFFF:%u.%u.%u.%u", a, b, c, d)
             }
 
-            Ipv6(a, b, c, d, e, f, g, h, p) =>
-                fmt!("[%x:%x:%x:%x:%x:%x:%x:%x]:%u",
+            Ipv6Addr(a, b, c, d, e, f, g, h) =>
+                fmt!("%x:%x:%x:%x:%x:%x:%x:%x",
                     a as uint, b as uint, c as uint, d as uint,
-                    e as uint, f as uint, g as uint, h as uint, p as uint)
+                    e as uint, f as uint, g as uint, h as uint)
+        }
+    }
+}
+
+#[deriving(Eq, TotalEq)]
+pub struct SocketAddr {
+    ip: IpAddr,
+    port: Port,
+}
+
+
+impl ToStr for SocketAddr {
+    fn to_str(&self) -> ~str {
+        match self.ip {
+            Ipv4Addr(*) => fmt!("%s:%u", self.ip.to_str(), self.port as uint),
+            Ipv6Addr(*) => fmt!("[%s]:%u", self.ip.to_str(), self.port as uint),
         }
     }
 }
diff --git a/src/libstd/rt/io/net/tcp.rs b/src/libstd/rt/io/net/tcp.rs
index 780aa9cb75c..27222542e08 100644
--- a/src/libstd/rt/io/net/tcp.rs
+++ b/src/libstd/rt/io/net/tcp.rs
@@ -10,7 +10,7 @@
 
 use option::{Option, Some, None};
 use result::{Ok, Err};
-use rt::io::net::ip::IpAddr;
+use rt::io::net::ip::SocketAddr;
 use rt::io::{Reader, Writer, Listener};
 use rt::io::{io_error, read_error, EndOfFile};
 use rt::rtio::{IoFactory, IoFactoryObject,
@@ -26,7 +26,7 @@ impl TcpStream {
         TcpStream(s)
     }
 
-    pub fn connect(addr: IpAddr) -> Option<TcpStream> {
+    pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
         let stream = unsafe {
             rtdebug!("borrowing io to connect");
             let io = Local::unsafe_borrow::<IoFactoryObject>();
@@ -44,7 +44,7 @@ impl TcpStream {
         }
     }
 
-    pub fn peer_name(&mut self) -> Option<IpAddr> {
+    pub fn peer_name(&mut self) -> Option<SocketAddr> {
         match (**self).peer_name() {
             Ok(pn) => Some(pn),
             Err(ioerr) => {
@@ -55,7 +55,7 @@ impl TcpStream {
         }
     }
 
-    pub fn socket_name(&mut self) -> Option<IpAddr> {
+    pub fn socket_name(&mut self) -> Option<SocketAddr> {
         match (**self).socket_name() {
             Ok(sn) => Some(sn),
             Err(ioerr) => {
@@ -100,7 +100,7 @@ impl Writer for TcpStream {
 pub struct TcpListener(~RtioTcpListenerObject);
 
 impl TcpListener {
-    pub fn bind(addr: IpAddr) -> Option<TcpListener> {
+    pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
         let listener = unsafe {
             let io = Local::unsafe_borrow::<IoFactoryObject>();
             (*io).tcp_bind(addr)
@@ -114,7 +114,7 @@ impl TcpListener {
         }
     }
 
-    pub fn socket_name(&mut self) -> Option<IpAddr> {
+    pub fn socket_name(&mut self) -> Option<SocketAddr> {
         match (**self).socket_name() {
             Ok(sn) => Some(sn),
             Err(ioerr) => {
@@ -145,7 +145,7 @@ mod test {
     use super::*;
     use cell::Cell;
     use rt::test::*;
-    use rt::io::net::ip::Ipv4;
+    use rt::io::net::ip::{Ipv4Addr, SocketAddr};
     use rt::io::*;
     use prelude::*;
 
@@ -157,7 +157,7 @@ mod test {
                 assert!(e.kind == PermissionDenied);
                 called = true;
             }).inside {
-                let addr = Ipv4(0, 0, 0, 0, 1);
+                let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
                 let listener = TcpListener::bind(addr);
                 assert!(listener.is_none());
             }
@@ -173,7 +173,7 @@ mod test {
                 assert!(e.kind == ConnectionRefused);
                 called = true;
             }).inside {
-                let addr = Ipv4(0, 0, 0, 0, 1);
+                let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
                 let stream = TcpStream::connect(addr);
                 assert!(stream.is_none());
             }
@@ -437,7 +437,7 @@ mod test {
 
             connect(0, addr);
 
-            fn connect(i: int, addr: IpAddr) {
+            fn connect(i: int, addr: SocketAddr) {
                 if i == MAX { return }
 
                 do spawntask {
@@ -476,7 +476,7 @@ mod test {
 
             connect(0, addr);
 
-            fn connect(i: int, addr: IpAddr) {
+            fn connect(i: int, addr: SocketAddr) {
                 if i == MAX { return }
 
                 do spawntask {
@@ -515,7 +515,7 @@ mod test {
 
             connect(0, addr);
 
-            fn connect(i: int, addr: IpAddr) {
+            fn connect(i: int, addr: SocketAddr) {
                 if i == MAX { return }
 
                 do spawntask_later {
@@ -553,7 +553,7 @@ mod test {
 
             connect(0, addr);
 
-            fn connect(i: int, addr: IpAddr) {
+            fn connect(i: int, addr: SocketAddr) {
                 if i == MAX { return }
 
                 do spawntask_later {
@@ -569,7 +569,7 @@ mod test {
     }
 
     #[cfg(test)]
-    fn socket_name(addr: IpAddr) {
+    fn socket_name(addr: SocketAddr) {
         do run_in_newsched_task {
             do spawntask {
                 let listener = TcpListener::bind(addr);
@@ -588,7 +588,7 @@ mod test {
     }
 
     #[cfg(test)]
-    fn peer_name(addr: IpAddr) {
+    fn peer_name(addr: SocketAddr) {
         do run_in_newsched_task {
             do spawntask {
                 let mut listener = TcpListener::bind(addr);
diff --git a/src/libstd/rt/io/net/udp.rs b/src/libstd/rt/io/net/udp.rs
index c04abfa899b..644abcbe145 100644
--- a/src/libstd/rt/io/net/udp.rs
+++ b/src/libstd/rt/io/net/udp.rs
@@ -10,7 +10,7 @@
 
 use option::{Option, Some, None};
 use result::{Ok, Err};
-use rt::io::net::ip::IpAddr;
+use rt::io::net::ip::SocketAddr;
 use rt::io::{Reader, Writer};
 use rt::io::{io_error, read_error, EndOfFile};
 use rt::rtio::{RtioSocket, RtioUdpSocketObject, RtioUdpSocket, IoFactory, IoFactoryObject};
@@ -19,7 +19,7 @@ use rt::local::Local;
 pub struct UdpSocket(~RtioUdpSocketObject);
 
 impl UdpSocket {
-    pub fn bind(addr: IpAddr) -> Option<UdpSocket> {
+    pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
         let socket = unsafe { (*Local::unsafe_borrow::<IoFactoryObject>()).udp_bind(addr) };
         match socket {
             Ok(s) => Some(UdpSocket(s)),
@@ -30,7 +30,7 @@ impl UdpSocket {
         }
     }
 
-    pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
+    pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> {
         match (**self).recvfrom(buf) {
             Ok((nread, src)) => Some((nread, src)),
             Err(ioerr) => {
@@ -43,18 +43,18 @@ impl UdpSocket {
         }
     }
 
-    pub fn sendto(&mut self, buf: &[u8], dst: IpAddr) {
+    pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) {
         match (**self).sendto(buf, dst) {
             Ok(_) => (),
             Err(ioerr) => io_error::cond.raise(ioerr),
         }
     }
 
-    pub fn connect(self, other: IpAddr) -> UdpStream {
+    pub fn connect(self, other: SocketAddr) -> UdpStream {
         UdpStream { socket: self, connectedTo: other }
     }
 
-    pub fn socket_name(&mut self) -> Option<IpAddr> {
+    pub fn socket_name(&mut self) -> Option<SocketAddr> {
         match (***self).socket_name() {
             Ok(sn) => Some(sn),
             Err(ioerr) => {
@@ -68,7 +68,7 @@ impl UdpSocket {
 
 pub struct UdpStream {
     socket: UdpSocket,
-    connectedTo: IpAddr
+    connectedTo: SocketAddr
 }
 
 impl UdpStream {
@@ -106,7 +106,7 @@ impl Writer for UdpStream {
 mod test {
     use super::*;
     use rt::test::*;
-    use rt::io::net::ip::Ipv4;
+    use rt::io::net::ip::{Ipv4Addr, SocketAddr};
     use rt::io::*;
     use option::{Some, None};
 
@@ -118,7 +118,7 @@ mod test {
                 assert!(e.kind == PermissionDenied);
                 called = true;
             }).inside {
-                let addr = Ipv4(0, 0, 0, 0, 1);
+                let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
                 let socket = UdpSocket::bind(addr);
                 assert!(socket.is_none());
             }
@@ -265,7 +265,7 @@ mod test {
     }
 
     #[cfg(test)]
-    fn socket_name(addr: IpAddr) {
+    fn socket_name(addr: SocketAddr) {
         do run_in_newsched_task {
             do spawntask {
                 let server = UdpSocket::bind(addr);