Beispiel #1
0
 internal static byte[] EncodeString(string str)
 {
     if (str == null)
     {
         str = "";
     }
     return(SmppEncodingUtil.GetBytesFromString(str));
 }
Beispiel #2
0
        private void PduReceivedEventHander(object sender, PduReceivedEventArgs e)
        {
            //This handler is interested in SingleDestinationPDU only
            SingleDestinationPdu pdu = e.Request as SingleDestinationPdu;

            if (pdu == null)
            {
                return;
            }

            //If we have just a normal message
            if (((byte)pdu.EsmClass | 0xc3) == 0xc3)
            {
                ShortMessage message;
                try { message = MessageFactory.CreateMessage(pdu); }
                catch (SmppException smppEx)
                {
                    if (VTraceSwitch.TraceError)
                    {
                        Trace.WriteLine(string.Format(
                                            "200019:SMPP message decoding failure - {0} - {1} {2};",
                                            smppEx.ErrorCode, new ByteBuffer(pdu.GetBytes()).DumpString(), smppEx.Message));
                    }
                    //Notify the SMSC that we encountered an error while processing the message
                    e.Response = pdu.CreateDefaultResponce();
                    e.Response.Header.ErrorCode = smppEx.ErrorCode;
                    return;
                }
                catch (Exception ex)
                {
                    if (VTraceSwitch.TraceError)
                    {
                        Trace.WriteLine(string.Format(
                                            "200019:SMPP message decoding failure - {0} {1};",
                                            new ByteBuffer(pdu.GetBytes()).DumpString(), ex.Message));
                    }
                    //Let the receiver know that this message was rejected
                    e.Response = pdu.CreateDefaultResponce();
                    e.Response.Header.ErrorCode = SmppErrorCode.EsmeRxPAppn; //ESME Receiver Reject Message
                    return;
                }
                RaiseMessageReceivedEvent(message);
            }
            //Or if we have received a delivery receipt
            else if ((pdu.EsmClass & EsmClass.DeliveryReceipt) == EsmClass.DeliveryReceipt)
            {
                // Extract receipted message id
                var    receiptedMessageIdTlv = pdu.Tlv.GetTlvByTag(Tag.ReceiptedMessageId);
                string receiptedMessageId    = null;
                if (receiptedMessageIdTlv != null)
                {
                    receiptedMessageId = SmppEncodingUtil.GetCStringFromBytes(receiptedMessageIdTlv.RawValue);
                }
                RaiseMessageDeliveredEvent(receiptedMessageId);
            }
        }
Beispiel #3
0
        public byte[] GetBytes()
        {
            ByteBuffer buffer = new ByteBuffer(32);

            buffer.Append(SmppEncodingUtil.GetBytesFromInt(_vCommandLength));
            buffer.Append(SmppEncodingUtil.GetBytesFromInt((uint)_vCommandType));
            buffer.Append(SmppEncodingUtil.GetBytesFromInt((uint)_vErrorCode));
            buffer.Append(SmppEncodingUtil.GetBytesFromInt(_vSequenceNumber));
            return(buffer.ToBytes());
        }
Beispiel #4
0
 internal static string DecodeString(ByteBuffer buffer, int length)
 {
     try { string value = SmppEncodingUtil.GetStringFromBytes(buffer.Remove(length)); return(value); }
     catch (ArgumentException ex)
     {
         //ByteBuffer.Remove(int count) throw ArgumentException if the buffer length is less than count
         //This is the indication that the amount of bytes required could not be met
         //We wrap this exception as NotEnoughtBytesException exception
         throw new NotEnoughBytesException(
                   "Octet String field could not be decoded because no enough bytes are evailable in the buffer",
                   ex);
     }
 }
Beispiel #5
0
        public virtual byte[] GetBytes()
        {
            if (_vRawValue == null || _vRawValue.Length != _vLength)
            {
                throw new TlvException("Tlv value length inconsistent with length field or has no data set");
            }
            ByteBuffer buffer = new ByteBuffer(_vLength + 4); //Reserve enough capacity for tag, length and value fields

            buffer.Append(SmppEncodingUtil.GetBytesFromShort((ushort)_vTag));
            buffer.Append(SmppEncodingUtil.GetBytesFromShort(_vLength));
            buffer.Append(_vRawValue);
            return(buffer.ToBytes());
        }
Beispiel #6
0
        public static Udh Parse(ByteBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            //There must be at least 3 bytes for UDHL, IEI, IEDL
            if (buffer.Length < 3)
            {
                throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Invalid UDH field");
            }
            int length = buffer.Remove(); //UDH Length
            int iei    = buffer.Remove(); //Information element identifier
            int ieidl  = buffer.Remove(); //Information element identifier data length

            /*
             * This udh implementation supports only concatenated messages with
             * 8 bits (IEI = 0) and 16 bits (IEI = 8) reference number.
             * Therefore, the expected number of bytes indicated by the UDHL field
             * should be either 5 or 6 octects, otherwise the udh is unsupported.
             */
            int segId = 0;
            int count = 0;
            int seq   = 0;

            //--
            //Confirm that we have enough bytes as indicated by the UDHL
            if (buffer.Length < ieidl)
            {
                throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Invalid UDH field");
            }
            if (length == 5 && iei == 0 && ieidl == 3) //8 bits message reference
            {
                segId = buffer.Remove();
                count = buffer.Remove();
                seq   = buffer.Remove();
            }
            else if (length == 6 && iei == 8 && ieidl == 4) //16 bits message reference
            {
                segId = SmppEncodingUtil.GetShortFromBytes(buffer.Remove(2));
                count = buffer.Remove();
                seq   = buffer.Remove();
            }
            else
            {
                throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Invalid or unsupported UDH field");
            }
            Udh udh = new Udh(segId, count, seq);

            return(udh);
        }
Beispiel #7
0
        public static Tlv Parse(ByteBuffer buffer)
        {
            //Buffer must have at least 4 bytes for tag and length plus at least one byte for the value field
            if (buffer.Length < 5)
            {
                throw new TlvException("Tlv required at least 5 bytes");
            }
            Tag    tag = (Tag)SmppEncodingUtil.GetShortFromBytes(buffer.Remove(2));
            ushort len = SmppEncodingUtil.GetShortFromBytes(buffer.Remove(2));
            Tlv    tlv = new Tlv(tag, len);

            tlv.ParseValue(buffer, len);
            return(tlv);
        }
Beispiel #8
0
        public virtual void SetMessageText(string message, DataCoding dataCoding, Udh udh)
        {
            ByteBuffer buffer = new ByteBuffer(160);

            if (udh != null)
            {
                buffer.Append(udh.GetBytes());
            }
            buffer.Append(SmppEncodingUtil.GetBytesFromString(message, dataCoding));
            SetMessageBytes(buffer.ToBytes());
            if (udh != null)
            {
                EsmClass = EsmClass | EsmClass.UdhiIndicator;
            }
            DataCoding = dataCoding;
        }
Beispiel #9
0
        public virtual void GetMessageText(out string message, out Udh udh)
        {
            message = null; udh = null;
            byte[] msgBytes = GetMessageBytes();
            if (msgBytes == null)
            {
                return;
            }
            ByteBuffer buffer = new ByteBuffer(msgBytes);

            //Check if the UDH is set in the esm_class field
            if ((EsmClass & EsmClass.UdhiIndicator) == EsmClass.UdhiIndicator)
            {
                if (_vTraceSwitch.TraceInfo)
                {
                    Trace.WriteLine("200020:UDH field presense detected;");
                }
                try { udh = Udh.Parse(buffer); }
                catch (Exception ex)
                {
                    if (_vTraceSwitch.TraceError)
                    {
                        Trace.WriteLine(string.Format(
                                            "20023:UDH field parsing error - {0} {1};",
                                            new ByteBuffer(msgBytes).DumpString(), ex.Message));
                    }
                    throw;
                }
            }
            //Check if we have something remaining in the buffer
            if (buffer.Length == 0)
            {
                return;
            }
            try { message = SmppEncodingUtil.GetStringFromBytes(buffer.ToBytes(), DataCoding); }
            catch (Exception ex1)
            {
                if (_vTraceSwitch.TraceError)
                {
                    Trace.WriteLine(string.Format(
                                        "200019:SMS message decoding failure - {0} {1};",
                                        new ByteBuffer(msgBytes).DumpString(), ex1.Message));
                }
                throw;
            }
        }
Beispiel #10
0
        internal static string DecodeCString(ByteBuffer buffer)
        {
            //Get next terminating null value
            int pos = buffer.Find(0x00);

            if (pos < 0)
            {
                throw new PduFormatException("CString type field could not be read. The terminating charactor is missing");
            }
            try { string value = SmppEncodingUtil.GetCStringFromBytes(buffer.Remove(pos + 1)); return(value); }
            catch (ArgumentException ex)
            {
                //ByteBuffer.Remove(int count) throw ArgumentException if the buffer length is less than count
                //This is the indication that the amount of bytes required could not be met
                //We wrap this exception as NotEnoughtBytesException exception
                throw new NotEnoughBytesException("PDU requires more bytes than supplied", ex);
            }
        }
Beispiel #11
0
        public static PduHeader Parse(ByteBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (buffer.Length < 16)
            {
                throw new ArgumentException("Buffer length must not be less than 16 bytes");
            }
            uint          cmdLength = SmppEncodingUtil.GetIntFromBytes(buffer.Remove(4));
            CommandType   cmdType   = (CommandType)SmppEncodingUtil.GetIntFromBytes(buffer.Remove(4));
            SmppErrorCode errorCode = (SmppErrorCode)SmppEncodingUtil.GetIntFromBytes(buffer.Remove(4));
            uint          seqNumber = SmppEncodingUtil.GetIntFromBytes(buffer.Remove(4));
            PduHeader     header    = new PduHeader(cmdType, seqNumber, errorCode, cmdLength);

            return(header);
        }
Beispiel #12
0
        protected override IEnumerable <SendSmPdu> GetPdUs(DataCoding defaultEncoding)
        {
            SubmitSm sm = new SubmitSm();

            sm.SourceAddress.Address      = VSourceAddress;
            sm.DestinationAddress.Address = VDestinatinoAddress; // Urgh, typo :(
            sm.DataCoding = defaultEncoding;
            if (VRegisterDeliveryNotification)
            {
                sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt;
            }

            _vMaxMessageLength = GetMaxMessageLength(defaultEncoding, false);
            byte[] bytes = SmppEncodingUtil.GetBytesFromString(_vText, defaultEncoding);

            // Unicode encoding return 2 items for 1 char
            // We check vText Length first
            if (_vText.Length > _vMaxMessageLength && bytes.Length > _vMaxMessageLength) // Split into multiple!
            {
                var segId = new Random().Next(1000, 9999);                               // create random SegmentID
                _vMaxMessageLength = GetMaxMessageLength(defaultEncoding, true);
                var messages      = Split(_vText, _vMaxMessageLength);
                var totalSegments = messages.Count;                   // get the number of (how many) parts
                var udh           = new Udh(segId, totalSegments, 0); // ID, Total, part

                for (int i = 0; i < totalSegments; i++)
                {
                    udh.MessageSequence = i + 1;                          // seq+1 , - parts of the message
                    sm.SetMessageText(messages[i], defaultEncoding, udh); // send parts of the message + all other UDH settings
                    yield return(sm);
                }
            }
            else
            {
                sm.SetMessageBytes(bytes);
                yield return(sm);
            }
        }