Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Interface.h
17  * Represents a network interface.
18  * Copyright (C) 2010-2014 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_INTERFACE_H_
22 #define INCLUDE_OLA_NETWORK_INTERFACE_H_
23 
24 #ifdef WIN32
25 // TODO(Peter): Do something else
26 #else
27 #include <net/if_arp.h>
28 #ifndef ARPHRD_VOID
29 // Not defined on FreeBSD
30 #define ARPHRD_VOID 0xFFFF /* Void type, nothing is known. */
31 #endif // ARPHRD_VOID
32 #endif
33 
34 #include <stdint.h>
36 #include <ola/network/MACAddress.h>
37 #include <string>
38 
39 namespace ola {
40 namespace network {
41 
42 /*
43  * Represents an interface.
44  */
45 class Interface {
46  public:
47  enum { DEFAULT_INDEX = -1 };
48 
49  Interface();
50  Interface(const std::string &name,
51  const IPV4Address &ip_address,
52  const IPV4Address &broadcast_address,
53  const IPV4Address &subnet_mask,
54  const MACAddress &hw_address,
55  bool loopback,
56  int32_t index = DEFAULT_INDEX,
57  uint16_t type = ARPHRD_VOID);
58  Interface(const Interface &other);
59  Interface& operator=(const Interface &other);
60  bool operator==(const Interface &other);
61 
62  std::string name;
63  IPV4Address ip_address;
64  IPV4Address bcast_address;
65  IPV4Address subnet_mask;
66  MACAddress hw_address;
67  bool loopback;
68  int32_t index;
69  uint16_t type;
70 };
71 
72 
77  public:
79  ~InterfaceBuilder() {}
80 
81  void SetName(const std::string &name) { m_name = name; }
82 
83  bool SetAddress(const std::string &ip_address);
84  void SetAddress(const IPV4Address &ip_address) {
85  m_ip_address = ip_address;
86  }
87 
88  bool SetBroadcast(const std::string &broadcast_address);
89  void SetBroadcast(const IPV4Address &broadcast_address) {
90  m_broadcast_address = broadcast_address;
91  }
92 
93  bool SetSubnetMask(const std::string &mask);
94  void SetSubnetMask(const IPV4Address &mask) {
95  m_subnet_mask = mask;
96  }
97 
98  void SetHardwareAddress(const MACAddress &mac_address) {
99  m_hw_address = mac_address;
100  }
101 
102  void SetLoopback(bool loopback);
103 
104  void SetIndex(int32_t index);
105 
106  void SetType(uint16_t type);
107 
108  void Reset();
110 
111  private:
112  std::string m_name;
113  IPV4Address m_ip_address;
114  IPV4Address m_broadcast_address;
115  IPV4Address m_subnet_mask;
116  MACAddress m_hw_address;
117  bool m_loopback;
118  int32_t m_index;
119  uint16_t m_type;
120 
121  bool SetAddress(const std::string &str, IPV4Address *target);
122 };
123 
124 // Sorts interfaces by index.
126  inline bool operator() (const Interface &if1, const Interface &if2) {
127  return (if1.index < if2.index);
128  }
129 };
130 } // namespace network
131 } // namespace ola
132 #endif // INCLUDE_OLA_NETWORK_INTERFACE_H_