VirtualBox

Changeset 103628 in vbox for trunk/include


Ignore:
Timestamp:
Mar 1, 2024 2:22:28 AM (15 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
161988
Message:

iprt/net.h: Some inlined IPv4 initialization and conversion to/from in_addr functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/net.h

    r98103 r103628  
    153153 */
    154154RTDECL(int) RTNetPrefixToMaskIPv4(int iPrefix, PRTNETADDRIPV4 pMask);
     155
     156/**
     157 * Initializes an IPv4 address from four octets.
     158 *
     159 * @returns The address (network endian).
     160 * @param   b3          The 4th and the most significant address octet.
     161 * @param   b2          The 3rd address octet.
     162 * @param   b1          The 2nd address octet.
     163 * @param   b0          The 1st and least significant address octet.
     164 */
     165DECLINLINE(RTNETADDRIPV4) RTNetIPv4AddrFromU8(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
     166{
     167    RTNETADDRIPV4 Ret;
     168#ifdef RT_LITTLE_ENDIAN
     169    Ret.u = RT_MAKE_U32_FROM_U8(    b3, b2, b1, b0);
     170#else
     171    Ret.u = RT_MAKE_U32_FROM_MSB_U8(b3, b2, b1, b0);
     172#endif
     173    return Ret;
     174}
     175
     176/**
     177 * Initializes an IPv4 address from four octets, host endian version.
     178 *
     179 * @returns The address (host endian).
     180 * @param   b3          The 4th and the most significant address octet.
     181 * @param   b2          The 3rd address octet.
     182 * @param   b1          The 2nd address octet.
     183 * @param   b0          The 1st and least significant address octet.
     184 */
     185DECLINLINE(RTNETADDRIPV4) RTNetIPv4AddrHEFromU8(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
     186{
     187    RTNETADDRIPV4 Ret;
     188    Ret.u = RT_MAKE_U32_FROM_MSB_U8(b3, b2, b1, b0);
     189    return Ret;
     190}
     191
     192#ifdef RTNET_INCL_IN_ADDR
     193
     194/**
     195 * Initializes an struct in_addr from four octets
     196 *
     197 * @returns The in_addr (network endian).
     198 * @param   b3          The 4th and the most significant address octet.
     199 * @param   b2          The 3rd address octet.
     200 * @param   b1          The 2nd address octet.
     201 * @param   b0          The 1st and least significant address octet.
     202 */
     203DECLINLINE(struct in_addr) RTNetInAddrFromU8(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
     204{
     205    struct in_addr Ret;
     206# ifdef RT_LITTLE_ENDIAN
     207    Ret.s_addr = RT_MAKE_U32_FROM_U8(    b3, b2, b1, b0);
     208# else
     209    Ret.s_addr = RT_MAKE_U32_FROM_MSB_U8(b3, b2, b1, b0);
     210# endif
     211    return Ret;
     212}
     213
     214/**
     215 * Converts an IPv4 address to the struct in_addr format.
     216 *
     217 * @returns Converted address (network endian).
     218 * @param   pAddr           The IPv4 address to convert (network endian).
     219 */
     220DECLINLINE(struct in_addr) RTNetIPv4AddrToInAddr(PCRTNETADDRIPV4 pAddr)
     221{
     222    struct in_addr Ret;
     223    Ret.s_addr = pAddr->u;
     224    return Ret;
     225}
     226
     227# ifdef IPRT_INCLUDED_asm_h /* for ASMByteSwapU32 */
     228/**
     229 * Converts an IPv4 address to the struct in_addr format, host endian version.
     230 *
     231 * @returns Converted address (network endian).
     232 * @param   pAddr           The IPv4 address to convert - host endian.
     233 */
     234DECLINLINE(struct in_addr) RTNetIPv4AddrHEToInAddr(PCRTNETADDRIPV4 pAddr)
     235{
     236    struct in_addr Ret;
     237    Ret.s_addr = RT_H2N_U32(pAddr->u);
     238    return Ret;
     239}
     240# endif
     241
     242/**
     243 * Converts an IPv4 address to the struct in_addr format.
     244 *
     245 * @returns Converted address (network endian).
     246 * @param   pInAddr         The in_addr to convert (network endian).
     247 */
     248DECLINLINE(RTNETADDRIPV4) RTNetIPv4AddrFromInAddr(struct in_addr const *pInAddr)
     249{
     250    RTNETADDRIPV4 Ret;
     251    Ret.u = pInAddr->s_addr;
     252    return Ret;
     253}
     254
     255# ifdef IPRT_INCLUDED_asm_h /* for ASMByteSwapU32 */
     256/**
     257 * Converts an IPv4 address to the struct in_addr format, host endian version.
     258 *
     259 * @returns Converted address (host endian).
     260 * @param   pInAddr         The in_addr to convert (network endian).
     261 */
     262DECLINLINE(RTNETADDRIPV4) RTNetIPv4AddrHEFromInAddr(struct in_addr const *pInAddr)
     263{
     264    RTNETADDRIPV4 Ret;
     265    Ret.u = RT_N2H_U32(pInAddr->s_addr);
     266    return Ret;
     267}
     268# endif
     269
     270#endif /* RTNET_INCL_IN_ADDR */
    155271
    156272
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette