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.

469 lines
17 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseModuleOnvifObserver.h"
  2. #include "rgooseKernel.h"
  3. #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>"
  4. #define SEARCH_REQUEST_STRING2323 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" \
  5. "<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" \
  6. "<SOAP:Header>\n" \
  7. "<wsa:MessageID>urn:uuid:ac9823ee-6813-498c-8c1b-6272a22353f7</wsa:MessageID>\n" \
  8. "<wsa:Action>\n" \
  9. "http ://schemas.xmlsoap.org/ws/2005/04/discovery/Probe\n" \
  10. "</wsa:Action>\n" \
  11. "<wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>\n" \
  12. "</SOAP:Header>\n" \
  13. "<SOAP:Body>\n" \
  14. "<DISCOVERY:Probe>\n" \
  15. "<DISCOVERY:Types>ssdp:any</DISCOVERY:Types>\n" \
  16. "</DISCOVERY:Probe>\n" \
  17. "</SOAP:Body>\n" \
  18. "</SOAP:Envelope>\r\n\r\n"
  19. #define HTTP_OK "200 OK"
  20. /******************************************************************
  21. ** Action Defines *
  22. *******************************************************************/
  23. #define HTTP_HEADER_ACTION "POST %s HTTP/1.1\r\n" \
  24. "HOST: %s:%u\r\n" \
  25. "SOAPACTION:\"%s#%s\"\r\n" \
  26. "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n"\
  27. "Content-Length: %d \r\n\r\n"
  28. #define SOAP_ACTION "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
  29. "<s:Envelope xmlns:s=" \
  30. "\"http://schemas.xmlsoap.org/soap/envelope/\" " \
  31. "s:encodingStyle=" \
  32. "\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
  33. "<s:Body>\r\n" \
  34. "<u:%s xmlns:u=\"%s\">\r\n%s" \
  35. "</u:%s>\r\n" \
  36. "</s:Body>\r\n" \
  37. "</s:Envelope>\r\n"
  38. #define PORT_MAPPING_LEASE_TIME "63072000" //two year
  39. #define ADD_PORT_MAPPING_PARAMS "<NewRemoteHost></NewRemoteHost>\r\n" \
  40. "<NewExternalPort>%u</NewExternalPort>\r\n"\
  41. "<NewProtocol>%s</NewProtocol>\r\n" \
  42. "<NewInternalPort>%u</NewInternalPort>\r\n"\
  43. "<NewInternalClient>%s</NewInternalClient>\r\n" \
  44. "<NewEnabled>1</NewEnabled>\r\n" \
  45. "<NewPortMappingDescription>%s</NewPortMappingDescription>\r\n" \
  46. "<NewLeaseDuration>" \
  47. PORT_MAPPING_LEASE_TIME \
  48. "</NewLeaseDuration>\r\n"
  49. #define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, X )
  50. #ifdef DEBUG
  51. #define LOG_ERROR( X ) {\
  52. char _tBfrErrMSG2w34[256];\
  53. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  54. argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  55. }
  56. #else
  57. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, X );
  58. #endif
  59. #define TLOG( X ) basicLogCollection::getInstance()->Log( "MOD_ONVIF_T", me, X )
  60. #define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "MOD_ONVIF_T", me, _LOG_ERROR, X )
  61. argusModuleOnvifObserver::argusModuleOnvifObserver( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_FLYHUNTER ), basicThread(){
  62. m_bEnabled = false;
  63. m_pEventHeap = basicEventHeap::getInstance();
  64. m_pIpAddresses = NULL;
  65. m_nStatus = 0;
  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. m_bEnabled = false;
  82. stopService();
  83. };
  84. bool argusModuleOnvifObserver::startService(){
  85. LOG("Starting Service!");
  86. m_bEnabled = true;
  87. //if( m_bEnabled ) return false;
  88. if( !m_bWSAOK ) return false;
  89. return init();
  90. };
  91. bool argusModuleOnvifObserver::stopService(){
  92. waitThreadEnd( true );
  93. if( m_vThreads.size() ) { // Kill-Wait Started Probes
  94. #ifdef DEBUG
  95. LOG("Have to Wait Probes!");
  96. #endif
  97. for( vectorOBSERVER_CTX::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++ ) {
  98. pthread_join( (*index)->m_hThreadHandle, NULL );
  99. };
  100. m_vThreads.clear();
  101. };
  102. #ifdef DEBUG
  103. LOG("No More Probes!");
  104. #endif
  105. if (m_vResultPool.size()){ // Clear structures
  106. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  107. free(*index);
  108. };
  109. m_vResultPool.clear();
  110. };
  111. #ifdef DEBUG
  112. LOG("Data is Cleared!");
  113. #endif
  114. return true;
  115. };
  116. void argusModuleOnvifObserver::m_fThreadProc(){
  117. double dStatus = 0, dStep = 30.0;
  118. int id = 0;
  119. dStatus = 0;
  120. m_bThreadStatus = TH_WORKING;
  121. if( !m_bEnabled ) {
  122. m_bThreadStatus = TH_ENDED;
  123. #ifdef DEBUG
  124. LOG("Module is Disabled!");
  125. #endif
  126. };
  127. #ifdef DEBUG
  128. LOG("Module Started!");
  129. #endif
  130. while( m_bThreadStatus < TH_ENDED ) {
  131. if( m_pIpAddresses != NULL ){
  132. LOG("Check IPS");
  133. if( m_pIpAddresses->size() > 0 ){
  134. LOG("Have IPS");
  135. //Start Reaper thread
  136. dStep = 30.0 / (m_pIpAddresses->size() + 1);
  137. // Start Discoverers on each broadcast IP
  138. id = 0;
  139. for( localIpAddresses::iterator index = m_pIpAddresses->begin(); index < m_pIpAddresses->end(); index++ ){
  140. if (strlen((*index)->broadcast)){
  141. LOG("Have BC Address");
  142. startProbe((*index)->broadcast, id, (int) dStep );
  143. LOG("Probe Started");
  144. };
  145. dStatus += dStep;
  146. m_nStatus = (int)dStatus;
  147. id++;
  148. };
  149. // default probe was here
  150. } else {
  151. LOG_ERROR("No Networks Detected!");
  152. #ifdef DEBUG
  153. LOG("Start Default Probe");
  154. #endif
  155. startProbe(HTTPMU_HOST_ADDRESS, id, (int)dStep);
  156. dStatus += dStep;
  157. m_nStatus = (int)dStatus;
  158. };
  159. } else {
  160. LOG_ERROR("ERROR FETCHING IP ADDRESSES!" );
  161. #ifdef DEBUG
  162. LOG("Start Default Probe");
  163. #endif
  164. startProbe(HTTPMU_HOST_ADDRESS, id, (int)dStep);
  165. dStatus += dStep;
  166. m_nStatus = (int)dStatus;
  167. };
  168. SLEEP( 30000 );
  169. };
  170. };
  171. void argusModuleOnvifObserver::dispatchEvent(EVENT_CONTAINER *Evt){
  172. if(!m_bEnabled) {
  173. LOG("Module Disabled!");
  174. return;
  175. }
  176. if( Evt == NULL ) return;
  177. };
  178. bool argusModuleOnvifObserver::reloadSettings(){
  179. return true;
  180. };
  181. PROBE_CTX *argusModuleOnvifObserver::init_PROBE_CTX(CSTR ADR, int ID, int stp){
  182. PROBE_CTX *RetVal;
  183. RetVal = (PROBE_CTX*)malloc(sizeof(PROBE_CTX));
  184. if (RetVal == NULL) return NULL;
  185. sprintf(RetVal->IpAddress, "%s", ADR);
  186. RetVal->Port = HTTPMU_HOST_PORT;
  187. RetVal->socketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  188. RetVal->send_buff = (CSTR) SEARCH_REQUEST_STRING;
  189. RetVal->r_address.sin_family = AF_INET;
  190. RetVal->r_address.sin_port = htons(RetVal->Port);
  191. RetVal->r_address.sin_addr.s_addr = inet_addr(RetVal->IpAddress);
  192. RetVal->bOptVal = true;
  193. RetVal->bOptLen = sizeof(bool);
  194. RetVal->iOptLen = sizeof(int);
  195. RetVal->AddressID = ID;
  196. RetVal->step = stp;
  197. RetVal->observer = this;
  198. return RetVal;
  199. };
  200. void *argusModuleOnvifObserver::probeThreadProc( void *ptr ){
  201. PROBE_CTX *me;
  202. if (ptr == NULL) return NULL;
  203. me = (PROBE_CTX *)ptr;
  204. #ifdef DEBUG
  205. //TLOG("Probe Request Sent");
  206. #endif
  207. me->RetVal = setsockopt(me->socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&me->bOptVal, me->bOptLen);
  208. me->RetVal = sendto(me->socketFD, me->send_buff, strlen(me->send_buff), 0, (struct sockaddr*)&me->r_address, sizeof(struct sockaddr_in));
  209. #ifdef DEBUG
  210. //sprintf(me->buff, "Sent %d Bytes", me->RetVal );
  211. //TLOG(me->buff);
  212. #endif
  213. #ifdef __linux__
  214. fcntl( me->socketFD, F_SETFL, fcntl(me->socketFD, F_GETFL, 0) | O_NONBLOCK );
  215. #else
  216. u_long val = 1;
  217. ioctlsocket(me->socketFD, FIONBIO, &val);//none block
  218. #endif
  219. for (me->index = 1; me->index <= 10; me->index++){
  220. memset(me->buff, 0, sizeof(me->buff));
  221. me->RetVal = recvfrom(me->socketFD, me->buff, MAX_BUFF_SIZE, 0, NULL, NULL);
  222. if( me->RetVal < 0 ){//SOCKET_ERROR){
  223. SLEEP(1000);
  224. continue;
  225. };
  226. me->recv_buff = me->buff;
  227. XMLResults tParseResults;
  228. XMLNode response = XMLNode::parseString( me->buff, "root", &tParseResults );
  229. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  230. FILE *fd;
  231. fd = fopen("ONVIF_MSGS.txt","a+");
  232. fprintf(fd,me->buff);
  233. fprintf(fd,"--------------------\r\n");
  234. fclose(fd);
  235. #endif
  236. if( tParseResults.error ){
  237. char tBuffer[CTEXT];
  238. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  239. TLOG_ERROR( tBuffer );
  240. } else {
  241. if(response.isEmpty()){
  242. TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  243. continue;
  244. } else {
  245. XMLNode Body = response.getChildNode("Body", 0);
  246. if(!Body.isEmpty()){
  247. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  248. if(!ProbeMatches.isEmpty()) {
  249. int matches = ProbeMatches.nChildNode();
  250. OBSERVER_RESULT *tRecord;
  251. for(int index=0;index<matches;index++){
  252. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  253. tRecord = (OBSERVER_RESULT *)malloc(sizeof(OBSERVER_RESULT));
  254. if (tRecord != NULL){
  255. memset(tRecord, 0, sizeof(OBSERVER_RESULT));
  256. tRecord->resultID = me->AddressID;
  257. if(!ProbeMatch.isEmpty()) {
  258. {
  259. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  260. if(!node.isEmpty()) {
  261. XMLNode nod = node.getChildNode("Address", 0);
  262. char tBuf[CLOG];
  263. sprintf(tBuf,"%s", nod.getText(0));
  264. CArray *tArr;
  265. tArr = explode(':',tBuf);
  266. for(int j=0; j<tArr->size(); j++) {
  267. if( strlen(tArr->at(j)) > 10 ) {
  268. sprintf(tRecord->RefID,"%s",tArr->at(j));
  269. break;
  270. };
  271. };
  272. tArr->clear();
  273. delete(tArr);
  274. };
  275. }
  276. {
  277. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  278. char tBuf[CLOG];
  279. sprintf(tBuf,"%s", node.getText(0));
  280. CArray *tArr;
  281. tArr = explode(' ',tBuf);
  282. sprintf(tRecord->Type,"%s",tArr->at(0));
  283. tArr->clear();
  284. delete(tArr);
  285. }
  286. {
  287. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  288. char tBuf[CLOG];
  289. sprintf(tBuf,"%s", node.getText(0));
  290. CArray *tArr;
  291. tArr = explode(' ',tBuf);
  292. if( tArr->size() > 0 ) {
  293. for( int index = 0; index < tArr->size(); index++){
  294. char *tPtr;
  295. if( tPtr = strstr( tArr->at(index), "mac/" ) ){
  296. sprintf(tRecord->MAC,"%s",tPtr+4);
  297. };
  298. if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
  299. sprintf(tRecord->Hardware,"%s",tPtr+9);
  300. };
  301. if( tPtr = strstr( tArr->at(index), "name/" ) ){
  302. sprintf(tRecord->Name,"%s",tPtr+5);
  303. };
  304. if (tPtr = strstr(tArr->at(index), "MAC/")) {
  305. sprintf(tRecord->MAC, "%s", tPtr + 4);
  306. };
  307. if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
  308. sprintf(tRecord->Hardware, "%s", tPtr + 9);
  309. };
  310. if (tPtr = strstr(tArr->at(index), "NAME/")) {
  311. sprintf(tRecord->Name, "%s", tPtr + 5);
  312. };
  313. };
  314. tArr->clear();
  315. };
  316. delete(tArr);
  317. }
  318. {
  319. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  320. char tBuf[CLOG];
  321. sprintf(tBuf,"%s", node.getText(0));
  322. CArray *tArr;
  323. tArr = explode(' ',tBuf);
  324. if( tArr->size() > 0 ) {
  325. for(int index=0; index < tArr->size(); index++) {
  326. if( strstr( tArr->at(index), "[" ) != NULL ){
  327. // IPv6
  328. } else{
  329. // IPv4
  330. sprintf(tRecord->XAddrs,"%s",tArr->at(0));
  331. };
  332. };
  333. tArr->clear();
  334. };
  335. delete(tArr);
  336. }
  337. };
  338. me->observer->m_vResultPool.push_back(tRecord);
  339. };
  340. };
  341. };
  342. };
  343. };
  344. };
  345. };
  346. me->observer->m_nStatus += me->step;
  347. #ifdef DEBUG
  348. TLOG("Probe Finished");
  349. #endif
  350. #ifdef __linux__
  351. close(me->socketFD);
  352. #else
  353. closesocket(me->socketFD);
  354. #endif
  355. free( me );
  356. return NULL;
  357. };
  358. bool argusModuleOnvifObserver::startProbe(CSTR IP, int ID, int stp){
  359. #ifdef DEBUG
  360. LOG("Starting Probe!");
  361. #endif
  362. PROBE_CTX *tVal = init_PROBE_CTX(IP, ID, stp);
  363. if( tVal == NULL ) return false;
  364. OBSERVER_CTX *tThread;
  365. tThread = (OBSERVER_CTX *) malloc(sizeof(OBSERVER_CTX));
  366. if( tThread != NULL ){
  367. memset( tThread, 0, sizeof(OBSERVER_CTX));
  368. #ifdef __linux__
  369. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  370. if( RetVal == -1 ) {
  371. LOG_ERROR("Thread Creation Faled.");
  372. free(tVal);
  373. free(tThread);
  374. return false;
  375. };
  376. #else
  377. tThread->m_hThreadHandle = CreateThread(
  378. NULL,
  379. 0,
  380. (LPTHREAD_START_ROUTINE)probeThreadProc,
  381. (LPVOID)tVal,
  382. 0,
  383. &tThread->m_dwThreadID);
  384. if( tThread->m_hThreadHandle==NULL ){
  385. LOG_ERROR("Thread Creation Faled.");
  386. free(tVal);
  387. free(tThread);
  388. return false;
  389. };
  390. #endif
  391. tThread->context = tVal;
  392. m_vThreads.push_back(tThread);
  393. #ifdef DEBUG
  394. LOG("Probe Created");
  395. #endif
  396. return true;
  397. } else {
  398. LOG_ERROR("Can not allocate Memory for Probe CTX");
  399. free(tVal);
  400. };
  401. return false;
  402. };
  403. bool argusModuleOnvifObserver::validate(){
  404. if( m_vResultPool.size() ){ // Clear structures
  405. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  406. #ifdef DEBUG
  407. char tBuffer[256];
  408. sprintf( tBuffer, "Found Camwera [%s] %s", (*index)->XAddrs, (*index)->RefID );
  409. LOG( tBuffer );
  410. #endif
  411. uint32_t cid = m_pDBC->getCamId( (*index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
  412. if( cid ) {
  413. #ifdef DEBUG
  414. sprintf( tBuffer, "Camera is Listed as %d", cid );
  415. LOG( tBuffer );
  416. #endif
  417. m_pDBC->updateIPCam( m_pKernel->getSiteId(), cid, (*index)->XAddrs );// DiscoverResults->at(index));
  418. } else {
  419. // Uncknown camera - no checking
  420. #ifdef DEBUG
  421. LOG("Camera is Not Listed");
  422. #endif
  423. m_pDBC->addIPCam( m_pKernel->getSiteId(), (*index) );
  424. };
  425. free(*index);
  426. }
  427. m_vResultPool.clear();
  428. #ifdef DEBUG_EXTRA
  429. LOG("Clear Structs Done!");
  430. #endif
  431. } else {
  432. #ifdef DEBUG_EXTRA
  433. LOG("No Online IP Cameras in Network");
  434. #endif
  435. };
  436. return true;
  437. };
  438. #undef LOG
  439. #undef LOG_ERROR
  440. #undef TLOG
  441. #undef TLOG_ERROR