cosmopolitan/libc/sock/ipclassify.h
Justine Tunney f4f4caab0e Add x86_64-linux-gnu emulator
I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
2020-08-25 04:43:42 -07:00

70 lines
2 KiB
C

#ifndef COSMOPOLITAN_LIBC_SOCK_IPCLASSIFY_H_
#define COSMOPOLITAN_LIBC_SOCK_IPCLASSIFY_H_
#ifndef __STRICT_ANSI__
#include "libc/sock/sock.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/errfuns.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/**
* Returns true if address is part of standardized test-only subnet.
*/
forceinline bool istestip(int sin_family, void *sin_addr) {
if (sin_family == AF_INET) {
uint32_t ip4net24 = ntohl(*(const uint32_t *)sin_addr) & 0xffffff00u;
if (ip4net24 == INADDR_TESTNET1 || ip4net24 == INADDR_TESTNET2 ||
ip4net24 == INADDR_TESTNET3) {
return true;
}
} else {
eafnosupport();
}
return false;
}
/**
* Returns true if address is part of a local-only subnet.
*/
forceinline bool islocalip(int sin_family, void *sin_addr) {
if (sin_family == AF_INET) {
uint32_t ip4net8 = ntohl(*(const uint32_t *)sin_addr) & 0xff000000u;
if (ip4net8 == 0x7f000000u) {
return true;
}
} else {
eafnosupport();
}
return false;
}
/**
* Returns true if address is part of a well-known private subnet.
*/
forceinline bool isprivateip(int sin_family, void *sin_addr) {
if (sin_family == AF_INET) {
uint32_t ip4 = ntohl(*(const uint32_t *)sin_addr);
if ((0x0a000000u <= ip4 && ip4 <= 0x0affffffu) /* 10.0.0.0/8 */ ||
(0xac100000u <= ip4 && ip4 <= 0xac1fffffu) /* 172.16.0.0/12 */ ||
(0xc0a80000u <= ip4 && ip4 <= 0xc0a8ffffu) /* 192.168.0.0/16 */) {
return true;
}
} else {
eafnosupport();
}
return false;
}
/**
* Returns true if address is most likely part of the public Internet.
*/
forceinline bool ispublicip(int sin_family, void *sin_addr) {
return !islocalip(sin_family, sin_addr) &&
!isprivateip(sin_family, sin_addr) && !istestip(sin_family, sin_addr);
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_SOCK_IPCLASSIFY_H_ */