Fixed problem with UCS2 encoded long messages

This commit is contained in:
2015-06-13 17:56:08 +02:00
parent 7c1f7ef485
commit c5a4b93aee
4 changed files with 115 additions and 19 deletions
+9 -2
View File
@@ -2,7 +2,8 @@
// More or less copied from https://github.com/farhadi/node-smpp
var iconv = require('iconv-lite'),
var log = require('winston'),
iconv = require('iconv-lite'),
encodings = {},
filters = {},
tlvsById = {},
@@ -135,7 +136,13 @@ types = {
},
write: function(value, buffer, offset) {
value = value || 0;
buffer.writeUInt8(value, offset);
try {
buffer.writeUInt8(value, offset);
} catch (err) {
log.error('larvitsmpp: lib/defs.js - Could not write integer value "' + value + '" on offset "' + offset + '" when buffer length is "' + buffer.length + '"');
throw err;
}
},
size: function() {
return 1;
+2
View File
@@ -357,6 +357,8 @@ function sendSms(smsOptions, callback) {
// Check if we must split this message into multiple
if (utils.bitCount(smsOptions.message) > 1120) {
log.debug('larvitsmpp: lib/session.js: sendSms() - Message larger than 1120 bits, send it as long message!');
this.sendLongSms(smsOptions, callback);
return;
+38 -17
View File
@@ -1,7 +1,8 @@
'use strict';
var log = require('winston'),
defs = require('./defs');
var log = require('winston'),
defs = require('./defs'),
bundleMsgId = 0;
/**
* Calcualte cmdLength from object
@@ -115,15 +116,19 @@ function writeBuffer(obj, cmdLength, callback) {
paramType = defs.cmds[obj.cmdName].params[param].type;
paramSize = paramType.size(obj.params[param]);
// Write parameter value to buffer using the types method write()
paramType.write(obj.params[param], buff, offset);
if (Buffer.isBuffer(obj.params[param])) {
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing param "' + param + '" with content "' + obj.params[param].toString('hex') + '" and size "' + paramSize + '"');
} else {
if (param === 'sm_length') {
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - sm_length is calculated by short_message: "' + obj.params.short_message.toString('hex') + '"');
}
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing param "' + param + '" with content "' + obj.params[param] + '"');
}
// Write parameter value to buffer using the types method write()
paramType.write(obj.params[param], buff, offset);
// Increase the offset for the next param
offset += paramSize;
}
@@ -424,9 +429,7 @@ function objToPdu(obj, callback) {
return;
}
writeBuffer(obj, cmdLength, function(err, buff) {
callback(err, buff);
});
writeBuffer(obj, cmdLength, callback);
});
}
@@ -582,7 +585,11 @@ function smppDate(jsDateObj) {
* @return integer
*/
function bitCount(msg, encoding) {
if (encoding === 'ASCII' || defs.encodings.ASCII.match(msg)) {
if (defs.encodings[encoding] === undefined) {
encoding = defs.encodings.detect(msg);
}
if (encoding === 'ASCII') {
return defs.encodings.ASCII.encode(msg).length * 7; // * 7 since each character takes up 7 bits
} else {
return defs.encodings.UCS2.encode(msg).length * 8; // * 8 since its encoded as 16-bits.
@@ -596,26 +603,40 @@ function bitCount(msg, encoding) {
* @return array of buffers
*/
function splitMsg(msg) {
var msgPart = '',
msgs = [],
msgId = Math.floor(Math.random() * (255 - 0)), // This will identify this message "bundle"
encoding = defs.encodings.detect(msg),
var msgPart = '',
msgs = [],
encoding = defs.encodings.detect(msg),
totBitCount = bitCount(msg, encoding),
partBitCount,
udh,
i;
i;
// A single message could contain up to 1120 bits
// Return directly if the message fits into that
if (bitCount(msg) < 1121) {
if (totBitCount < 1121) {
log.silly('larvitsmpp: lib/utils.js: splitMsg() - bitCount below 1121 (' + totBitCount + ') return only one part');
return [defs.encodings[encoding].encode(msg)];
}
bundleMsgId ++; // This will identify this message "bundle"
if (bundleMsgId === 256) {
bundleMsgId = 1;
}
log.silly('larvitsmpp: lib/utils.js: splitMsg() - bundleMsgId set to ' + bundleMsgId);
i = 0;
while (msg[i] !== undefined) {
msgPart += msg[i];
if (bitCount(msgPart, encoding) > 1072) {
partBitCount = bitCount(msgPart, encoding);
if (partBitCount > 1072) {
// We've reached the message limit
log.debug('larvitsmpp: lib/utils.js: splitMsg() - Msg part defined. partBitCount: ' + partBitCount + ' msgPart: "' + msgPart + '"');
// Add this msgPart minus the last character to the msgs array as an encoded buffer
msgs.push(defs.encodings[encoding].encode(msgPart.slice(0, - 1)));
@@ -641,7 +662,7 @@ function splitMsg(msg) {
0x05, // Length of User Data Header, in this case 05.
0x00, // Information Element Identifier, equal to 00 (Concatenated short messages, 8-bit reference number)
0x03, // Length of the header, excluding the first two fields; equal to 03
msgId, // CSMS reference number, must be same for all the SMS parts in the CSMS
bundleMsgId, // CSMS reference number, must be same for all the SMS parts in the CSMS
msgs.length, // Total number of parts. The value shall remain constant for every short message which makes up the concatenated short message. If the value is zero then the receiving entity shall ignore the whole information element
i + 1 // This part's number in the sequence. The value shall start at 1 and increment for every short message which makes up the concatenated short message.
]);