Commit | Line | Data |
---|---|---|
b118cefd TM |
1 | #!/bin/bash |
2 | #Experimental BASH script to compile DHCP options | |
3 | ||
4 | dec_to_hex() { | |
5 | echo 'obase=16; '"$1" | bc | |
6 | } | |
7 | ||
8 | dec_to_hex_right() { | |
9 | echo -n $(while true; do echo -n 0; done | head -c "$2")$(dec_to_hex "$1") | tail -c "$2" | |
10 | } | |
11 | ||
12 | escape() { | |
13 | while read -n 2 i; do | |
14 | [ -n "$i" ] && echo -n '\x'"$i"; | |
15 | done | |
16 | } | |
17 | ||
18 | dnsmasq() { | |
19 | sed -e 's/^\\x//g' | sed -e 's/\\x/:/g' | |
20 | } | |
21 | ||
22 | dhcp_option() { | |
23 | separator='\x' | |
24 | option_id="$1" | |
25 | option_data="$2" | |
26 | ||
27 | len=$(echo -n "$option_data" | wc -c) | |
28 | dec_to_hex_right $option_id 2 | escape | |
29 | dec_to_hex_right $len 2 | escape | |
30 | printf "$option_data" | xxd -ps -c 256 | escape | |
31 | } | |
32 | ||
eb907e03 TM |
33 | pd_prefix() { |
34 | #Generate prefix sub-option to be included in PD option of DHCPv6 | |
35 | ||
36 | option_id=26 | |
37 | lifetime_preferred='\x00\x00\x01\x2C' | |
38 | lifetime_valid='\x00\x00\x01\x2C' | |
39 | ||
40 | prefix_length='64' | |
41 | #prefix = 16 octets: | |
42 | prefix='\x20\x01\x06\x7c\x21\x90\x1a\x01''\x00\x00\x00\x00\x00\x00\x00\x00' | |
43 | ||
44 | prefix_length_hex=$(dec_to_hex $prefix_length) | |
45 | ||
46 | #echo $prefix_length_hex | |
47 | dhcp_option $option_id "$lifetime_preferred$lifetime_valid$prefix_length_hex$prefix" | |
48 | } | |
49 | ||
b118cefd | 50 | pd() { |
eb907e03 TM |
51 | #Generate data of PF option of DHCPv6 |
52 | ||
b118cefd TM |
53 | iaid='\x00\x00\x00\x00' |
54 | t1='\x00\x00\x01\x2C' | |
55 | t2='\x00\x00\x01\x2C' | |
eb907e03 TM |
56 | ia_pd_opts="$(pd_prefix)" |
57 | echo -n "$iaid$t1$t2$ia_pd_opts" | |
58 | } | |
59 | ||
60 | pd_option() { | |
61 | #Generate PD option of DHCPv6 (including header) | |
62 | ||
63 | option_id=25 | |
64 | dhcp_option $option_id "$(pd)" | |
b118cefd TM |
65 | } |
66 | ||
67 | pd | dnsmasq | |
68 | echo |