3

I am trying to craft a regexp to validate iSCSI qualified names. An example of a qualified name is iqn.2011-08.com.example:storage This is example is minimal, I have seen other examples that are more extended.

So far what I have to validate off of it this:

print "Enter a new target name: ";

my $target_name = <STDIN>;

chomp $target_name;

if ($target_name =~ /^iqn\.\d{4}-\d{2}/xmi) {

    print GREEN . "Target name is valid!" . RESET . "\n";

} else {

    print RED . "Target name is not valid!" . RESET . "\n";

}

How can I extend that to work with rest up to the : I am not going to parse after the : becuase it is a description tag.

Is there a limit to how big a domain name can be?

ikegami
  • 367,544
  • 15
  • 269
  • 518
AtomicPorkchop
  • 2,625
  • 5
  • 36
  • 55

2 Answers2

5

According to RFC3270 (and in turn RFC1035),

/
   (?(DEFINE)
      (?<IQN_PAT>
         iqn
         \. 
         [0-9]{4}-[0-9]{2}
         \.
         (?&REV_SUBDOMAIN_PAT)
         (?: : .* )?
      )

      (?<EUI_PAT>
         eui
         \.
         [0-9A-Fa-f]{16}
      )

      (?<REV_SUBDOMAIN_PAT>
         (?&LABEL_PAT) (?: \. (?&LABEL_PAT) )*
      )

      (?<LABEL_PAT>
         [A-Za-z] (?: [A-Za-z0-9\-]* [A-Za-z0-9] )?
      )
   )

   ^ (?: (?&IQN_PAT) | (?&EUI_PAT) ) \z
/sx

It's not clear if the eui names accept lowercase hex digits or not. I figured it was safer to allow them.

If you condense the above, you get /^(?:iqn\.[0-9]{4}-[0-9]{2}(?:\.[A-Za-z](?:[A-Za-z0-9\-]*[A-Za-z0-9])?)+(?::.*)?|eui\.[0-9A-Fa-f]{16})\z/s.

(By the way, your use /m is wrong, your use of /i is wrong, and \d can match far more than the allowed [0-9].)

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

If you only need part before : then you can use following regexp:

if ($target_name =~ /^iqn\.(\d{4}-\d{2})\.([^:]+):/xmi) {
    my ($date, $reversed_domain_name) = ($1, $2);

Regexp [^:]+ matches to 1 or more non-: symbols. It will match even if domain name is not well formed. Further improvements depends on your goal: do you need just get individual components of iSCSI name or do you need to validate its syntax?


Is there a limit to how big a domain name can be?

From Wikipedia:

The full domain name may not exceed a total length of 253

Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
  • Mainly I am trying to validate the syntax. I just want to prevent an invalid syntax from being passed. – AtomicPorkchop Sep 09 '11 at 01:52
  • what would be an easy way to check for < or = 253 chars after the date but before the : – AtomicPorkchop Sep 09 '11 at 03:20
  • @Solignis, Extract it and use `length`. It doesn't gain you anything by testing that, though. One that's 253 chars long is going to be just as wrong as the one that's 254 chars long, even tough it's within the limit. – ikegami Sep 09 '11 at 09:57
  • @Ivan Nevostruev, the `:` is optional, btw. – ikegami Sep 09 '11 at 10:07
  • @Solignis, You could use `[^:]{1,253}` instead of `[^:]+`, but then you still have to extract the domain name to see if it's valid. Checking the length will find way fewer problems then checking for allowed characters. – ikegami Sep 09 '11 at 10:08