proto-shell: add a library function for sending protocol handler updates
authorFelix Fietkau <nbd@openwrt.org>
Mon, 10 Oct 2011 14:30:42 +0000 (16:30 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 10 Oct 2011 14:30:47 +0000 (16:30 +0200)
dummy/netifd-proto.sh
dummy/proto/ppp.sh

index 7ce88ee9bef2697f60b1a2397332def516f60b85..85e86983b0fa07a1907ff9b0f3118cfb9bfdcfd0 100755 (executable)
@@ -36,6 +36,93 @@ _proto_do_setup() {
        eval "$1_setup \"$interface\" \"$ifname\""
 }
 
+proto_init_update() {
+       local ifname="$1"
+       local up="$2"
+       local external="$3"
+
+       PROTO_INIT=1
+       PROTO_IPADDR=
+       PROTO_IP6ADDR=
+       PROTO_ROUTE=
+       PROTO_ROUTE6=
+       json_init
+       json_add_string "ifname" "$ifname"
+       json_add_boolean "link-up" "$up"
+       [ -n "$3" ] && json_add_boolean "address-external" "$external"
+}
+
+proto_add_ipv4_address() {
+       local address="$1"
+       local mask="$2"
+
+       jshn_append PROTO_IPADDR "$address/$mask"
+}
+
+proto_add_ipv6_address() {
+       local address="$1"
+       local mask="$2"
+
+       jshn_append PROTO_IP6ADDR "$address/$mask"
+}
+
+proto_add_ipv4_route() {
+       local target="$1"
+       local mask="$2"
+       local gw="$3"
+
+       jshn_append PROTO_ROUTE "$target/$mask/$gw"
+}
+
+proto_add_ipv6_route() {
+       local target="$1"
+       local mask="$2"
+       local gw="$3"
+
+       jshn_append PROTO_ROUTE6 "$target/$mask/$gw"
+}
+
+_proto_push_ip() {
+       json_add_string "" "$1"
+}
+
+_proto_push_route() {
+       local str="$1";
+       local target="${str%%/*}"
+       str="${str#*/}"
+       local mask="${str%%/*}"
+       local gw="${str#*/}"
+
+       json_add_table ""
+       json_add_string target "$target"
+       json_add_integer mask "$mask"
+       json_add_string gateway "$gw"
+       json_close_table
+}
+
+_proto_push_array() {
+       local name="$1"
+       local val="$2"
+       local cb="$3"
+
+       [ -n "$val" ] || return 0
+       json_add_array "$name"
+       for item in $val; do
+               eval "$cb \"\$item\""
+       done
+       json_close_array
+}
+
+proto_send_update() {
+       local interface="$1"
+
+       _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ip
+       _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ip
+       _proto_push_array "route" "$PROTO_ROUTE" _proto_push_route
+       _proto_push_array "route6" "$PROTO_ROUTE6" _proto_push_route
+       ubus call network.interface."$interface" notify_proto "$(json_dump)" &
+}
+
 init_proto() {
        proto="$1"; shift
        cmd="$1"; shift
index 50788334c014a1ef92c4f65826e40135dafc371f..adce2caa49a50ffd6c5dba42d4784d6eaa3db2cc 100755 (executable)
@@ -36,6 +36,9 @@ pppoe_setup() {
        json_get_var username username
        json_get_var password password
        echo "pppoe_setup($1, $2), username=$username, password=$password"
+       proto_init_update ppp0 1
+       proto_add_ipv4_address "192.168.2.1" 32
+       proto_send_update "$1"
 }
 
 pppoe_teardown() {