Page 1 of 1

Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 6:22 am
by npeca75
Hi to all

last night i realized that MKT does not have Option 39 in dhcp v6 client :(

so, here it is script implementation
#ascii chars
:local ascii1 ("0123456789")
:local ascii2 ("abcdefghijklmnopqrstuvwxyz")
:local ascii3 ("-")
:local ascii ($ascii1 . $ascii2 . $ascii3)

#ascii values
:local val1 "30,31,32,33,34,35,36,37,38,39,"
:local val2 "61,62,63,64,65,66,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,"
:local val3 "2d"
:local val [:toarray ($val1 . $val2 . $val3)]

#hex chars
:local hex "0123456789ABCDEF"

:local hn  [/system identity get name]
:local hnld [:len $hn]

:global hout ""
:global tmp ""
:local cnt 0

:for pc from 0 to ($hnld-1) do={
    :local chr [:pick $hn $pc]

    :if ($chr != "." ) do={
        :local pa [:find $ascii $chr]
        :local hv [:pick $val $pa]
        :set tmp ( $tmp . $hv )
        :set cnt ($cnt + 1)
    } else {
        :local hnlh [:pick $hex $cnt]
        :set hout ( $hout . "0" . $hnlh . $tmp)
        :set cnt 0
        :set tmp ""
    }
}

:local hnlh [:pick $hex $cnt]
:set hout ( $hout . "0" . $hnlh . $tmp)

:local out ( "0x01" . $hout . "00")
:log info "$hn -> $out"
/ipv6 dhcp-client option set 0 value=$out code=39 name=option39

Script is not nice :) and does not make any check on hostname valid chars
so, only 0-9, a-z , hyphen and dot

OpenWRT odhcpd work well with this script, and wireshark dump confirm that packet is valid
so, mikrotik name is visible in DHCPv6 server

if someone want to improve / clean / rewrite this script ... :)

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 10:14 am
by rextended
The script not work if the fqdn have one part with more than 9 characters, like www.thisismydomain.com
because the length on encoded dns when is bigger than 9 must be converted to hexadecimal, not "0" + ":len" as string

Why not simply:
/ipv6 dhcp-client option
add code=39 name=option-39 value="0x0108'test-mkt'0x03'lan'0x00"
Without use scripting?

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 11:28 am
by npeca75
The script not work if the fqdn have one part with more than 9 characters, like www.thisismydomain.com
hi @rexetended
www.thisismydomain.com -> 0x01037777770E7468697369736d79646f6d61696e03636f6d00

i dont see problem here
0x0F is maximum between dot's (15)
yes, it is limited, but not on 9 as you say
#hex chars
:local hex "0123456789ABCDEF"
...
:local hnlh [:pick $hex $cnt]
hnlh = length in hex
why script ?
Simple tryout to implement missing function
and maybe learn some about Option39, scripting, etc

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 11:30 am
by rextended
9 or 15, is limited, this not change the point
The max length for label and domain is 63 characters

Re: Option39 DHCPv6 Client  [SOLVED]

Posted: Fri Sep 17, 2021 11:32 am
by rextended
search tag # rextended DHCPv6 option 39 fqdn2encdns FQDN to DNS encoding DNS encoder

I just finished to write this to directly encode the string:
:global fqdn2encdns do={
    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    :local chr2lcase do={
        :local chrValue [:find $2 $1 -1]
        :if (($chrValue > 64) and ($chrValue < 91)) do={
            :return [:pick $2 ($chrValue + 32) ($chrValue + 33)]
        } else={
            :return $1
        }
    }

    :local numbyte2hex do={
        :local input [:tonum $1]
        :local hexchars "0123456789ABCDEF"
        :local convert [:pick $hexchars (($input >> 4) & 0xF)]
        :set convert ($convert.[:pick $hexchars ($input & 0xF)])
        :return $convert
    }

    :local input "$1"
    :local fqdn ""
    :local encdns "0x01''"
    :if ($input~"^(([a-zA-Z0-9][a-zA-Z0-9-]{0,61}){0,1}[a-zA-Z]\\.){1,9}[a-zA-Z][a-zA-Z0-9-]{0,28}[a-zA-Z]\$") do={
         :for y from=0 to=([:len $input]-1) step=1 do={
             :set fqdn "$fqdn$[$chr2lcase [:pick $input $y] $charsString]"
         }
         :local workstr $fqdn
         :local dotidx  0
         :while ([:typeof [:find $workstr "." -1]] != "nil") do={
             :set dotidx [:find $workstr "." -1]
             :set encdns "$"encdns"0x$[$numbyte2hex $dotidx]'$[:pick $workstr 0 $dotidx]'"
             :set workstr [:pick $workstr ($dotidx + 1) [:len $workstr]]
         }
         :return "$"encdns"0x$[$numbyte2hex [:len $workstr]]'$workstr'0x00"
    } else={
        :return ""
    }
}

How to use, for example call it to put on terminal the ecncoded DNS
:put [$fqdn2encdns www.thisismydomainname.net]     
0x01''0x03'www'0x12'thisismydomainname'0x03'net'0x00

Or like this:
/ipv6 dhcp-client option
set [find where code=39] value=[$fqdn2encdns www.thisismydomainname.net] 

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 11:41 am
by npeca75
search tag #rextended fqdn2encdns FQDN to DNS encoding DNS encoder
after all, it was the whole point of my post
to learn, and to induce people to make it better

it is so sad that users need to implement missing features :(

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 11:42 am
by rextended
Yes, I hope you like my script....

Re: Option39 DHCPv6 Client

Posted: Fri Sep 17, 2021 11:48 am
by rextended
Bugfix:

Wrong
0x010x03'www'0x12'thisismydomainname'0x03'net'0x00

Correct:
0x01''0x03'www'0x12'thisismydomainname'0x03'net'0x00

Script fixed

Re: Option39 DHCPv6 Client

Posted: Mon Sep 20, 2021 1:02 pm
by npeca75
Yes, I hope you like my script....
Hi

sorry for late reply

yes, it is working fine
tested on 6x and 7x

ok, two different approach for same problem :)
thank you