00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrcommon/config.h"
00009 #include "ibrcommon/net/NetInterface.h"
00010
00011 #include <sys/types.h>
00012 #include <netinet/ip.h>
00013 #include <netinet/ip6.h>
00014 #include <arpa/inet.h>
00015 #include <sys/socket.h>
00016
00017 #include <stdlib.h>
00018 #include <net/if.h>
00019 #include <ifaddrs.h>
00020 #include <errno.h>
00021
00022 #ifdef HAVE_LOWPAN_SUPPORT
00023 extern "C" {
00024 #include <netlink/netlink.h>
00025 #include <netlink/attr.h>
00026 #include <netlink/genl/genl.h>
00027 #include <netlink/genl/ctrl.h>
00028
00029 #include "ibrcommon/net/ieee802154.h"
00030 #include "ibrcommon/net/nl802154.h"
00031 extern struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1];
00032 };
00033 #endif
00034
00035 #include <unistd.h>
00036
00037 using namespace std;
00038
00039 namespace ibrcommon
00040 {
00041 NetInterface::NetInterface(std::string interface)
00042 : _interface(interface)
00043 {
00044 }
00045
00046 NetInterface::~NetInterface()
00047 {
00048
00049 }
00050
00051 bool NetInterface::operator<(const NetInterface &other) const
00052 {
00053 if (_interface < other._interface) return true;
00054 return false;
00055 }
00056
00057 bool NetInterface::operator==(const NetInterface &other) const
00058 {
00059 if (_interface == other._interface) return true;
00060 return false;
00061 }
00062
00063 std::string NetInterface::getInterface() const
00064 {
00065 return _interface;
00066 }
00067
00068 std::string NetInterface::getAddress() const
00069 {
00070 return NetInterface::getAddress(_interface);
00071 }
00072
00073 std::string NetInterface::getBroadcast() const
00074 {
00075 return NetInterface::getBroadcast(_interface);
00076 }
00077
00078 void NetInterface::getAddress(struct in_addr *ret) const
00079 {
00080 NetInterface::getAddress(_interface, ret);
00081 }
00082
00083 void NetInterface::getAddress(struct ieee802154_addr *ret) const
00084 {
00085 NetInterface::getAddress(_interface, ret);
00086 }
00087
00088 void NetInterface::getBroadcast(struct in_addr *ret) const
00089 {
00090 NetInterface::getBroadcast(_interface, ret);
00091 }
00092
00093 void NetInterface::getAddress(std::string interface, struct in_addr *ret)
00094 {
00095
00096 ret->s_addr = htonl(INADDR_ANY);
00097
00098 struct ifaddrs *ifap = NULL;
00099 int status = getifaddrs(&ifap);
00100
00101 if ((status != 0) || (ifap == NULL))
00102 {
00103
00104 return;
00105 }
00106
00107 for (struct ifaddrs *iter = ifap; iter != NULL; iter = iter->ifa_next)
00108 {
00109 if (iter->ifa_addr == NULL) continue;
00110 if ((iter->ifa_flags & IFF_UP) == 0) continue;
00111
00112 sockaddr *addr = iter->ifa_addr;
00113
00114
00115 if (addr->sa_family != AF_INET) continue;
00116
00117
00118 if (string(iter->ifa_name).compare(interface) != 0) continue;
00119
00120 sockaddr_in *ip = (sockaddr_in*)iter->ifa_addr;
00121
00122 ret->s_addr = ip->sin_addr.s_addr;
00123 break;
00124 }
00125
00126 freeifaddrs(ifap);
00127 }
00128
00129
00130
00131 void NetInterface::getAddress(std::string interface, struct ieee802154_addr *ret)
00132 {
00133 #ifdef HAVE_LOWPAN_SUPPORT
00134 struct nl_handle *nl = nl_handle_alloc();
00135 unsigned char *buf = NULL;
00136 struct sockaddr_nl nla;
00137 struct nlattr *attrs[IEEE802154_ATTR_MAX+1];
00138 struct genlmsghdr *ghdr;
00139 struct nlmsghdr *nlh;
00140 struct nl_msg *msg;
00141 int family;
00142
00143 if (!nl)
00144 return;
00145
00146 genl_connect(nl);
00147
00148
00149 msg = nlmsg_alloc();
00150 family = genl_ctrl_resolve(nl, "802.15.4 MAC");
00151 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, IEEE802154_LIST_IFACE, 1);
00152 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, interface.c_str());
00153 nl_send_auto_complete(nl, msg);
00154 nlmsg_free(msg);
00155
00156
00157 nl_recv(nl, &nla, &buf, NULL);
00158 nlh = (struct nlmsghdr*)buf;
00159 genlmsg_parse(nlh, 0, attrs, IEEE802154_ATTR_MAX, ieee802154_policy);
00160 ghdr = (genlmsghdr*)nlmsg_data(nlh);
00161 if (!attrs[IEEE802154_ATTR_SHORT_ADDR] || !attrs[IEEE802154_ATTR_SHORT_ADDR])
00162 return;
00163
00164
00165 ret->addr_type = IEEE802154_ADDR_SHORT;
00166 ret->pan_id = nla_get_u16(attrs[IEEE802154_ATTR_PAN_ID]);
00167 ret->short_addr = nla_get_u16(attrs[IEEE802154_ATTR_SHORT_ADDR]);
00168
00169 free(buf);
00170 nl_close(nl);
00171 #endif
00172 }
00173
00174 void NetInterface::getBroadcast(std::string interface, struct in_addr *ret)
00175 {
00176
00177 ret->s_addr = htonl(INADDR_BROADCAST);
00178
00179 struct ifaddrs *ifap = NULL;
00180 int status = getifaddrs(&ifap);
00181
00182 if ((status != 0) || (ifap == NULL))
00183 {
00184
00185 return;
00186 }
00187
00188 for (struct ifaddrs *iter = ifap; iter != NULL; iter = iter->ifa_next)
00189 {
00190 if (iter->ifa_addr == NULL) continue;
00191 if ((iter->ifa_flags & IFF_UP) == 0) continue;
00192
00193 sockaddr *addr = iter->ifa_addr;
00194
00195
00196 if (addr->sa_family != AF_INET) continue;
00197
00198
00199 if (string(iter->ifa_name).compare(interface) != 0) continue;
00200
00201 if (iter->ifa_flags & IFF_BROADCAST)
00202 {
00203 sockaddr_in *broadcast = (sockaddr_in*)iter->ifa_broadaddr;
00204 ret->s_addr = broadcast->sin_addr.s_addr;
00205 break;
00206 }
00207 }
00208
00209 freeifaddrs(ifap);
00210 }
00211
00212 std::string NetInterface::getAddress(std::string interface)
00213 {
00214 struct in_addr addr;
00215 NetInterface::getAddress(interface, &addr);
00216 return string(inet_ntoa(addr));
00217 }
00218
00219 std::string NetInterface::getBroadcast(std::string interface)
00220 {
00221 struct in_addr addr;
00222 NetInterface::getBroadcast(interface, &addr);
00223 return string(inet_ntoa(addr));
00224 }
00225 }