• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcp6c/odhcp6c-example-script.sh

  1 #!/bin/sh
  2 [ -z "$2" ] && echo "Error: should be run by odhcpc6c" && exit 1
  3 
  4 update_resolv() {
  5         local device="$1"
  6         local dns="$2"
  7         
  8         (
  9                 flock 9
 10                 grep -v "#odhcp6c:$device:" /etc/resolv.conf > /tmp/resolv.conf.tmp
 11                 for c in $dns; do
 12                         echo "nameserver $c #odhcp6c:$device:" >> /tmp/resolv.conf.tmp
 13                 done
 14                 mv /tmp/resolv.conf.tmp /etc/resolv.conf
 15                 chmod 0644 /etc/resolv.conf
 16         ) 9>/tmp/resolv.conf.lock
 17         rm -f /tmp/resolv.conf.lock /tmp/resolv.conf.tmp
 18 }
 19 
 20 setup_interface () {
 21         local device="$1"
 22 
 23         # Merge RA-DNS
 24         for radns in $RA_DNS; do
 25                 local duplicate=0
 26                 for dns in $RDNSS; do
 27                         [ "$radns" = "$dns" ] && duplicate=1
 28                 done
 29                 [ "$duplicate" = 0 ] && RDNSS="$RDNSS $radns"
 30         done
 31 
 32         local dnspart=""
 33         for dns in $RDNSS; do
 34                 if [ -z "$dnspart" ]; then
 35                         dnspart="\"$dns\""
 36                 else
 37                         dnspart="$dnspart, \"$dns\""
 38                 fi
 39         done
 40 
 41         update_resolv "$device" "$dns"
 42 
 43         local prefixpart=""
 44         for entry in $PREFIXES; do
 45                 local addr="${entry%%,*}"
 46                 entry="${entry#*,}"
 47                 local preferred="${entry%%,*}"
 48                 entry="${entry#*,}"
 49                 local valid="${entry%%,*}"
 50                 entry="${entry#*,}"
 51                 [ "$entry" = "$valid" ] && entry=
 52 
 53                 local class=""
 54                 local excluded=""
 55 
 56                 while [ -n "$entry" ]; do
 57                         local key="${entry%%=*}"
 58                         entry="${entry#*=}"
 59                         local val="${entry%%,*}"
 60                         entry="${entry#*,}"
 61                         [ "$entry" = "$val" ] && entry=
 62 
 63                         if [ "$key" = "class" ]; then
 64                                 class=", \"class\": $val"
 65                         elif [ "$key" = "excluded" ]; then
 66                                 excluded=", \"excluded\": \"$val\""
 67                         fi
 68                 done
 69 
 70                 local prefix="{\"address\": \"$addr\", \"preferred\": $preferred, \"valid\": $valid $class $excluded}"
 71                 
 72                 if [ -z "$prefixpart" ]; then
 73                         prefixpart="$prefix"
 74                 else
 75                         prefixpart="$prefixpart, $prefix"
 76                 fi
 77 
 78                 # TODO: delete this somehow when the prefix disappears
 79                 ip -6 route add unreachable "$addr"
 80         done
 81 
 82         ip -6 route flush dev "$device"
 83         ip -6 address flush dev "$device" scope global
 84 
 85         # Merge addresses
 86         for entry in $RA_ADDRESSES; do
 87                 local duplicate=0
 88                 local addr="${entry%%/*}"
 89                 for dentry in $ADDRESSES; do
 90                         local daddr="${dentry%%/*}"
 91                         [ "$addr" = "$daddr" ] && duplicate=1
 92                 done
 93                 [ "$duplicate" = "0" ] && ADDRESSES="$ADDRESSES $entry"
 94         done
 95 
 96         for entry in $ADDRESSES; do
 97                 local addr="${entry%%,*}"
 98                 entry="${entry#*,}"
 99                 local preferred="${entry%%,*}"
100                 entry="${entry#*,}"
101                 local valid="${entry%%,*}"
102 
103                 ip -6 address add "$addr" dev "$device" preferred_lft "$preferred" valid_lft "$valid" 
104         done
105 
106         for entry in $RA_ROUTES; do
107                 local addr="${entry%%,*}"
108                 entry="${entry#*,}"
109                 local gw="${entry%%,*}"
110                 entry="${entry#*,}"
111                 local valid="${entry%%,*}"
112                 entry="${entry#*,}"
113                 local metric="${entry%%,*}"
114 
115                 if [ -n "$gw" ]; then
116                         ip -6 route add "$addr" via "$gw" metric "$metric" dev "$device" from "::/128"
117                 else
118                         ip -6 route add "$addr" metric "$metric" dev "$device"
119                 fi
120 
121                 for prefix in $PREFIXES; do
122                         local paddr="${prefix%%,*}"
123                         [ -n "$gw" ] && ip -6 route add "$addr" via "$gw" metric "$metric" dev "$device" from "$paddr"
124                 done
125         done
126 }
127 
128 teardown_interface() {
129         local device="$1"
130         ip -6 route flush dev "$device"
131         ip -6 address flush dev "$device" scope global
132         update_resolv "$device" ""
133 }
134 
135 (
136         flock 9
137         case "$2" in
138                 bound)
139                         teardown_interface "$1"
140                         setup_interface "$1"
141                 ;;
142                 informed|updated|rebound|ra-updated)
143                         setup_interface "$1"
144                 ;;
145                 stopped|unbound)
146                         teardown_interface "$1"
147                 ;;
148                 started)
149                         teardown_interface "$1"
150                 ;;
151         esac
152 
153         # user rules
154         [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user
155 ) 9>/tmp/odhcp6c.lock.$1
156 rm -f /tmp/odhcp6c.lock.$1

This page was automatically generated by LXR 0.3.1.  •  OpenWrt