@@ -67,6 +67,17 @@ impl SocketAddr {
67
67
}
68
68
}
69
69
70
+ /// Change the IP address associated with this socket address.
71
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
72
+ pub fn set_ip ( & mut self , new_ip : IpAddr ) {
73
+ // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
74
+ match ( self , new_ip) {
75
+ ( & mut SocketAddr :: V4 ( ref mut a) , IpAddr :: V4 ( new_ip) ) => a. set_ip ( new_ip) ,
76
+ ( & mut SocketAddr :: V6 ( ref mut a) , IpAddr :: V6 ( new_ip) ) => a. set_ip ( new_ip) ,
77
+ ( self_, new_ip) => * self_ = Self :: new ( new_ip, self_. port ( ) ) ,
78
+ }
79
+ }
80
+
70
81
/// Returns the port number associated with this socket address.
71
82
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
72
83
pub fn port ( & self ) -> u16 {
@@ -75,6 +86,15 @@ impl SocketAddr {
75
86
SocketAddr :: V6 ( ref a) => a. port ( ) ,
76
87
}
77
88
}
89
+
90
+ /// Change the port number associated with this socket address.
91
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
92
+ pub fn set_port ( & mut self , new_port : u16 ) {
93
+ match * self {
94
+ SocketAddr :: V4 ( ref mut a) => a. set_port ( new_port) ,
95
+ SocketAddr :: V6 ( ref mut a) => a. set_port ( new_port) ,
96
+ }
97
+ }
78
98
}
79
99
80
100
impl SocketAddrV4 {
@@ -99,9 +119,17 @@ impl SocketAddrV4 {
99
119
}
100
120
}
101
121
122
+ /// Change the IP address associated with this socket address.
123
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
124
+ pub fn set_ip ( & mut self , new_ip : Ipv4Addr ) { self . inner . sin_addr = * new_ip. as_inner ( ) }
125
+
102
126
/// Returns the port number associated with this socket address.
103
127
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
128
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin_port ) }
129
+
130
+ /// Change the port number associated with this socket address.
131
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
132
+ pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin_port = hton ( new_port) }
105
133
}
106
134
107
135
impl SocketAddrV6 {
@@ -130,19 +158,39 @@ impl SocketAddrV6 {
130
158
}
131
159
}
132
160
161
+ /// Change the IP address associated with this socket address.
162
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
163
+ pub fn set_ip ( & mut self , new_ip : Ipv6Addr ) { self . inner . sin6_addr = * new_ip. as_inner ( ) }
164
+
133
165
/// Returns the port number associated with this socket address.
134
166
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
135
167
pub fn port ( & self ) -> u16 { ntoh ( self . inner . sin6_port ) }
136
168
169
+ /// Change the port number associated with this socket address.
170
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
171
+ pub fn set_port ( & mut self , new_port : u16 ) { self . inner . sin6_port = hton ( new_port) }
172
+
137
173
/// Returns the flow information associated with this address,
138
174
/// corresponding to the `sin6_flowinfo` field in C.
139
175
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
140
176
pub fn flowinfo ( & self ) -> u32 { ntoh ( self . inner . sin6_flowinfo ) }
141
177
178
+ /// Change the flow information associated with this socket address.
179
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
180
+ pub fn set_flowinfo ( & mut self , new_flowinfo : u32 ) {
181
+ self . inner . sin6_flowinfo = hton ( new_flowinfo)
182
+ }
183
+
142
184
/// Returns the scope ID associated with this address,
143
185
/// corresponding to the `sin6_scope_id` field in C.
144
186
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
145
187
pub fn scope_id ( & self ) -> u32 { ntoh ( self . inner . sin6_scope_id ) }
188
+
189
+ /// Change the scope ID associated with this socket address.
190
+ #[ unstable( feature = "sockaddr_setters" , reason = "recent addition" , issue = "31572" ) ]
191
+ pub fn set_scope_id ( & mut self , new_scope_id : u32 ) {
192
+ self . inner . sin6_scope_id = hton ( new_scope_id)
193
+ }
146
194
}
147
195
148
196
impl FromInner < c:: sockaddr_in > for SocketAddrV4 {
@@ -385,16 +433,9 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
385
433
fn resolve_socket_addr ( s : & str , p : u16 ) -> io:: Result < vec:: IntoIter < SocketAddr > > {
386
434
let ips = try!( lookup_host ( s) ) ;
387
435
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
- }
436
+ a. map ( |mut a| {
437
+ a. set_port ( p) ;
438
+ a
398
439
} )
399
440
} ) . collect ( ) ) ;
400
441
Ok ( v. into_iter ( ) )
@@ -511,4 +552,73 @@ mod tests {
511
552
fn to_socket_addr_str_bad ( ) {
512
553
assert ! ( tsa( "1200::AB00:1234::2552:7777:1313:34300" ) . is_err( ) ) ;
513
554
}
555
+
556
+ #[ test]
557
+ fn set_ip ( ) {
558
+ fn ip4 ( low : u8 ) -> Ipv4Addr { Ipv4Addr :: new ( 77 , 88 , 21 , low) }
559
+ fn ip6 ( low : u16 ) -> Ipv6Addr { Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , low) }
560
+
561
+ let mut v4 = SocketAddrV4 :: new ( ip4 ( 11 ) , 80 ) ;
562
+ assert_eq ! ( v4. ip( ) , & ip4( 11 ) ) ;
563
+ v4. set_ip ( ip4 ( 12 ) ) ;
564
+ assert_eq ! ( v4. ip( ) , & ip4( 12 ) ) ;
565
+
566
+ let mut addr = SocketAddr :: V4 ( v4) ;
567
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 12 ) ) ) ;
568
+ addr. set_ip ( IpAddr :: V4 ( ip4 ( 13 ) ) ) ;
569
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 13 ) ) ) ;
570
+ addr. set_ip ( IpAddr :: V6 ( ip6 ( 14 ) ) ) ;
571
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 14 ) ) ) ;
572
+
573
+ let mut v6 = SocketAddrV6 :: new ( ip6 ( 1 ) , 80 , 0 , 0 ) ;
574
+ assert_eq ! ( v6. ip( ) , & ip6( 1 ) ) ;
575
+ v6. set_ip ( ip6 ( 2 ) ) ;
576
+ assert_eq ! ( v6. ip( ) , & ip6( 2 ) ) ;
577
+
578
+ let mut addr = SocketAddr :: V6 ( v6) ;
579
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 2 ) ) ) ;
580
+ addr. set_ip ( IpAddr :: V6 ( ip6 ( 3 ) ) ) ;
581
+ assert_eq ! ( addr. ip( ) , IpAddr :: V6 ( ip6( 3 ) ) ) ;
582
+ addr. set_ip ( IpAddr :: V4 ( ip4 ( 4 ) ) ) ;
583
+ assert_eq ! ( addr. ip( ) , IpAddr :: V4 ( ip4( 4 ) ) ) ;
584
+ }
585
+
586
+ #[ test]
587
+ fn set_port ( ) {
588
+ let mut v4 = SocketAddrV4 :: new ( Ipv4Addr :: new ( 77 , 88 , 21 , 11 ) , 80 ) ;
589
+ assert_eq ! ( v4. port( ) , 80 ) ;
590
+ v4. set_port ( 443 ) ;
591
+ assert_eq ! ( v4. port( ) , 443 ) ;
592
+
593
+ let mut addr = SocketAddr :: V4 ( v4) ;
594
+ assert_eq ! ( addr. port( ) , 443 ) ;
595
+ addr. set_port ( 8080 ) ;
596
+ assert_eq ! ( addr. port( ) , 8080 ) ;
597
+
598
+ let mut v6 = SocketAddrV6 :: new ( Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , 1 ) , 80 , 0 , 0 ) ;
599
+ assert_eq ! ( v6. port( ) , 80 ) ;
600
+ v6. set_port ( 443 ) ;
601
+ assert_eq ! ( v6. port( ) , 443 ) ;
602
+
603
+ let mut addr = SocketAddr :: V6 ( v6) ;
604
+ assert_eq ! ( addr. port( ) , 443 ) ;
605
+ addr. set_port ( 8080 ) ;
606
+ assert_eq ! ( addr. port( ) , 8080 ) ;
607
+ }
608
+
609
+ #[ test]
610
+ fn set_flowinfo ( ) {
611
+ let mut v6 = SocketAddrV6 :: new ( Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , 1 ) , 80 , 10 , 0 ) ;
612
+ assert_eq ! ( v6. flowinfo( ) , 10 ) ;
613
+ v6. set_flowinfo ( 20 ) ;
614
+ assert_eq ! ( v6. flowinfo( ) , 20 ) ;
615
+ }
616
+
617
+ #[ test]
618
+ fn set_scope_id ( ) {
619
+ let mut v6 = SocketAddrV6 :: new ( Ipv6Addr :: new ( 0x2a02 , 0x6b8 , 0 , 1 , 0 , 0 , 0 , 1 ) , 80 , 0 , 10 ) ;
620
+ assert_eq ! ( v6. scope_id( ) , 10 ) ;
621
+ v6. set_scope_id ( 20 ) ;
622
+ assert_eq ! ( v6. scope_id( ) , 20 ) ;
623
+ }
514
624
}
0 commit comments