Skip to content

Commit

Permalink
Don't use htonl, etc. Use BigDWord and friends.
Browse files Browse the repository at this point in the history
Yes those are weird and non-standard.  But on Windows ntohl actually is a (dll
import) function call.  This is slower than our defines and inlines (probably
not a huge deal).  Also, in situations where we just want to parse/print IP
addresses we need to link with winsock?  That is dumb.
  • Loading branch information
zpostfacto committed Jan 26, 2022
1 parent 7b3d6ab commit 5613e2a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/tier1/netadr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ bool CIPAddress::SetFromSockadr(const void *addr, size_t addr_size, uint16 *punP
}
const auto *sin = (const sockaddr_in *)addr;
m_usType = k_EIPTypeV4;
m_unIPv4 = ntohl ( sin->sin_addr.s_addr );
m_unIPv4 = BigDWord( sin->sin_addr.s_addr );
if ( punPort )
*punPort = ntohs( sin->sin_port );
*punPort = BigWord( sin->sin_port );

return true;
}
Expand All @@ -469,7 +469,7 @@ bool CIPAddress::SetFromSockadr(const void *addr, size_t addr_size, uint16 *punP
memcpy( m_rgubIPv6, &sin6->sin6_addr, sizeof(m_rgubIPv6) );
m_unIPv6Scope = sin6->sin6_scope_id;
if ( punPort )
*punPort = ntohs( sin6->sin6_port );
*punPort = BigWord( sin6->sin6_port );

return true;
}
Expand Down Expand Up @@ -650,8 +650,8 @@ size_t CIPAndPort::ToSockadr(void *addr, size_t addr_size) const
}
auto *s = (struct sockaddr_in*)addr;
s->sin_family = AF_INET;
s->sin_addr.s_addr = htonl( INADDR_LOOPBACK );
s->sin_port = htons( m_usPort );
s->sin_addr.s_addr = BigDWord( INADDR_LOOPBACK );
s->sin_port = BigWord( m_usPort );
struct_size = sizeof(sockaddr_in);
}
break;
Expand All @@ -665,8 +665,8 @@ size_t CIPAndPort::ToSockadr(void *addr, size_t addr_size) const
}
auto *s = (struct sockaddr_in*)addr;
s->sin_family = AF_INET;
s->sin_addr.s_addr = htonl( INADDR_BROADCAST );
s->sin_port = htons( m_usPort );
s->sin_addr.s_addr = BigDWord( INADDR_BROADCAST );
s->sin_port = BigWord( m_usPort );
struct_size = sizeof(sockaddr_in);
}
break;
Expand All @@ -680,8 +680,8 @@ size_t CIPAndPort::ToSockadr(void *addr, size_t addr_size) const
}
auto *s = (struct sockaddr_in*)addr;
s->sin_family = AF_INET;
s->sin_addr.s_addr = htonl( m_unIPv4 );
s->sin_port = htons( m_usPort );
s->sin_addr.s_addr = BigDWord( m_unIPv4 );
s->sin_port = BigWord( m_usPort );
struct_size = sizeof(sockaddr_in);
}
break;
Expand All @@ -698,7 +698,7 @@ size_t CIPAndPort::ToSockadr(void *addr, size_t addr_size) const
COMPILE_TIME_ASSERT( sizeof(s->sin6_addr) == sizeof(m_rgubIPv6) );
memcpy( &s->sin6_addr, m_rgubIPv6, sizeof(s->sin6_addr) );
s->sin6_scope_id = m_unIPv6Scope;
s->sin6_port = htons( m_usPort );
s->sin6_port = BigWord( m_usPort );
struct_size = sizeof(sockaddr_in6);
}
break;
Expand Down Expand Up @@ -726,5 +726,5 @@ void CIPAndPort::ToSockadrIPV6(void *addr, size_t addr_size) const
GetIPV6( s->sin6_addr.s6_addr );
if ( m_usType == k_EIPTypeV6 )
s->sin6_scope_id = m_unIPv6Scope;
s->sin6_port = htons( m_usPort );
s->sin6_port = BigWord( m_usPort );
}

0 comments on commit 5613e2a

Please sign in to comment.