Skip to content

Commit

Permalink
fix: still a bug setting IPv4 address
Browse files Browse the repository at this point in the history
  • Loading branch information
mromulus committed Jul 16, 2024
1 parent 0e05e78 commit bc5e9ba
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bc5e9ba

Please sign in to comment.