eth_interface.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef NMRPFLASH_ETH_INTERFACE
  2. #define NMRPFLASH_ETH_INTERFACE
  3. #include <boost/predef.h>
  4. #include <string>
  5. #include <pcap.h>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include "address.h"
  10. namespace nmrpflash {
  11. ///! An Ethernet interface.
  12. //
  13. //
  14. //
  15. //
  16. class eth_interface
  17. {
  18. public:
  19. #if !BOOST_OS_WINDOWS
  20. typedef unsigned index_type;
  21. typedef std::string pretty_type;
  22. #else
  23. typedef DWORD index_type;
  24. typedef std::wstring pretty_type;
  25. #endif
  26. /**
  27. * Constructs an interface from a given name.
  28. *
  29. * On POSIX systems, this is a device name
  30. * such as `en0`, or `eth2`, etc.
  31. *
  32. * On Windows, this can be any of the following:
  33. * * ANSI interface name
  34. * * Interface GUID
  35. * * Npcap/WinPCAP name (`\Device\NPF_<GUID>`)
  36. */
  37. eth_interface(const std::string& name);
  38. eth_interface(const pcap_if_t* intf);
  39. ~eth_interface();
  40. /// Returns this interface's MAC address.
  41. const mac_addr& get_mac_addr() const { return m_mac_addr; }
  42. /// Returns this interface's interface index.
  43. index_type get_index() const { return m_index; }
  44. /// Returns a name that can be passed to `pcap_open_live`.
  45. const std::string& get_pcap_name() const { return m_pcap_name; }
  46. /**
  47. * Returns a short, human readable name.
  48. *
  49. * On POSIX, this is the device name, while
  50. * on Windows it's the ANSI device name.
  51. */
  52. std::string get_name() const;
  53. /**
  54. * Returns a less technical name.
  55. *
  56. * On Linux, returns NetworkManager's `GENERAL.CONNECTION` attribute,
  57. * if this device is being managed by NetworkManager. Otherwise
  58. * an empty string.
  59. *
  60. * On Windows, returns the name used in the Control Panel/Settings,
  61. * and by the `ipconfig` utility.
  62. *
  63. * On macOS, returns the interface name used by System Preferences.
  64. */
  65. const std::string& get_pretty_name() const { return m_pretty_name; }
  66. /// Returns a list of this interface's IP addresses.
  67. std::vector<ip_net> list_networks(bool ipv4_only = false) const;
  68. /**
  69. * Adds an IPv4 address to this interface.
  70. *
  71. * If `permanent` is `false`, the address is removed when the
  72. * `eth_interface` object is destroyed.
  73. */
  74. void add_ip_addr(const ip4_addr& addr, bool permanent = false);
  75. void del_ip_addr(const ip4_addr& addr);
  76. /**
  77. * Adds an entry to the device's ARP table.
  78. *
  79. * If `permanent` is `false`, the mapping is removed, when the
  80. * `eth_interface` object is destroyed.
  81. */
  82. void add_arp_entry(const mac_addr& mac, const ip4_addr& ip, bool permanent = false);
  83. void del_arp_entry(const mac_addr& mac);
  84. static std::vector<eth_interface> list();
  85. private:
  86. void with_pcap_if(std::function<void(const pcap_if_t&)> f) const;
  87. index_type m_index;
  88. mac_addr m_mac_addr;
  89. bool m_is_bridge = false;
  90. std::string m_pcap_name;
  91. pretty_type m_pretty_name;
  92. std::set<ip4_addr> m_ip_undo;
  93. std::map<mac_addr, ip4_addr> m_arp_undo;
  94. };
  95. }
  96. #endif