@@ -75,6 +75,15 @@ impl SocketAddr {
75
75
SocketAddr :: V6 ( ref a) => a. port ( ) ,
76
76
}
77
77
}
78
+
79
+ /// Change the port number associated with this socket address.
80
+ #[ unstable( feature = "sockaddr_set_port" , reason = "recent addition" , issue = "0" ) ] // FIXME add tracking issue
81
+ pub fn set_port ( & mut self , new_port : u16 ) {
82
+ match * self {
83
+ SocketAddr :: V4 ( ref mut a) => a. set_port ( new_port) ,
84
+ SocketAddr :: V6 ( ref mut a) => a. set_port ( new_port) ,
85
+ }
86
+ }
78
87
}
79
88
80
89
impl SocketAddrV4 {
@@ -102,6 +111,10 @@ impl SocketAddrV4 {
102
111
/// Returns the port number associated with this socket address.
103
112
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
113
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin_port ) }
114
+
115
+ /// Change the port number associated with this socket address.
116
+ #[ unstable( feature = "sockaddr_set_port" , reason = "recent addition" , issue = "0" ) ] // FIXME add tracking issue
117
+ pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin_port = hton ( new_port) }
105
118
}
106
119
107
120
impl SocketAddrV6 {
@@ -134,6 +147,10 @@ impl SocketAddrV6 {
134
147
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
135
148
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin6_port ) }
136
149
150
+ /// Change the port number associated with this socket address.
151
+ #[ unstable( feature = "sockaddr_set_port" , reason = "recent addition" , issue = "0" ) ] // FIXME add tracking issue
152
+ pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin6_port = hton ( new_port) }
153
+
137
154
/// Returns the flow information associated with this address,
138
155
/// corresponding to the `sin6_flowinfo` field in C.
139
156
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -385,16 +402,9 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
385
402
fn resolve_socket_addr ( s : & str , p : u16 ) -> io:: Result < vec:: IntoIter < SocketAddr > > {
386
403
let ips = try!( lookup_host ( s) ) ;
387
404
let v: Vec < _ > = try!( ips. map ( |a| {
388
- a. map ( |a| {
389
- match a {
390
- SocketAddr :: V4 ( ref a) => {
391
- SocketAddr :: V4 ( SocketAddrV4 :: new ( * a. ip ( ) , p) )
392
- }
393
- SocketAddr :: V6 ( ref a) => {
394
- SocketAddr :: V6 ( SocketAddrV6 :: new ( * a. ip ( ) , p, a. flowinfo ( ) ,
395
- a. scope_id ( ) ) )
396
- }
397
- }
405
+ a. map ( |mut a| {
406
+ a. set_port ( p) ;
407
+ a
398
408
} )
399
409
} ) . collect ( ) ) ;
400
410
Ok ( v. into_iter ( ) )
@@ -511,4 +521,27 @@ mod tests {
511
521
fn to_socket_addr_str_bad ( ) {
512
522
assert ! ( tsa( "1200::AB00:1234::2552:7777:1313:34300" ) . is_err( ) ) ;
513
523
}
524
+
525
+ #[ test]
526
+ fn set_port ( ) {
527
+ let mut v4 = SocketAddrV4 :: new ( Ipv4Addr :: new ( 77 , 88 , 21 , 11 ) , 80 ) ;
528
+ assert_eq ! ( v4. port( ) , 80 ) ;
529
+ v4. set_port ( 443 ) ;
530
+ assert_eq ! ( v4. port( ) , 443 ) ;
531
+
532
+ let mut addr = SocketAddr :: V4 ( v4) ;
533
+ assert_eq ! ( addr. port( ) , 443 ) ;
534
+ addr. set_port ( 8080 ) ;
535
+ assert_eq ! ( addr. port( ) , 8080 ) ;
536
+
537
+ let mut v6 = SocketAddrV6 :: new ( Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , 1 ) , 80 , 0 , 0 ) ;
538
+ assert_eq ! ( v6. port( ) , 80 ) ;
539
+ v6. set_port ( 443 ) ;
540
+ assert_eq ! ( v6. port( ) , 443 ) ;
541
+
542
+ let mut addr = SocketAddr :: V6 ( v6) ;
543
+ assert_eq ! ( addr. port( ) , 443 ) ;
544
+ addr. set_port ( 8080 ) ;
545
+ assert_eq ! ( addr. port( ) , 8080 ) ;
546
+ }
514
547
}
0 commit comments