Skip to content

Commit 22f9ce4

Browse files
committed
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?
2 parents f7c4359 + 9046516 commit 22f9ce4

File tree

8 files changed

+142
-148
lines changed

8 files changed

+142
-148
lines changed

src/libstd/rt/io/net/ip.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,61 @@ type Port = u16;
1515

1616
#[deriving(Eq, TotalEq)]
1717
pub enum IpAddr {
18-
Ipv4(u8, u8, u8, u8, Port),
19-
Ipv6(u16, u16, u16, u16, u16, u16, u16, u16, Port)
18+
Ipv4Addr(u8, u8, u8, u8),
19+
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16)
2020
}
2121

2222
impl ToStr for IpAddr {
2323
fn to_str(&self) -> ~str {
2424
match *self {
25-
Ipv4(a, b, c, d, p) =>
26-
fmt!("%u.%u.%u.%u:%u",
27-
a as uint, b as uint, c as uint, d as uint, p as uint),
25+
Ipv4Addr(a, b, c, d) =>
26+
fmt!("%u.%u.%u.%u",
27+
a as uint, b as uint, c as uint, d as uint),
2828

2929
// Ipv4 Compatible address
30-
Ipv6(0, 0, 0, 0, 0, 0, g, h, p) => {
30+
Ipv6Addr(0, 0, 0, 0, 0, 0, g, h) => {
3131
let a = fmt!("%04x", g as uint);
3232
let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
3333
let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
3434
let c = fmt!("%04x", h as uint);
3535
let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
3636
let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
3737

38-
fmt!("[::%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
38+
fmt!("::%u.%u.%u.%u", a, b, c, d)
3939
}
4040

4141
// Ipv4-Mapped address
42-
Ipv6(0, 0, 0, 0, 0, 1, g, h, p) => {
42+
Ipv6Addr(0, 0, 0, 0, 0, 1, g, h) => {
4343
let a = fmt!("%04x", g as uint);
4444
let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
4545
let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
4646
let c = fmt!("%04x", h as uint);
4747
let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
4848
let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
4949

50-
fmt!("[::FFFF:%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
50+
fmt!("::FFFF:%u.%u.%u.%u", a, b, c, d)
5151
}
5252

53-
Ipv6(a, b, c, d, e, f, g, h, p) =>
54-
fmt!("[%x:%x:%x:%x:%x:%x:%x:%x]:%u",
53+
Ipv6Addr(a, b, c, d, e, f, g, h) =>
54+
fmt!("%x:%x:%x:%x:%x:%x:%x:%x",
5555
a as uint, b as uint, c as uint, d as uint,
56-
e as uint, f as uint, g as uint, h as uint, p as uint)
56+
e as uint, f as uint, g as uint, h as uint)
57+
}
58+
}
59+
}
60+
61+
#[deriving(Eq, TotalEq)]
62+
pub struct SocketAddr {
63+
ip: IpAddr,
64+
port: Port,
65+
}
66+
67+
68+
impl ToStr for SocketAddr {
69+
fn to_str(&self) -> ~str {
70+
match self.ip {
71+
Ipv4Addr(*) => fmt!("%s:%u", self.ip.to_str(), self.port as uint),
72+
Ipv6Addr(*) => fmt!("[%s]:%u", self.ip.to_str(), self.port as uint),
5773
}
5874
}
5975
}

src/libstd/rt/io/net/tcp.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use option::{Option, Some, None};
1212
use result::{Ok, Err};
13-
use rt::io::net::ip::IpAddr;
13+
use rt::io::net::ip::SocketAddr;
1414
use rt::io::{Reader, Writer, Listener};
1515
use rt::io::{io_error, read_error, EndOfFile};
1616
use rt::rtio::{IoFactory, IoFactoryObject,
@@ -26,7 +26,7 @@ impl TcpStream {
2626
TcpStream(s)
2727
}
2828

29-
pub fn connect(addr: IpAddr) -> Option<TcpStream> {
29+
pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
3030
let stream = unsafe {
3131
rtdebug!("borrowing io to connect");
3232
let io = Local::unsafe_borrow::<IoFactoryObject>();
@@ -44,7 +44,7 @@ impl TcpStream {
4444
}
4545
}
4646

47-
pub fn peer_name(&mut self) -> Option<IpAddr> {
47+
pub fn peer_name(&mut self) -> Option<SocketAddr> {
4848
match (**self).peer_name() {
4949
Ok(pn) => Some(pn),
5050
Err(ioerr) => {
@@ -55,7 +55,7 @@ impl TcpStream {
5555
}
5656
}
5757

58-
pub fn socket_name(&mut self) -> Option<IpAddr> {
58+
pub fn socket_name(&mut self) -> Option<SocketAddr> {
5959
match (**self).socket_name() {
6060
Ok(sn) => Some(sn),
6161
Err(ioerr) => {
@@ -100,7 +100,7 @@ impl Writer for TcpStream {
100100
pub struct TcpListener(~RtioTcpListenerObject);
101101

102102
impl TcpListener {
103-
pub fn bind(addr: IpAddr) -> Option<TcpListener> {
103+
pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
104104
let listener = unsafe {
105105
let io = Local::unsafe_borrow::<IoFactoryObject>();
106106
(*io).tcp_bind(addr)
@@ -114,7 +114,7 @@ impl TcpListener {
114114
}
115115
}
116116

117-
pub fn socket_name(&mut self) -> Option<IpAddr> {
117+
pub fn socket_name(&mut self) -> Option<SocketAddr> {
118118
match (**self).socket_name() {
119119
Ok(sn) => Some(sn),
120120
Err(ioerr) => {
@@ -145,7 +145,7 @@ mod test {
145145
use super::*;
146146
use cell::Cell;
147147
use rt::test::*;
148-
use rt::io::net::ip::Ipv4;
148+
use rt::io::net::ip::{Ipv4Addr, SocketAddr};
149149
use rt::io::*;
150150
use prelude::*;
151151

@@ -157,7 +157,7 @@ mod test {
157157
assert!(e.kind == PermissionDenied);
158158
called = true;
159159
}).inside {
160-
let addr = Ipv4(0, 0, 0, 0, 1);
160+
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
161161
let listener = TcpListener::bind(addr);
162162
assert!(listener.is_none());
163163
}
@@ -173,7 +173,7 @@ mod test {
173173
assert!(e.kind == ConnectionRefused);
174174
called = true;
175175
}).inside {
176-
let addr = Ipv4(0, 0, 0, 0, 1);
176+
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
177177
let stream = TcpStream::connect(addr);
178178
assert!(stream.is_none());
179179
}
@@ -437,7 +437,7 @@ mod test {
437437

438438
connect(0, addr);
439439

440-
fn connect(i: int, addr: IpAddr) {
440+
fn connect(i: int, addr: SocketAddr) {
441441
if i == MAX { return }
442442

443443
do spawntask {
@@ -476,7 +476,7 @@ mod test {
476476

477477
connect(0, addr);
478478

479-
fn connect(i: int, addr: IpAddr) {
479+
fn connect(i: int, addr: SocketAddr) {
480480
if i == MAX { return }
481481

482482
do spawntask {
@@ -515,7 +515,7 @@ mod test {
515515

516516
connect(0, addr);
517517

518-
fn connect(i: int, addr: IpAddr) {
518+
fn connect(i: int, addr: SocketAddr) {
519519
if i == MAX { return }
520520

521521
do spawntask_later {
@@ -553,7 +553,7 @@ mod test {
553553

554554
connect(0, addr);
555555

556-
fn connect(i: int, addr: IpAddr) {
556+
fn connect(i: int, addr: SocketAddr) {
557557
if i == MAX { return }
558558

559559
do spawntask_later {
@@ -569,7 +569,7 @@ mod test {
569569
}
570570

571571
#[cfg(test)]
572-
fn socket_name(addr: IpAddr) {
572+
fn socket_name(addr: SocketAddr) {
573573
do run_in_newsched_task {
574574
do spawntask {
575575
let listener = TcpListener::bind(addr);
@@ -588,7 +588,7 @@ mod test {
588588
}
589589

590590
#[cfg(test)]
591-
fn peer_name(addr: IpAddr) {
591+
fn peer_name(addr: SocketAddr) {
592592
do run_in_newsched_task {
593593
do spawntask {
594594
let mut listener = TcpListener::bind(addr);

src/libstd/rt/io/net/udp.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use option::{Option, Some, None};
1212
use result::{Ok, Err};
13-
use rt::io::net::ip::IpAddr;
13+
use rt::io::net::ip::SocketAddr;
1414
use rt::io::{Reader, Writer};
1515
use rt::io::{io_error, read_error, EndOfFile};
1616
use rt::rtio::{RtioSocket, RtioUdpSocketObject, RtioUdpSocket, IoFactory, IoFactoryObject};
@@ -19,7 +19,7 @@ use rt::local::Local;
1919
pub struct UdpSocket(~RtioUdpSocketObject);
2020

2121
impl UdpSocket {
22-
pub fn bind(addr: IpAddr) -> Option<UdpSocket> {
22+
pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
2323
let socket = unsafe { (*Local::unsafe_borrow::<IoFactoryObject>()).udp_bind(addr) };
2424
match socket {
2525
Ok(s) => Some(UdpSocket(s)),
@@ -30,7 +30,7 @@ impl UdpSocket {
3030
}
3131
}
3232

33-
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
33+
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> {
3434
match (**self).recvfrom(buf) {
3535
Ok((nread, src)) => Some((nread, src)),
3636
Err(ioerr) => {
@@ -43,18 +43,18 @@ impl UdpSocket {
4343
}
4444
}
4545

46-
pub fn sendto(&mut self, buf: &[u8], dst: IpAddr) {
46+
pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) {
4747
match (**self).sendto(buf, dst) {
4848
Ok(_) => (),
4949
Err(ioerr) => io_error::cond.raise(ioerr),
5050
}
5151
}
5252

53-
pub fn connect(self, other: IpAddr) -> UdpStream {
53+
pub fn connect(self, other: SocketAddr) -> UdpStream {
5454
UdpStream { socket: self, connectedTo: other }
5555
}
5656

57-
pub fn socket_name(&mut self) -> Option<IpAddr> {
57+
pub fn socket_name(&mut self) -> Option<SocketAddr> {
5858
match (***self).socket_name() {
5959
Ok(sn) => Some(sn),
6060
Err(ioerr) => {
@@ -68,7 +68,7 @@ impl UdpSocket {
6868

6969
pub struct UdpStream {
7070
socket: UdpSocket,
71-
connectedTo: IpAddr
71+
connectedTo: SocketAddr
7272
}
7373

7474
impl UdpStream {
@@ -106,7 +106,7 @@ impl Writer for UdpStream {
106106
mod test {
107107
use super::*;
108108
use rt::test::*;
109-
use rt::io::net::ip::Ipv4;
109+
use rt::io::net::ip::{Ipv4Addr, SocketAddr};
110110
use rt::io::*;
111111
use option::{Some, None};
112112

@@ -118,7 +118,7 @@ mod test {
118118
assert!(e.kind == PermissionDenied);
119119
called = true;
120120
}).inside {
121-
let addr = Ipv4(0, 0, 0, 0, 1);
121+
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
122122
let socket = UdpSocket::bind(addr);
123123
assert!(socket.is_none());
124124
}
@@ -265,7 +265,7 @@ mod test {
265265
}
266266

267267
#[cfg(test)]
268-
fn socket_name(addr: IpAddr) {
268+
fn socket_name(addr: SocketAddr) {
269269
do run_in_newsched_task {
270270
do spawntask {
271271
let server = UdpSocket::bind(addr);

src/libstd/rt/rtio.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use option::*;
1212
use result::*;
1313

1414
use rt::io::IoError;
15-
use super::io::net::ip::IpAddr;
15+
use super::io::net::ip::{IpAddr, SocketAddr};
1616
use rt::uv::uvio;
1717

1818
// XXX: ~object doesn't work currently so these are some placeholder
@@ -44,9 +44,9 @@ pub trait RemoteCallback {
4444
}
4545

4646
pub trait IoFactory {
47-
fn tcp_connect(&mut self, addr: IpAddr) -> Result<~RtioTcpStreamObject, IoError>;
48-
fn tcp_bind(&mut self, addr: IpAddr) -> Result<~RtioTcpListenerObject, IoError>;
49-
fn udp_bind(&mut self, addr: IpAddr) -> Result<~RtioUdpSocketObject, IoError>;
47+
fn tcp_connect(&mut self, addr: SocketAddr) -> Result<~RtioTcpStreamObject, IoError>;
48+
fn tcp_bind(&mut self, addr: SocketAddr) -> Result<~RtioTcpListenerObject, IoError>;
49+
fn udp_bind(&mut self, addr: SocketAddr) -> Result<~RtioUdpSocketObject, IoError>;
5050
fn timer_init(&mut self) -> Result<~RtioTimerObject, IoError>;
5151
}
5252

@@ -59,20 +59,20 @@ pub trait RtioTcpListener : RtioSocket {
5959
pub trait RtioTcpStream : RtioSocket {
6060
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError>;
6161
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
62-
fn peer_name(&mut self) -> Result<IpAddr, IoError>;
62+
fn peer_name(&mut self) -> Result<SocketAddr, IoError>;
6363
fn control_congestion(&mut self) -> Result<(), IoError>;
6464
fn nodelay(&mut self) -> Result<(), IoError>;
6565
fn keepalive(&mut self, delay_in_seconds: uint) -> Result<(), IoError>;
6666
fn letdie(&mut self) -> Result<(), IoError>;
6767
}
6868

6969
pub trait RtioSocket {
70-
fn socket_name(&mut self) -> Result<IpAddr, IoError>;
70+
fn socket_name(&mut self) -> Result<SocketAddr, IoError>;
7171
}
7272

7373
pub trait RtioUdpSocket : RtioSocket {
74-
fn recvfrom(&mut self, buf: &mut [u8]) -> Result<(uint, IpAddr), IoError>;
75-
fn sendto(&mut self, buf: &[u8], dst: IpAddr) -> Result<(), IoError>;
74+
fn recvfrom(&mut self, buf: &mut [u8]) -> Result<(uint, SocketAddr), IoError>;
75+
fn sendto(&mut self, buf: &[u8], dst: SocketAddr) -> Result<(), IoError>;
7676

7777
fn join_multicast(&mut self, multi: IpAddr) -> Result<(), IoError>;
7878
fn leave_multicast(&mut self, multi: IpAddr) -> Result<(), IoError>;

src/libstd/rt/test.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use clone::Clone;
1616
use container::Container;
1717
use iterator::{Iterator, range};
1818
use vec::{OwnedVector, MutableVector};
19-
use super::io::net::ip::{IpAddr, Ipv4, Ipv6};
19+
use super::io::net::ip::{SocketAddr, Ipv4Addr, Ipv6Addr};
2020
use rt::sched::Scheduler;
2121
use unstable::run_in_bare_thread;
2222
use rt::thread::Thread;
@@ -306,13 +306,13 @@ pub fn next_test_port() -> u16 {
306306
}
307307

308308
/// Get a unique IPv4 localhost:port pair starting at 9600
309-
pub fn next_test_ip4() -> IpAddr {
310-
Ipv4(127, 0, 0, 1, next_test_port())
309+
pub fn next_test_ip4() -> SocketAddr {
310+
SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: next_test_port() }
311311
}
312312

313313
/// Get a unique IPv6 localhost:port pair starting at 9600
314-
pub fn next_test_ip6() -> IpAddr {
315-
Ipv6(0, 0, 0, 0, 0, 0, 0, 1, next_test_port())
314+
pub fn next_test_ip6() -> SocketAddr {
315+
SocketAddr { ip: Ipv6Addr(0, 0, 0, 0, 0, 0, 0, 1), port: next_test_port() }
316316
}
317317

318318
/*

src/libstd/rt/uv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use libc::{c_void, c_int, size_t, malloc, free};
4747
use cast::transmute;
4848
use ptr::null;
4949
use unstable::finally::Finally;
50-
use rt::io::net::ip::IpAddr;
50+
use rt::io::net::ip::SocketAddr;
5151

5252
use rt::io::IoError;
5353

@@ -128,7 +128,7 @@ pub type ConnectionCallback = ~fn(StreamWatcher, Option<UvError>);
128128
pub type FsCallback = ~fn(FsRequest, Option<UvError>);
129129
pub type TimerCallback = ~fn(TimerWatcher, Option<UvError>);
130130
pub type AsyncCallback = ~fn(AsyncWatcher, Option<UvError>);
131-
pub type UdpReceiveCallback = ~fn(UdpWatcher, int, Buf, IpAddr, uint, Option<UvError>);
131+
pub type UdpReceiveCallback = ~fn(UdpWatcher, int, Buf, SocketAddr, uint, Option<UvError>);
132132
pub type UdpSendCallback = ~fn(UdpWatcher, Option<UvError>);
133133

134134

0 commit comments

Comments
 (0)