IBR-DTNSuite  0.12
vinterface.cpp
Go to the documentation of this file.
1 /*
2  * vinterface.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "ibrcommon/config.h"
24 #include "ibrcommon/net/vsocket.h"
25 
26 #ifdef __WIN32__
27 #include <winsock2.h>
28 #include <windows.h>
29 #include <ddk/ntddndis.h>
30 #include <ws2tcpip.h>
31 #include <iphlpapi.h>
32 #else
33 #include <net/if.h>
34 #endif
35 
36 namespace ibrcommon
37 {
38  const std::string vinterface::LOOPBACK = "loopback";
39  const std::string vinterface::ANY = "any";
40 
42  : _name()
43  {
44  }
45 
46  vinterface::vinterface(const std::string &name)
47  : _name(name)
48  {
49  }
50 
51 #ifdef __WIN32__
52  vinterface::vinterface(const std::string &name, const std::wstring &friendlyName)
53  : _name(name), _friendlyName(friendlyName)
54  {
55  }
56 #endif
57 
59  {
60  }
61 
62  const std::string vinterface::toString() const
63  {
64  if (_name.length() == 0) return "<any>";
65  return _name;
66  }
67 
68 #ifdef __WIN32__
69  const std::wstring& vinterface::getFriendlyName() const
70  {
71  return _friendlyName;
72  }
73 #endif
74 
76  {
77  return (_name == LOOPBACK);
78  }
79 
80  bool vinterface::isAny() const
81  {
82  return (_name == ANY);
83  }
84 
85  uint32_t vinterface::getIndex() const
86  {
87 #ifdef __WIN32__
88  PULONG index = 0;
89  GetAdapterIndex(LPWSTR(_name.c_str()), index);
90  return (uint32_t)index;
91 #else
92  return if_nametoindex(_name.c_str());
93 #endif
94  }
95 
96  const std::list<vaddress> vinterface::getAddresses(const std::string &scope) const
97  {
98  if (empty()) throw interface_not_set();
100  }
101 
102  bool vinterface::up() const {
103  return !getAddresses().empty();
104  }
105 
106  bool vinterface::operator<(const vinterface &obj) const
107  {
108  return (_name < obj._name);
109  }
110 
111  bool vinterface::operator==(const vinterface &obj) const
112  {
113  return (obj._name == _name);
114  }
115 
116  bool vinterface::operator!=(const vinterface &obj) const
117  {
118  return (obj._name != _name);
119  }
120 
121  bool vinterface::empty() const
122  {
123  return (_name.length() == 0);
124  }
125 }