@@ -30,6 +30,19 @@ struct Socket
3030 }
3131}
3232
33+ /+ +
34+ Determines whether the passed string is an IPv6 address (and not an IPv4 one).
35+ Use this function to differentiate between IPv4 and IPv6 addresses.
36+
37+ Limitation:
38+ This functions does only very basic and cheap testing.
39+ It does not validate the IPv6 address at all.
40+ Never pass any sockets to it - IPv4 ones will get detected as IPv6 addresses.
41+ Do not use it for anything else than differentiating IPv4/IPv6 addresses.
42+
43+ Returns:
44+ true if the passed string could looks like an IPv6 address
45+ +/
3346bool isIPv6 (string address) nothrow @nogc
3447{
3548 foreach (c; address)
@@ -43,11 +56,21 @@ bool isIPv6(string address) nothrow @nogc
4356 return false ;
4457}
4558
59+ /+ +
60+ Tries to parse a socket string
61+
62+ Supports both IPv4 and IPv6.
63+ Does limited validating.
64+
65+ Returns:
66+ true if parsing was successfull,
67+ false indicates bad/invalid input
68+ +/
4669bool tryParseSocket (string s, out Socket socket)
4770{
4871 socket = Socket ();
4972
50- if ((s is null ) && (s.length == 0 ))
73+ if ((s is null ) && (s.length == 0 )) // validate
5174 {
5275 return false ;
5376 }
@@ -74,22 +97,33 @@ bool tryParseSocket(string s, out Socket socket)
7497 {
7598 // IPv4
7699 socket.address = s[0 .. possiblePortSep];
100+
101+ if (socket.address.count! (c => c == ' .' ) != 3 ) // validate
102+ {
103+ return false ;
104+ }
77105 }
78106 else
79107 {
80108 return false ;
81109 }
82110
83111 immutable portSep = (isIPv6) ? isIPv6 : possiblePortSep;
112+
113+ if (portSep == 0 ) // validate
114+ {
115+ return false ;
116+ }
117+
84118 string port = s[(portSep + 1 ) .. $];
85119
86- if (port.canFind! (d => ! d.isDigit)() || (port.length > 5 ) || (port[0 ] == ' -' ))
120+ if (port.canFind! (d => ! d.isDigit)() || (port.length > 5 ) || (port[0 ] == ' -' )) // validate
87121 {
88122 return false ;
89123 }
90124
91125 immutable portInt = port.to! int ;
92- if (portInt > ushort .max)
126+ if (portInt > ushort .max) // validate
93127 {
94128 return false ;
95129 }
0 commit comments