Log actual listen address
authorMegaBrutal <code+git@megabrutal.com>
Mon, 12 Nov 2018 23:05:00 +0000 (00:05 +0100)
committerMegaBrutal <code+git@megabrutal.com>
Mon, 12 Nov 2018 23:05:00 +0000 (00:05 +0100)
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
MgSMTP.pas
Network.pas

index fc92edd86a77c31ac0a808354e03fc3710940c71..52d753d3d59c94a1bfc1ba3f1574fc6c2815911e 100644 (file)
@@ -42,6 +42,8 @@ type
    protected
       procedure HandleClient(Connection: TTCPConnection); override;
       procedure ReceiveEMailData(TCP: TTCPRFCConnection; Response: TRFCReply; SpoolObject: TSpoolObjectCreator);
    protected
       procedure HandleClient(Connection: TTCPConnection); override;
       procedure ReceiveEMailData(TCP: TTCPRFCConnection; Response: TRFCReply; SpoolObject: TSpoolObjectCreator);
+   public
+      function StartListen: boolean;
    end;
 
 
    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);
 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;
 
 
 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.
 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.
index a9c51e7d2e1ca64260742f5b1888f195d81b0ade..1627479c1b590e297992f1b9cc437d57652db626 100644 (file)
@@ -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. }
      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
 
 
 var
 
index d5b458da2d5919570e2805ae5416b63eac408752..cd13cb2ae4740ec5368c70686d381395fbd14660 100644 (file)
@@ -37,7 +37,7 @@
 unit Network;
 
 interface
 unit Network;
 
 interface
-uses Classes, Sockets, SocketUtils, DNSResolve, NetRFC, Common;
+uses Classes, Sockets, SocketUtils, SysUtils, DNSResolve, NetRFC, Common;
 
 const
 
 
 const
 
@@ -101,17 +101,18 @@ type
    TTCPListener = class(TThread)
       constructor Create(const Address: string; Port: word; FeatureRequest: word);
       {destructor Destroy; override;}
    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;
       FFeatureRequest: word;
       FListenAddress: string;
       FListenPort: word;
       FListenSocket: socket;
       SockAddr: TSockAddr;
-   protected
       procedure HandleClient(Connection: TTCPConnection); virtual; abstract;
       procedure Execute; override;
    public
       procedure HandleClient(Connection: TTCPConnection); virtual; abstract;
       procedure Execute; override;
    public
+      property ListenAddress: string read FListenAddress;
       property ListenPort: word read FListenPort;
       property ListenPort: word read FListenPort;
+      function GetSockAddrStr: string;
       function StartListen: boolean;
       procedure StopListen;
    end;
       function StartListen: boolean;
       procedure StopListen;
    end;
@@ -300,6 +301,11 @@ begin
 end;
 
 
 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
 function TTCPListener.StartListen: boolean;
 var GAIResult: TGAIResult;
 begin