lib/ronin/mac_address.rb in ronin-1.2.0 vs lib/ronin/mac_address.rb in ronin-1.3.0
- old
+ new
@@ -15,30 +15,68 @@
#
# You should have received a copy of the GNU General Public License
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
#
+require 'ronin/extensions/regexp'
require 'ronin/address'
require 'ronin/ip_address'
require 'ronin/ip_address_mac_address'
+require 'strscan'
+
module Ronin
#
# Represents MAC addresses that can be stored in the {Database}.
#
class MACAddress < Address
# The MAC address
property :address, String, :length => 17..17,
:required => true,
- :unique => true
+ :unique => true,
+ :format => /^#{Regexp::MAC}$/,
+ :messages => {
+ :format => 'Must be a valid MAC address'
+ }
# The IP Addresses the MAC Address hosts
has 0..n, :ip_address_mac_addresses, :model => 'IPAddressMACAddress'
# The IP Addresses associated with the MAC Address
has 0..n, :ip_addresses, :through => :ip_address_mac_addresses,
:model => 'IPAddress'
+
+ #
+ # Extracts MAC addresses from the given text.
+ #
+ # @param [String] text
+ # The text to parse.
+ #
+ # @yield [mac]
+ # The given block will be passed each extracted MAC address.
+ #
+ # @yieldparam [MACAddress] mac
+ # An extracted MAC Address
+ #
+ # @return [Array<MACAddress>]
+ # If no block is given, an Array of MACAddress will be returned.
+ #
+ # @see 1.3.0
+ #
+ # @api public
+ #
+ def self.extract(text)
+ return enum_for(:extract,text).to_a unless block_given?
+
+ scanner = StringScanner.new(text)
+
+ while scanner.skip_until(Regexp::MAC)
+ yield parse(scanner.matched)
+ end
+
+ return nil
+ end
#
# The IP Address that most recently used the MAC Address.
#
# @return [IpAddress]