Class: Ronin::DB::ASN

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Model::HasName::ClassMethods
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::HasName::ClassMethods

named, with_name

Methods included from Model

included

Instance Attribute Details

#country_codeString?

The country code of the ASN.

Returns:

  • (String, nil)


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

attribute :country_code, :string

#idInteger

The primary key of the ASN range.

Returns:

  • (Integer)


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

attribute :id, :integer

#nameString?

The organization the ASN is currently assigned to.

Returns:

  • (String, nil)


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

attribute :name, :string

#numberInteger

The ASN number.

Returns:

  • (Integer)


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

attribute :number, :integer

#range_endString

The ending IP address of the ASN range.

Returns:

  • (String)


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

attribute :range_end, :string

#range_end_htonString (readonly)

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

Returns:

  • (String)


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

attribute :range_end_hton, :binary

#range_startString

The starting IP address of the ASN range.

Returns:

  • (String)


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

attribute :range_start, :string

#range_start_htonString (readonly)

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

Returns:

  • (String)


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

attribute :range_start_hton, :binary

#versionInteger

Whether the ASN range represents an IPv4 or IPv6 range.

Returns:

  • (Integer)


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

attribute :version, :integer

Class Method Details

.containing_ip(ip) ⇒ ASN?

Queries the ASN that contains the given IP address.

Parameters:

  • ip (IPAddr, String)

Returns:



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

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.



104
105
106
# File 'lib/ronin/db/asn.rb', line 104

def self.v4
  where(version: 4)
end

.v6Array<ASNs>

Searches for all IPv6 ASNs.

Returns:

  • (Array<ASNs>)

    The IPv6 ASNs.



116
117
118
# File 'lib/ronin/db/asn.rb', line 116

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:



140
141
142
# File 'lib/ronin/db/asn.rb', line 140

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:



152
153
154
# File 'lib/ronin/db/asn.rb', line 152

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:



128
129
130
# File 'lib/ronin/db/asn.rb', line 128

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:



196
197
198
# File 'lib/ronin/db/asn.rb', line 196

def ip_addresses
  IPAddress.between(range_start,range_end)
end

#range_end_ipaddrIPAddr?

Returns:

  • (IPAddr, nil)


185
186
187
188
189
# File 'lib/ronin/db/asn.rb', line 185

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

#range_start_ipaddrIPAddr?

Returns:

  • (IPAddr, nil)


176
177
178
179
180
# File 'lib/ronin/db/asn.rb', line 176

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

#to_sString

Converts the ASN to a String.

Returns:

  • (String)

    The AS<number> String.

Since:

  • 0.2.0



208
209
210
# File 'lib/ronin/db/asn.rb', line 208

def to_s
  "AS#{self.number}"
end