Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /sml/branches/SMLNJ/src/compiler/PervEnv/Sockets/net-db.sml
ViewVC logotype

View of /sml/branches/SMLNJ/src/compiler/PervEnv/Sockets/net-db.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 249 - (download) (annotate)
Sat Apr 17 18:57:03 1999 UTC (20 years, 4 months ago) by monnier
File size: 2026 byte(s)
version 110.16
(* net-db.sml
 *
 * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
 *
 *)

structure NetDB : NET_DB =
  struct

    structure SysW = SysWord

    fun netdbFun x = CInterface.c_function "SMLNJ-Sockets" x

    datatype net_addr = NETADDR of SysW.word

    type addr_family = PreSock.addr_family

    datatype entry = NETENT of {
	name : string,
	aliases : string list,
	addrType : addr_family,
	addr : net_addr
      }

    local
      fun conc field (NETENT a) = field a
    in
    val name = conc #name
    val aliases = conc #aliases
    val addrType = conc #addrType
    val addr = conc #addr
    end (* local *)

  (* Network DB query functions *)
    local
      type netent
	= (string * string list * PreSock.af * SysWord.word)
      fun getNetEnt NONE = NONE
	| getNetEnt (SOME(name, aliases, addrType, addr)) = SOME(NETENT{
	      name = name, aliases = aliases,
	      addrType = PreSock.AF addrType, addr = NETADDR addr
	    })
      val getNetByName' : string -> netent option
	    = netdbFun "getNetByName"
      val getNetByAddr' : (SysWord.word * PreSock.af) -> netent option
	    = netdbFun "getNetByAddr"
    in
    val getByName = getNetEnt o getNetByName'
    fun getByAddr (NETADDR addr, PreSock.AF af) =
	  getNetEnt(getNetByAddr'(addr, af))
    end (* local *)

    fun scan getc strm = let
	  val (op +) = SysW.+
	  in
	    case (PreSock.toWords getc strm)
	     of SOME([a, b, c, d], strm) =>
		  SOME(
		    NETADDR(SysW.<<(a, 0w24)+SysW.<<(b, 0w16)+SysW.<<(c, 0w8)+d),
		    strm)
	      | SOME([a, b, c], strm) =>
		  SOME(NETADDR(SysW.<<(a, 0w24)+SysW.<<(b, 0w16)+c), strm)
	      | SOME([a, b], strm) =>
		  SOME(NETADDR(SysW.<<(a, 0w24)+b), strm)
	      | SOME([a], strm) => SOME(NETADDR a, strm)
	      | _ => NONE
	    (* end case *)
	  end

    val fromString = StringCvt.scanString scan

    fun toString (NETADDR addr) = let
	  fun get n = Word8.fromLargeWord(SysW.toLargeWord((SysW.>>(addr, n))))
	  in
	    PreSock.fromBytes (get 0w24, get 0w16, get 0w8, get 0w0)
	  end

  end

(*
 * $Log$
 *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0