【问题标题】:Convert openvswitch types to little endian将 openvswitch 类型转换为小端
【发布时间】:2015-11-03 03:12:19
【问题描述】:

在 Open vSwitch 2.4.0 实现中,我们确实有以下内容:

#ifdef __CHECKER__
#define OVS_BITWISE __attribute__((bitwise))
#define OVS_FORCE __attribute__((force))
#else
#define OVS_BITWISE
#define OVS_FORCE
#endif

/* The ovs_be<N> types indicate that an object is in big-endian, not
 * native-endian, byte order.  They are otherwise equivalent to uint<N>_t. */

typedef uint16_t OVS_BITWISE ovs_be16;
typedef struct {
    ovs_be16 hi, lo;
} ovs_16aligned_be32;

我有以下变量:

ovs_16aligned_be32 srcIP;
ovs_be16 srcPort ;

如何将 srcIP 转换为 xxx.yyy.zzz.ttt 并将 srcPort 转换为 little-endian uint16_t

【问题讨论】:

    标签: c endianness openvswitch


    【解决方案1】:

    功能已经有了:

    htonl()
    htons()
    ntohl()
    ntohs()
    

    原型在

    #include <arpa/inet.h>
    

    如果我正确理解您的问题,这些功能将满足您的需求。

    【讨论】:

      【解决方案2】:

      好的,我不知道什么是 openvswitch,但我认为这应该可以:

      ovs_16aligned_be32 ovs_16aligned_be32_to_littleEndian(ovs_16aligned_be32 a) {
          ovs_16aligned_be32 b.high = ovs_be16_toLittleEndian(a.low);
          b.low = ovs_be16_toLittleEndian(a.high);
          return b;
      }
      
      ovs_be16 ovs_be16_toLittleEndian(ovs_be16 srcPort a) {
         ovs_be16 srcPort b = (a >> 8) & 0x00ff;
         b |= a << 8;
      
         return b;
      } 
      

      【讨论】:

        猜你喜欢
        • 2014-05-24
        • 2013-10-17
        • 2011-10-08
        • 2010-12-13
        • 1970-01-01
        • 2021-12-07
        • 2022-11-18
        相关资源
        最近更新 更多