X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=dummy%2Fnetifd-proto.sh;h=c50d776a6b0f4fa7d8a036709a0a9dcfc1e0a074;hp=104edc7b6b55fad89c0f1504716daba288541244;hb=10c7565766962a876844c71dd3a230aefeaf193b;hpb=e8a8f555aac97b47bd036264b598affec66f96d5 diff --git a/dummy/netifd-proto.sh b/dummy/netifd-proto.sh index 104edc7..c50d776 100755 --- a/dummy/netifd-proto.sh +++ b/dummy/netifd-proto.sh @@ -36,40 +36,146 @@ _proto_do_setup() { eval "$1_setup \"$interface\" \"$ifname\"" } -proto="$1"; shift -cmd="$1"; shift -interface="$1"; shift -data="$1"; shift -ifname="$1"; shift - -case "$cmd" in - dump) - add_protocol() { - no_device=0 - available=0 - - add_default_handler "$1_init_config" - - json_init - json_add_string "name" "$1" - eval "$1_init" - json_add_boolean no-device "$no_device" - json_add_boolean available "$available" - json_add_array "config" - eval "$1_init_config" - json_close_array - json_dump - } - ;; - *) - add_protocol() { - [[ "$proto" == "$1" ]] || return 0 - - case "$cmd" in - setup) _proto_do_setup "$1";; - teardown) _proto_do_teardown "$1" ;; - *) return 1 ;; - esac - } - ;; -esac +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_int action 0 + 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_notify() { + ubus call network.interface."$interface" notify_proto "$(json_dump)" +} + +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 + _proto_notify +} + +proto_run_command() { + json_init + json_add_int action 1 + json_add_array command + while [ $# -gt 0 ]; do + json_add_string "" "$1" + shift + done + _proto_notify +} + +init_proto() { + proto="$1"; shift + cmd="$1"; shift + + case "$cmd" in + dump) + add_protocol() { + no_device=0 + available=0 + + add_default_handler "$1_init_config" + + json_init + json_add_string "name" "$1" + eval "$1_init" + json_add_boolean no-device "$no_device" + json_add_boolean available "$available" + json_add_array "config" + eval "$1_init_config" + json_close_array + json_dump + } + ;; + setup|teardown) + interface="$1"; shift + data="$1"; shift + ifname="$1"; shift + + add_protocol() { + [[ "$proto" == "$1" ]] || return 0 + + case "$cmd" in + setup) _proto_do_setup "$1";; + teardown) _proto_do_teardown "$1" ;; + *) return 1 ;; + esac + } + ;; + esac +}