You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "onvifMediaObserver.h"
  2. #define LOG(X) basicLogCollection::getInstance()->Log("onvifMediaObserver", this, X)
  3. #define LOG_ERROR(X) basicLogCollection::getInstance()->Log("onvifMediaObserver", this, _LOG_ERROR, X)
  4. #define TLOG(X) basicLogCollection::getInstance()->Log("TonvifMediaObserver", me, X)
  5. #define TLOG_ERROR(X) basicLogCollection::getInstance()->Log("TonvifMediaObserver", me, _LOG_ERROR, X)
  6. onvifMediaObserver * onvifMediaObserver::_instance = 0;
  7. /**
  8. * Returns the single instance of the object.
  9. */
  10. onvifMediaObserver * onvifMediaObserver::getInstance()
  11. {
  12. // check if the instance has been created yet
  13. if (0 == _instance)
  14. {
  15. // if not, then create it
  16. _instance = new onvifMediaObserver;
  17. }
  18. // return the single instance
  19. return _instance;
  20. }
  21. // Constructor implementation
  22. inline onvifMediaObserver::onvifMediaObserver()
  23. {
  24. m_pLog = basicLogCollection::getInstance();
  25. // m_pSettings = basicSettingsProvider::getInstance();
  26. // m_pSettingsNode = m_pSettings->getSubNode(NULL, "ONVIF_MEDIA_OBSERVER");
  27. // Must Be checked to not to take any VPN TUN devices!!
  28. m_pIpAddresses = getLocalIP();
  29. m_nStatus = 0;
  30. #ifdef __linux__
  31. m_bWSAOK = true;
  32. #else
  33. m_bWSAOK = false;
  34. WORD wVersionRequested;
  35. WSADATA wsaData;
  36. int err;
  37. wVersionRequested = MAKEWORD(2, 2);
  38. err = WSAStartup(wVersionRequested, &wsaData);
  39. if (err != 0)
  40. {
  41. LOG_ERROR("WSAStartup failed with error");
  42. }
  43. else
  44. {
  45. m_bWSAOK = true;
  46. }
  47. #endif
  48. }
  49. inline onvifMediaObserver::~onvifMediaObserver()
  50. {
  51. if (m_vThreads.size())
  52. { // Kill-Wait Started Probes
  53. LOG("Have to Wait Probes!");
  54. for (onvifObserverThreads::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++)
  55. {
  56. pthread_join((*index)->m_hThreadHandle, NULL);
  57. };
  58. m_vThreads.clear();
  59. };
  60. LOG("No More Probes!");
  61. if (m_vResultPool.size())
  62. { // Clear structures
  63. for (vectorWoWoResults::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++)
  64. {
  65. free(*index);
  66. };
  67. m_vResultPool.clear();
  68. };
  69. LOG("Data is Cleared!");
  70. _instance = NULL;
  71. }
  72. bool onvifMediaObserver::startDiscovery(void)
  73. {
  74. m_nStatus = 0;
  75. double dStatus, dStep;
  76. dStatus = 0;
  77. if (! m_bWSAOK)
  78. {
  79. return false;
  80. }
  81. if (m_vResultPool.size())
  82. { // Clear structures
  83. for (vectorWoWoResults::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++)
  84. {
  85. free(*index);
  86. };
  87. m_vResultPool.clear();
  88. };
  89. if (m_pIpAddresses != NULL)
  90. {
  91. if (m_pIpAddresses->size() > 0)
  92. {
  93. // Start Reaper thread
  94. dStep = 30.0 / (m_pIpAddresses->size() + 1);
  95. // Start Discoverers on each broadcast IP
  96. int id = 0;
  97. for (localIpAddresses::iterator index = m_pIpAddresses->begin(); index < m_pIpAddresses->end(); index++)
  98. {
  99. if (strlen((*index)->broadcast))
  100. {
  101. startProbe((*index)->broadcast, id, (int) dStep);
  102. }
  103. dStatus += dStep;
  104. m_nStatus = (int) dStatus;
  105. id++;
  106. };
  107. startProbe(HTTPMU_HOST_ADDRESS, id, (int) dStep);
  108. dStatus += dStep;
  109. m_nStatus = (int) dStatus;
  110. return true;
  111. }
  112. else
  113. {
  114. LOG_ERROR("No Networks Detected!");
  115. };
  116. }
  117. else
  118. {
  119. LOG_ERROR("ERROR FETCHING LOCAL IP ADDRESSES!");
  120. }
  121. return false;
  122. }
  123. probeWoWo * onvifMediaObserver::init_WoWo(CSTR ADR, int ID, int stp)
  124. {
  125. probeWoWo * RetVal;
  126. RetVal = (probeWoWo *) malloc(sizeof(probeWoWo));
  127. if (RetVal == NULL)
  128. {
  129. return NULL;
  130. }
  131. sprintf(RetVal->IpAddress, "%s", ADR);
  132. RetVal->Port = HTTPMU_HOST_PORT;
  133. RetVal->socketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  134. RetVal->send_buff = (CSTR) SEARCH_REQUEST_STRING;
  135. RetVal->r_address.sin_family = AF_INET;
  136. RetVal->r_address.sin_port = htons(RetVal->Port);
  137. RetVal->r_address.sin_addr.s_addr = inet_addr(RetVal->IpAddress);
  138. RetVal->bOptVal = true;
  139. RetVal->bOptLen = sizeof(bool);
  140. RetVal->iOptLen = sizeof(int);
  141. RetVal->AddressID = ID;
  142. RetVal->step = stp;
  143. RetVal->observer = this;
  144. return RetVal;
  145. }
  146. void * onvifMediaObserver::probeThreadProc(void * ptr)
  147. {
  148. probeWoWo * me;
  149. if (ptr == NULL)
  150. {
  151. return NULL;
  152. }
  153. me = (probeWoWo *) ptr;
  154. // TLOG("Probe Request Sent");
  155. me->RetVal = setsockopt(me->socketFD, SOL_SOCKET, SO_BROADCAST, (char *) &me->bOptVal, me->bOptLen);
  156. me->RetVal = sendto(me->socketFD, me->send_buff, strlen(me->send_buff), 0, (struct sockaddr *) &me->r_address, sizeof(struct sockaddr_in));
  157. // sprintf(me->buff, "Sent %d Bytes", me->RetVal );
  158. // TLOG(me->buff);
  159. #ifdef __linux__
  160. fcntl(me->socketFD, F_SETFL, fcntl(me->socketFD, F_GETFL, 0) | O_NONBLOCK);
  161. #else
  162. u_long val = 1;
  163. ioctlsocket(me->socketFD, FIONBIO, &val); // none block
  164. #endif
  165. for (me->index = 1; me->index <= 10; me->index++)
  166. {
  167. memset(me->buff, 0, sizeof(me->buff));
  168. me->RetVal = recvfrom(me->socketFD, me->buff, MAX_BUFF_SIZE, 0, NULL, NULL);
  169. if (me->RetVal < 0)
  170. { // SOCKET_ERROR){
  171. SLEEP(1000);
  172. continue;
  173. };
  174. me->recv_buff = me->buff;
  175. XMLResults tParseResults;
  176. XMLNode response = XMLNode::parseString(me->buff, "root", &tParseResults);
  177. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  178. FILE * fd;
  179. fd = fopen("ONVIF_MSGS.txt", "a+");
  180. fprintf(fd, me->buff);
  181. fprintf(fd, "--------------------\r\n");
  182. fclose(fd);
  183. #endif
  184. if (tParseResults.error)
  185. {
  186. char tBuffer[CTEXT];
  187. sprintf(tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  188. TLOG_ERROR(tBuffer);
  189. }
  190. else
  191. {
  192. if (response.isEmpty())
  193. {
  194. TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  195. continue;
  196. }
  197. else
  198. {
  199. XMLNode Body = response.getChildNode("Body", 0);
  200. if (! Body.isEmpty())
  201. {
  202. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0); // EndpointReference
  203. if (! ProbeMatches.isEmpty())
  204. {
  205. int matches = ProbeMatches.nChildNode();
  206. resultWoWo * tRecord;
  207. for (int index = 0; index < matches; index++)
  208. {
  209. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  210. tRecord = (resultWoWo *) malloc(sizeof(resultWoWo));
  211. if (tRecord != NULL)
  212. {
  213. memset(tRecord, 0, sizeof(resultWoWo));
  214. tRecord->wowoID = me->AddressID;
  215. if (! ProbeMatch.isEmpty())
  216. {
  217. {
  218. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  219. if (! node.isEmpty())
  220. {
  221. XMLNode nod = node.getChildNode("Address", 0);
  222. char tBuf[CLOG];
  223. sprintf(tBuf, "%s", nod.getText(0));
  224. CArray * tArr;
  225. tArr = explode(':', tBuf);
  226. for (int j = 0; j < tArr->size(); j++)
  227. {
  228. if (strlen(tArr->at(j)) > 10)
  229. {
  230. sprintf(tRecord->RefID, "%s", tArr->at(j));
  231. break;
  232. };
  233. };
  234. tArr->clear();
  235. delete(tArr);
  236. };
  237. }
  238. {
  239. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  240. char tBuf[CLOG];
  241. sprintf(tBuf, "%s", node.getText(0));
  242. CArray * tArr;
  243. tArr = explode(' ', tBuf);
  244. sprintf(tRecord->Type, "%s", tArr->at(0));
  245. tArr->clear();
  246. delete(tArr);
  247. }
  248. {
  249. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  250. char tBuf[CLOG];
  251. sprintf(tBuf, "%s", node.getText(0));
  252. CArray * tArr;
  253. tArr = explode(' ', tBuf);
  254. if (tArr->size() > 0)
  255. {
  256. for (int index = 0; index < tArr->size(); index++)
  257. {
  258. char * tPtr;
  259. if (tPtr = strstr(tArr->at(index), "mac/"))
  260. {
  261. sprintf(tRecord->MAC, "%s", tPtr + 4);
  262. };
  263. if (tPtr = strstr(tArr->at(index), "hardware/"))
  264. {
  265. sprintf(tRecord->Hardware, "%s", tPtr + 9);
  266. };
  267. if (tPtr = strstr(tArr->at(index), "name/"))
  268. {
  269. sprintf(tRecord->Name, "%s", tPtr + 5);
  270. };
  271. if (tPtr = strstr(tArr->at(index), "MAC/"))
  272. {
  273. sprintf(tRecord->MAC, "%s", tPtr + 4);
  274. };
  275. if (tPtr = strstr(tArr->at(index), "HARDWARE/"))
  276. {
  277. sprintf(tRecord->Hardware, "%s", tPtr + 9);
  278. };
  279. if (tPtr = strstr(tArr->at(index), "NAME/"))
  280. {
  281. sprintf(tRecord->Name, "%s", tPtr + 5);
  282. };
  283. };
  284. tArr->clear();
  285. };
  286. delete(tArr);
  287. }
  288. {
  289. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  290. char tBuf[CLOG];
  291. sprintf(tBuf, "%s", node.getText(0));
  292. CArray * tArr;
  293. tArr = explode(' ', tBuf);
  294. if (tArr->size() > 0)
  295. {
  296. for (int index = 0; index < tArr->size(); index++)
  297. {
  298. if (strstr(tArr->at(index), "[") != NULL)
  299. {
  300. // IPv6
  301. }
  302. else
  303. {
  304. // IPv4
  305. sprintf(tRecord->XAddrs, "%s", tArr->at(0));
  306. };
  307. };
  308. tArr->clear();
  309. };
  310. delete(tArr);
  311. }
  312. };
  313. me->observer->m_vResultPool.push_back(tRecord);
  314. };
  315. };
  316. };
  317. };
  318. };
  319. };
  320. };
  321. me->observer->m_nStatus += me->step;
  322. // TLOG("Probe Finished");
  323. #ifdef __linux__
  324. close(me->socketFD);
  325. #else
  326. closesocket(me->socketFD);
  327. #endif
  328. free(me);
  329. return NULL;
  330. }
  331. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp)
  332. {
  333. probeWoWo * tVal;
  334. tVal = init_WoWo(IP, ID, stp);
  335. if (tVal == NULL)
  336. {
  337. return false;
  338. }
  339. onvifObserverThread * tThread;
  340. tThread = (onvifObserverThread *) malloc(sizeof(onvifObserverThread));
  341. if (tThread != NULL)
  342. {
  343. #ifdef __linux__
  344. int RetVal = pthread_create(&tThread->m_hThreadHandle, NULL, probeThreadProc, (void *) tVal);
  345. if (RetVal == -1)
  346. {
  347. LOG_ERROR("Thread Creation Faled.");
  348. return false;
  349. };
  350. #else
  351. tThread->m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) probeThreadProc, (LPVOID) tVal, 0, &tThread->m_dwThreadID);
  352. if (tThread->m_hThreadHandle == NULL)
  353. {
  354. free(tVal);
  355. free(tThread);
  356. return false;
  357. };
  358. #endif
  359. // LOG("Thread Started");
  360. tThread->context = tVal;
  361. m_vThreads.push_back(tThread);
  362. return true;
  363. };
  364. return false;
  365. }
  366. #undef LOG
  367. #undef LOG_ERROR
  368. #undef TLOG
  369. #undef TLOG_ERROR