Method: Inspec::Resources::AixPorts#parse_net_address

Defined in:
lib/resources/port.rb

#parse_net_address(net_addr, protocol) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/resources/port.rb', line 331

def parse_net_address(net_addr, protocol)
  # local/foreign addresses on AIX use a '.' to separate the addresss
  # from the port
  address, _sep, port = net_addr.rpartition('.')
  if protocol.eql?('tcp6') || protocol.eql?('udp6')
    ip6addr = address
    # AIX uses the wildcard character for ipv6 addresses listening on
    # all interfaces.
    ip6addr = '::' if ip6addr =~ /^\*$/

    # v6 addresses need to end in a double-colon when using
    # shorthand notation. netstat ends with a single colon.
    # IPAddr will fail to properly parse an address unless it
    # uses a double-colon for short-hand notation.
    ip6addr += ':' if ip6addr =~ /\w:$/

    begin
      ip_parser = IPAddr.new(ip6addr)
    rescue IPAddr::InvalidAddressError
      # This IP is not parsable. There appears to be a bug in netstat
      # output that truncates link-local IP addresses:
      # example: udp6 0 0 fe80::42:acff:fe11::123 :::* 0 54550 3335/ntpd
      # actual link address: inet6 fe80::42:acff:fe11:5/64 scope link
      #
      # in this example, the "5" is truncated making the netstat output
      # an invalid IP address.
      return [nil, nil]
    end

    # Check to see if this is a IPv4 address in a tcp6/udp6 line.
    # If so, don't put brackets around the IP or URI won't know how
    # to properly handle it.
    # example: f000000000000000 tcp6       0      0 127.0.0.1.8005          *.*                    LISTEN
    if ip_parser.ipv4?
      ip_addr = URI("addr://#{ip6addr}:#{port}")
      host = ip_addr.host
    else
      ip_addr = URI("addr://[#{ip6addr}]:#{port}")
      host = ip_addr.host[1..ip_addr.host.size-2]
    end
  else
    ip4addr = address
    # In AIX the wildcard character is used to match all interfaces
    ip4addr = '0.0.0.0' if ip4addr =~ /^\*$/
    ip_addr = URI("addr://#{ip4addr}:#{port}")
    host = ip_addr.host
  end

  [host, port.to_i]
end