UNSAFE MODULE-------------------------------------------------------------- internal ---; IMPORT Ctypes, IP, IPError, TCP, TCPPosix, Uin, Usocket, Word; TYPE Addr = Uin.struct_sockaddr_in; PROCEDURE TCPPeer Get (channel: TCP.T): IP.Endpoint RAISES {IP.Error} = VAR addr: Addr; endpoint: IP.Endpoint; BEGIN GetSockAddr(channel, addr); endpoint.port := Uin.ntohs (addr.sin_port); endpoint.addr := LOOPHOLE (addr.sin_addr.s_addr, IP.Address); RETURN endpoint; END Get; PROCEDUREGetName (channel: TCP.T): TEXT RAISES {IP.Error} = VAR addr: Addr; BEGIN GetSockAddr (channel, addr); RETURN IP.GetCanonicalByAddr (LOOPHOLE (addr.sin_addr.s_addr, IP.Address)); END GetName; PROCEDUREMatch (channel: TCP.T; address: IP.Address; maskBits: [0 .. 32]): BOOLEAN RAISES {IP.Error} = VAR addr: Addr; peer, mask: INTEGER; addrWord: Ctypes.int := LOOPHOLE(address, Ctypes.int); BEGIN GetSockAddr(channel, addr); peer := Word.Extract (addr.sin_addr.s_addr, 32 - maskBits, maskBits); mask := Word.Extract (addrWord, 32 - maskBits, maskBits); RETURN (peer = mask); END Match;
PROCEDUREGetSockAddr (channel: TCP.T; VAR(*OUT*) addr: Addr) RAISES {IP.Error} = VAR len: Ctypes.int := BYTESIZE (addr); BEGIN LOCK channel DO IF (channel.closed) THEN IPError.Raise (TCP.Closed); END; IF (Usocket.getpeername (channel.fd, ADR (addr), ADR (len)) < 0) THEN IPError.RaiseUnexpected (); END; END; END GetSockAddr; BEGIN END TCPPeer.