fork download
  1. #include <iostream>
  2. #include <netinet/in.h>
  3. #include <stdint.h>
  4. using namespace std;
  5.  
  6. using u32=uint32_t;
  7. typedef u32 mpls_label_t;
  8.  
  9. typedef struct {
  10. /* Label: top 20 bits [in network byte order] */
  11. /* Experimental: 3 bits ... */
  12. /* S (bottom of label stack): 1 bit */
  13. /* TTL: 8 bits */
  14. mpls_label_t label_exp_s_ttl;
  15. } mpls_unicast_header_t;
  16.  
  17.  
  18. #define MPLS_ENTRY_LABEL_SHIFT 12
  19. #define MPLS_ENTRY_EOS_SHIFT 8
  20.  
  21. #define MPLS_ENTRY_EOS_MASK 0x01 /* EOS bit in its byte */
  22. #define MPLS_ENTRY_EOS(mpls) \
  23.   (((mpls) >> MPLS_ENTRY_EOS_SHIFT) & MPLS_ENTRY_EOS_MASK)
  24.  
  25. static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl)
  26. {
  27. return (MPLS_ENTRY_EOS(label_exp_s_ttl));
  28. }
  29.  
  30.  
  31. static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl)
  32. {
  33. return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT);
  34. }
  35.  
  36. int main() {
  37. auto label_exp_s_ttl = 0xFFA10100;
  38. cout << "s_ttl:" << label_exp_s_ttl << endl;
  39.  
  40. auto label = ntohl(label_exp_s_ttl);
  41. auto part1 = (vnet_mpls_uc_get_label(label) << 1);
  42. cout << "part1:" << part1 << endl;
  43. auto part2 = vnet_mpls_uc_get_s(label);
  44. cout << "part2:" << part2 << endl;
  45. auto key = (vnet_mpls_uc_get_label(label) << 1) | vnet_mpls_uc_get_s(label);
  46. cout << "label:" << label << endl;
  47. cout << "key:" << key << endl;
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
s_ttl:4288741632
part1:52
part2:1
label:107007
key:53