The Pedigree Project  0.1
user/applications/netconfig/main.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <arpa/inet.h>
21 #include <iostream>
22 #include <sstream>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <string>
27 #include <vector>
28 
29 using namespace std;
30 
31 static uint32_t g_Options = 0;
32 
33 template <class T>
34 void convert(std::string &out, T t)
35 {
36  stringstream ss;
37  ss << t;
38  out = ss.str();
39 }
40 
41 namespace Pedigree
42 {
44 class IpAddress
45 {
46  public:
48  enum IpType
49  {
50  IPv4 = 0,
51  IPv6
52  };
53 
55  IpAddress() : m_Type(IPv4), m_Ipv4(0), m_Ipv6(), m_bSet(false){};
56  IpAddress(IpType type) : m_Type(type), m_Ipv4(0), m_Ipv6(), m_bSet(false){};
57  IpAddress(uint32_t ipv4)
58  : m_Type(IPv4), m_Ipv4(ipv4), m_Ipv6(), m_bSet(true){};
59  IpAddress(uint8_t *ipv6) : m_Type(IPv6), m_Ipv4(0), m_Ipv6(), m_bSet(true)
60  {
61  memcpy(m_Ipv6, ipv6, 16);
62  };
63 
66  void setIp(uint32_t ipv4)
67  {
68  m_Ipv4 = ipv4;
69  memset(m_Ipv6, 0, 16);
70  m_bSet = true;
71  m_Type = IPv4;
72  }
73 
74  void setIp(uint8_t *ipv6)
75  {
76  m_Ipv4 = 0;
77  memcpy(m_Ipv6, ipv6, 16);
78  m_bSet = true;
79  m_Type = IPv6;
80  }
81 
84  inline uint32_t getIp()
85  {
86  return m_Ipv4;
87  }
88 
89  inline void getIp(uint8_t *ipv6)
90  {
91  if (ipv6)
92  memcpy(ipv6, m_Ipv6, 16);
93  }
94 
98  {
99  return m_Type;
100  }
101 
105  {
106  if (a.getType() == IPv6)
107  {
108  a.getIp(m_Ipv6);
109  m_Ipv4 = 0;
110  m_Type = IPv6;
111  m_bSet = true;
112  }
113  else
114  setIp(a.getIp());
115  return *this;
116  }
117 
118  IpAddress &operator=(uint32_t a)
119  {
120  setIp(a);
121  return *this;
122  }
123 
124  bool operator==(IpAddress a)
125  {
126  if (a.getType() == IPv4)
127  return (a.getIp() == getIp());
128  else
129  {
130  // probably use memcmp for IPv6... too lazy to check at the moment
131  return false;
132  }
133  }
134 
135  bool operator==(uint32_t a)
136  {
137  if (getType() == IPv4)
138  return (a == getIp());
139  else
140  return false;
141  }
142 
143  string toString()
144  {
146  if (m_Type == IPv4)
147  {
148  string str;
149  string tmp;
150  str.clear();
151  convert<uint32_t>(tmp, m_Ipv4 & 0xff);
152  str += tmp;
153  str += ".";
154  convert<uint32_t>(tmp, (m_Ipv4 >> 8) & 0xff);
155  str += tmp;
156  str += ".";
157  convert<uint32_t>(tmp, (m_Ipv4 >> 16) & 0xff);
158  str += tmp;
159  str += ".";
160  convert<uint32_t>(tmp, (m_Ipv4 >> 24) & 0xff);
161  str += tmp;
162  return str;
163  }
164  return "";
165  }
166 
167  private:
168  IpType m_Type;
169 
170  uint32_t m_Ipv4; // the IPv4 address
171  uint8_t m_Ipv6[16]; // the IPv6 address
172 
173  bool m_bSet; // has the IP been set yet?
174 };
175 
177 {
178  public:
179  NetworkDevice() : name(""), ipv4(), subnet(), gateway(), dns(){};
180  virtual ~NetworkDevice(){};
181 
182  string name;
183  IpAddress ipv4;
185  IpAddress gateway;
186  vector<IpAddress> dns;
187 };
188 
189 class Network
190 {
191  public:
192  Network() : m_Devices()
193  {
194  struct NetworkDevice dev;
195  dev.name = "eth0";
196  dev.ipv4 = 0x0100007f;
197  dev.subnet = 0x000000ff;
198  dev.gateway = 0;
199  dev.dns.clear();
200  m_Devices.push_back(dev);
201  dev.name = "eth1";
202  dev.ipv4 = inet_addr("10.0.0.2");
203  dev.subnet = 0x00ffffff;
204  dev.gateway = inet_addr("10.0.0.1");
205  dev.dns.push_back(inet_addr("10.0.0.10"));
206  dev.dns.push_back(inet_addr("10.0.0.11"));
207  dev.dns.push_back(inet_addr("10.0.0.12"));
208  dev.dns.push_back(inet_addr("10.0.0.13"));
209  m_Devices.push_back(dev);
210  dev.dns.clear();
211  dev.name = "eth2";
212  dev.ipv4 = inet_addr("192.168.1.100");
213  dev.subnet = 0x00ffffff;
214  dev.gateway = inet_addr("192.168.1.254");
215  dev.dns.push_back(inet_addr("192.168.1.254"));
216  m_Devices.push_back(dev);
217  };
218  virtual ~Network()
219  {
220  m_Devices.clear();
221  };
222 
223  static Network &instance()
224  {
225  return m_Instance;
226  }
227 
228  vector<NetworkDevice> &getDevices()
229  {
230  return m_Devices;
231  }
232 
233  private:
234  static Network m_Instance;
235 
236  vector<NetworkDevice> m_Devices;
237 };
238 
239 Network Network::m_Instance;
240 } // namespace Pedigree
241 
242 int main(int argc, char *argv[])
243 {
244  // Parse arguments
245  for (int arg = 1; arg < argc; arg++)
246  {
247  // TODO: Write Me :D
248  }
249 
250  // Enumerate network devices
251  vector<Pedigree::NetworkDevice> devices =
252  Pedigree::Network::instance().getDevices();
253  for (vector<Pedigree::NetworkDevice>::iterator it = devices.begin();
254  it != devices.end(); ++it)
255  {
256  Pedigree::NetworkDevice dev = *it;
257  cout << "Device '" << dev.name << "':" << endl;
258  cout << "\tIPv4: " << dev.ipv4.toString()
259  << ", Subnet Mask: " << dev.subnet.toString()
260  << ", Gateway: " << dev.gateway.toString() << endl;
261  cout << "\tDNS servers:";
262  for (vector<Pedigree::IpAddress>::iterator dns = dev.dns.begin();
263  dns != dev.dns.end(); ++dns)
264  cout << " " << (*dns).toString();
265  cout << endl;
266  }
267 
268  return 0;
269 }
IpAddress & operator=(IpAddress a)
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition: Iterator.h:326