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