BIG_ENDIAN代码也要修复LITTLE_ENDIAN机器

BIG_ENDIAN code to be fixed for LITTLE_ENDIAN machine as well

本文关键字:ENDIAN LITTLE 机器 代码 BIG      更新时间:2023-10-16

我有这段遗留代码,我需要在BIG &小小的Endian机器。问题在于hton()。

msg->Mac是char Mac[16+1]现有代码:(仅适用于BIG)

if (sscanf(msg->Mac, "%4hx.%4hx.%4hx", (unsigned short *)&new_mac[0],
              (unsigned short *)&new_mac[2],
              (unsigned short *)&new_mac[4]) != 3) {
  return (ERROR_ADDRESS_TRANSLATION);
 }
*(unsigned short *)&new_mac[0] = hton(*(unsigned short *)&new_mac[0]);
*(unsigned short *)&new_mac[2] = hton(*(unsigned short *)&new_mac[2]);
*(unsigned short *)&new_mac[4] = hton(*(unsigned short *)&new_mac[4]);
sprintf((char *)newMac, "%04x.%04x.%04x", *(unsigned short *)&new_mac[0],
        *(unsigned short *)&new_mac[2], *(unsigned short *)&new_mac[4]);
 /* Get the MAC address */
if (sscanf((char *)newMac, "%4hx.%4hx.%4hx", (unsigned short *)&mac_addr[0],
              (unsigned short *)&mac_addr[2],
              (unsigned short *)&mac_addr[4]) != 3) {
      return (ERROR_ADDRESS_TRANSLATION);
 }

 /* Convert to network order */
 *(unsigned short *)&mac_addr[0] = hton(*(unsigned short *)&mac_addr[0]);
 *(unsigned short *)&mac_addr[2] = hton(*(unsigned short *)&mac_addr[2]);
 *(unsigned short *)&mac_addr[4] = hton(*(unsigned short *)&mac_addr[4]);

为了在LITTLE Endian机器上修复这个问题,我使用了一个SWAP宏,它将在short内交换字节。这是正确的方法吗?

我在上面添加的代码:(使它在LITTLE上也能工作)

#if __BYTE_ORDER != __BIG_ENDIAN
*(unsigned short *)&mac_addr[0] = SWAP(*(unsigned short *)&mac_addr[0]);
*(unsigned short *)&mac_addr[2] = SWAP(*(unsigned short *)&mac_addr[2]);
*(unsigned short *)&mac_addr[4] = SWAP(*(unsigned short *)&mac_addr[4]);
#endif

你在小端机上测试了吗?因为hton已经考虑了arch字节:

/* Copyright (C) 1993-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */
#include <stdint.h>
#include <netinet/in.h>
#undef  htonl
#undef  ntohl
uint32_t
htonl (uint32_t x)
{
#if BYTE_ORDER == BIG_ENDIAN
  return x;
#elif BYTE_ORDER == LITTLE_ENDIAN
  return __bswap_32 (x);
#else
# error "What kind of system is this?"
#endif
}
weak_alias (htonl, ntohl)

特别针对MAC地址:它们应该被视为一个6字节的数组,所以应该没有端序问题。试图映射其他类型(为了效率?)只是在自找麻烦。

所以scanf应该是:

sscanf(msg->Mac, "%2hhx%2hhx.%2hhx%2hhx.%2hhx%2hhx", 
              &new_mac[0], &new_mac[1],
              &new_mac[2], &new_mac[3],
              &new_mac[4], &new_mac[5])