X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Fhooks%2Fconfigs%2Fipv4-static;h=72b748c777054c232436981b0de765d813f75b3f;hb=b6d9bf2bf70a358d48321621f02b5aecc60d7f1c;hp=9f9b3de38ca75c5ad0fca306425a2f25c939447a;hpb=e9df08ad7a55a403ebd4f36b7d249b9bf27b276c;p=people%2Fstevee%2Fnetwork.git diff --git a/src/hooks/configs/ipv4-static b/src/hooks/configs/ipv4-static index 9f9b3de3..72b748c7 100644 --- a/src/hooks/configs/ipv4-static +++ b/src/hooks/configs/ipv4-static @@ -21,9 +21,11 @@ . /usr/lib/network/header-config -HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" +HOOK_MANPAGE="network-config-ipv4-static" -function hook_check() { +HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" + +hook_check_config_settings() { assert isset ADDRESS assert isinteger PREFIX @@ -33,39 +35,84 @@ function hook_check() { fi } -function hook_create() { - local zone=${1} +hook_new() { + local zone="${1}" + assert zone_exists "${zone}" shift - while [ $# -gt 0 ]; do - case "${1}" in - --address=*) - ADDRESS=${1#--address=} + local arg + while read -r arg; do + local key="$(cli_get_key "${arg}")" + local val="$(cli_get_val "${arg}")" + + case "${key}" in + address) + if ! ipv4_is_valid "${val}"; then + error "Invalid IPv4 address: ${val}" + exit ${EXIT_CONF_ERROR} + fi + + ADDRESS="${val}" + ;; + + prefix) + if ! ipv4_prefix_is_valid "${val}"; then + error "Invalid IPv4 prefix: ${val}" + exit ${EXIT_CONF_ERROR} + fi + + PREFIX="${val}" ;; - --netmask=*) - NETMASK=${1#--netmask=} + + gateway) + if ! ipv4_is_valid "${val}"; then + error "Invalid IPv4 address for gateway: ${val}" + exit ${EXIT_CONF_ERROR} + fi + + GATEWAY="${val}" ;; - --prefix=*) - PREFIX=${1#--prefix=} + + # Compatibility switches + netmask) + if ! ipv4_netmask_is_valid "${val}"; then + error "Invalid netmask: ${val}" + exit ${EXIT_CONF_ERROR} + fi + + # The netmask will be converted into a prefix + PREFIX="$(ipv4_netmask2prefix ${val})" ;; - --gateway=*) - GATEWAY=${1#--gateway=} + + # Unknown switches + *) + error "Unhandled argument: ${arg}" + exit ${EXIT_CONF_ERROR} ;; esac - shift - done + done <<< "$(args $@)" + + if ! isset ADDRESS; then + error "You need to provide an IPv4 address" + exit ${EXIT_CONF_ERROR} + fi - if [ -z "${PREFIX}" -a -n "${NETMASK}" ]; then - PREFIX=$(ipv4_mask_to_cidr ${NETMASK}) + if ! isset PREFIX; then + error "You need to provide an IPv4 prefix" + exit ${EXIT_CONF_ERROR} + fi + + if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then + warning "You did not configure a gateway for a non-local zone" fi # XXX maybe we can add some hashing to identify a configuration again - zone_config_settings_write "${zone}" "${HOOK}.$(uuid)" ${HOOK_SETTINGS} + zone_config_settings_write "${zone}" "${HOOK}.$(uuid)" exit ${EXIT_OK} } -function hook_up() { +hook_up() { local zone=${1} local config=${2} shift 2 @@ -75,15 +122,15 @@ function hook_up() { exit ${EXIT_ERROR} fi - zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} + zone_config_settings_read "${zone}" "${config}" ip_address_add ${zone} ${ADDRESS}/${PREFIX} # Save configuration - routing_db_set ${zone} ipv4 type "${HOOK}" - routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}" - routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}" - routing_db_set ${zone} ipv4 active 1 + db_set "${zone}/ipv4/type" "${HOOK}" + db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}" + db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}" + db_set "${zone}/ipv4/active" 1 routing_update ${zone} ipv4 routing_default_update @@ -91,7 +138,7 @@ function hook_up() { exit ${EXIT_OK} } -function hook_down() { +hook_down() { local zone=${1} local config=${2} shift 2 @@ -101,7 +148,7 @@ function hook_down() { exit ${EXIT_ERROR} fi - zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} + zone_config_settings_read "${zone}" "${config}" ip_address_del ${zone} ${ADDRESS}/${PREFIX} @@ -111,9 +158,13 @@ function hook_down() { exit ${EXIT_OK} } -function hook_status() { - local zone=${1} - local config=${2} +hook_status() { + local zone="${1}" + assert isset zone + + local config="${2}" + assert isset config + shift 2 if ! device_exists ${zone}; then @@ -121,7 +172,7 @@ function hook_status() { exit ${EXIT_ERROR} fi - zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} + zone_config_settings_read "${zone}" "${config}" local status if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then @@ -139,26 +190,3 @@ function hook_status() { exit ${EXIT_OK} } - -function ipv4_mask_to_cidr() { - local mask=0 - - local field - for field in $(tr '.' ' ' <<<${1}); do - mask=$(( $(( ${mask} << 8 )) | ${field} )) - done - - local cidr=0 - local x=$(( 128 << 24 )) # 0x80000000 - - while [ $(( ${x} & ${mask} )) -ne 0 ]; do - [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 )) - cidr=$((${cidr} + 1)) - done - - if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff - echo "Invalid net mask: $1" >&2 - else - echo ${cidr} - fi -}