Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Interface.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Interface.h
17  * Represents a network interface.
18  * Copyright (C) 2010 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_INTERFACE_H_
22 #define INCLUDE_OLA_NETWORK_INTERFACE_H_
23 
24 #include <stdint.h>
26 #include <ola/network/MACAddress.h>
27 #include <string>
28 
29 namespace ola {
30 namespace network {
31 
32 /*
33  * Represents an interface.
34  */
35 class Interface {
36  public:
37  enum { DEFAULT_INDEX = -1 };
38 
39  Interface();
40  Interface(const std::string &name,
41  const IPV4Address &ip_address,
42  const IPV4Address &broadcast_address,
43  const IPV4Address &subnet_mask,
44  const MACAddress &hw_address,
45  bool loopback,
46  int32_t index = DEFAULT_INDEX,
47  uint16_t type = ARP_VOID_TYPE);
48  Interface(const Interface &other);
49  Interface& operator=(const Interface &other);
50  bool operator==(const Interface &other);
51 
52  std::string name;
53  IPV4Address ip_address;
54  IPV4Address bcast_address;
55  IPV4Address subnet_mask;
56  MACAddress hw_address;
57  bool loopback;
58  int32_t index;
59  uint16_t type;
60 
61  /* Void type, nothing is known */
62  static const uint16_t ARP_VOID_TYPE;
63  static const uint16_t ARP_ETHERNET_TYPE;
64 };
65 
66 
71  public:
73  ~InterfaceBuilder() {}
74 
75  void SetName(const std::string &name) { m_name = name; }
76 
77  bool SetAddress(const std::string &ip_address);
78  void SetAddress(const IPV4Address &ip_address) {
79  m_ip_address = ip_address;
80  }
81 
82  bool SetBroadcast(const std::string &broadcast_address);
83  void SetBroadcast(const IPV4Address &broadcast_address) {
84  m_broadcast_address = broadcast_address;
85  }
86 
87  bool SetSubnetMask(const std::string &mask);
88  void SetSubnetMask(const IPV4Address &mask) {
89  m_subnet_mask = mask;
90  }
91 
92  void SetHardwareAddress(const MACAddress &mac_address) {
93  m_hw_address = mac_address;
94  }
95 
96  void SetLoopback(bool loopback);
97 
98  void SetIndex(int32_t index);
99 
100  void SetType(uint16_t type);
101 
102  void Reset();
104 
105  private:
106  std::string m_name;
107  IPV4Address m_ip_address;
108  IPV4Address m_broadcast_address;
109  IPV4Address m_subnet_mask;
110  MACAddress m_hw_address;
111  bool m_loopback;
112  int32_t m_index;
113  uint16_t m_type;
114 
115  bool SetAddress(const std::string &str, IPV4Address *target);
116 };
117 
118 // Sorts interfaces by index.
120  inline bool operator() (const Interface &if1, const Interface &if2) {
121  return (if1.index < if2.index);
122  }
123 };
124 } // namespace network
125 } // namespace ola
126 #endif // INCLUDE_OLA_NETWORK_INTERFACE_H_