Open Lighting Architecture  Latest Git
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) const;
51 
57  std::string ToString(const std::string &separator = ", ") const;
58 
65  friend std::ostream& operator<<(std::ostream &out,
66  const Interface &iface) {
67  return out << iface.ToString();
68  }
69 
70  std::string name;
71  IPV4Address ip_address;
72  IPV4Address bcast_address;
73  IPV4Address subnet_mask;
74  MACAddress hw_address;
75  bool loopback;
76  int32_t index;
77  uint16_t type;
78 
79  /* Void type, nothing is known */
80  static const uint16_t ARP_VOID_TYPE;
81  static const uint16_t ARP_ETHERNET_TYPE;
82 };
83 
84 
89  public:
91  ~InterfaceBuilder() {}
92 
93  void SetName(const std::string &name) { m_name = name; }
94 
95  bool SetAddress(const std::string &ip_address);
96  void SetAddress(const IPV4Address &ip_address) {
97  m_ip_address = ip_address;
98  }
99 
100  bool SetBroadcast(const std::string &broadcast_address);
101  void SetBroadcast(const IPV4Address &broadcast_address) {
102  m_broadcast_address = broadcast_address;
103  }
104 
105  bool SetSubnetMask(const std::string &mask);
106  void SetSubnetMask(const IPV4Address &mask) {
107  m_subnet_mask = mask;
108  }
109 
110  void SetHardwareAddress(const MACAddress &mac_address) {
111  m_hw_address = mac_address;
112  }
113 
114  void SetLoopback(bool loopback);
115 
116  void SetIndex(int32_t index);
117 
118  void SetType(uint16_t type);
119 
120  void Reset();
121  Interface Construct();
122 
123  private:
124  std::string m_name;
125  IPV4Address m_ip_address;
126  IPV4Address m_broadcast_address;
127  IPV4Address m_subnet_mask;
128  MACAddress m_hw_address;
129  bool m_loopback;
130  int32_t m_index;
131  uint16_t m_type;
132 
133  bool SetAddress(const std::string &str, IPV4Address *target);
134 };
135 
136 // Sorts interfaces by index.
138  inline bool operator() (const Interface &if1, const Interface &if2) {
139  return (if1.index < if2.index);
140  }
141 };
142 } // namespace network
143 } // namespace ola
144 #endif // INCLUDE_OLA_NETWORK_INTERFACE_H_
std::string ToString(const std::string &separator=", ") const
Convert the Interface to a string.
Definition: Interface.cpp:132
Represents a IPv4 Address.
Definition: IPV4Address.h:55
Represents a MAC Address.
Definition: MACAddress.h:50
Definition: Interface.h:35
Represents an IPv4 Address.
Definition: Interface.h:137
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Represents a MAC Address.
Definition: Interface.h:88
friend std::ostream & operator<<(std::ostream &out, const Interface &iface)
Write the string representation of this Interface to an ostream.
Definition: Interface.h:65