UNSAFE MODULE; IMPORT Herrno, IPError, M3toC, Unetdb, Usocket, Unix, Uin, Utypes; VAR mu := NEW(MUTEX); PROCEDURE IP GetHostByName (nm: TEXT; VAR (*out*) res: Address): BOOLEAN RAISES {Error} = VAR hostent: Unetdb.struct_hostent; BEGIN LOCK mu DO VAR s := M3toC.SharedTtoS(nm); h := Unetdb.gethostbyname(s, ADR(hostent)); BEGIN M3toC.FreeSharedS(nm, s); IF h = NIL THEN InterpretError(); RETURN FALSE; END; res := GetAddress(h); END; END; RETURN TRUE; END GetHostByName; PROCEDUREGetCanonicalByName (nm: TEXT): TEXT RAISES {Error} = VAR hostent: Unetdb.struct_hostent; BEGIN LOCK mu DO VAR s := M3toC.SharedTtoS(nm); h := Unetdb.gethostbyname(s, ADR(hostent)); BEGIN M3toC.FreeSharedS(nm, s); IF h # NIL THEN RETURN M3toC.CopyStoT(h.h_name); END; InterpretError(); END; END; RETURN NIL; END GetCanonicalByName; PROCEDUREGetCanonicalByAddr (addr: Address): TEXT RAISES {Error} = VAR ua: Uin.struct_in_addr; hostent: Unetdb.struct_hostent; BEGIN ua.s_addr := LOOPHOLE(addr, Utypes.u_int); LOCK mu DO VAR h := Unetdb.gethostbyaddr( ADR(ua), BYTESIZE(ua), Usocket.AF_INET, ADR(hostent)); BEGIN IF h # NIL THEN RETURN M3toC.CopyStoT(h.h_name); END; InterpretError(); END; END; RETURN NIL; END GetCanonicalByAddr; PROCEDUREGetAddress (ent: Unetdb.struct_hostent_star): Address = VAR ua: Uin.struct_in_addr; BEGIN <* ASSERT ent.h_length <= BYTESIZE(Address) *> ua := LOOPHOLE(ent.h_addr_list, UNTRACED REF UNTRACED REF Uin.struct_in_addr)^^; RETURN LOOPHOLE(ua.s_addr, Address); END GetAddress; PROCEDUREGetHostAddr (): Address = VAR hname: ARRAY [0..255] OF CHAR; hostent: Unetdb.struct_hostent; BEGIN LOCK mu DO IF Unix.gethostname(ADR(hname[0]), BYTESIZE(hname)) # 0 THEN IPError.Die (); END; VAR h := Unetdb.gethostbyname(ADR(hname[0]), ADR(hostent)); BEGIN IF h = NIL THEN IPError.Die(); END; RETURN GetAddress(h); END; END; END GetHostAddr; PROCEDUREInterpretError () RAISES {Error} = VAR err := Herrno.Get_h_errno(); BEGIN IF (err = Unetdb.TRY_AGAIN) OR (err = Unetdb.NO_RECOVERY) OR (err = Unetdb.NO_ADDRESS) THEN IPError.Raise (LookupFailure); END; END InterpretError; BEGIN END IP.