options/bsd: Add a new option and add several BSD extensions related to DNS

Signed-off-by: Dennis Bonke <admin@dennisbonke.com>
This commit is contained in:
Dennis Bonke 2021-11-29 00:06:20 +01:00
parent a569f1eddd
commit eea71c5ff9
No known key found for this signature in database
GPG key ID: F456F05FBF825330
7 changed files with 289 additions and 0 deletions

View file

@ -32,6 +32,7 @@ disable_iconv_option = get_option('disable_iconv_option')
disable_intl_option = get_option('disable_intl_option')
disable_glibc_option = get_option('disable_glibc_option')
disable_crypt_option = get_option('disable_crypt_option')
disable_bsd_option = get_option('disable_bsd_option')
internal_conf = configuration_data()
mlibc_conf = configuration_data()
@ -142,6 +143,7 @@ mlibc_conf.set('__MLIBC_INTL_OPTION', not disable_intl_option)
mlibc_conf.set('__MLIBC_ICONV_OPTION', not disable_iconv_option)
mlibc_conf.set('__MLIBC_GLIBC_OPTION', not disable_glibc_option)
mlibc_conf.set('__MLIBC_CRYPT_OPTION', not disable_crypt_option)
mlibc_conf.set('__MLIBC_BSD_OPTION', not disable_bsd_option)
if not disable_ansi_option
rtdl_include_dirs += include_directories('options/ansi/include')
@ -176,6 +178,10 @@ if not disable_crypt_option
libc_include_dirs += include_directories('options/crypt/include')
endif
if not disable_bsd_option
libc_include_dirs += include_directories('options/bsd/include')
endif
rtdl_include_dirs += include_directories('options/linux-headers/include')
libc_include_dirs += include_directories('options/linux-headers/include')
rtdl_include_dirs += include_directories('options/elf/include')
@ -266,6 +272,7 @@ subdir('options/linux-headers')
subdir('options/iconv')
subdir('options/intl')
subdir('options/crypt')
subdir('options/bsd')
if not headers_only
if not static

View file

@ -10,3 +10,4 @@ option('disable_linux_headers_option', type: 'boolean', value : false)
option('disable_iconv_option', type: 'boolean', value : false)
option('disable_intl_option', type: 'boolean', value : false)
option('disable_glibc_option', type: 'boolean', value : false)
option('disable_bsd_option', type: 'boolean', value : false)

View file

@ -0,0 +1,22 @@
#include <arpa/nameser.h>
// The ns_get* and ns_put* functions are taken from musl.
unsigned ns_get16(const unsigned char *cp) {
return cp[0] << 8 | cp[1];
}
unsigned long ns_get32(const unsigned char *cp) {
return (unsigned)cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3];
}
void ns_put16(unsigned s, unsigned char *cp) {
*cp++ = s >> 8;
*cp++ = s;
}
void ns_put32(unsigned long l, unsigned char *cp) {
*cp++ = l >> 24;
*cp++ = l >> 16;
*cp++ = l >> 8;
*cp++ = l;
}

View file

@ -0,0 +1,209 @@
#ifndef _ARPA_NAMESER_H
#define _ARPA_NAMESER_H
#ifdef __cplusplus
extern "C" {
#endif
#define NS_PACKETSZ 512
typedef enum __ns_rcode {
ns_r_noerror = 0,
ns_r_formerr = 1,
ns_r_servfail = 2,
ns_r_nxdomain = 3,
ns_r_notimpl = 4,
ns_r_refused = 5,
ns_r_yxdomain = 6,
ns_r_yxrrset = 7,
ns_r_nxrrset = 8,
ns_r_notauth = 9,
ns_r_notzone = 10,
ns_r_max = 11,
ns_r_badvers = 16,
ns_r_badsig = 16,
ns_r_badkey = 17,
ns_r_badtime = 18
} ns_rcode;
typedef enum __ns_type {
ns_t_invalid = 0,
ns_t_a = 1,
ns_t_ns = 2,
ns_t_md = 3,
ns_t_mf = 4,
ns_t_cname = 5,
ns_t_soa = 6,
ns_t_mb = 7,
ns_t_mg = 8,
ns_t_mr = 9,
ns_t_null = 10,
ns_t_wks = 11,
ns_t_ptr = 12,
ns_t_hinfo = 13,
ns_t_minfo = 14,
ns_t_mx = 15,
ns_t_txt = 16,
ns_t_rp = 17,
ns_t_afsdb = 18,
ns_t_x25 = 19,
ns_t_isdn = 20,
ns_t_rt = 21,
ns_t_nsap = 22,
ns_t_nsap_ptr = 23,
ns_t_sig = 24,
ns_t_key = 25,
ns_t_px = 26,
ns_t_gpos = 27,
ns_t_aaaa = 28,
ns_t_loc = 29,
ns_t_nxt = 30,
ns_t_eid = 31,
ns_t_nimloc = 32,
ns_t_srv = 33,
ns_t_atma = 34,
ns_t_naptr = 35,
ns_t_kx = 36,
ns_t_cert = 37,
ns_t_a6 = 38,
ns_t_dname = 39,
ns_t_sink = 40,
ns_t_opt = 41,
ns_t_apl = 42,
ns_t_tkey = 249,
ns_t_tsig = 250,
ns_t_ixfr = 251,
ns_t_axfr = 252,
ns_t_mailb = 253,
ns_t_maila = 254,
ns_t_any = 255,
ns_t_zxfr = 256,
ns_t_max = 65536
} ns_type;
typedef enum __ns_class {
ns_c_invalid = 0,
ns_c_in = 1,
ns_c_2 = 2,
ns_c_chaos = 3,
ns_c_hs = 4,
ns_c_none = 254,
ns_c_any = 255,
ns_c_max = 65536
} ns_class;
#define NS_GET16(s, cp) (void)((s) = ns_get16(((cp) += 2) - 2))
#define NS_GET32(l, cp) (void)((l) = ns_get32(((cp) += 4) - 4))
#define NS_PUT16(s, cp) ns_put16((s), ((cp) += 2) - 2)
#define NS_PUT32(l, cp) ns_put32((l), ((cp) += 4) - 4)
unsigned ns_get16(const unsigned char *);
unsigned long ns_get32(const unsigned char *);
void ns_put16(unsigned, unsigned char *);
void ns_put32(unsigned long, unsigned char *);
typedef struct {
unsigned id :16;
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned qr: 1;
unsigned opcode: 4;
unsigned aa: 1;
unsigned tc: 1;
unsigned rd: 1;
unsigned ra: 1;
unsigned unused :1;
unsigned ad: 1;
unsigned cd: 1;
unsigned rcode :4;
#else
unsigned rd :1;
unsigned tc :1;
unsigned aa :1;
unsigned opcode :4;
unsigned qr :1;
unsigned rcode :4;
unsigned cd: 1;
unsigned ad: 1;
unsigned unused :1;
unsigned ra :1;
#endif
unsigned qdcount :16;
unsigned ancount :16;
unsigned nscount :16;
unsigned arcount :16;
} HEADER;
#define PACKETSZ NS_PACKETSZ
#define NOERROR ns_r_noerror
#define FORMERR ns_r_formerr
#define SERVFAIL ns_r_servfail
#define NXDOMAIN ns_r_nxdomain
#define NOTIMP ns_r_notimpl
#define REFUSED ns_r_refused
#define YXDOMAIN ns_r_yxdomain
#define YXRRSET ns_r_yxrrset
#define NXRRSET ns_r_nxrrset
#define NOTAUTH ns_r_notauth
#define NOTZONE ns_r_notzone
#define T_A ns_t_a
#define T_NS ns_t_ns
#define T_MD ns_t_md
#define T_MF ns_t_mf
#define T_CNAME ns_t_cname
#define T_SOA ns_t_soa
#define T_MB ns_t_mb
#define T_MG ns_t_mg
#define T_MR ns_t_mr
#define T_NULL ns_t_null
#define T_WKS ns_t_wks
#define T_PTR ns_t_ptr
#define T_HINFO ns_t_hinfo
#define T_MINFO ns_t_minfo
#define T_MX ns_t_mx
#define T_TXT ns_t_txt
#define T_RP ns_t_rp
#define T_AFSDB ns_t_afsdb
#define T_X25 ns_t_x25
#define T_ISDN ns_t_isdn
#define T_RT ns_t_rt
#define T_NSAP ns_t_nsap
#define T_NSAP_PTR ns_t_nsap_ptr
#define T_SIG ns_t_sig
#define T_KEY ns_t_key
#define T_PX ns_t_px
#define T_GPOS ns_t_gpos
#define T_AAAA ns_t_aaaa
#define T_LOC ns_t_loc
#define T_NXT ns_t_nxt
#define T_EID ns_t_eid
#define T_NIMLOC ns_t_nimloc
#define T_SRV ns_t_srv
#define T_ATMA ns_t_atma
#define T_NAPTR ns_t_naptr
#define T_A6 ns_t_a6
#define T_DNAME ns_t_dname
#define T_TSIG ns_t_tsig
#define T_IXFR ns_t_ixfr
#define T_AXFR ns_t_axfr
#define T_MAILB ns_t_mailb
#define T_MAILA ns_t_maila
#define T_ANY ns_t_any
#define C_IN ns_c_in
#define C_CHAOS ns_c_chaos
#define C_HS ns_c_hs
#define C_NONE ns_c_none
#define C_ANY ns_c_any
#define GETSHORT NS_GET16
#define GETLONG NS_GET32
#define PUTSHORT NS_PUT16
#define PUTLONG NS_PUT32
#ifdef __cplusplus
}
#endif
#endif // _ARPA_NAMESER_H

14
options/bsd/meson.build Normal file
View file

@ -0,0 +1,14 @@
if disable_bsd_option
subdir_done()
endif
libc_sources += files(
'generic/arpa-nameser-stubs.cpp',
)
if not no_headers
install_headers(
'include/arpa/nameser.h',
subdir: 'arpa'
)
endif

20
tests/bsd/ns_get_put.c Normal file
View file

@ -0,0 +1,20 @@
#include <assert.h>
#include <arpa/nameser.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
uint8_t *buf = malloc(256);
uint16_t original16 = 4891;
uint32_t original32 = 4861984;
memset(buf, 0x0, sizeof(*buf));
ns_put16(original16, buf);
uint16_t result16 = ns_get16(buf);
assert(result16 == original16);
memset(buf, 0x0, sizeof(*buf));
ns_put32(original32, buf);
uint32_t result32 = ns_get32(buf);
assert(result32 == original32);
return 0;
}

View file

@ -14,6 +14,10 @@ ansi_test_cases = [
'time'
]
bsd_test_cases = [
'ns_get_put'
]
posix_test_cases = [
'inet_ntop',
'inet_pton',
@ -84,6 +88,18 @@ if not disable_ansi_option
endforeach
endif
if not disable_bsd_option
foreach f : bsd_test_cases
exec = executable('ansi-' + f, ['bsd/' + f + '.c', test_sources],
link_with: libc, include_directories: libc_include_dirs,
build_rpath: meson.build_root(),
c_args: '-no-pie',
link_args: ['-Wl,--dynamic-linker=' + meson.build_root() + '/ld.so',
'-no-pie'])
test('bsd/' + f, exec)
endforeach
endif
if not disable_posix_option
foreach f : posix_test_cases
exec = executable('posix-' + f, ['posix/' + f + '.c', test_sources],