docs
[mirrors/Programs.git] / bash / dhcp-option.sh
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 #Adds \x escapes to hexdump string
14
15 while read -n 2 i; do
16 [ -n "$i" ] && echo -n '\x'"$i";
17 done
18 }
19
20 dnsmasq() {
21 #Converts \x00\x00 notation to 00:00 notation
22
23 sed -e 's/^\\x//g' | sed -e 's/\\x/:/g'
24 }
25
26 dhcp_option() {
27 #Compile DHCP option using option number and data.
28 #Generates hex string including header with option number and data length.
29
30 separator='\x'
31 option_id="$1"
32 option_data="$2"
33
34 len=$(echo -n "$option_data" | wc -c)
35 dec_to_hex_right $option_id 2 | escape
36 dec_to_hex_right $len 2 | escape
37 printf "$option_data" | xxd -ps -c 256 | escape
38 }
39
40 pd_prefix() {
41 #Generate prefix sub-option to be included in PD option of DHCPv6
42
43 option_id=26
44 lifetime_preferred='\x00\x00\x01\x2C'
45 lifetime_valid='\x00\x00\x01\x2C'
46
47 prefix_length='64'
48 #prefix = 16 octets:
49 prefix='\x20\x01\x06\x7c\x21\x90\x1a\x01''\x00\x00\x00\x00\x00\x00\x00\x00'
50
51 prefix_length_hex=$(dec_to_hex $prefix_length)
52
53 #echo $prefix_length_hex
54 dhcp_option $option_id "$lifetime_preferred$lifetime_valid$prefix_length_hex$prefix"
55 }
56
57 pd() {
58 #Generate data of PF option of DHCPv6
59
60 iaid='\x00\x00\x00\x00'
61 t1='\x00\x00\x01\x2C'
62 t2='\x00\x00\x01\x2C'
63 ia_pd_opts="$(pd_prefix)"
64 echo -n "$iaid$t1$t2$ia_pd_opts"
65 }
66
67 pd_option() {
68 #Generate PD option of DHCPv6 (including header)
69
70 option_id=25
71 dhcp_option $option_id "$(pd)"
72 }
73
74 pd | dnsmasq
75 echo
This page took 0.266198 seconds and 4 git commands to generate.