ISAAC
Overview :: Library Doc :: Server Doc :: JSON Commands

In Situ Animation of Accelerated Computations

NetworkInterfaces.cpp
Go to the documentation of this file.
1 /* This file is part of ISAAC.
2  *
3  * ISAAC is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * ISAAC is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with ISAAC. If not, see <www.gnu.org/licenses/>. */
15 
16 #include "NetworkInterfaces.hpp"
17 #include <sys/socket.h>
18 #include <netdb.h>
19 #include <arpa/inet.h>
20 
21 struct ifaddrs * NetworkInterfaces::ifaddr = NULL;
22 
23 void NetworkInterfaces::initIfaddr()
24 {
25  if (ifaddr == NULL)
26  getifaddrs(&ifaddr);
27 }
28 
29 void NetworkInterfaces::bindInterface(in_addr_t &s_addr,std::string interface,bool ipv6)
30 {
31  if (interface.compare(std::string("*")) == 0)
32  {
33  s_addr = INADDR_ANY;
34  return;
35  }
36  else
37  s_addr = INADDR_NONE;
38  initIfaddr();
39  int correct_family = AF_INET;
40  int size_of_struct = sizeof(struct sockaddr_in);
41  if (ipv6)
42  {
43  correct_family = AF_INET6;
44  size_of_struct = sizeof(struct sockaddr_in6);
45  }
46  struct ifaddrs * it = ifaddr;
47  while (it)
48  {
49  int family, s, n;
50  char host[NI_MAXHOST];
51  family = it->ifa_addr->sa_family;
52  if (family == correct_family)
53  {
54  s = getnameinfo(it->ifa_addr,size_of_struct,host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
55  if (interface.compare(std::string(it->ifa_name)) == 0)
56  {
57  s_addr = inet_addr(host);
58  return;
59  }
60  }
61  it = it->ifa_next;
62  }
63  printf("Warning: interface %s does not exist, therefore no connection can be established!\n",interface.c_str());
64 }
static struct ifaddrs * ifaddr
static void bindInterface(in_addr_t &s_addr, std::string interface, bool ipv6=false)