Code Monkey home page Code Monkey logo

literal_ipaddr's Introduction

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.

literal_ipaddr's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

literal_ipaddr's Issues

Feature request: work at runtime

This library would be very useful for me if it could be used at runtime, because I wouldn't need to bother including inet_aton for different operating systems.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.