我正在为ipv4编写的旧代码。现在,我为ipv6添加了类似的代码。我已经在主机上进行了测试和测试..但是由于字节顺序,我担心它是否可以在powerpc上工作...代码是从传入流中提取ip header 并将其压缩..任何评论都将有所帮助

class st_ipv6
{
  //==================================================================================================
public:
  //==================================================================================================

#ifdef _BIG_ENDIAN
  uint32  version:4;              // version
  uint32  trafficclass:8;         // header length
  uint32  flowlabel:20;           // flow label
#else
  uint32  trafficclass_1:4;
  uint32  version:4;              // version
  uint32  flowlabel_1:4;
  uint32  trafficclass_2:4;         // traffic class
  uint32  flowlabel_2:4;           // flow label
  uint32  flowlabel_3:4;           // flow label
  uint32  flowlabel_4:4;           // flow label
  uint32  flowlabel_5:4;           // flow label
#endif
  uint16  payloadlength;          // payload length
  uint8   nextheader;             // next header
  uint8   hoplimit;               // hop limit
  uint32  sourceaddress[4];       // source address
  uint32  destinationaddress[4];  // destination address




#ifdef _BIG_ENDIAN
  //**********************************************************************************************
  //* Description:
  /**  Specify/Return IPv6 Version
  */
  //**********************************************************************************************
  inline void setVersion(uint8 ubyVersion)
  {
    version = ubyVersion;
  }
  inline uint8 getVersion()
  {
    return version;
  }

最佳答案

位域和可移植性通常是一场噩梦。通常最好使用和/或移动。

参见例如下面。

只要您在读取/写入整数时对各种整数调用hton *()/ ntoh *(),(希望)不会有任何问题。

我还要编写其余的访问器,并将数据字段设为私有(private)。

class st_ipv6
{
public:
    uint32 version_info;
    uint16 payloadlength;
    uint8  nextheader;
    uint8  hoplimit;
    uint32 sourceaddress[4];
    uint32 destinationaddress[4];

    uint8 GetVersion()
    {
        return version_info & 0xF;
    }
    void  SetVersion(uint8 v)
    {
        version_info = (version_info & ~0xF) | (v & 0xF);
    }
    uint8 GetTrafficClass()
    {
        return (version_info & 0xFF0)>>4;
    }
    void  SetTrafficClass(uint8 c)
    {
        version_info = (version_info & ~0xFF0) | ((c & 0xFF) << 4);
    }
    uint32 GetFlowLabel()
    {
        return (version_info & 0xFFFFF000)>>12;
    }
    void  SetFlowLabel(uint32 f)
    {
        version_info = (version_info & ~0xFFFFF000) | ((f & 0xFFFFF) << 12);
    }
};

关于c++ - Bigendian/Littleendian困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5444111/

10-12 16:08