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