]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/hooks/configs/ipv4-static
config hook: prevent two hooks with the same settings
[people/ms/network.git] / src / hooks / configs / ipv4-static
index 7c9d94f968c6bdb111a3e889c47ab5e3ea0f43cf..ef74991ec44de20def2a80d3956ce41481bb3029 100644 (file)
@@ -23,9 +23,9 @@
 
 HOOK_MANPAGE="network-config-ipv4-static"
 
-HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
+HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
-hook_check() {
+hook_check_config_settings() {
        assert isset ADDRESS
        assert isinteger PREFIX
 
@@ -35,12 +35,9 @@ hook_check() {
        fi
 }
 
-hook_create() {
-       local zone="${1}"
-       assert zone_exists "${zone}"
-       shift
-
+hook_parse_cmdline() {
        local arg
+
        while read -r arg; do
                local key="$(cli_get_key "${arg}")"
                local val="$(cli_get_val "${arg}")"
@@ -102,12 +99,28 @@ hook_create() {
                exit ${EXIT_CONF_ERROR}
        fi
 
+       if zone_config_check_same_setting "${zone}" "ipv4-static" "ADDRESS" "${ADDRESS}"; then
+               error "An ipv4-static config with the same IPv4 address is already configured"
+               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}
+hook_new() {
+       local zone="${1}"
+       shift
+
+       assert zone_exists "${zone}"
+
+       if ! hook_parse_cmdline $@; then
+               # Return an error if the parsing of the cmd line fails
+               return ${EXIT_ERROR}
+       fi
+
+       zone_config_settings_write "${zone}" "${HOOK}"
 
        exit ${EXIT_OK}
 }
@@ -122,7 +135,7 @@ 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}
 
@@ -147,8 +160,11 @@ hook_down() {
                error "Zone '${zone}' doesn't exist."
                exit ${EXIT_ERROR}
        fi
-       
-       zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
+
+       # Remove routing information from database.
+       db_delete "${zone}/ipv4"
+
+       zone_config_settings_read "${zone}" "${config}"
 
        ip_address_del ${zone} ${ADDRESS}/${PREFIX}
 
@@ -172,7 +188,7 @@ 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
@@ -190,4 +206,3 @@ hook_status() {
 
        exit ${EXIT_OK}
 }
-