PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/ruby-ifconfig-1.2/lib/ifconfig/common/interface_types.rb

https://github.com/kazuyas/trema
Ruby | 130 lines | 96 code | 17 blank | 17 comment | 9 complexity | 4205af09c4cad7a37a6e2e48062d0bfd MD5 | raw file
  1. # $Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
  2. #
  3. class NetworkAdapter
  4. def initialize(name, ifacetxt)
  5. @name = name
  6. @ifconfig = ifacetxt
  7. @status = false
  8. @protos = ['inet','inet6','IPX/Ethernet II',
  9. 'IPX/Ethernet SNAP',
  10. 'IPX/Ethernet 802.2',
  11. 'IPX/Ethernet 802.3',
  12. 'EtherTalk Phase 2'].join("|")
  13. @networks = {}
  14. @flags = []
  15. @mtu = nil
  16. @metric = nil
  17. @rx = @tx = {}
  18. parse_ifconfig
  19. end
  20. attr_reader :status, :name, :flags, :mtu, :networks
  21. attr_accessor :tx, :rx
  22. # take array and turn each two entries into
  23. # hash key and value, also converts each value to an integer
  24. #
  25. # [1,2,3,4] => { 1 => 2, 3 => 4}
  26. #
  27. # Internal utility function used to populate rx and tx hashes
  28. def array_to_hash_elem(array)
  29. h = {}
  30. if array.length.modulo(2) != 0
  31. puts "Array mus have even number of elements to turn into a hash"
  32. return nil
  33. end
  34. while array.length > 0
  35. h[array.shift] = array.shift.to_i
  36. end
  37. return h
  38. end
  39. # Return all addresses bound to this interface or
  40. # optionally only the specified type of network address
  41. #
  42. def addresses(type=nil)
  43. a = []
  44. @networks.each_value { |network|
  45. a << (network.addr) if network.nettype == type or type.nil?
  46. }
  47. return a
  48. end
  49. def ifacetype
  50. return self.class
  51. end
  52. def up?
  53. return status
  54. end
  55. # returns array of arrays
  56. # [ [address , type ] ]
  57. #
  58. def addrs_with_type
  59. addrs = []
  60. @networks.each_value { |network|
  61. addrs.push([network.addr,network.nettype])
  62. }
  63. return addrs
  64. end
  65. def addr_types
  66. types = []
  67. @networks.each_value { |network|
  68. types.push(network.nettype) unless types.include?(network.nettype)
  69. }
  70. return types
  71. end
  72. def has_addr?(addr)
  73. return self.addresses.include?(addr)
  74. end
  75. def to_s
  76. s = @name+":"+self.ifacetype.to_s+"\n"
  77. @networks.keys.sort.each { |network|
  78. s += @networks[network].to_s+"\n"
  79. }
  80. if self.rx['bytes'] && self.tx['bytes']
  81. s += " RX bytes: #{self.rx['bytes']}, TX bytes: #{self.tx['bytes']}\n"
  82. elsif self.rx['packets'] && self.tx['packets']
  83. s += " RX packets: #{self.rx['packets']}, TX packets: #{self.tx['packets']}\n"
  84. end
  85. s += " MTU: #{@mtu}\n"
  86. s += " Metric: #{@metric}\n"
  87. s += " Flags: #{@flags.join(',')}\n"
  88. s += " Status: UP" if self.status
  89. return s
  90. end
  91. end
  92. # each platform defines it's own set_mac
  93. # function to get the mac address
  94. #
  95. class EthernetAdapter < NetworkAdapter
  96. def initialize(name,ifconfigtxt)
  97. super(name,ifconfigtxt)
  98. @mac = set_mac
  99. end
  100. attr_reader :mac, :interrupt, :rxbytes, :txbytes, :rxpackets,
  101. :txpackets
  102. def to_s
  103. super + "\n MAC: #{@mac}"
  104. end
  105. end
  106. class PPP < NetworkAdapter
  107. end
  108. class LoopbackInterface < NetworkAdapter
  109. end
  110. class IPv6_in_IPv4 < NetworkAdapter
  111. end
  112. class SerialLineIP < NetworkAdapter
  113. end