Thread hostname checking (11 answers)
Opened by esskar at 2004-08-04 02:30

esskar
 2004-08-11 05:04
#85355 #85355
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
habe das ganze jetzt wohl vollendet:

Code: (dl )
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
use Net::IDN::Encode;

use constant REGEXP_HOSTNAME => qr!^[\w\-.]+$!;
use constant REGEXP_IPADDR   => qr!^((?:(?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})))$!;

sub host_toidn
{
  return domain_to_ascii(shift);
}

sub host_fromidn
{
  return domain_to_unicode(shift);
}

sub valid_hostname
{
  my $value = host_toidn(shift);

  return 0 unless $value =~ REGEXP_HOSTNAME;
  return 0 unless index($value, ".") > 0;

  my @parts = split /\./, $value;
  my $valid = scalar @parts;

  foreach my $label (@parts)
  {
     $valid = 0 if (length $label > 63) or ($label =~ qr!^\-!) or ($label =~ qr!\-$!);
     last unless $valid;
  }

  $valid = 0 if $parts[-1] =~ qr!^\d.*!;

  return $valid;
}

sub valid_ipaddr
{
  my $value = shift;
  return $value =~ REGEXP_IPADDR;
}

sub valid_host
{
  my $value = shift;
  return valid_hostname($value) || valid_ipaddr($value);
}


Spaß!\n\n

<!--EDIT|esskar|1092205010-->

View full thread hostname checking