Bind to user-specified address (BindAddress6)
[mgsmtp.git] / Listener.pas
index b37662b15302704304dfaa0ad059ea99ab4337ec..bff8dcf0ce9429d9ffb130433a0410cec9cf5ae2 100644 (file)
@@ -1,6 +1,6 @@
 {
    MegaBrutal's SMTP Server (MgSMTP)
-   Copyright (C) 2010-2014 MegaBrutal
+   Copyright (C) 2010-2018 MegaBrutal
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
@@ -38,7 +38,7 @@ uses SysUtils, Classes, Base64, Network, NetRFC, RFCSMTP,
 type
 
    TMgSMTPListener = class(TTCPListener)
-      constructor Create(Port: word);
+      constructor Create(const Address: string; Port, Family: word);
    protected
       procedure HandleClient(Connection: TTCPConnection); override;
       procedure ReceiveEMailData(TCP: TTCPRFCConnection; Response: TRFCReply; SpoolObject: TSpoolObjectCreator);
@@ -57,13 +57,20 @@ var
 
 
 procedure StartListeners;
-var i: integer;
+var i, j: integer; address, port: string; nport: word;
 begin
-   SetLength(MgSMTPListeners, MainServerConfig.ListenPorts.Count);
-   for i:= 0 to Length(MgSMTPListeners) - 1 do begin
-      MgSMTPListeners[i]:= TMgSMTPListener.Create(StrToIntDef(MainServerConfig.ListenPorts.Strings[i], STANDARD_SMTP_PORT));
+   SetLength(MgSMTPListeners, MainServerConfig.ListenAddresses.Count + MainServerConfig.ListenAddresses6.Count);
+   for i:= 0 to MainServerConfig.ListenAddresses.Count - 1 do begin
+      SplitParameters(MainServerConfig.ListenAddresses.Strings[i], address, port, ':');
+      MgSMTPListeners[i]:= TMgSMTPListener.Create(address, StrToIntDef(port, STANDARD_SMTP_PORT), AF_INET);
       MgSMTPListeners[i].StartListen;
    end;
+   j:= MainServerConfig.ListenAddresses.Count;
+   for i:= 0 to MainServerConfig.ListenAddresses6.Count - 1 do begin
+      ParseIPv6Address(MainServerConfig.ListenAddresses6.Strings[i], address, nport);
+      MgSMTPListeners[j+i]:= TMgSMTPListener.Create(address, nport, AF_INET6);
+      MgSMTPListeners[j+i].StartListen;
+   end;
 end;
 
 procedure StopListeners;
@@ -127,11 +134,11 @@ begin
 end;
 
 
-constructor TMgSMTPListener.Create(Port: word);
+constructor TMgSMTPListener.Create(const Address: string; Port, Family: word);
 begin
    { Request connection objects with support for RFC-style commands & responses. }
-   inherited Create(Port, NET_TCP_RFCSUPPORT);
-   Logger.AddLine('Server', 'Listening on port: ' + IntToStr(Port));
+   inherited Create(Address, Port, Family, NET_TCP_RFCSUPPORT);
+   Logger.AddLine('Server', 'Listening on address: ' + Address + ':' + IntToStr(Port));
 end;