Permalink
Browse files

Multiple port listen improvements

  • Loading branch information...
1 parent 6ee70ad commit 927060a537f1e0a8aecb59f7157612452b02c0ce @smfreegard smfreegard committed Mar 18, 2013
Showing with 46 additions and 13 deletions.
  1. +3 −3 config/smtp.ini
  2. +8 −2 connection.js
  3. +11 −0 docs/Connection.md
  4. +24 −8 server.js
View
@@ -1,6 +1,6 @@
-; address to listen on (default: all IPv4 addresses, port 25)
-; use "::0:25" to listen on IPv6 and IPv4 (not all OSes)
-;listen=0.0.0.0:25
+; address to listen on (default: all IPv6 and IPv4 addresses, port 25)
+; use "[::0]:25" to listen on IPv6 and IPv4 (not all OSes)
+;listen=[::0]:25
; Note you can listen on multiple IPs/ports using commas:
;listen=127.0.0.1:2529,127.0.0.2:2529,127.0.0.3:2530
View
@@ -66,10 +66,14 @@ function setupClient(self) {
self.client.destroy();
return;
}
-
+
+ var local_addr = self.server.address();
+ self.local_ip = ipaddr.process(local_addr.address).toString();
+ self.local_port = local_addr.port;
self.remote_ip = ipaddr.process(ip).toString();
self.remote_port = self.client.remotePort;
- self.lognotice("connect ip=" + self.remote_ip + ' port=' + self.remote_port);
+ self.lognotice('connect ip=' + self.remote_ip + ' port=' + self.remote_port +
+ ' local_ip=' + self.local_ip + ' local_port=' + self.local_port);
self.client.on('end', function() {
if (!self.disconnected) {
@@ -124,6 +128,8 @@ function setupClient(self) {
function Connection(client, server) {
this.client = client;
this.server = server;
+ this.local_ip = null;
+ this.local_port = null;
this.remote_ip = null;
this.remote_host = null
this.remote_port = null;
View
@@ -18,6 +18,17 @@ The remote IP address
The rDNS of the remote IP
+* connection.local_ip
+
+The bound IP address of the server as reported by the OS
+
+* connection.local_port
+
+The bound port number of the server which is handling the connection.
+If you have specified multiple listen= ports this variable is useful
+if you only want a plugin to run when connections are made to a specific
+port
+
* connection.greeting
Either 'EHLO' or 'HELO' whichever the remote end used
View
@@ -81,11 +81,13 @@ Server.createServer = function (params) {
var listeners = (config_data.main.listen || '').split(/\s*,\s*/);
if (listeners[0] === '') listeners = [];
if (config_data.main.port) {
- var host = config_data.main.listen_host || '0.0.0.0';
+ Server.default_host = true;
+ var host = config_data.main.listen_host || '[::0]';
listeners.unshift(host + ':' + config_data.main.port);
}
if (!listeners.length) {
- listeners.push('0.0.0.0:25');
+ Server.default_host = true;
+ listeners.push('[::0]:25');
}
Server.notes = {};
@@ -137,11 +139,10 @@ Server.createServer = function (params) {
};
function setup_listeners (listeners, plugins, type, inactivity_timeout) {
- console.log("About to listen: ", listeners);
async.each(listeners, function (host_port, cb) {
- var hp = /^(.*):(\d+)$/.exec(host_port);
+ var hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) {
- return cb("Invalid format for listen parameter in smtp.ini");
+ return cb(new Error("Invalid format for listen parameter in smtp.ini"));
}
var server = net.createServer(function (client) {
@@ -152,13 +153,28 @@ function setup_listeners (listeners, plugins, type, inactivity_timeout) {
server.notes = Server.notes;
if (Server.cluster) server.cluster = Server.cluster;
- server.listen(hp[2], hp[1], function () {
- logger.lognotice("Listening on " + host_port);
+ server.on('listening', function () {
+ var addr = this.address();
+ logger.lognotice("Listening on " + addr.address + ':' + addr.port);
cb();
});
+
+ // Fallback from IPv6 to IPv4 if not supported
+ // But only if we supplied the default of [::0]:25
+ server.on('error', function (e) {
+ if (e.code === 'EAFNOSUPPORT' && /^::0?/.test(hp[1]) && Server.default_host) {
+ server.listen(hp[2], '0.0.0.0');
+ }
+ else {
+ // Pass error to callback
+ cb(e);
+ }
+ });
+
+ server.listen(hp[2], hp[1]);
}, function (err) {
if (err) {
- logger.logerror("Failed to setup listeners: " + err);
+ logger.logerror("Failed to setup listeners: " + err.message);
return process.exit(-1);
}
listening();

0 comments on commit 927060a

Please sign in to comment.