PreviousUpNext

15.4.1172  src/lib/std/src/socket/net-db.pkg

## net-db.pkg

# Compiled by:
#     src/lib/std/src/standard-core.sublib


stipulate
    package hu  =  host_unt_guts;                                       # host_unt_guts                         is from   src/lib/std/src/bind-sysword-32.pkg
    package u1b =  one_byte_unt_guts;                                   # one_byte_unt_guts                     is from   src/lib/std/src/one-byte-unt-guts.pkg
    package ci  =  mythryl_callable_c_library_interface;                # mythryl_callable_c_library_interface  is from   src/lib/std/src/unsafe/mythryl-callable-c-library-interface.pkg
    package ps  =  proto_socket__premicrothread;                        # proto_socket__premicrothread          is from   src/lib/std/src/socket/proto-socket--premicrothread.pkg
    #
    fun cfun  fun_name
        =
        ci::find_c_function'' { lib_name => "socket", fun_name };
herein

    package   net_db
    : (weak)  Net_Db                                                    # Net_Db                                is from   src/lib/std/src/socket/net-db.api
    {
        Network_Address =  NETWORK_ADDRESS  hu::Unt;

        Address_Family  =  ps::af::Address_Family;

        Entry = ENTRY { name:           String,
                        aliases:        List( String ),
                        address_type:   Address_Family,
                        address:        Network_Address
                      };

        stipulate
            fun conc field' (ENTRY a) =  field' a;
        herein
            name         =  conc .name;
            aliases      =  conc .aliases;
            address_type =  conc .address_type;
            address      =  conc .address;
        end;

        Netent =   (String, List(String), ps::Raw_Address_Family, hu::Unt);

        (cfun "get_network_by_name")                                                            # "get_network_by_name"         def in    src/c/lib/socket/get-network-by-name.c 
            ->
            (      get_network_by_name__sysref:     String -> Null_Or(Netent),
                   get_network_by_name__ref,
              set__get_network_by_name__ref
            );

        (cfun "get_network_by_address")                                                         # "get_network_by_address"      def in    src/c/lib/socket/get-network-by-address.c 
            ->
            (      get_network_by_address__syscall:    (hu::Unt, ps::Raw_Address_Family) -> Null_Or( Netent ),
                   get_network_by_address__ref,
              set__get_network_by_address__ref
            );

        # Network DB query functions 
        #
        stipulate
            #

            fun get_net_typechecked_package (THE (name, aliases, address_type, address))
                    =>
                    THE (   ENTRY { name,
                                    aliases,
                                    address_type =>  ps::af::ADDRESS_FAMILY  address_type,
                                    address      =>  NETWORK_ADDRESS address
                                  }
                        );

                get_net_typechecked_package  NULL
                    =>
                    NULL;
            end;

        herein

            fun get_by_name  name
                =
                get_net_typechecked_package  (*get_network_by_name__ref  name);

            fun get_by_address (NETWORK_ADDRESS address, ps::af::ADDRESS_FAMILY af)
                =
                get_net_typechecked_package (*get_network_by_address__ref (address, af));
        end;

        fun scan getc stream
            =
            {   (+) = hu::(+);
                #
                case (ps::to_unts  getc  stream)
                    #
                    THE ([a, b, c, d], stream)
                        =>
                        THE   ( NETWORK_ADDRESS (hu::(<<) (a, 0u24)+hu::(<<) (b, 0u16)+hu::(<<) (c, 0u8)+d),
                                stream
                              );

                    THE ([a, b, c], stream)
                        =>
                        THE (NETWORK_ADDRESS (hu::(<<) (a, 0u24)+hu::(<<) (b, 0u16)+c), stream);

                    THE ([a, b], stream)
                        =>
                        THE (NETWORK_ADDRESS (hu::(<<) (a, 0u24)+b), stream);

                    THE ([a], stream)
                        =>
                        THE (NETWORK_ADDRESS a, stream);

                    _ => NULL;
                esac;
              };

        from_string =  number_string::scan_string  scan;

        fun to_string (NETWORK_ADDRESS address)
            =
            ps::from_bytes (get 0u24, get 0u16, get 0u8, get 0u0)
            where
                fun get n
                    =
                    u1b::from_large_unt  (hu::to_large_unt (hu::(>>) (address, n)));
            end;

    };
end;



## COPYRIGHT (c) 1995 AT&T Bell Laboratories.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2013,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext