All Projects → gdelugre → literal_ipaddr

gdelugre / literal_ipaddr

Licence: MIT license
C++17 constexpr implementation of inet_addr / inet_aton / inet_pton

Programming Languages

C++
36643 projects - #6 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to literal ipaddr

constexpr-java
build-time code-execution for java, a bit like constexpr in C++11
Stars: ✭ 58 (+205.26%)
Mutual labels:  constexpr
flags17
A comparison of different options to implement binary flags in C++17
Stars: ✭ 16 (-15.79%)
Mutual labels:  constexpr
BitLens
🔎 Have your bits and eat them too! A C++17 bit lens container for vector types.
Stars: ✭ 20 (+5.26%)
Mutual labels:  cxx17
uberswitch
A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original.
Stars: ✭ 83 (+336.84%)
Mutual labels:  cxx17
fameta-counter
Compile time counter that works with all major modern compilers
Stars: ✭ 34 (+78.95%)
Mutual labels:  cxx17
opzioni
The wanna-be-simplest command line arguments library for C++
Stars: ✭ 29 (+52.63%)
Mutual labels:  constexpr
Compile Time Regular Expressions
A Compile time PCRE (almost) compatible regular expression matcher.
Stars: ✭ 2,144 (+11184.21%)
Mutual labels:  constexpr
fixed string
C++17 string with fixed size
Stars: ✭ 64 (+236.84%)
Mutual labels:  constexpr
uninttp
A universal type for non-type template parameters for C++20 or later.
Stars: ✭ 16 (-15.79%)
Mutual labels:  constexpr
bitset2
std::bitset with constexpr implementations plus additional features.
Stars: ✭ 76 (+300%)
Mutual labels:  constexpr
bing-ip2hosts
bingip2hosts is a Bing.com web scraper that discovers websites by IP address
Stars: ✭ 99 (+421.05%)
Mutual labels:  ipaddress
Libcudacxx
The C++ Standard Library for your entire system.
Stars: ✭ 1,861 (+9694.74%)
Mutual labels:  cxx17
Thrust
The C++ parallel algorithms library.
Stars: ✭ 3,595 (+18821.05%)
Mutual labels:  cxx17

Parsing IP addresses at compile-time in C++

This repository contains a C++17 constexpr implementation of the IP address parsing functions inet_addr, inet_aton and inet_pton. It supports both v4 and v6 address families.

When passed a literal IP address string, the compiler will parse the string and generate automatically the in_addr or in6_addr structure.

The address format is compatible with standard representations:

  • inet_addr and inet_aton accept IPv4 addresses in format a.b.c.d, a.b.c, a.b, or a, with address components expressed in decimal, octal or hexadecimal.
// Alternative syntaxes producing the same result:
//   IPAddr::inet_addr("127.0.1")
//   IPAddr::inet_addr("127.1")
//   IPAddr::inet_addr("0x7f.1")
//   IPAddr::inet_addr("2130706433")
//
constexpr in_addr_t addr = IPAddr::inet_addr("127.0.0.1");
  • inet_pton supports IPv4 addresses in canonical form (4 decimal components with no leading zeros) and IPv6 addresses represented according to RFC 2373, including with embedded IPv4 addresses.
constexpr auto in_addr1 = IPAddr::inet_pton<AF_INET>("127.0.0.1");
constexpr auto in_addr2 = IPAddr::inet_pton<AF_INET6>("::1");
constexpr auto in_addr3 = IPAddr::inet_pton<AF_INET6>("fe80::127.0.0.1"); // IPv4-in-IPv6 syntax

The ipaddr.h header also defines _ipaddr and _ipport literals. The _ipaddr suffix will automatically detect the format of the IP address and return the according in[6]_addr structure, or raise a static assertion if the format is incorrect.

Example

int s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

const struct sockaddr_in dst {
    .sin_family = AF_INET,
    .sin_port   = 1234_ipport,
    .sin_addr   = "127.0.0.1"_ipaddr
};

connect(s, (struct sockaddr *)&dst, sizeof(dst));

The above example, with clang 8.0.0 on Linux x86_64, generates:

        mov    $0x2,%edi
        mov    $0x1,%esi
        xor    %edx,%edx
        callq  <socket@plt>

        movups 0xe7e(%rip),%xmm0            # sockaddr in rodata section
        mov    %rsp,%rsi
        movaps %xmm0,(%rsi)

        mov    %eax,%edi
        mov    $0x10,%edx
        callq  <connect@plt>

With gcc 8.3.0:

        xor    %edx,%edx
        mov    $0x1,%esi
        mov    $0x2,%edi
        callq  <socket@plt>

        mov    $0x10,%edx
        lea    0x8(%rsp),%rsi
        movabs $0x100007fd2040002,%rcx      # sockaddr as an immediate operand

        mov    %eax,%edi
        mov    %rcx,0x8(%rsp)
        movq   $0x0,0x10(%rsp)
        callq  <connect@plt>

License

Distributed under the MIT license, see LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].