Class: Ronin::DB::ASN

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Model
Defined in:
lib/ronin/db/asn.rb

Overview

Represents an ASN range.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Instance Attribute Details

#country_codeString?

The country code of the ASN.

Returns:

  • (String, nil)


86
# File 'lib/ronin/db/asn.rb', line 86

attribute :country_code, :string

#idInteger

The primary key of the ASN range.

Returns:

  • (Integer)


36
# File 'lib/ronin/db/asn.rb', line 36

attribute :id, :integer

#nameString?

The organization the ASN is currently assigned to.

Returns:

  • (String, nil)


92
# File 'lib/ronin/db/asn.rb', line 92

attribute :name, :string

#numberInteger

The ASN number.

Returns:

  • (Integer)


78
# File 'lib/ronin/db/asn.rb', line 78

attribute :number, :integer

#range_endString

The ending IP address of the ASN range.

Returns:

  • (String)


57
# File 'lib/ronin/db/asn.rb', line 57

attribute :range_end, :string

#range_end_htonString (readonly)

The ending IP address of the ASN range, but in network byte-order.

Returns:

  • (String)


70
# File 'lib/ronin/db/asn.rb', line 70

attribute :range_end_hton, :binary

#range_startString

The starting IP address of the ASN range.

Returns:

  • (String)


50
# File 'lib/ronin/db/asn.rb', line 50

attribute :range_start, :string

#range_start_htonString (readonly)

The starting IP address of the ASN range, but in network byte-order.

Returns:

  • (String)


64
# File 'lib/ronin/db/asn.rb', line 64

attribute :range_start_hton, :binary

#versionInteger

Whether the ASN range represents an IPv4 or IPv6 range.

Returns:

  • (Integer)


42
# File 'lib/ronin/db/asn.rb', line 42

attribute :version, :integer

Class Method Details

.containing_ip(ip) ⇒ ASN?

Queries the ASN that contains the given IP address.

Parameters:

  • ip (IPAddr, String)

Returns:



161
162
163
164
165
166
167
168
169
# File 'lib/ronin/db/asn.rb', line 161

def self.containing_ip(ip)
  ip      = IPAddr.new(ip) unless ip.kind_of?(IPAddr)
  ip_hton = ip.hton

  range_start_hton = self.arel_table[:range_start_hton]
  range_end_hton   = self.arel_table[:range_end_hton]

  where(range_start_hton.lteq(ip_hton).and(range_end_hton.gteq(ip_hton))).first
end

.v4Array<ASN>

Searches for all IPv4 ASNs.

Returns:

  • (Array<ASN>)

    The IPv4 ASNs.



102
103
104
# File 'lib/ronin/db/asn.rb', line 102

def self.v4
  where(version: 4)
end

.v6Array<ASNs>

Searches for all IPv6 ASNs.

Returns:

  • (Array<ASNs>)

    The IPv6 ASNs.



114
115
116
# File 'lib/ronin/db/asn.rb', line 114

def self.v6
  where(version: 6)
end

.with_country_code(country_code) ⇒ Array<ASN>

Searches for all ASNs with the matching country code.

Parameters:

  • country_code (String)

    The two letter country code to search for.

Returns:



138
139
140
# File 'lib/ronin/db/asn.rb', line 138

def self.with_country_code(country_code)
  where(country_code: country_code)
end

.with_name(name) ⇒ Array<ASN>

Searches for all ASNs with the matching name.

Parameters:

  • name (String)

    The name to search for.

Returns:



150
151
152
# File 'lib/ronin/db/asn.rb', line 150

def self.with_name(name)
  where(name: name)
end

.with_number(number) ⇒ Array<ASN>

Searches for all ASNs with the matching AS number.

Parameters:

  • number (Integer)

    The AS number to search for.

Returns:



126
127
128
# File 'lib/ronin/db/asn.rb', line 126

def self.with_number(number)
  where(number: number)
end

Instance Method Details

#ip_addressesArray<IPAddress>

Queries all IP addresses within the ASN IP range.

Returns:



194
195
196
# File 'lib/ronin/db/asn.rb', line 194

def ip_addresses
  IPAddress.between(range_start,range_end)
end

#range_end_ipaddrIPAddr?

Returns:

  • (IPAddr, nil)


183
184
185
186
187
# File 'lib/ronin/db/asn.rb', line 183

def range_end_ipaddr
  @range_end_ipaddr ||= if self.range_end
                          IPAddr.new(self.range_end)
                        end
end

#range_start_ipaddrIPAddr?

Returns:

  • (IPAddr, nil)


174
175
176
177
178
# File 'lib/ronin/db/asn.rb', line 174

def range_start_ipaddr
  @range_start_ipaddr ||= if self.range_start
                            IPAddr.new(self.range_start)
                          end
end