diff --git a/lib/client.js b/lib/client.js index 68917fb..bb2241d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -115,8 +115,8 @@ function client(options, callback) { }); session.on('loginFailed', function() { - var err = new Error('larvitsmpp: lib/client.js: client() - Remote host refused login.'); - log.warn(err.message); + var err = new Error('Remote host refused login.'); + log.warn('larvitsmpp: lib/client.js: client() - ' + err.message); callback(err); }); }); diff --git a/lib/server.js b/lib/server.js index d215fc8..fdc7730 100644 --- a/lib/server.js +++ b/lib/server.js @@ -6,6 +6,85 @@ var log = require('winston'), session = require('./session'), smppUtils = require('./utils'); +/** + * Try to log a connecting peer in + * + * @param obj pduObj + */ +function login(pduObj) { + var that = this; + + log.debug('larvitsmpp: lib/server.js: login() - Data received and session is not loggedIn'); + + // Pause socket so we do not receive any other commands until we have processed the login + this.sock.pause(); + + // Only bind_* is accepted when the client is not logged in + if (pduObj.cmdName !== 'bind_transceiver' && pduObj.cmdName !== 'bind_receiver' && pduObj.cmdName !== 'bind_transmitter') { + log.debug('larvitsmpp: lib/server.js: login() - Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS'); + + smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function(err, retPdu) { + if (err) { + that.closeSocket(); + return; + } + + that.sock.resume(); + that.sockWrite(retPdu); + }); + + return; + } + + // If there is a checkuserpass(), use it to check system_id and password from the PDU + if (typeof this.options.checkuserpass === 'function') { + this.options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function(err, res) { + if (err) { + that.closeSocket(); + return; + } + + if ( ! res) { + log.info('larvitsmpp: lib/server.js: serverSession() - login() - Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"'); + + that.sock.resume(); + that.sendReturn(pduObj, 'ESME_RBINDFAIL'); + return; + } + + log.verbose('larvitsmpp: lib/server.js: serverSession() - login() - Login successful! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"'); + that.loggedIn = true; + that.sock.resume(); + that.sendReturn(pduObj); + }); + + return; + } + + // If we arrived here it means we are not logged in and that a bind_* event happened and no checkuserpass() method exists. Lets login! + this.loggedIn = true; + this.sock.resume(); + this.sendReturn(pduObj); +} + +/** + * Reset the enquire link timer + * If this is not ran within options.timeout milliseconds, this session will self terminate + */ +function resetEnqLinkTimer() { + var that = this; + + log.silly('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Resetting the kill timer'); + if (this.enqLinkTimer) { + clearTimeout(this.enqLinkTimer); + } + + this.enqLinkTimer = setTimeout(function() { + log.info('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Closing session due to timeout'); + that.closeSocket(); + }, this.options.timeout); +} + /** * Server session function - inherits from session() * @@ -16,80 +95,9 @@ var log = require('winston'), function serverSession(sock, options) { var returnObj = session(sock); - /** - * Try to log a connecting peer in - * - * @param obj pduObj - */ - returnObj.login = function(pduObj) { - log.debug('larvitsmpp: lib/server.js: serverSession() - login() - Data received and session is not loggedIn'); - - // Pause socket so we do not receive any other commands until we have processed the login - returnObj.sock.pause(); - - // Only bind_* is accepted when the client is not logged in - if (pduObj.cmdName !== 'bind_transceiver' && pduObj.cmdName !== 'bind_receiver' && pduObj.cmdName !== 'bind_transmitter') { - log.debug('larvitsmpp: lib/server.js: serverSession() - login()) - Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS'); - - smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function(err, retPdu) { - if (err) { - returnObj.closeSocket(); - return; - } - - returnObj.sock.resume(); - returnObj.sockWrite(retPdu); - }); - - return; - } - - // If there is a checkuserpass(), use it to check system_id and password from the PDU - if (typeof options.checkuserpass === 'function') { - options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function(err, res) { - if (err) { - returnObj.closeSocket(); - return; - } - - if ( ! res) { - log.info('larvitsmpp: lib/server.js: serverSession() - login() - Login failed! Connected host: ' + sock.remoteAddress + ':' + sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"'); - - returnObj.sock.resume(); - returnObj.sendReturn(pduObj, 'ESME_RBINDFAIL'); - return; - } - - log.verbose('larvitsmpp: lib/server.js: serverSession() - login() - Login successful! Connected host: ' + sock.remoteAddress + ':' + sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"'); - returnObj.loggedIn = true; - returnObj.sock.resume(); - returnObj.sendReturn(pduObj); - }); - - return; - } - - // If we arrived here it means we are not logged in and that a bind_* event happened and no checkuserpass() method exists. Lets login! - returnObj.loggedIn = true; - returnObj.sock.resume(); - returnObj.sendReturn(pduObj); - }; - - /** - * Reset the enquire link timer - * If this is not ran within options.timeout milliseconds, this session will self terminate - */ - returnObj.resetEnqLinkTimer = function() { - log.silly('larvitsmpp: lib/server.js: serverSession() - resetEnqLinkTimer() - Resetting the kill timer'); - if (returnObj.enqLinkTimer) { - clearTimeout(returnObj.enqLinkTimer); - } - - returnObj.enqLinkTimer = setTimeout(function() { - log.info('larvitsmpp: lib/server.js: serverSession() - resetEnqLinkTimer() - Closing session due to timeout'); - returnObj.closeSocket(); - }, options.timeout); - }; + returnObj.options = options; + returnObj.login = login; + returnObj.resetEnqLinkTimer = resetEnqLinkTimer; returnObj.resetEnqLinkTimer();