== Why is this an issue?

Network addresses have to be encoded in the network byte order, as specified by RFC-1700, which may be different from that of the host running the code, depending on the endianness of its architecture. This is usually done by using ``++ntohs++``, ``++ntohl++``, ``++htons++`` or ``++htonl++``.


This rule raises an issue when the following fields are assigned to or from without proper conversion:

* ``++struct sockaddr_in.sin_port++``
* ``++struct sockaddr_in.sin_addr.s_addr++``
* ``++struct sockaddr_in6.sin6_port++``


=== Noncompliant code example

[source,cpp]
----
int16_t port = 80;

sa->sin_port = port; // Noncompliant
sa->sin_addr.s_addr = address; // Noncompliant
sa->sin_family = AF_INET;

// ...

port = sa->sin_port; // Noncompliant
----


=== Compliant solution

[source,cpp]
----
int16_t port = 80;

sa->sin_port = htons(port);
sa->sin_addr.s_addr = htonl(address);
sa->sin_family = AF_INET;

// ...

port = ntohs(sa->sin_port);
----


== Resources

* https://tools.ietf.org/html/rfc1700[RFC-1700]
* Wikipedia - https://en.wikipedia.org/wiki/Endianness[Endianness]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Convert this value with {htons|htonl|ntohs|ntohl}


=== Highlighting

sockaddr_in, or sockaddr_in6 field assignment


endif::env-github,rspecator-view[]
