1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#![cfg_attr(docsrs, feature(doc_cfg))]
//! # to-socket-addrs
//!  
//! A small replacement for `std::net::ToSocketAddrs` for specifying addresses without a port.
//!
//! [![Build Status](https://github.com/nvksv/to-socket-addrs/actions/workflows/rust.yml/badge.svg?branch=main)](https://github.com/nvksv/to-socket-addrs/actions)
//! [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
//! [![Latest Version](https://img.shields.io/crates/v/to-socket-addrs.svg)](https://crates.io/crates/to-socket-addrs)
//! [![Docs](https://docs.rs/to-socket-addrs/badge.svg)](https://docs.rs/to-socket-addrs)
//! 
//! ## Usage
//!
//! To use this crate, add `to-socket-addrs` as a dependency to your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! to-socket-addrs = "0.2"
//! ```
//!
//! After that, just use `ToSocketAddrsWithDefaultPort` instead of `std::net::ToSocketAddrs` and 
//! specify the default port number using `.with_default_port(...)` when creating a stream or
//! listener.
//! 
//! Asynchronous analogs are also supported (if the corresponding features are enabled):
//! - use `ToSocketAddrsWithDefaultPortAsync` instead of `async_std::net::ToSocketAddrs`,
//! - use `ToSocketAddrsWithDefaultPortTokio` instead of `tokio::net::ToSocketAddrs`.
//!
//! ## Features
//! 
//! - `sync` *(enabled by default)*
//! 
//!     Enables `ToSocketAddrsWithDefaultPort`.
//! 
//! - `async`
//! 
//!     Enables `ToSocketAddrsWithDefaultPortAsync`.
//! 
//! - `tokio`
//! 
//!     Enables `ToSocketAddrsWithDefaultPortTokio`.
//! 
//! 
//! ## Explanation
//!
//! The standard library assumes explicit indication of the port number when creating a stream or
//! listener:
//!
//! ```rust
//! use std::net::{TcpStream, ToSocketAddrs};
//!
//! fn connect_http<A: ToSocketAddrs>(addr: A) -> TcpStream {
//!     TcpStream::connect(addr).unwrap()
//! }
//!
//! let stream = connect_http("www.google.com:80");
//! ```
//!
//! Most often there is a certain standard port number (80 for HTTP, 21 for FTP, etc), which should
//! be used by default if the user specifies only the server address without explicitly specifying
//! the port number.
//!
//! An ordinary user does not know what the port number is and which one should be specified in each
//! case. The user usually just wants to specify the address (DNS name) of the server.
//!
//! But the naive call `connect_http("www.google.com")` will fail. You should force the user to
//! enter addresses along with the port number. Or you have to process its input and add the port
//! number if there is none (for example, from `"www.google.com"` to `"www.google.com:80"`).
//!
//! It's inconvenient.
//!
//! Therefore you can use this crate and write simply:
//!
//! ```rust
//! use std::net::TcpStream;
//! use to_socket_addrs::ToSocketAddrsWithDefaultPort as ToSocketAddrs;
//!
//! fn connect_http<A: ToSocketAddrs>(addr: A) -> TcpStream {
//!     TcpStream::connect(addr.with_default_port(80)).unwrap()
//! }
//!
//! let stream = connect_http("www.google.com");
//! ```
//!
//! The `.with_default_port(...)` function will check if the port number is specified and add it if
//! necessary.
maybe_async_cfg::content! {

#![maybe_async_cfg::default(
    idents(
        async_std(sync="std", async, tokio="tokio"),
        ToSocketAddrs(use, sync, async="ToSocketAddrsAsync", tokio="ToSocketAddrsTokio"),
        ToSocketAddrsWithDefaultPort(sync, async="ToSocketAddrsWithDefaultPortAsync", tokio="ToSocketAddrsWithDefaultPortTokio"),
        into_vec4(fn, tokio="into_vec4_tokio"),
        into_vec6(fn, tokio="into_vec6_tokio"),
    )
)]

use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6, IpAddr, Ipv4Addr, Ipv6Addr};

////////////////////////////////////////////////////////////////////////////////////////////////////

#[maybe_async_cfg::maybe(
    sync(key="sync", feature="sync"),
    async(key="async", feature="async"), 
    async(key="tokio", feature="tokio"), 
)]
use async_std::net::ToSocketAddrs;

#[maybe_async_cfg::maybe(
    sync(key="sync", feature="sync", inner(cfg_attr(docsrs, doc(cfg(feature = "sync"))), doc="A trait to use instead of `std::net::ToSocketAddrs`")),
    async(key="async", feature="async", inner(cfg_attr(docsrs, doc(cfg(feature = "async"))), doc="A trait to use instead of `async_std::net::ToSocketAddrs`")), 
    async(key="tokio", feature="tokio", inner(cfg_attr(docsrs, doc(cfg(feature = "tokio"))), doc="A trait to use instead of `tokio::net::ToSocketAddrs`")), 
)]
pub trait ToSocketAddrsWithDefaultPort {
    type Inner: Sized + ToSocketAddrs;
    fn with_default_port(&self, default_port: u16) -> Self::Inner;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

// This types already hold port inside (default port must be ignored)
macro_rules! std_impl {
    ($ty:ty) => {
        #[maybe_async_cfg::maybe(
            keep_self, 
            sync(key="sync", feature="sync"),
            async(key="async", feature="async"), 
            async(key="tokio", feature="tokio"), 
        )]
        impl ToSocketAddrsWithDefaultPort for $ty {
            type Inner = Self;
            fn with_default_port(&self, _default_port: u16) -> Self::Inner {
                *self
            }
        }
    }
}

std_impl!(SocketAddr);
std_impl!(SocketAddrV4);
std_impl!(SocketAddrV6);
std_impl!((IpAddr, u16));
std_impl!((Ipv4Addr, u16));
std_impl!((Ipv6Addr, u16));

////////////////////////////////////////////////////////////////////////////////////////////////////

// This types hold IP address only, so we always have to use default port
macro_rules! tuple_impl {
    ($ty:ty) => {
        #[maybe_async_cfg::maybe(
            keep_self, 
            sync(key="sync", feature="sync"),
            async(key="async", feature="async"), 
            async(key="tokio", feature="tokio"), 
        )]
        impl ToSocketAddrsWithDefaultPort for $ty {
            type Inner = (Self, u16);
            fn with_default_port(&self, default_port: u16) -> Self::Inner {
                (*self, default_port)
            }
        }
    }
}

tuple_impl!(IpAddr);
tuple_impl!(Ipv4Addr);
tuple_impl!(Ipv6Addr);

////////////////////////////////////////////////////////////////////////////////////////////////////

#[maybe_async_cfg::maybe(
    sync(key="sync", feature="sync"),
    async(key="async", feature="async"), 
    async(key="tokio", feature="tokio"), 
)]
impl<'s> ToSocketAddrsWithDefaultPort for &'s [SocketAddr] {
    type Inner = &'s [SocketAddr];
    fn with_default_port(&self, _default_port: u16) -> Self::Inner {
        self
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

#[maybe_async_cfg::maybe(
    sync(key="sync", feature="sync"),
    async(key="async", feature="async"), 
    async(key="tokio", feature="tokio"), 
)]
impl<T: ToSocketAddrs + ?Sized> ToSocketAddrsWithDefaultPort for &T where T: ToSocketAddrsWithDefaultPort {
    type Inner = <T as ToSocketAddrsWithDefaultPort>::Inner;
    fn with_default_port(&self, default_port: u16) -> Self::Inner {
        (**self).with_default_port( default_port )
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

macro_rules! str_impl {
    ($ty:ty) => {
        #[maybe_async_cfg::maybe(
            keep_self,
            sync(key="sync", feature="sync"),
            async(key="async", feature="async"), 
            async(key="tokio", feature="tokio"), 
        )]
        impl ToSocketAddrsWithDefaultPort for $ty {
            type Inner = String;

            fn with_default_port(&self, default_port: u16) -> Self::Inner {
                let inner = if let Some(pcolon) = self.rfind(":") {
                    if let Some(pbracket) = self.rfind("]") {
                        if pbracket < pcolon {
                            // "__]__:__" => IPv6 in brackets with port
                            self.to_string()
                        } else {
                            // "__:__]__" => IPv6 in brackets without port
                            format!("{}:{}", self, default_port)
                        }
                    } else {
                        // "__:__", no brackets => IPv4 with port or bare IPv6
                        if let Some(_) = self[..pcolon].rfind(":") {
                            // "__:__:__", no brackets => bare IPv6
                            format!("[{}]:{}", self, default_port)
                        } else {
                            // "__:__", no brackets, no more colons => IPv4 with port
                            self.to_string()
                        }
                    }
                } else {
                    // "__", no colons => IPv4 without port
                    format!("{}:{}", self, default_port)
                };
                inner
            }
        }
    }
}

str_impl!(str);
str_impl!(String);

////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="tokio", feature="tokio"), 
        async(key="async", feature="async"), 
    )]
    #[maybe_async_cfg::only_if(sync)]
    fn into_vec4<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = addr
            .with_default_port(default_port)
            .to_socket_addrs()
            .unwrap()
            .filter_map(|a| if let SocketAddr::V4(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="tokio", feature="tokio"), 
        async(key="async", feature="async"), 
    )]
    #[maybe_async_cfg::only_if(sync)]
    fn into_vec6<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = addr
            .with_default_port(default_port)
            .to_socket_addrs()
            .unwrap()
            .filter_map(|a| if let SocketAddr::V6(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="async", feature="async"), 
        async(key="tokio", feature="tokio"), 
    )]
    #[maybe_async_cfg::only_if(async)]
    async fn into_vec4<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = addr
            .with_default_port(default_port)
            .to_socket_addrs()
            .await
            .unwrap()
            .filter_map(|a| if let SocketAddr::V4(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="async", feature="async"), 
        async(key="tokio", feature="tokio"), 
    )]
    #[maybe_async_cfg::only_if(async)]
    async fn into_vec6<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = addr
            .with_default_port(default_port)
            .to_socket_addrs()
            .await
            .unwrap()
            .filter_map(|a| if let SocketAddr::V6(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="async", feature="async"), 
        async(key="tokio", feature="tokio"), 
    )]
    #[maybe_async_cfg::only_if(tokio)]
    async fn into_vec4<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = tokio::net::lookup_host(addr.with_default_port(default_port))
            .await
            .unwrap()
            .filter_map(|a| if let SocketAddr::V4(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync"),
        async(key="async", feature="async"), 
        async(key="tokio", feature="tokio"), 
    )]
    #[maybe_async_cfg::only_if(tokio)]
    async fn into_vec6<A: ToSocketAddrsWithDefaultPort>(addr: A, default_port: u16) -> Vec<String> {
        let mut v: Vec<String> = tokio::net::lookup_host(addr.with_default_port(default_port))
            .await
            .unwrap()
            .filter_map(|a| if let SocketAddr::V6(_) = a {Some(a.to_string())} else {None})
            .collect();
        v.sort();
        v
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync", test), 
        async(key="async", feature="async", async_attributes::test),
        async(key="tokio", feature="tokio", self="ipv4_tokio", tokio::test)
    )]
    async fn ipv4() {
        // IPv4 without port
        assert_eq!(into_vec4("8.8.8.8", 443).await,            ["8.8.8.8:443"]);
        // IPv4 with port
        assert_eq!(into_vec4("8.8.8.8:8080", 443).await,       ["8.8.8.8:8080"]);
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync", test), 
        async(key="async", feature="async", async_attributes::test),
        async(key="tokio", feature="tokio", self="ipv6_tokio", tokio::test)
    )]
    async fn ipv6() {
        // IPv6 without port
        assert_eq!(into_vec6("::1", 80).await,                 ["[::1]:80"]);
        assert_eq!(into_vec6("[::1]", 80).await,               ["[::1]:80"]);
        assert_eq!(into_vec6("[::1]:31337", 80).await,         ["[::1]:31337"]);
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync", test), 
        async(key="async", feature="async", async_attributes::test),
        async(key="tokio", feature="tokio", self="dns_ipv4_tokio", tokio::test)
    )]
    async fn dns_ipv4() {
        // DNS without port (must be resolved to IPv4)
        assert_eq!(into_vec4("dns.google", 5353).await,        ["8.8.4.4:5353", "8.8.8.8:5353"]);
        assert_eq!(into_vec4("dns.quad9.net", 53).await,       ["149.112.112.112:53", "9.9.9.9:53"]);
        assert_eq!(into_vec4("dns11.quad9.net", 3389).await,   ["149.112.112.11:3389", "9.9.9.11:3389"]);
        // DNS with port (must be resolved to IPv4)
        assert_eq!(into_vec4("dns.google:53", 8080).await,     ["8.8.4.4:53", "8.8.8.8:53"]);
        assert_eq!(into_vec4("dns.quad9.net:80", 53).await,    ["149.112.112.112:80", "9.9.9.9:80"]);
        assert_eq!(into_vec4("dns11.quad9.net:21", 3389).await,["149.112.112.11:21", "9.9.9.11:21"]);
    }

    #[maybe_async_cfg::maybe(
        sync(key="sync", feature="sync", test), 
        async(key="async", feature="async", async_attributes::test),
        async(key="tokio", feature="tokio", self="dns_ipv6_tokio", tokio::test)
    )]
    #[cfg_attr(not(feature="test_dns_ipv6"), ignore)]
    async fn dns_ipv6() {
        // DNS without port (must be resolved to IPv6)
        assert_eq!(into_vec6("dns64.dns.google", 53).await,        ["[2001:4860:4860::6464]:53", "[2001:4860:4860::64]:53"]);
        // DNS with port (must be resolved to IPv6)
        assert_eq!(into_vec6("dns64.dns.google:443", 53).await,    ["[2001:4860:4860::6464]:443", "[2001:4860:4860::64]:443"]);
    }
}

}