From a48c41148c3655c6c0de176060d739d3438e25f7 Mon Sep 17 00:00:00 2001 From: MegaBrutal Date: Tue, 13 Nov 2018 00:05:00 +0100 Subject: [PATCH] Log actual listen address Even if ListenAddress is a hostname, log the actual IP address the server tries to listen on, instead of the supplied hostname. Indicate when listen fails for any reason. modified: Listener.pas modified: MgSMTP.pas modified: Network.pas --- Listener.pas | 12 +++++++++++- MgSMTP.pas | 2 +- Network.pas | 12 +++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Listener.pas b/Listener.pas index fc92edd..52d753d 100644 --- a/Listener.pas +++ b/Listener.pas @@ -42,6 +42,8 @@ type protected procedure HandleClient(Connection: TTCPConnection); override; procedure ReceiveEMailData(TCP: TTCPRFCConnection; Response: TRFCReply; SpoolObject: TSpoolObjectCreator); + public + function StartListen: boolean; end; @@ -132,10 +134,18 @@ constructor TMgSMTPListener.Create(const Address: string; Port: word); begin { Request connection objects with support for RFC-style commands & responses. } inherited Create(Address, Port, NET_TCP_RFCSUPPORT); - Logger.AddLine('Server', 'Listening on address: ' + Address + ':' + IntToStr(Port)); end; +function TMgSMTPListener.StartListen: boolean; +begin + Result:= inherited StartListen; + if Result then + Logger.AddLine('Server', 'Listening on address: ' + GetSockAddrStr) + else + Logger.AddLine('Server', 'Failed to listen on address: ' + GetSockAddrStr); +end; + procedure TMgSMTPListener.HandleClient(Connection: TTCPConnection); { This is the procedure that actually handles the clients. It receives an object that manages the established connection in the parameter. diff --git a/MgSMTP.pas b/MgSMTP.pas index a9c51e7..1627479 100644 --- a/MgSMTP.pas +++ b/MgSMTP.pas @@ -49,7 +49,7 @@ const document what bugfix/feature are you testing with the actual build. This will be logged to help you differentiate outputs of subsequent builds in your logs. If left empty, it won't be added to the logs. } - DEVCOMMENT = 'Use getaddrinfo & getnameinfo'; + DEVCOMMENT = 'Log actual listen address if hostname given'; var diff --git a/Network.pas b/Network.pas index d5b458d..cd13cb2 100644 --- a/Network.pas +++ b/Network.pas @@ -37,7 +37,7 @@ unit Network; interface -uses Classes, Sockets, SocketUtils, DNSResolve, NetRFC, Common; +uses Classes, Sockets, SocketUtils, SysUtils, DNSResolve, NetRFC, Common; const @@ -101,17 +101,18 @@ type TTCPListener = class(TThread) constructor Create(const Address: string; Port: word; FeatureRequest: word); {destructor Destroy; override;} - private + protected FFeatureRequest: word; FListenAddress: string; FListenPort: word; FListenSocket: socket; SockAddr: TSockAddr; - protected procedure HandleClient(Connection: TTCPConnection); virtual; abstract; procedure Execute; override; public + property ListenAddress: string read FListenAddress; property ListenPort: word read FListenPort; + function GetSockAddrStr: string; function StartListen: boolean; procedure StopListen; end; @@ -300,6 +301,11 @@ begin end; +function TTCPListener.GetSockAddrStr: string; +begin + Result:= NetAddrToStr(SockAddr.sin_addr) + ':' + IntToStr(ntohs(SockAddr.sin_port)); +end; + function TTCPListener.StartListen: boolean; var GAIResult: TGAIResult; begin -- 2.34.1