summaryrefslogtreecommitdiffstats
path: root/src/sockets/interfaces.cpp
diff options
context:
space:
mode:
authorMichal Kubecek <mkubecek@suse.cz>2015-04-13 09:21:39 +0200
committerMichal Kubecek <mkubecek@suse.cz>2015-04-13 09:21:39 +0200
commite2bc6f4153813cc570ae814c8ddb74628009b488 (patch)
treea40b171be1d859c2232ccc94f758010f9ae54d3c /src/sockets/interfaces.cpp
downloadtwinkle-e2bc6f4153813cc570ae814c8ddb74628009b488.tar
twinkle-e2bc6f4153813cc570ae814c8ddb74628009b488.tar.gz
twinkle-e2bc6f4153813cc570ae814c8ddb74628009b488.tar.lz
twinkle-e2bc6f4153813cc570ae814c8ddb74628009b488.tar.xz
twinkle-e2bc6f4153813cc570ae814c8ddb74628009b488.zip
initial checkin
Check in contents of upstream 1.4.2 tarball, exclude generated files.
Diffstat (limited to 'src/sockets/interfaces.cpp')
-rw-r--r--src/sockets/interfaces.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/sockets/interfaces.cpp b/src/sockets/interfaces.cpp
new file mode 100644
index 0000000..293c48d
--- /dev/null
+++ b/src/sockets/interfaces.cpp
@@ -0,0 +1,114 @@
+/*
+ Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include <cstring>
+
+#include "interfaces.h"
+#include "url.h"
+
+using namespace std;
+
+t_interface::t_interface(string _name) : name(_name) {}
+
+string t_interface::get_ip_addr(void) const {
+ return inet_ntoa(address);
+}
+
+string t_interface::get_ip_netmask(void) const {
+ return inet_ntoa(netmask);
+}
+
+list <t_interface> *get_interfaces(bool include_loopback) {
+ struct ifaddrs *ifa, *ifaddrs;
+ struct sockaddr_in *sin;
+ t_interface *intf;
+
+ list<t_interface> *result = new list<t_interface>;
+
+ if (getifaddrs(&ifaddrs)) {
+ // No interfaces found
+ return result;
+ }
+
+ for (ifa = ifaddrs; ifa ; ifa = ifa -> ifa_next) {
+ // Skip interface without address
+ // Skip interfaces marked DOWN and LOOPBACK.
+ if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
+ ((ifa->ifa_flags & IFF_LOOPBACK) && !include_loopback)) {
+ continue;
+ }
+
+ // Add the interface to the list if it has an IP4 address
+ switch(ifa->ifa_addr->sa_family) {
+ case AF_INET:
+ intf = new t_interface(ifa->ifa_name);
+ sin = (struct sockaddr_in *)ifa->ifa_addr;
+ memcpy(&intf->address, &sin->sin_addr,
+ sizeof(struct in_addr));
+ sin = (struct sockaddr_in *)ifa->ifa_netmask;
+ memcpy(&intf->netmask, &sin->sin_addr,
+ sizeof(struct in_addr));
+
+ result->push_back(*intf);
+ delete intf;
+ break;
+ }
+ }
+
+ freeifaddrs(ifaddrs);
+
+ return result;
+}
+
+bool exists_interface(const string &hostname) {
+ struct hostent *h;
+
+ h = gethostbyname(hostname.c_str());
+ if (h == NULL) return false;
+ string ipaddr = inet_ntoa(*((struct in_addr *)h->h_addr));
+
+ list<t_interface> *l = get_interfaces(true);
+
+ for (list<t_interface>::iterator i = l->begin(); i != l->end(); i++) {
+ if (i->get_ip_addr() == ipaddr) {
+ delete l;
+ return true;
+ }
+ }
+
+ delete l;
+ return false;
+}
+
+
+
+bool exists_interface_dev(const string &devname, string &ip_address) {
+
+ list<t_interface> *l = get_interfaces(true);
+
+ for (list<t_interface>::iterator i = l->begin(); i != l->end(); i++) {
+ if (i->name == devname) {
+ ip_address = i->get_ip_addr();
+ delete l;
+ return true;
+ }
+ }
+
+ delete l;
+ return false;
+}