Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid usage of <arpa/inet.h> #470

Merged
merged 6 commits into from May 22, 2019
Merged

Avoid usage of <arpa/inet.h> #470

merged 6 commits into from May 22, 2019

Conversation

@ilammy
Copy link
Contributor

ilammy commented May 21, 2019

  • Avoid usage of <arpa/inet.h>

Unfortunately, Windows systems do not provide this header normally. We use to get access to htonl() and ntohl() functions. On Windows these functions are provided by <WinSock2.h> header (and ws2_32.dll library).

We could litter the code with ifdefs, but that's not nice. Instead, let's use the portable_endian.h header. It contains all necessary ifdefs, but provides slightly different (and explicit) API:

  • htonl == htobe32
  • ntohl == be32toh

Update all code to include common header and use new portable names.

  • Move portable_endian.h to Soter

We're going to need these functions in both Soter and Themis, so move the header to Soter. Keep a stub in Themis to avoid breaking existing code.

Also note that <sys/param.h> is a POSIX header and it is not available on Windows. Remove it. All conversions are provided by <WinSock2.h>.

  • Turn Soter container accessors into functions

soter_container.h uses htonl() and ntohl() functions directly in macros. This is not nice because it leaks implementation details and requires POSIX API which is not available on Windows. Furthermore, this header does not even include relevant headers for these functions!

Replace this macros with proper functions and move the implementation to soter_container.c which includes "portable_endian.h". This allows our API to not include private headers and does not unnecessarily expose implementation details.

Theoretically, this will impact performance, but in practice the effect should be negligible. These macros are not used in any hot paths, and the compiler may even optimize these functions when using LTO.

ilammy added 3 commits May 17, 2019
Unfortunately, Windows systems do not provide this header normally.
We use to get access to htonl() and ntohl() functions. On Windows
these functions are provided by <WinSock2.h> header (and ws2_32.dll
library).

We could litter the code with ifdefs, but that's not nice. Instead,
let's use the "portable_endian.h" header. It contains all necessary
ifdefs, but provides slightly different (and explicit) API:

  - htonl == htobe32
  - ntohl == be32toh

Update all code to include common header and use new portable names.
We're going to need these functions in both Soter and Themis, so move
the header to Soter. Keep a stub in Themis to avoid breaking existing
code.

Also note that <sys/param.h> is a POSIX header and it is not available
on Windows. Remove it. All conversions are provided by <WinSock2.h>.
soter_container.h uses htonl() and ntohl() functions directly in macros.
This is not nice because it leaks implementation details and requires
POSIX API which is not available on Windows. Furthermore, this header
does not even include relevant headers for these functions!

Replace this macros with proper functions and move the implementation
to soter_container.c which includes "portable_endian.h". This allows
our API to not include private headers and does not unnecessarily expose
implementation details.

Theoretically, this will impact performance, but in practice the effect
should be negligible. These macros are not used in any hot paths, and
the compiler may even optimize these functions when using LTO.
@ilammy ilammy added the core label May 21, 2019
@ilammy ilammy requested review from vixentael and Lagovas May 21, 2019
@ilammy ilammy requested review from ignatk and storojs72 as code owners May 21, 2019
Copy link
Contributor

Lagovas left a comment

lgtm

ilammy added 2 commits May 21, 2019
Use spaces instead of tabs. Add a newline in the end of file. This
makes code style checker happy.
src/soter/soter_container.c Outdated Show resolved Hide resolved
Copy link
Member

vixentael left a comment

lgtm, although i feel a bit worried about bytes computations

Now that we have replaced macros with functions, we can make them more
resilient to invalid input. Check pointer parameters for null, return
errors where appropriate. Also avoid integer overflow when computing
data sizes.
@ilammy ilammy mentioned this pull request May 22, 2019
@ilammy ilammy merged commit 57485fb into cossacklabs:master May 22, 2019
7 of 8 checks passed
7 of 8 checks passed
ci/circleci: android Your tests failed on CircleCI
Details
ci/bitrise/69a1d5c9d42fa60f/pr Passed - themis-tests
Details
ci/circleci: analyze Your tests passed on CircleCI!
Details
ci/circleci: integration_tests Your tests passed on CircleCI!
Details
ci/circleci: php5 Your tests passed on CircleCI!
Details
ci/circleci: php70 Your tests passed on CircleCI!
Details
ci/circleci: php71 Your tests passed on CircleCI!
Details
ci/circleci: x86_64 Your tests passed on CircleCI!
Details
@ilammy ilammy deleted the ilammy:windows/endian branch May 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.