From bc5e9ba0b90fb329d2e1c6d2a0e28ddb5de1ac4e Mon Sep 17 00:00:00 2001 From: Mikk Romulus Date: Tue, 16 Jul 2024 10:29:32 +0300 Subject: [PATCH] fix: still a bug setting IPv4 address --- app/models/address.rb | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/app/models/address.rb b/app/models/address.rb index bda39f40..ff4aa18c 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -48,7 +48,7 @@ class Address < ApplicationRecord .where('addresses.offset = address_pools.gateway::varchar') } - before_validation :parse_ipv6, :parse_ipv4, + before_validation :parse_ipv6, :clear_on_mode_change, :clear_offset, :set_to_connection_if_first_address, on: :update @@ -132,7 +132,20 @@ def offset_address ip_object.to_s end - attr_writer :offset_address + def offset_address=(input) + if input && input.blank? + self.offset = nil + else + network_object = ip_family_network + address = IPAddress::IPv4.new("#{input}/#{network_object.prefix}") rescue nil + if address && network_object.include?(address) + self.offset = address.u32 - network_object.network_u32 - 1 + else + errors.add(:offset, :invalid) + errors.add(:offset_address, :invalid) + end + end + end def all_ip_objects return unless offset @@ -175,22 +188,6 @@ def parse_ipv6 self.errors.add(:parsed_ipv6, :invalid) end - def parse_ipv4 - return if ipv6? - if offset_address && offset_address.blank? - self.offset = nil - else - network_object = ip_family_network - address = IPAddress::IPv4.new("#{offset_address}/#{network_object.prefix}") rescue nil - if address && network_object.include?(address) - self.offset = address.u32 - network_object.network_u32 - 1 - else - errors.add(:offset, :invalid) - errors.add(:offset_address, :invalid) - end - end - end - def check_ip_offset4 return unless mode_ipv4_static? && offset