| 1 | require 'socket' |
| 2 | |
| 3 | # A (very) partial implementation of the functionality available in ethtool |
| 4 | # (and the associated ioctl interface) in pure Ruby. |
| 5 | # |
| 6 | # Why? Because I like to do crazy things. |
| 7 | # |
| 8 | # All the functionality you're likely to actually want is in subclasses of |
| 9 | # Rethtool itself: |
| 10 | # |
| 11 | # Rethtool::InterfaceSettings |
| 12 | # Get the settings (speed, duplex, etc) of an interface |
| 13 | # |
| 14 | # Rethtool::LinkStatus |
| 15 | # Whether the link is up or down |
| 16 | # |
| 17 | class Rethtool |
| 18 | # From /u/i/linux/sockios.h |
| 19 | SIOCETHTOOL = 0x8946 |
| 20 | |
| 21 | # From /u/i/linux/ethtool.h |
| 22 | ETHTOOL_CMD_GLINK = 0x0000000a |
| 23 | ETHTOOL_CMD_GSET = 0x00000001 |
| 24 | |
| 25 | class << self |
| 26 | # Issue an SIOCETHTOOL ioctl. ecmd must respond to #data (such |
| 27 | # as a CStruct subclass instance). The modified data will be |
| 28 | # unserialized and returned. The data you pass in WILL NOT be |
| 29 | # modified, you *must* capture the return value if you want any |
| 30 | # results. |
| 31 | def ioctl(interface, ecmd) |
| 32 | sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) |
| 33 | |
| 34 | ifreq = [interface, ecmd.data].pack("a16p") |
| 35 | |
| 36 | sock.ioctl(SIOCETHTOOL, ifreq) |
| 37 | |
| 38 | rv = ecmd.class.new |
| 39 | rv.data = ifreq.unpack("a16p")[1] |
| 40 | rv |
| 41 | end |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | require 'rethtool/interface_settings' |
| 46 | require 'rethtool/link_status' |