Esempio n. 1
0
 internal static byte[] EncodeString(string str)
 {
     if (str == null)
     {
         str = "";
     }
     return(SMPPEncodingUtil.GetBytesFromString(str));
 }
Esempio n. 2
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());
        }
Esempio n. 3
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());
        }
Esempio n. 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);
     }
 }
Esempio n. 5
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.ESME_RUNKNOWNERR, "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.ESME_RUNKNOWNERR, "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.ESME_RUNKNOWNERR, "Invalid or unsupported UDH field");
            }
            Udh udh = new Udh(segId, count, seq);

            return(udh);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        protected override IEnumerable <SendSmPDU> GetPDUs(DataCoding defaultEncoding)
        {
            SubmitSm sm = CreateSubmitSm();

            sm.SourceAddress.Address      = vSourceAddress;
            sm.DestinationAddress.Address = vDestinatinoAddress; // Urgh, typo :(
            sm.DataCoding = defaultEncoding;
            if (!string.IsNullOrEmpty(UserMessageReference))
            {
                var msgIdBytes = SMPPEncodingUtil.GetBytesFromCString(UserMessageReference);
                sm.Tlv.Add(new Lib.Protocol.Tlv.Tlv(Lib.Protocol.Tlv.Tag.user_message_reference, (ushort)msgIdBytes.Length, msgIdBytes));
            }

            if (vRegisterDeliveryNotification)
            {
                sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt;
            }

            vMaxMessageLength = GetMaxMessageLength(defaultEncoding, false);
            byte[] bytes = SMPPEncodingUtil.GetBytesFromString(vText, defaultEncoding);

            string uni   = Encoding.Unicode.GetString(bytes);
            string ascii = Encoding.ASCII.GetString(bytes);
            string utf8  = Encoding.UTF8.GetString(bytes);

            // 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);
            }
        }
Esempio n. 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;
        }
Esempio n. 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;
            }
        }
Esempio n. 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);
            }
        }
Esempio n. 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);
        }
Esempio n. 12
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;
            }
            ShortMessage message = null;

            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.ESME_RX_P_APPN; //ESME Receiver Reject Message
                return;
            }
            //If we have just a normal message
            if ((((byte)pdu.EsmClass) | 0xc3) == 0xc3)
            {
                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.receipted_message_id);
                string receiptedMessageId    = null;
                if (receiptedMessageIdTlv != null)
                {
                    receiptedMessageId = SMPPEncodingUtil.GetCStringFromBytes(receiptedMessageIdTlv.RawValue);
                }
                message.ReceiptedMessageId = receiptedMessageId;

                // Extract user message reference
                var    userMessageReferenceTlv = pdu.Tlv.GetTlvByTag(Tag.user_message_reference);
                string userMessageReference    = null;
                if (userMessageReferenceTlv != null)
                {
                    userMessageReference = SMPPEncodingUtil.GetCStringFromBytes(userMessageReferenceTlv.RawValue);
                }
                message.UserMessageReference = userMessageReference;
                RaiseMessageDeliveredEvent(message);
            }
        }