mirror of
https://kernel.googlesource.com/pub/scm/network/tftp/tftp-hpa
synced 2025-05-09 16:31:39 +03:00
Implement is_numeric_ipv6() as a state machine
Implement is_numeric_ipv6() as a state machine, so we can avoid in-place modification. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
parent
0c6f7f86d3
commit
77fbfeebee
2 changed files with 48 additions and 28 deletions
|
@ -331,7 +331,7 @@ set_sock_addr(char *host,union sock_addr *s, char **name)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
int is_numeric_ipv6(char *addr)
|
int is_numeric_ipv6(const char *p)
|
||||||
{
|
{
|
||||||
/* A numeric IPv6 address consist at least of 2 ':' and
|
/* A numeric IPv6 address consist at least of 2 ':' and
|
||||||
* it may have sequences of hex-digits and maybe contain
|
* it may have sequences of hex-digits and maybe contain
|
||||||
|
@ -339,31 +339,55 @@ int is_numeric_ipv6(char *addr)
|
||||||
* we do not check here, if it is a valid IPv6 address
|
* we do not check here, if it is a valid IPv6 address
|
||||||
* only if is something like a numeric IPv6 address or something else
|
* only if is something like a numeric IPv6 address or something else
|
||||||
*/
|
*/
|
||||||
size_t l;
|
int colon = 0;
|
||||||
char *p, s = 0;
|
int dot = 0;
|
||||||
|
int bracket = 0;
|
||||||
|
char c;
|
||||||
|
|
||||||
if (!addr)
|
if (!p)
|
||||||
return 0;
|
return 0;
|
||||||
p = strrchr(addr, ']');
|
|
||||||
if (p) {
|
if (*p == '[') {
|
||||||
s = *p;
|
bracket = 1;
|
||||||
*p = 0;
|
|
||||||
}
|
|
||||||
l = strlen(addr);
|
|
||||||
if (p)
|
|
||||||
*p = s;
|
|
||||||
if (l<2)
|
|
||||||
return 0;
|
|
||||||
if (l != strspn(addr, "0123456789ABCDEFabcdef:.["))
|
|
||||||
return 0;
|
|
||||||
p = strchr(addr, ':');
|
|
||||||
if (p) {
|
|
||||||
p++;
|
p++;
|
||||||
p = strchr(addr, ':');
|
|
||||||
if (p)
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while ((c = *p++) && c != ']') {
|
||||||
|
switch (c) {
|
||||||
|
case ':':
|
||||||
|
colon++;
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
dot++;
|
||||||
|
break;
|
||||||
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||||
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0; /* Invalid character */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colon < 2 || colon > 7)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (dot) {
|
||||||
|
/* An IPv4-mapped address in dot-quad form will have 3 dots */
|
||||||
|
if (dot != 3)
|
||||||
|
return 0;
|
||||||
|
/* The IPv4-mapped address takes the space of one colon */
|
||||||
|
if (colon > 6)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If bracketed, must be closed, and vice versa */
|
||||||
|
if (bracket ^ (c == ']'))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Otherwise, assume we're okay */
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* strip [] from numeric IPv6 addreses */
|
/* strip [] from numeric IPv6 addreses */
|
||||||
|
|
|
@ -74,15 +74,11 @@ union sock_addr {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
int is_numeric_ipv6(char *);
|
int is_numeric_ipv6(const char *);
|
||||||
char *strip_address(char *);
|
char *strip_address(char *);
|
||||||
#else
|
#else
|
||||||
#define is_numeric_ipv6(a) 0
|
#define is_numeric_ipv6(a) 0
|
||||||
|
#define strip_address(a) (a)
|
||||||
static inline char *strip_address(char *addr)
|
|
||||||
{
|
|
||||||
return addr;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static inline int sa_set_port(union sock_addr *s, u_short port)
|
static inline int sa_set_port(union sock_addr *s, u_short port)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue