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.

498 lines
21 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseModuleOnvifObserver.h"
  2. #include "rgooseKernel.h"
  3. #include "rgooseHardwareResourceNetworkProvider.h"
  4. #define SEARCH_REQUEST_STRING "<?xml version=\"1.0\" encoding=\"UTF-8\"?><e:Envelope xmlns:e=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:w=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dn=\"http://www.onvif.org/ver10/network/wsdl\"><e:Header><w:MessageID>uuid:84ede3de-7dec-11d0-c360-f01234567890</w:MessageID><w:To e:mustUnderstand=\"true\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To><w:Action a:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action></e:Header><e:Body><d:Probe><d:Types>dn:NetworkVideoTransmitter</d:Types></d:Probe></e:Body></e:Envelope>"
  5. #define SEARCH_REQUEST_STRING2323 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" \
  6. "<SOAP:Envelope xmlns:SOAP=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:DISCOVERY=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:ssdp=\"http://example.com/wsdp/sample/print\">\n" \
  7. "<SOAP:Header>\n" \
  8. "<wsa:MessageID>urn:uuid:ac9823ee-6813-498c-8c1b-6272a22353f7</wsa:MessageID>\n" \
  9. "<wsa:Action>\n" \
  10. "http ://schemas.xmlsoap.org/ws/2005/04/discovery/Probe\n" \
  11. "</wsa:Action>\n" \
  12. "<wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>\n" \
  13. "</SOAP:Header>\n" \
  14. "<SOAP:Body>\n" \
  15. "<DISCOVERY:Probe>\n" \
  16. "<DISCOVERY:Types>ssdp:any</DISCOVERY:Types>\n" \
  17. "</DISCOVERY:Probe>\n" \
  18. "</SOAP:Body>\n" \
  19. "</SOAP:Envelope>\r\n\r\n"
  20. #define HTTP_OK "200 OK"
  21. /******************************************************************
  22. ** Action Defines *
  23. *******************************************************************/
  24. #define HTTP_HEADER_ACTION "POST %s HTTP/1.1\r\n" \
  25. "HOST: %s:%u\r\n" \
  26. "SOAPACTION:\"%s#%s\"\r\n" \
  27. "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n"\
  28. "Content-Length: %d \r\n\r\n"
  29. #define SOAP_ACTION "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
  30. "<s:Envelope xmlns:s=" \
  31. "\"http://schemas.xmlsoap.org/soap/envelope/\" " \
  32. "s:encodingStyle=" \
  33. "\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
  34. "<s:Body>\r\n" \
  35. "<u:%s xmlns:u=\"%s\">\r\n%s" \
  36. "</u:%s>\r\n" \
  37. "</s:Body>\r\n" \
  38. "</s:Envelope>\r\n"
  39. #define PORT_MAPPING_LEASE_TIME "63072000" //two year
  40. #define ADD_PORT_MAPPING_PARAMS "<NewRemoteHost></NewRemoteHost>\r\n" \
  41. "<NewExternalPort>%u</NewExternalPort>\r\n"\
  42. "<NewProtocol>%s</NewProtocol>\r\n" \
  43. "<NewInternalPort>%u</NewInternalPort>\r\n"\
  44. "<NewInternalClient>%s</NewInternalClient>\r\n" \
  45. "<NewEnabled>1</NewEnabled>\r\n" \
  46. "<NewPortMappingDescription>%s</NewPortMappingDescription>\r\n" \
  47. "<NewLeaseDuration>" \
  48. PORT_MAPPING_LEASE_TIME \
  49. "</NewLeaseDuration>\r\n"
  50. #define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, X )
  51. #ifdef DEBUG
  52. #define LOG_ERROR( X ) {\
  53. char _tBfrErrMSG2w34[256];\
  54. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  55. argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  56. }
  57. #else
  58. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, X );
  59. #endif
  60. argusModuleOnvifObserver::argusModuleOnvifObserver( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_ONVIF_OBSERVER ), basicThread(){
  61. m_bEnabled = false;
  62. m_pEventHeap = basicEventHeap::getInstance();
  63. m_bCurrentNIF = 0;
  64. m_bProbeIsActive = false;
  65. m_bDetached = false;
  66. #ifdef __linux__
  67. m_bWSAOK = true;
  68. #else
  69. m_bWSAOK = false;
  70. WORD wVersionRequested;
  71. WSADATA wsaData;
  72. int err;
  73. wVersionRequested = MAKEWORD(2, 2);
  74. err = WSAStartup(wVersionRequested, &wsaData);
  75. if (err != 0){
  76. LOG_ERROR("WSAStartup failed with error");
  77. } else m_bWSAOK = true;
  78. #endif
  79. };
  80. argusModuleOnvifObserver::~argusModuleOnvifObserver(){
  81. if( !m_bDetached ) stopService( true );
  82. };
  83. bool argusModuleOnvifObserver::startService(){
  84. LOG("Starting Service!");
  85. if( !m_bWSAOK ) return false;
  86. m_bEnabled = reloadSettings();
  87. return init();
  88. };
  89. bool argusModuleOnvifObserver::stopService( bool force ){
  90. m_bEnabled = false;
  91. if( !m_bDetached ) m_bDetached = uninit();
  92. if( m_bDetached ) {
  93. //Clear Results
  94. m_mResultProtector.lock();
  95. if (m_vResultPool.size()){ // Clear structures
  96. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  97. free(*index);
  98. };
  99. m_vResultPool.clear();
  100. };
  101. m_mResultProtector.unlock();
  102. };
  103. return m_bDetached;
  104. };
  105. void argusModuleOnvifObserver::m_fThreadProc(){
  106. int socketFD;
  107. int RetVal;
  108. struct sockaddr_in r_address;
  109. bool bOptVal;
  110. int bOptLen;
  111. int iOptLen;
  112. uint8_t bCounterTO = 100; // 1 second
  113. char rBuffer[MAX_BUFF_SIZE];
  114. #ifdef DEBUG
  115. char tB[128];
  116. LOG("Module Started!");
  117. #endif
  118. m_bDetached = false;
  119. m_bThreadStatus = TH_WORKING;
  120. while( m_bThreadStatus < TH_ENDED ) {
  121. if ( m_bEnabled ) {
  122. if ( m_bProbeIsActive ) {
  123. if( socketFD > -1 ){
  124. if( bCounterTO-- ){
  125. memset( rBuffer, 0, MAX_BUFF_SIZE );
  126. #ifdef DEBUG_EXTRA
  127. struct sockaddr src_addr;
  128. socklen_t addrlen = sizeof( src_addr );
  129. RetVal = recvfrom( socketFD, rBuffer, MAX_BUFF_SIZE, 0, &src_addr, &addrlen );
  130. sprintf( tB, "(%d){%d}MSG[%d] from %s", bCounterTO, socketFD, RetVal, inet_ntoa(((struct sockaddr_in *)&src_addr)->sin_addr) );
  131. LOG( tB );
  132. #else
  133. RetVal = recvfrom( socketFD, rBuffer, MAX_BUFF_SIZE, 0, NULL, NULL );
  134. #endif
  135. if ((RetVal == -1) && /*(errno != EAGAIN) &&*/ (errno != EINTR) && (errno != EWOULDBLOCK)) {
  136. LOG_ERROR("Error acures while reading UDP package");
  137. close( socketFD );
  138. m_bProbeIsActive = false;
  139. continue; // Error ocured but continue to next address
  140. };
  141. if( RetVal < 1 ){
  142. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  143. continue; // No Data - wait to next time
  144. };
  145. OBSERVER_RESULT *tResult = parseXML( rBuffer );
  146. if ( tResult ) {
  147. m_mResultProtector.lock();
  148. m_vResultPool.push_back( tResult );
  149. m_mResultProtector.unlock();
  150. #ifdef DEBUG
  151. LOG("Probe Finished!");
  152. #endif
  153. } else {
  154. #ifdef DEBUG
  155. LOG("Probe Finished with unrecognized data!");
  156. #endif
  157. };
  158. } else {
  159. #ifdef DEBUG
  160. LOG("Probe Finished By Timeout!");
  161. #endif
  162. };
  163. #ifdef __linux__
  164. close(socketFD);
  165. #else
  166. closesocket(socketFD);
  167. #endif
  168. };
  169. m_bProbeIsActive = false;
  170. //Sleep 5 minutes if done all scans
  171. if ( m_bCurrentNIF == 0 ){
  172. for( int i=30000; i, m_bThreadStatus < TH_ENDED; i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  173. };
  174. } else {
  175. // start new probe;
  176. m_mNIFMUTEX.lock();
  177. if ( m_vNIFIPS.size() ) {
  178. #ifdef DEBUG_EXTRA
  179. sprintf( tB, "Starting probe on %s address", m_vNIFIPS.at(m_bCurrentNIF) );
  180. LOG( tB );
  181. #endif
  182. socketFD = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  183. if( socketFD > -1 ){
  184. CSTR send_buff = (CSTR)SEARCH_REQUEST_STRING;
  185. r_address.sin_family = AF_INET;
  186. r_address.sin_port = htons(HTTPMU_HOST_PORT);
  187. r_address.sin_addr.s_addr = inet_addr(m_vNIFIPS.at(m_bCurrentNIF));
  188. bOptVal = true;
  189. #ifdef DEBUG_EXTRA
  190. LOG("Sending Probe Request");
  191. #endif
  192. RetVal = setsockopt( socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen );
  193. RetVal = sendto( socketFD, send_buff, strlen(send_buff), 0, (struct sockaddr*)&r_address, sizeof(struct sockaddr_in));
  194. if ( RetVal > -1 ) {
  195. #ifdef DEBUG_EXTRA
  196. sprintf( tB, "Sent %d Bytes of %d", RetVal, strlen(send_buff) );
  197. LOG( tB );
  198. #endif
  199. #ifdef __linux__
  200. fcntl( socketFD, F_SETFL, fcntl( socketFD, F_GETFL, 0) | O_NONBLOCK );
  201. #else
  202. u_long val = 1;
  203. ioctlsocket( socketFD, FIONBIO, &val );//none block
  204. #endif
  205. bCounterTO = 100;
  206. m_bProbeIsActive = true;
  207. } else {
  208. #ifdef DEBUG
  209. // Failure to send probe package
  210. LOG_ERROR("Error in sending Probe Request");
  211. #endif
  212. #ifdef __linux__
  213. close(socketFD);
  214. #else
  215. closesocket(socketFD);
  216. #endif
  217. };
  218. };
  219. m_bCurrentNIF++;
  220. if ( m_bCurrentNIF == m_vNIFIPS.size() ) m_bCurrentNIF = 0;
  221. };
  222. m_mNIFMUTEX.unlock();
  223. };
  224. } else {
  225. //Sleep 3 Sec.
  226. for( int i=300; i, m_bThreadStatus < TH_ENDED; i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  227. };
  228. };
  229. m_bDetached = true;
  230. };
  231. void argusModuleOnvifObserver::dispatchEvent(EVENT_CONTAINER *Evt){
  232. if(!m_bEnabled) {
  233. #ifdef DEBUG
  234. LOG("Module Disabled!");
  235. #endif
  236. return;
  237. }
  238. if( Evt == NULL ) return;
  239. };
  240. bool argusModuleOnvifObserver::reloadSettings(){
  241. m_mNIFMUTEX.lock();
  242. if( m_vNIFIPS.size() ) {
  243. for ( vectorCSTR::iterator idx=m_vNIFIPS.begin(); idx < m_vNIFIPS.end(); idx++ ) free(*idx);
  244. m_vNIFIPS.clear();
  245. };
  246. m_mNIFMUTEX.unlock();
  247. if( m_pKernel != NULL ){
  248. #ifdef DEBUG
  249. LOG("Check IPS");
  250. #endif
  251. argusHardwareManager *tHWM = m_pKernel->getHardwareManager();
  252. if( tHWM != NULL ) {
  253. DBC_INT_LIST *rIDS = tHWM->requestResourcesByType( ARGUS_HARDWARE_PROVIDER_NETWORK );
  254. if( rIDS != NULL ) {
  255. for( DBC_INT_LIST::iterator idx = rIDS->begin(); idx < rIDS->end(); idx++ ){
  256. argusHardwareResourceNetwork *tRES = (argusHardwareResourceNetwork *) tHWM->requestResource( ARGUS_HARDWARE_PROVIDER_NETWORK, *idx );
  257. if( tRES != NULL ) {
  258. CSTR BC = tRES->getBroadcast();
  259. if( BC!= NULL ) {
  260. #ifdef DEBUG
  261. LOG("Have BC Address");
  262. LOG(BC);
  263. #endif
  264. m_mNIFMUTEX.lock();
  265. m_vNIFIPS.push_back( BC );
  266. m_mNIFMUTEX.unlock();
  267. #ifdef DEBUG
  268. LOG("NIF defined");
  269. #endif
  270. }
  271. #ifdef DEBUG
  272. else {
  273. LOG("NO BC Address");
  274. }
  275. #endif
  276. ;
  277. } else {
  278. LOG_ERROR("Cannot Get Resource!");
  279. };
  280. };
  281. delete( rIDS );
  282. } else {
  283. LOG("No Networks Detected!");
  284. m_bEnabled = false;
  285. };
  286. } else {
  287. LOG_ERROR("No HW Manager");
  288. };
  289. } else {
  290. LOG_ERROR("Kernel Not Assigned!" );
  291. };
  292. #ifdef DEBUG
  293. LOG("Default Probe");
  294. LOG( HTTPMU_HOST_ADDRESS );
  295. #endif
  296. CSTR tBC = (CSTR) malloc(16);
  297. if ( tBC ) {
  298. strncpy(tBC, HTTPMU_HOST_ADDRESS, 16);
  299. m_mNIFMUTEX.lock();
  300. m_vNIFIPS.push_back( tBC );
  301. m_mNIFMUTEX.unlock();
  302. } else {
  303. LOG_ERROR("Malloc Error!");
  304. };
  305. return true;
  306. };
  307. OBSERVER_RESULT *argusModuleOnvifObserver::parseXML( CSTR data ){
  308. OBSERVER_RESULT *retVal = NULL;
  309. if( data!=NULL ){
  310. XMLResults tParseResults;
  311. XMLNode response = XMLNode::parseString( data, "root", &tParseResults );
  312. if( tParseResults.error ){
  313. char tBuffer[CTEXT];
  314. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  315. LOG_ERROR( tBuffer );
  316. } else {
  317. if( response.isEmpty() ){
  318. LOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  319. } else {
  320. #ifdef DEBUG
  321. LOG("Probe XML parsed");
  322. #endif
  323. XMLNode Body = response.getChildNode( "Body", 0 );
  324. if(!Body.isEmpty()){
  325. LOG("Have Body elm");
  326. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  327. if(!ProbeMatches.isEmpty()) {
  328. LOG("Have ProbeMathes elm");
  329. int matches = ProbeMatches.nChildNode();
  330. for(int index=0;index<matches;index++){
  331. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  332. retVal = (OBSERVER_RESULT *)malloc(sizeof(OBSERVER_RESULT));
  333. if( retVal != NULL ){
  334. memset( retVal, 0, sizeof(OBSERVER_RESULT));
  335. if(!ProbeMatch.isEmpty()) {
  336. LOG("Have ProbeMatch elm");
  337. {
  338. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  339. if(!node.isEmpty()) {
  340. LOG("Have ProbeMatch EndpointReference");
  341. XMLNode nod = node.getChildNode("Address", 0);
  342. if(!nod.isEmpty()){
  343. char tBuf[CLOG];
  344. sprintf(tBuf,"%s", nod.getText(0));
  345. CArray *tArr;
  346. LOG("Before Explode");
  347. tArr = explode(':',tBuf);
  348. LOG("After Explode");
  349. for(int j=0; j<tArr->size(); j++) {
  350. if( strlen(tArr->at(j)) > 10 ) {
  351. sprintf(retVal->RefID,"%s",tArr->at(j));
  352. break;
  353. };
  354. };
  355. tArr->clear();
  356. delete(tArr);
  357. };
  358. };
  359. }
  360. {
  361. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  362. if( !node.isEmpty() ){
  363. char tBuf[CLOG];
  364. sprintf(tBuf,"%s", node.getText(0));
  365. CArray *tArr;
  366. tArr = explode(' ',tBuf);
  367. sprintf(retVal->Type,"%s",tArr->at(0));
  368. tArr->clear();
  369. delete(tArr);
  370. }
  371. }
  372. {
  373. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  374. char tBuf[CLOG];
  375. sprintf( tBuf,"%s", node.getText(0) );
  376. CArray *tArr = explode(' ',tBuf);
  377. if( tArr->size() > 0 ) {
  378. for( int index = 0; index < tArr->size(); index++){
  379. char *tPtr;
  380. if( tPtr = strstr( tArr->at(index), "mac/" ) ){
  381. sprintf(retVal->MAC,"%s",tPtr+4);
  382. };
  383. if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
  384. sprintf(retVal->Hardware,"%s",tPtr+9);
  385. };
  386. if( tPtr = strstr( tArr->at(index), "name/" ) ){
  387. sprintf(retVal->Name,"%s",tPtr+5);
  388. };
  389. if (tPtr = strstr(tArr->at(index), "MAC/")) {
  390. sprintf(retVal->MAC, "%s", tPtr + 4);
  391. };
  392. if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
  393. sprintf(retVal->Hardware, "%s", tPtr + 9);
  394. };
  395. if (tPtr = strstr(tArr->at(index), "NAME/")) {
  396. sprintf(retVal->Name, "%s", tPtr + 5);
  397. };
  398. };
  399. tArr->clear();
  400. };
  401. delete(tArr);
  402. }
  403. {
  404. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  405. char tBuf[CLOG];
  406. sprintf(tBuf,"%s", node.getText(0));
  407. CArray *tArr;
  408. tArr = explode(' ',tBuf);
  409. if( tArr->size() > 0 ) {
  410. for(int index=0; index < tArr->size(); index++) {
  411. if( strstr( tArr->at(index), "[" ) != NULL ){
  412. // IPv6
  413. } else{
  414. // IPv4
  415. sprintf(retVal->XAddrs,"%s",tArr->at(0));
  416. };
  417. };
  418. tArr->clear();
  419. };
  420. delete(tArr);
  421. }
  422. };
  423. };
  424. };
  425. };
  426. };
  427. };
  428. };
  429. };
  430. return retVal;
  431. };
  432. bool argusModuleOnvifObserver::validate(){
  433. m_mResultProtector.lock();
  434. if( m_vResultPool.size() ){ // Clear structures
  435. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  436. #ifdef DEBUG
  437. char tBuffer[256];
  438. sprintf( tBuffer, "Found Camwera [%s] %s", (*index)->XAddrs, (*index)->RefID );
  439. LOG( tBuffer );
  440. #endif
  441. uint32_t cid = m_pDBC->getCamId( (*index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
  442. if( cid ) {
  443. #ifdef DEBUG
  444. sprintf( tBuffer, "Camera is Listed as %d", cid );
  445. LOG( tBuffer );
  446. #endif
  447. m_pDBC->updateIPCam( m_pKernel->getSiteId(), cid, (*index)->XAddrs );// DiscoverResults->at(index));
  448. } else {
  449. // Uncknown camera - no checking
  450. #ifdef DEBUG
  451. LOG("Camera is Not Listed");
  452. #endif
  453. m_pDBC->addIPCam( m_pKernel->getSiteId(), (*index) );
  454. };
  455. free(*index);
  456. }
  457. m_vResultPool.clear();
  458. #ifdef DEBUG_EXTRA
  459. LOG("Clear Structs Done!");
  460. #endif
  461. } else {
  462. #ifdef DEBUG_EXTRA
  463. LOG("No Online IP Cameras in Network");
  464. #endif
  465. };
  466. m_mResultProtector.unlock();
  467. return true;
  468. };
  469. #undef LOG
  470. #undef LOG_ERROR