libvisiontransfer  6.0.0
deviceenumeration.cpp
1 /*******************************************************************************
2  * Copyright (c) 2018 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <cstring>
16 
17 #include "visiontransfer/deviceenumeration.h"
18 #include "visiontransfer/exceptions.h"
19 #include "visiontransfer/networking.h"
20 #include "visiontransfer/internalinformation.h"
21 
22 using namespace std;
23 
24 /*************** Pimpl class containing all private members ***********/
25 
26 class DeviceEnumeration::Pimpl {
27 public:
28  Pimpl();
29  DeviceEnumeration::DeviceList discoverDevices();
30 
31 private:
32  static constexpr int RESPONSE_WAIT_TIME_MS = 50;
33  SOCKET sock;
34 
35  std::vector<sockaddr_in> findBroadcastAddresses();
36  void sendDiscoverBroadcast();
37  DeviceEnumeration::DeviceList collectDiscoverResponses();
38 };
39 
40 /******************** Stubs for all public members ********************/
41 
42 DeviceEnumeration::DeviceEnumeration():
43  pimpl(new Pimpl()) {
44  // All initialization in the pimpl class
45 }
46 
47 DeviceEnumeration::~DeviceEnumeration() {
48  delete pimpl;
49 }
50 
51 DeviceEnumeration::DeviceList DeviceEnumeration::discoverDevices() {
52  return pimpl->discoverDevices();
53 }
54 
55 /******************** Implementation in pimpl class *******************/
56 
57 DeviceEnumeration::Pimpl::Pimpl() {
58 #ifdef _WIN32
59  // In windows, we first have to initialize winsock
60  WSADATA wsaData;
61  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
62  throw TransferException("WSAStartup failed!");
63  }
64 #endif
65 
66  // Create socket
67  if((sock = ::socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
68  TransferException ex("Error creating broadcast socket: " + string(strerror(errno)));
69  throw ex;
70  }
71 
72  // Set broadcast flag
73  int broadcastPermission = 1;
74  if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&broadcastPermission),
75  sizeof(broadcastPermission)) < 0) {
76  TransferException ex("Error setting socket broadcast flag: " + string(strerror(errno)));
77  throw ex;
78  }
79 
80  // Set sending and receive timeouts
81 #ifdef _WIN32
82  unsigned int timeout = RESPONSE_WAIT_TIME_MS;
83 #else
84  struct timeval timeout;
85  timeout.tv_sec = 0;
86  timeout.tv_usec = RESPONSE_WAIT_TIME_MS*1000;
87 #endif
88 
89  setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
90  setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
91 }
92 
93 DeviceEnumeration::DeviceList DeviceEnumeration::Pimpl::discoverDevices() {
94  sendDiscoverBroadcast();
95  return collectDiscoverResponses();
96 }
97 
98 void DeviceEnumeration::Pimpl::sendDiscoverBroadcast() {
99  std::vector<sockaddr_in> addresses = findBroadcastAddresses();
100  for(sockaddr_in addr: addresses) {
101  addr.sin_port = htons(InternalInformation::DISCOVERY_BROADCAST_PORT);
102 
103  if (sendto(sock, InternalInformation::DISCOVERY_BROADCAST_MSG,
104  sizeof(InternalInformation::DISCOVERY_BROADCAST_MSG)-1, 0,
105  (struct sockaddr *) &addr, sizeof(addr))
106  != sizeof(InternalInformation::DISCOVERY_BROADCAST_MSG)-1) {
107  throw std::runtime_error("Error sending broadcast message");
108  }
109  }
110 }
111 
112 DeviceEnumeration::DeviceList DeviceEnumeration::Pimpl::collectDiscoverResponses() {
113  DeviceList ret;
114 
115  while(true) {
117  sockaddr_in senderAddress;
118  socklen_t senderLength = sizeof(senderAddress);
119 
120  int received = recvfrom(sock, reinterpret_cast<char*>(&msg), sizeof(msg),
121  0, (sockaddr *)&senderAddress, &senderLength);
122 
123  if(received < 0) {
124  // There are no more replies
125  break;
126  } else if(received != sizeof(msg)) {
127  // Invalid message
128  continue;
129  }
130 
131  // Zero terminate version string
132  char fwVersion[sizeof(msg.firmwareVersion)+1];
133  memcpy(fwVersion, msg.firmwareVersion, sizeof(msg.firmwareVersion));
134  fwVersion[sizeof(msg.firmwareVersion)] = '\0';
135 
136  // Add to result list
137  DeviceInfo info(
138  inet_ntoa(senderAddress.sin_addr),
139  msg.useTcp ? DeviceInfo::PROTOCOL_TCP : DeviceInfo::PROTOCOL_UDP,
140  fwVersion,
141  msg.proVersion ? DeviceInfo::SCENESCAN_PRO : DeviceInfo::SCENESCAN,
142  msg.protocolVersion == InternalInformation::CURRENT_PROTOCOL_VERSION
143  );
144  ret.push_back(info);
145  }
146 
147  return ret;
148 }
149 
150 std::vector<sockaddr_in> DeviceEnumeration::Pimpl::findBroadcastAddresses() {
151  std::vector<sockaddr_in> ret;
152 
153 #ifndef _WIN32
154  // BSD-style implementation
155  struct ifaddrs * ifap;
156  if (getifaddrs(&ifap) == 0) {
157  struct ifaddrs * p = ifap;
158  while(p) {
159  if(p->ifa_dstaddr != nullptr && p->ifa_dstaddr->sa_family == AF_INET) {
160  ret.push_back(*reinterpret_cast<sockaddr_in*>(p->ifa_dstaddr));
161  }
162  p = p->ifa_next;
163  }
164  freeifaddrs(ifap);
165  }
166 #else
167  // Windows XP style implementation
168 
169  // Adapted from example code at http://msdn2.microsoft.com/en-us/library/aa365917.aspx
170  // Now get Windows' IPv4 addresses table. We gotta call GetIpAddrTable()
171  // multiple times in order to deal with potential race conditions properly.
172  MIB_IPADDRTABLE* ipTable = nullptr;
173  ULONG bufLen = 0;
174  for (int i=0; i<5; i++) {
175  DWORD ipRet = GetIpAddrTable(ipTable, &bufLen, false);
176  if (ipRet == ERROR_INSUFFICIENT_BUFFER) {
177  if(ipTable != nullptr) {
178  delete []reinterpret_cast<unsigned char*>(ipTable); // in case we had previously allocated it
179  }
180  ipTable = reinterpret_cast<MIB_IPADDRTABLE *>(new unsigned char[bufLen]);
181  memset(ipTable, 0, bufLen);
182  } else if (ipRet == NO_ERROR) {
183  break;
184  } else {
185  if(ipTable != nullptr) {
186  delete []reinterpret_cast<unsigned char*>(ipTable);
187  }
188  break;
189  }
190  }
191 
192  if (ipTable != nullptr) {
193  for (DWORD i=0; i<ipTable->dwNumEntries; i++) {
194  const MIB_IPADDRROW & row = ipTable->table[i];
195 
196  uint32_t ipAddr = row.dwAddr;
197  uint32_t netmask = row.dwMask;
198  uint32_t baddr = ipAddr & netmask;
199  if (row.dwBCastAddr) {
200  baddr |= ~netmask;
201  }
202 
203  sockaddr_in addr;
204  memset(&addr, 0, sizeof(addr));
205  addr.sin_family = AF_INET;
206  addr.sin_addr.s_addr = baddr;
207  ret.push_back(addr);
208  }
209 
210  delete []reinterpret_cast<unsigned char*>(ipTable);
211  }
212 #endif
213 
214  return ret;
215 }
Exception class that is used for all transfer exceptions.
Definition: exceptions.h:31
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Aggregates information about a discovered device.
Definition: deviceinfo.h:23
Nerian Vision Technologies