Skip to content

HostsEntry

Description

One row from /etc/hosts – a hostname (or alias) paired with its IP address. We store IPv4 addresses in the first four bytes of ip[]; IPv6 uses all 16.

Usage example (Cross-references)

Usage examples (Cross-references)
            u8   ip[16];
            bool is_ipv6;
        } HostsEntry;
    
        typedef Vec(HostsEntry) HostsTable;
        } HostsEntry;
    
        typedef Vec(HostsEntry) HostsTable;
        typedef Vec(SocketAddr) DnsAddrs;
                }
    
                HostsEntry e = {0};
                e.name       = StrInitFromCstr((Zstr)StrIterDataAt(&si, nm_start), nm_len, alloc);
                ascii_lower((u8 *)StrBegin(&e.name), StrLen(&e.name));
    
    static void hosts_push_v4(DnsResolver *r, Zstr name, u8 b0, u8 b1, u8 b2, u8 b3) {
        HostsEntry e = {0};
        e.name       = StrInitFromCstr(name, ZstrLen(name), r->allocator);
        e.ip[0]      = b0;
    
    static void hosts_push_v6(DnsResolver *r, Zstr name, const u8 ip16[16]) {
        HostsEntry e = {0};
        e.name       = StrInitFromCstr(name, ZstrLen(name), r->allocator);
        for (u64 i = 0; i < 16; ++i) {
    }
    
    static const HostsEntry *find_host(HostsTable *t, Zstr name) {
        VecForeachPtr(t, e) {
            if (StrLen(&e->name) > 0 && ZstrCompare(StrBegin(&e->name), name) == 0) {
            }
        }
        return (const HostsEntry *)0;
    }
    }
    
    static bool host_v4_is(const HostsEntry *e, u8 a, u8 b, u8 c, u8 d) {
        return e && !e->is_ipv6 && e->ip[0] == a && e->ip[1] == b && e->ip[2] == c && e->ip[3] == d;
    }
        DnsResolverAddHostsPath(&r, &path);
    
        const HostsEntry *e = find_host(&r.hosts, "localhost");
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(e, 127, 0, 0, 1) && ZstrCompare(StrBegin(&e->name), "localhost") == 0;
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "a"), 10, 0, 0, 1) &&
                  find_host(&r.hosts, "commented.host") == (const HostsEntry *)0;
    
        drop_temp(&path);
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "namex"), 10, 0, 0, 2) &&
                  find_host(&r.hosts, "trailing") == (const HostsEntry *)0 &&
                  find_host(&r.hosts, "#") == (const HostsEntry *)0;
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "namex"), 10, 0, 0, 2) &&
                  find_host(&r.hosts, "trailing") == (const HostsEntry *)0 &&
                  find_host(&r.hosts, "#") == (const HostsEntry *)0;
    
        drop_temp(&path);
        DnsResolverAddHostsPath(&r, &path);
    
        const HostsEntry *e = find_host(&r.hosts, "namex");
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(e, 10, 0, 0, 2) && ZstrCompare(StrBegin(&e->name), "namex") == 0;
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "mixedcasehost"), 10, 1, 1, 1) &&
                  find_host(&r.hosts, "MixedCaseHost") == (const HostsEntry *)0;
    
        drop_temp(&path);
        DnsResolverAddHostsPath(&r, &path);
    
        const HostsEntry *e = find_host(&r.hosts, "@az[`m");
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(e, 10, 1, 1, 2) && ZstrCompare(StrBegin(&e->name), "@az[`m") == 0;
        DnsResolverAddHostsPath(&r, &path);
    
        const HostsEntry *e  = find_host(&r.hosts, "ip6-localhost");
        bool              ok = VecLen(&r.hosts) == 1 && e && e->is_ipv6;
        if (ok) {
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "realname"), 10, 0, 0, 7) &&
                  find_host(&r.hosts, "somename") == (const HostsEntry *)0;
    
        drop_temp(&path);
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "goodname"), 10, 0, 0, 4) &&
                  find_host(&r.hosts, "spuriousname") == (const HostsEntry *)0;
    
        drop_temp(&path);
    
        bool ok = VecLen(&r.hosts) == 1 && host_v4_is(find_host(&r.hosts, "realone"), 10, 0, 0, 6) &&
                  find_host(&r.hosts, "x") == (const HostsEntry *)0;
    
        drop_temp(&path);
Last updated on