filsif

network

May 23rd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.26 KB | None | 0 0
  1.  
  2. //=== platform specific
  3. #include <common/pgas_platform.h>
  4.  
  5. #if defined ( _PLATFORM_LINUX_)
  6. #include <netinet/ether.h>
  7. //#include <sys/ioctl.h>
  8. #include <net/if.h>
  9. #elif defined ( _PLATFORM_WINDOWS_)
  10. //#include <winsock2.h>
  11. #include <iphlpapi.h>
  12. #elif defined ( _PLATFORM_IOS_)
  13. //#include <sys/types.h>
  14. //#include <sys/socket.h>
  15. #include <ifaddrs.h>
  16. //#include <sys/ioctl.h>
  17. #include <net/if.h>
  18.  
  19. #endif
  20.  
  21. //=== libpgas
  22. #include "protocol_base.h"
  23.  
  24. //=== libprotocol
  25. #include "sys/SYS_network_utils.h"
  26.  
  27.  
  28. //=============================================================================
  29. // local trace defines
  30. //=============================================================================
  31.  
  32. #undef _FORCE_TRACE_DEBUG_
  33. #undef _ENABLE_TRACE_DEBUG_
  34. #undef _DISABLE_TRACE_WARNINGS_
  35. #undef _BREAK_ON_ERROR_
  36. #undef _BACKTRACE_ON_ERROR_
  37.  
  38. //=
  39. //#define _FORCE_TRACE_DEBUG_
  40. #define _ENABLE_TRACE_DEBUG_
  41. //#define _DISABLE_TRACE_WARNINGS_
  42. //#define _BREAK_ON_ERROR_
  43. //#define _BACKTRACE_ON_ERROR_
  44. #include <tools/pgas_trace.h>
  45. //=
  46.  
  47. #undef TLS_LOG_MYMODULE
  48. #define TLS_LOG_MYMODULE TLS_LOG_MODULE_PROTOCOL
  49.  
  50. //=============================================================================
  51.  
  52.  
  53.  
  54.  
  55. namespace pga
  56. {
  57.  
  58. namespace protocol
  59. {
  60.  
  61.  
  62.  
  63. INT32 GetNbNetworkInterface( )
  64. {
  65. INT32 ret = PRT_FAILED;
  66.  
  67. #if defined (_PLATFORM_LINUX_)
  68. struct ifconf ifc;
  69. struct ifreq ifr[ 32 ];
  70. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  71.  
  72. if (sock >= 0)
  73. {
  74. // demande des interfaces disponibles
  75. ifc.ifc_buf = (char *)ifr;
  76. ifc.ifc_len = sizeof(ifr);
  77. if (ioctl(sock, SIOCGIFCONF, &ifc) >= 0)
  78. {
  79. ret = ifc.ifc_len / sizeof(struct ifreq);
  80. }
  81. else
  82. {
  83. PGAS_ERROR(("<GetNbNetworkInterface> ioctl(SIOCGIFCONF)" ));
  84. }
  85. close(sock);
  86. }
  87. else
  88. {
  89. PGAS_ERROR(("<GetNbNetworkInterface> socket() " ));
  90. }
  91.  
  92. #elif defined (_PLATFORM_WINDOWS_)
  93. //ECP 1ere version, mais risque car GetNumberOfInterfaces ne compte pas le loopback
  94. //DWORD dwNombre;
  95. //if (GetNumberOfInterfaces(&dwNombre) != NO_ERROR)
  96. // ret = dwNombre;
  97.  
  98. //ECP 2eme version, sans risque car elle utilise la meme API que GetConfigurationInterface()
  99. PIP_ADAPTER_INFO adapter_info_p , adapter_p = NULL;
  100.  
  101. adapter_info_p = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
  102.  
  103. if ( adapter_info_p != NULL)
  104. {
  105. ULONG outbuf_len = sizeof(IP_ADAPTER_INFO);
  106. if ( GetAdaptersInfo( adapter_info_p , &outbuf_len ) == ERROR_BUFFER_OVERFLOW ) // buffer trop petit
  107. {
  108. free( adapter_info_p );
  109. adapter_info_p = (IP_ADAPTER_INFO *)malloc( outbuf_len );
  110. if ( adapter_info_p == NULL )
  111. {
  112. PGAS_ERROR(("< GetNbNetworkInterface > : memory allocation failed !"));
  113. goto fin;
  114. }
  115. }
  116.  
  117. DWORD retval = 0;
  118. if ( ( retval = GetAdaptersInfo( adapter_info_p , &outbuf_len ) ) == NO_ERROR )
  119. {
  120. adapter_p = adapter_info_p;
  121. ret = 0;
  122. do {ret++;}
  123. while (( adapter_p = adapter_p->Next));
  124. }
  125. else
  126. {
  127. PGAS_ERROR(( "< GetNbNetworkInterface > : GetAdaptersInfo() returned error %d !" , retval ));
  128. }
  129. }
  130. else
  131. {
  132. PGAS_ERROR(( "< GetNbNetworkInterface > : memory allocation failed !" ));
  133. }
  134.  
  135. fin:
  136. free( adapter_info_p );
  137.  
  138. #elif defined ( _PLATFORM_IOS_ )
  139.  
  140.  
  141. struct ifaddrs * ifap , * myifap = NULL;
  142.  
  143. int value = getifaddrs( &ifap);
  144.  
  145. if ( value == 0 )
  146. {
  147. myifap = ifap;
  148.  
  149. ret = 0;
  150.  
  151. while ( myifap )
  152. {
  153. if ( myifap->ifa_addr->sa_family == AF_INET ) ret++;
  154. myifap = myifap->ifa_next;
  155. }
  156.  
  157. freeifaddrs( ifap );
  158. }
  159. else
  160. {
  161. PGAS_ERROR(("Erreur getifaddrs %d" , errno));
  162. }
  163.  
  164.  
  165.  
  166. #endif // defined (_PLATFORM_LINUX_) / defined (_PLATFORM_WINDOWS_)
  167.  
  168.  
  169.  
  170. return ret;
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. //=================================================================================
  179. //=================================================================================
  180. //=================================================================================
  181. // GetConfigurationInterface
  182. // permet de recuperer les parametres d'une interface donnee
  183.  
  184. PRT_RESULT
  185. //GetConfigurationNetworkInterface( int _interface_index , char * _name_sz , char * _ip_sz , char * _mask_sz , char * _broadcast_sz , char * _mac_sz , size_t _size_l )
  186. GetConfigurationNetworkInterface( INT32 _interface_index, CString& _out_name_str, CString& _out_ip_str, CString& _out_mask_str, CString& _out_broadcast_str, CString& _out_mac_str )
  187. {
  188. PRT_RESULT ret = PRT_FAILED;
  189.  
  190. _out_name_str.Empty();
  191. _out_ip_str.Empty();
  192. _out_mask_str.Empty();
  193. _out_broadcast_str.Empty();
  194.  
  195. #if defined (_PLATFORM_LINUX_) //|| defined(_PLATFORM_IOS_)
  196. struct ifconf ifc;
  197. struct ifreq ifr[ 32 ];
  198. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  199.  
  200. if (sock >= 0)
  201. {
  202. // demande des interfaces disponibles
  203. ifc.ifc_buf = (char *)ifr;
  204. ifc.ifc_len = sizeof(ifr);
  205. if (ioctl(sock, SIOCGIFCONF, &ifc) >= 0)
  206. {
  207. /*
  208. if ( _name_sz )
  209. {
  210. pgas_strncpy( _name_sz , ifr[ _interface_index ].ifr_name, _size_l );
  211. }
  212. */
  213. _out_name_str = ifr[ _interface_index ].ifr_name;
  214. /*
  215. if ( _ip_sz )
  216. {
  217. pgas_strncpy( _ip_sz , inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_addr)->sin_addr),_size_l );
  218. }
  219. */
  220. _out_ip_str = inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_addr)->sin_addr);
  221. /*
  222. if ( _mask_sz )
  223. {
  224. if ( ioctl( sock , SIOCGIFNETMASK , &ifr[ _interface_index ] ) >= 0 )
  225. {
  226. pgas_strncpy( _mask_sz , inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_addr)->sin_addr),_size_l );
  227. }
  228. else
  229. {
  230. PGAS_WARNING(("<GetConfigurationNetworkInterface> ioctl(SIOCGIFNETMASK): "));
  231. }
  232. }
  233. */
  234. if ( ioctl( sock , SIOCGIFNETMASK , &ifr[ _interface_index ] ) >= 0 )
  235. {
  236. _out_mask_str = inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_addr)->sin_addr);
  237. }
  238. else
  239. {
  240. PGAS_WARNING(("< GetConfigurationNetworkInterface > ioctl(SIOCGIFNETMASK) failed !"));
  241. }
  242. /*
  243. if ( _broadcast_sz )
  244. {
  245. if ( ioctl( sock , SIOCGIFBRDADDR , &ifr[ _interface_index ] ) >= 0 )
  246. {
  247. pgas_strncpy( _broadcast_sz , inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_broadaddr)->sin_addr), _size_l );
  248. }
  249. else
  250. {
  251. PGAS_WARNING(("<GetConfigurationNetworkInterface> ioctl(SIOCGIFBRDADDR): "));
  252. }
  253. }
  254. */
  255. if ( ioctl( sock , SIOCGIFBRDADDR , &ifr[ _interface_index ] ) >= 0 )
  256. {
  257. _out_broadcast_str = inet_ntoa(((struct sockaddr_in *)&ifr[ _interface_index ].ifr_broadaddr)->sin_addr);
  258. }
  259. else
  260. {
  261. PGAS_WARNING(("< GetConfigurationNetworkInterface > ioctl(SIOCGIFBRDADDR) failed !"));
  262. }
  263. #if 0
  264. if ( _mac_sz )
  265. {
  266. #if !defined (_PLATFORM_FREEBSD_) && !defined (_PLATFORM_ANDROID_)
  267. /*
  268. if ( ioctl( sock , SIOCGIFHWADDR , &ifr[ _interface_index ] ) >= 0 )
  269. {
  270. //pgas_strncpy( _mac_sz , ether_ntoa((struct ether_addr *)&ifr[ _interface_index ].ifr_hwaddr.sa_data ) , _size_l );
  271. _out_mac_str
  272. }
  273. else
  274. {
  275. PGAS_WARNING(("<GetConfigurationNetworkInterface> ioctl(SIOCGIFHWADDR): "));
  276. }
  277. */
  278. #else
  279. pgas_snprintf( _mac_sz , sizeof( _mac_sz ) , "" );
  280. #endif
  281. }
  282. #endif
  283.  
  284. #if !defined (_PLATFORM_FREEBSD_) && !defined (_PLATFORM_ANDROID_)
  285. /*
  286. if ( ioctl( sock , SIOCGIFHWADDR , &ifr[ _interface_index ] ) >= 0 )
  287. {
  288. _out_mac_str = ether_ntoa((struct ether_addr *)&ifr[ _interface_index ].ifr_hwaddr.sa_data );
  289. }
  290. else
  291. {
  292. PGAS_WARNING(("< GetConfigurationNetworkInterface > ioctl(SIOCGIFHWADDR) failed !"));
  293. }
  294. */
  295. #endif
  296.  
  297.  
  298. ret = PRT_SUCCESS;
  299. }
  300. else
  301. {
  302. PGAS_ERROR(("<GetConfigurationNetworkInterface> ioctl(SIOCGIFCONF)" ));
  303. }
  304.  
  305. close(sock);
  306. }
  307. else
  308. {
  309. PGAS_ERROR(("<GetConfigurationNetworkInterface> socket()" ));
  310. }
  311.  
  312. #elif defined (_PLATFORM_WINDOWS_)
  313.  
  314. PIP_ADAPTER_INFO adapter_info_p , adapter_p = NULL;
  315.  
  316. adapter_info_p = (IP_ADAPTER_INFO *)malloc( sizeof(IP_ADAPTER_INFO) );
  317. if ( adapter_info_p != NULL )
  318. {
  319. ULONG outbuf_len = sizeof(IP_ADAPTER_INFO);
  320. if ( GetAdaptersInfo( adapter_info_p , &outbuf_len ) == ERROR_BUFFER_OVERFLOW ) // buffer trop petit
  321. {
  322. free( adapter_info_p );
  323. adapter_info_p = (IP_ADAPTER_INFO *)malloc( outbuf_len+1 );
  324. if (adapter_info_p == NULL)
  325. {
  326. PGAS_ERROR(("< CPRTPapClient::GetConfigurationInterface() > : memory allocation failed !"));
  327. goto FINALIZE;
  328. }
  329. }
  330.  
  331. DWORD retval = 0;
  332. if ( ( retval = GetAdaptersInfo(adapter_info_p, &outbuf_len ) ) == NO_ERROR)
  333. {
  334. adapter_p = adapter_info_p;
  335. INT32 courant_l = 0;
  336. /*
  337. while ( courant_l != _interface_index ) // on n'est pas sur l'interface demandee
  338. if (( adapter_p = adapter_p->Next)) // la fin de liste chainee n'est pas encore atteinte
  339. courant_l++; // interface suivante
  340. else break; // la liste chainee est finie
  341. */
  342.  
  343. while ( (courant_l != _interface_index) && ((adapter_p = adapter_p->Next) != NULL) ) ++courant_l; // interface suivante
  344.  
  345. if ( courant_l == _interface_index ) // on a trouve l'interface
  346. {
  347. /*
  348. if ( _name_sz )
  349. {
  350. pgas_strncpy( _name_sz , adapter_p->Description, _size_l );
  351. }
  352. */
  353. _out_name_str = adapter_p->Description;
  354. /*
  355. if ( _ip_sz )
  356. {
  357. pgas_strncpy( _ip_sz , adapter_p->IpAddressList.IpAddress.String, _size_l );
  358. }
  359. */
  360. _out_ip_str = adapter_p->IpAddressList.IpAddress.String;
  361. /*
  362. if ( _mask_sz )
  363. {
  364. pgas_strncpy( _mask_sz , adapter_p->IpAddressList.IpMask.String, _size_l );
  365. }
  366. */
  367. _out_mask_str = adapter_p->IpAddressList.IpMask.String;
  368.  
  369. /*
  370. if ( _broadcast_sz )
  371. { // GetAdaptersInfo() ne permet pas d'avoir l'adresse de broadcast, alors on la calcule
  372. UINT32 ulMasque = inet_addr( _mask_sz ),
  373.  
  374. ulReseau = inet_addr( _ip_sz),
  375. ulBroadcast = ulReseau | (~ulMasque);
  376. in_addr iaBroadcast;
  377. iaBroadcast.s_addr = ulBroadcast;
  378. pgas_strncpy( _broadcast_sz , inet_ntoa(iaBroadcast) , _size_l );
  379. }
  380. */
  381. UINT32 ulMasque = inet_addr( _out_mask_str.GetChars() );
  382. UINT32 ulReseau = inet_addr( _out_ip_str.GetChars() );
  383. UINT32 ulBroadcast = ulReseau | (~ulMasque);
  384.  
  385. in_addr iaBroadcast;
  386. iaBroadcast.s_addr = ulBroadcast;
  387.  
  388. _out_broadcast_str = inet_ntoa( iaBroadcast );
  389.  
  390. /*
  391. if ( _mac_sz )
  392. {
  393. CHAR* mymac_sz = _mac_sz;
  394. for ( UINT i = 0; ( i < adapter_p->AddressLength) && ( mymac_sz < &_mac_sz[ _size_l ] ) ; i++ )
  395. {
  396. if (i == ( adapter_p->AddressLength - 1))
  397. {
  398. mymac_sz += pgas_snprintf(mymac_sz , _size_l - ( mymac_sz - _mac_sz ) , "%02X", (int)adapter_p->Address[i]);
  399. }
  400. else
  401. {
  402. mymac_sz += pgas_snprintf(mymac_sz , _size_l - ( mymac_sz - _mac_sz ) , "%02X:", (int)adapter_p->Address[i]);
  403. }
  404. }
  405.  
  406. PGAS_DEBUG( ("< GetConfigurationNetworkInterface > [ %d ] => MAC = '%s'", _interface_index, _mac_sz) );
  407. }
  408. */
  409. CString nibble;
  410. for ( UINT i = 0; i < adapter_p->AddressLength ; i++ )
  411. {
  412. nibble.Format( "%02X", adapter_p->Address[i] );
  413. _out_mac_str += nibble;
  414.  
  415. if ( i < ( adapter_p->AddressLength-1) )
  416. {
  417. _out_mac_str += ':';
  418. }
  419. }
  420.  
  421. ret = PRT_SUCCESS;
  422. }
  423. else
  424. {
  425. PGAS_WARNING(( "<GetConfigurationNetworkInterface> : couldn't find configuration for interface index %d", _interface_index ));
  426. }
  427. }
  428. else
  429. {
  430. PGAS_ERROR(( "<GetConfigurationNetworkInterface> GetAdaptersInfo() returned error %d !" , retval ));
  431. }
  432. }
  433. else
  434. {
  435. PGAS_ERROR(( "< GetConfigurationNetworkInterface > : memory allocation failed !" ));
  436. }
  437.  
  438. FINALIZE:
  439. free( adapter_info_p );
  440.  
  441. #elif defined (_PLATFORM_IOS_)
  442. struct ifaddrs * ifap , * myifap = NULL;
  443.  
  444. int value = getifaddrs( &ifap);
  445.  
  446. if ( value == 0 )
  447. {
  448. myifap = ifap;
  449.  
  450. int cpt = 0;
  451. int ok = false;
  452. while ( myifap )
  453. {
  454. if ( myifap->ifa_addr->sa_family == AF_INET )
  455. {
  456. if ( cpt == _interface_index)
  457. {
  458. ok = true;
  459. break;
  460. }
  461. else
  462. {
  463. cpt++;
  464. }
  465. }
  466. myifap = myifap->ifa_next;
  467. }
  468.  
  469. if ( ok )
  470. {
  471. /*
  472. if ( _name_sz )
  473. {
  474. pgas_strncpy( _name_sz , myifap->ifa_name, _size_l );
  475. }
  476. */
  477. _out_name_str = myifap->ifa_name;
  478. /*
  479. if ( _ip_sz )
  480. {
  481. pgas_strncpy( _ip_sz , inet_ntoa(((struct sockaddr_in *)myifap->ifa_addr)->sin_addr),_size_l );
  482. }
  483. */
  484. _out_ip_str = inet_ntoa(((struct sockaddr_in *)myifap->ifa_addr)->sin_addr);
  485.  
  486. /*
  487. if ( _mask_sz )
  488. {
  489. pgas_strncpy( _mask_sz , inet_ntoa(((struct sockaddr_in *)myifap->ifa_netmask)->sin_addr),_size_l );
  490. }
  491. */
  492. _out_mask_str = inet_ntoa(((struct sockaddr_in *)myifap->ifa_netmask)->sin_addr);
  493.  
  494. /*
  495. if ( _broadcast_sz )
  496. { // GetAdaptersInfo() ne permet pas d'avoir l'adresse de broadcast, alors on la calcule
  497. ULONG ulMasque = inet_addr( _mask_sz ),
  498. ulReseau = inet_addr( _ip_sz),
  499. ulBroadcast = ulReseau | (~ulMasque);
  500. in_addr iaBroadcast;
  501. iaBroadcast.s_addr = ulBroadcast;
  502. pgas_strncpy( _broadcast_sz , inet_ntoa(iaBroadcast) , _size_l );
  503. }
  504. */
  505.  
  506. UINT32 ulMasque = inet_addr( _out_mask_str.GetChars() );
  507. UINT32 ulReseau = inet_addr( _out_ip_str.GetChars() );
  508. UINT32 ulBroadcast = ulReseau | (~ulMasque);
  509.  
  510. in_addr iaBroadcast;
  511. iaBroadcast.s_addr = ulBroadcast;
  512.  
  513. _out_broadcast_str = inet_ntoa( iaBroadcast );
  514.  
  515. ret = PRT_SUCCESS;
  516. }
  517.  
  518. freeifaddrs( ifap );
  519. }
  520. else
  521. {
  522. PGAS_ERROR(("< GetConfigurationNetworkInterface > : getifaddrs returned error %d !", errno));
  523. }
  524.  
  525. #endif
  526.  
  527.  
  528. return ret;
  529. }
  530.  
  531. /*
  532. * Returns address of interface matching with dest IP addr
  533. *
  534. * For example, I want to dialog with 172.17.4.5
  535. *
  536. * There is 2 interfaces :
  537. * - 10.4.30.1 / 255.0.0.0
  538. * - 172.17.0.141 / 255.255.0.0
  539. *
  540. * This function will choose the second interface because dest and itf are on the same sub-network.
  541. *
  542. *
  543. * Doesn't work if there is gateway on 10.4.30.1 and if we want to use 10.4.30.1
  544. *
  545. *
  546. */
  547.  
  548. PRT_RESULT GetMatchingInterface( const CHAR* _dest_ip_sz, CString &_out_itf_ip_str )
  549. {
  550. PRT_RESULT ret = PRT_FAILED;
  551.  
  552. _out_itf_ip_str.Empty();
  553.  
  554. //if ( _dest == NULL || _itf == NULL ) return PRT_FAILED;
  555. if ( _dest_ip_sz == NULL ) return PRT_FAILED; // DRS : replace by "255.255.255.255" ???
  556.  
  557. INT32 nb = GetNbNetworkInterface();
  558.  
  559. if ( nb != PRT_FAILED )
  560. {
  561. for ( INT32 i = 0 ; i < nb ; i++ )
  562. {
  563. /*
  564. CHAR name_sz[128]; name_sz[0] = '\0';
  565. CHAR ip_sz[128]; ip_sz[0] = '\0';
  566. CHAR mask_sz[128]; mask_sz[0] = '\0';
  567. CHAR broadcast_sz[128]; broadcast_sz[0] = '\0';
  568. CHAR mac_sz[128]; mac_sz[0] = '\0';
  569. */
  570. CString name_str;
  571. CString ip_str;
  572. CString mask_str;
  573. CString broadcast_str;
  574. CString mac_str;
  575.  
  576. //if ( PRT_SUCCESS == GetConfigurationNetworkInterface( i , name_sz , ip_sz , mask_sz , broadcast_sz , mac_sz , 128 ) )
  577. if ( PRT_SUCCESS == GetConfigurationNetworkInterface( i, name_str, ip_str, mask_str, broadcast_str, mac_str ) )
  578. {
  579. // recuperer mask
  580.  
  581. /*
  582. UINT32 mask = inet_addr( mask_sz );
  583. UINT32 dest = inet_addr( _dest );
  584. UINT32 ip = inet_addr( ip_sz );
  585. */
  586. UINT32 dest = inet_addr( _dest_ip_sz );
  587. UINT32 ip = inet_addr( ip_str.GetChars() );
  588. UINT32 mask = inet_addr( mask_str.GetChars() );
  589.  
  590. if ( ((dest & mask) == (ip & mask)) && ((ip != 0) || (mask != 0)) )
  591. {
  592. //pgas_sprintf( _itf , ip_sz );
  593. _out_itf_ip_str = ip_str;
  594.  
  595. return PRT_SUCCESS;
  596. }
  597. }
  598.  
  599. }
  600. }
  601.  
  602. return ret;
  603. }
  604.  
  605.  
  606. #if defined ( _PLATFORM_WINDOWS_ )
  607.  
  608. void PrintBufEntry ( WSAPROTOCOL_INFO *pProtocolBuf )
  609. {
  610. int j;
  611.  
  612. #if defined ( _PLATFORM_WINDESKTOP_ )
  613.  
  614. PGAS_TRACE((">> Protocol <%s>", pProtocolBuf->szProtocol));
  615.  
  616. #elif defined ( _PLATFORM_WINCE_ )
  617.  
  618. char message_sz[1024];
  619. int cw=lstrlenW( pProtocolBuf->szProtocol );
  620. WideCharToMultiByte(CP_ACP,0,pProtocolBuf->szProtocol ,cw,message_sz,1024,NULL,NULL);
  621.  
  622. PGAS_TRACE(("Protocol <%s>", message_sz));
  623.  
  624. #endif
  625.  
  626. /* // A guid is the same as a uuid.
  627. unsigned char *pszUuid;
  628. UuidToString(&pProtocolBuf->ProviderId, &pszUuid);
  629. PGAS_TRACE((" ProviderId {%s}", pszUuid));
  630. RpcStringFree(&pszUuid);
  631. */
  632. if (!pProtocolBuf->dwServiceFlags1)
  633. PGAS_TRACE((" dwServiceFlags1: 0"));
  634. else
  635. PGAS_TRACE((" dwServiceFlags1: 0x%08X",
  636. pProtocolBuf->dwServiceFlags1));
  637.  
  638. // Check which bit flags are set.
  639. if (pProtocolBuf->dwServiceFlags1 & XP1_CONNECTIONLESS)
  640. PGAS_TRACE((" XP1_CONNECTIONLESS"));
  641. if (pProtocolBuf->dwServiceFlags1 & XP1_GUARANTEED_DELIVERY)
  642. PGAS_TRACE((" XP1_GUARANTEED_DELIVERY"));
  643. if (pProtocolBuf->dwServiceFlags1 & XP1_GUARANTEED_ORDER)
  644. PGAS_TRACE((" XP1_GUARANTEED_ORDER"));
  645. if (pProtocolBuf->dwServiceFlags1 & XP1_MESSAGE_ORIENTED)
  646. PGAS_TRACE((" XP1_MESSAGE_ORIENTED"));
  647. if (pProtocolBuf->dwServiceFlags1 & XP1_PSEUDO_STREAM)
  648. PGAS_TRACE((" XP1_PSEUDO_STREAM"));
  649. if (pProtocolBuf->dwServiceFlags1 & XP1_GRACEFUL_CLOSE)
  650. PGAS_TRACE((" XP1_GRACEFUL_CLOSE"));
  651. if (pProtocolBuf->dwServiceFlags1 & XP1_EXPEDITED_DATA)
  652. PGAS_TRACE((" XP1_EXPEDITED_DATA"));
  653. if (pProtocolBuf->dwServiceFlags1 & XP1_CONNECT_DATA)
  654. PGAS_TRACE((" XP1_CONNECT_DATA"));
  655. if (pProtocolBuf->dwServiceFlags1 & XP1_DISCONNECT_DATA)
  656. PGAS_TRACE((" XP1_DISCONNECT_DATA"));
  657. if (pProtocolBuf->dwServiceFlags1 & XP1_SUPPORT_BROADCAST)
  658. PGAS_TRACE((" XP1_SUPPORT_BROADCAST"));
  659. if (pProtocolBuf->dwServiceFlags1 & XP1_SUPPORT_MULTIPOINT)
  660. PGAS_TRACE((" XP1_SUPPORT_MULTIPOINT"));
  661. if (pProtocolBuf->dwServiceFlags1 & XP1_MULTIPOINT_CONTROL_PLANE)
  662. PGAS_TRACE((" XP1_MULTIPOINT_CONTROL_PLANE"));
  663. if (pProtocolBuf->dwServiceFlags1 & XP1_MULTIPOINT_DATA_PLANE)
  664. PGAS_TRACE((" XP1_MULTIPOINT_DATA_PLANE"));
  665. if (pProtocolBuf->dwServiceFlags1 & XP1_QOS_SUPPORTED)
  666. PGAS_TRACE((" XP1_QOS_SUPPORTED"));
  667. if (pProtocolBuf->dwServiceFlags1 & XP1_INTERRUPT)
  668. PGAS_TRACE((" XP1_INTERRUPT"));
  669. if (pProtocolBuf->dwServiceFlags1 & XP1_UNI_SEND)
  670. PGAS_TRACE((" XP1_UNI_SEND"));
  671. if (pProtocolBuf->dwServiceFlags1 & XP1_UNI_RECV)
  672. PGAS_TRACE((" XP1_UNI_RECV"));
  673. if (pProtocolBuf->dwServiceFlags1 & XP1_IFS_HANDLES)
  674. PGAS_TRACE((" XP1_IFS_HANDLES"));
  675. if (pProtocolBuf->dwServiceFlags1 & XP1_PARTIAL_MESSAGE)
  676. PGAS_TRACE((" XP1_PARTIAL_MESSAGE"));
  677.  
  678. PGAS_TRACE((" dwServiceFlags2: reserved"));
  679.  
  680. PGAS_TRACE((" dwServiceFlags3: reserved"));
  681.  
  682. PGAS_TRACE((" dwServiceFlags4: reserved"));
  683.  
  684. PGAS_TRACE((" dwProviderFlags:"));
  685. if (pProtocolBuf->dwProviderFlags & PFL_MULTIPLE_PROTO_ENTRIES)
  686. PGAS_TRACE((" PFL_MULTIPLE_PROTO_ENTRIES"));
  687. if (pProtocolBuf->dwProviderFlags & PFL_RECOMMENDED_PROTO_ENTRY)
  688. PGAS_TRACE((" PFL_RECOMMENDED_PROTO_ENTRY"));
  689. if (pProtocolBuf->dwProviderFlags & PFL_HIDDEN)
  690. PGAS_TRACE((" PFL_HIDDEN"));
  691. if (pProtocolBuf->dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
  692. PGAS_TRACE((" PFL_MATCHES_PROTOCOL_ZERO"));
  693.  
  694. PGAS_TRACE((" dwCatalogEntryId = %u", pProtocolBuf->dwCatalogEntryId));
  695.  
  696. PGAS_TRACE((" ProtocolChain.ChainLen = %d ",
  697. pProtocolBuf->ProtocolChain.ChainLen));
  698. if (1 == pProtocolBuf->ProtocolChain.ChainLen)
  699. PGAS_TRACE((" ==> this is a base service provider"));
  700. else if (pProtocolBuf->ProtocolChain.ChainLen > 1)
  701. {
  702. PGAS_TRACE((" ==> ProtocolChain layered to base protocol"));
  703. for (j=0; j<pProtocolBuf->ProtocolChain.ChainLen; j++)
  704. PGAS_TRACE((" Chain Catalog Entry Id = %u",
  705. pProtocolBuf->ProtocolChain.ChainEntries[j]));
  706. }
  707. else if (0 == pProtocolBuf->ProtocolChain.ChainLen)
  708. PGAS_TRACE((" ==> this is a layered service provider"));
  709. else
  710. PGAS_TRACE((" Invalid"));
  711.  
  712. PGAS_TRACE((" iVersion = %d", pProtocolBuf->iVersion));
  713.  
  714. PGAS_TRACE((" iAddressFamily = %d", pProtocolBuf->iAddressFamily));
  715.  
  716. PGAS_TRACE((" iMaxSockAddr = %d", pProtocolBuf->iMaxSockAddr));
  717.  
  718. PGAS_TRACE((" iMinSockAddr = %d", pProtocolBuf->iMinSockAddr));
  719.  
  720. // iProtocols returns a negative number for Microsoft NetBIOS
  721. // service providers corresponding to the lana number * -1 (for
  722. // example, -2 implies lana 2), except for lana 0 which is equal to
  723. // 0x80000000 because protocol 0 is reserved for special use.
  724. PGAS_TRACE((" iProtocol = %d", pProtocolBuf->iProtocol));
  725.  
  726. PGAS_TRACE((" iProtocolMaxOffset = %d",
  727. pProtocolBuf->iProtocolMaxOffset));
  728.  
  729. PGAS_TRACE((" iNetworkByteOrder = %s",
  730. ((pProtocolBuf->iNetworkByteOrder == LITTLEENDIAN) ?
  731. "LITTLEENDIAN" : "BIGENDIAN")));
  732.  
  733. PGAS_TRACE((" iSecurityScheme = %d", pProtocolBuf->iSecurityScheme));
  734.  
  735. PGAS_TRACE((" dwMessageSize = %u", pProtocolBuf->dwMessageSize));
  736.  
  737. PGAS_TRACE((" dwProviderReserved = reserved"));
  738. }
  739.  
  740.  
  741. #endif
  742.  
  743. int GetCapabilities( )
  744. {
  745. int ret = PRT_FAILED;
  746.  
  747. #if defined ( _PLATFORM_WINDOWS_ )
  748.  
  749. int i, nRet;
  750. DWORD dwErr;
  751. WSAPROTOCOL_INFO *lpProtocolBuf = NULL;
  752. DWORD dwBufLen = 0;
  753.  
  754. // First, have WSAEnumProtocols tell you how big a buffer you
  755. // need.
  756. nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);
  757. if (SOCKET_ERROR != nRet)
  758. printf("WSAEnumProtocols: should not have succeeded\n");
  759. else if (WSAENOBUFS != (dwErr = WSAGetLastError()))
  760. // WSAEnumProtocols failed for some reason not relating
  761. // to buffer size - also odd.
  762. printf("WSAEnumProtocols(1): %d\n", WSAGetLastError());
  763. else
  764. {
  765. // WSAEnumProtocols failed for the "expected" reason.
  766. // Now you need to allocate a buffer that is the right size.
  767. lpProtocolBuf = (WSAPROTOCOL_INFO *)malloc(dwBufLen);
  768. if (lpProtocolBuf)
  769. {
  770. // Now you can call WSAEnumProtocols again with the
  771. // expectation that it will succeed
  772. // because you have allocated a big enough buffer.
  773. nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);
  774. if (SOCKET_ERROR == nRet)
  775. printf("WSAEnumProtocols(3): %d\n",
  776. WSAGetLastError());
  777. else
  778. {
  779. // Enumerate the protocols.
  780. for (i=0; i<nRet; i++)
  781. PrintBufEntry(&lpProtocolBuf[i]);
  782. }
  783. free(lpProtocolBuf);
  784. }
  785. }
  786.  
  787. #endif
  788.  
  789. return ret;
  790. }
  791.  
  792.  
  793.  
  794.  
  795.  
  796. } // namespace protocol
  797.  
  798. } // namespace pga
Add Comment
Please, Sign In to add comment