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