예제 #1
0
        public override int OnMessagingEvent(MessagingEvent e)
        {
            short code = e.getCode();
            tsip_message_event_type_t type    = e.getType();
            MessagingSession          session = e.getSession();
            SipMessage message = e.getSipMessage();

            if (session == null && message != null)
            { /* "Server-side-session" e.g. Initial MESSAGE/INVITE sent by the remote party */
                session = e.takeSessionOwnership();
            }

            //Console.WriteLine("From:{0} == To:{1}", message.getSipHeaderValue("f"), message.getSipHeaderValue("t"));
            if (message == null)
            {
                return(0);
            }

            switch (type)
            {
            case tsip_message_event_type_t.tsip_i_message:
                byte[] content = message.getSipContent();
                if (content != null)
                {
                    Console.WriteLine("Message Content ==> {0}", Encoding.UTF8.GetString(content));
                    session.accept();

                    String contentType = message.getSipHeaderValue("c");
                    if (contentType != null && contentType.Equals("application/vnd.3gpp.sms", StringComparison.InvariantCultureIgnoreCase))
                    {
                        SMSData smsData = SMSEncoder.decode(content, (uint)content.Length, false);
                        if (smsData != null)
                        {
                            twrap_sms_type_t smsType = smsData.getType();
                            if (smsType == twrap_sms_type_t.twrap_sms_type_rpdata)
                            {
                                uint   payLength           = smsData.getPayloadLength();
                                String P_Asserted_Identity = message.getSipHeaderValue("P-Asserted-Identity");
                                if (payLength > 0)
                                {
                                    /* Send RP-ACK */
                                    String    destination = smsData.getOA();
                                    RPMessage rpACK       = SMSEncoder.encodeACK(smsData.getMR(), Program.SMSC, destination == null ? "123456789" : destination, false);
                                    if (rpACK != null)
                                    {
                                        uint pay_len = rpACK.getPayloadLength();
                                        if (pay_len > 0)
                                        {
                                            byte[] pay = new byte[pay_len];
                                            rpACK.getPayload(pay, (uint)pay.Length);

                                            //byte[] pay = Encoding.UTF8.GetBytes("\x03\x01\x41\x09\x01\x00\x01\x80\x01\x32\x42\x00\x69");

                                            MessagingSession m = new MessagingSession(Program.sipStack);
                                            m.setToUri(String.Format("sip:{0}@{1}", Program.SMSC, Program.REALM));
                                            m.addHeader("Content-Type", "application/vnd.3gpp.sms");
                                            m.addHeader("Content-Transfer-Encoding", "binary");
                                            m.send(pay, (uint)pay.Length);

                                            m.Dispose();
                                        }
                                        rpACK.Dispose();
                                    }

                                    /* Print payload */
                                    byte[] payload = new byte[payLength];
                                    smsData.getPayload(payload, (uint)payload.Length);
                                    Console.WriteLine("SMS content ={0} and OA={1}", Encoding.UTF8.GetString(payload), smsData.getOA());
                                }
                                else
                                {
                                    /* Send RP-ERROR */
                                    /* payload is mandatory in RP-DATA messages */
                                    /* Send RP-ACK */
                                    String    destination = smsData.getOA();
                                    RPMessage rpError     = SMSEncoder.encodeError(smsData.getMR(), Program.SMSC, destination == null ? "123456789" : destination, false);
                                    if (rpError != null)
                                    {
                                        uint pay_len = rpError.getPayloadLength();
                                        if (pay_len > 0)
                                        {
                                            byte[] pay = new byte[pay_len];
                                            rpError.getPayload(pay, (uint)pay.Length);

                                            MessagingSession m = new MessagingSession(Program.sipStack);
                                            m.setToUri(String.Format("sip:{0}@{1}", Program.SMSC, Program.REALM));
                                            m.addHeader("Content-Type", "application/vnd.3gpp.sms");
                                            m.addHeader("Content-Transfer-Encoding", "binary");
                                            m.send(pay, (uint)pay.Length);

                                            m.Dispose();
                                        }
                                        rpError.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    session.reject();
                }
                break;

            case tsip_message_event_type_t.tsip_ao_message:
                break;
            }

            Console.WriteLine("OnMessagingEvent() ==> {0}:{1}", code, e.getPhrase());

            return(0);
        }
예제 #2
0
            /// <summary>
            /// Messaging events
            /// </summary>
            /// <param name="e"></param>
            /// <returns></returns>
            public override int OnMessagingEvent(MessagingEvent e)
            {
                tsip_message_event_type_t type = e.getType();

                switch (type)
                {
                case tsip_message_event_type_t.tsip_ao_message:
                    break;

                case tsip_message_event_type_t.tsip_i_message:
                {
                    SipMessage       message = e.getSipMessage();
                    MessagingSession session = e.getSession();
                    uint             sessionId;

                    if (session == null)
                    {
                        /* "Server-side-session" e.g. Initial MESSAGE sent by the remote party */
                        session = e.takeSessionOwnership();
                    }

                    if (session == null)
                    {
                        LOG.Error("Failed to take session ownership");
                    }

                    if (message == null)
                    {
                        LOG.Error("Invalid message");

                        session.reject();
                        session.Dispose();
                        return(0);
                    }

                    sessionId = session.getId();
                    String from        = message.getSipHeaderValue("f");
                    String contentType = message.getSipHeaderValue("c");
                    byte[] bytes       = message.getSipContent();
                    byte[] content     = null;

                    if (bytes == null || bytes.Length == 0)
                    {
                        LOG.Error("Invalid MESSAGE");
                        session.reject();
                        session.Dispose();
                        return(0);
                    }

                    // Send 200 OK
                    session.accept();
                    session.Dispose();

                    if (String.Equals(contentType, ContentType.SMS_3GPP, StringComparison.InvariantCultureIgnoreCase))
                    {
                        /* ==== 3GPP SMSIP  === */
                        byte[]  buffer  = (bytes.Clone() as byte[]);
                        SMSData smsData = SMSEncoder.decode(buffer, (uint)buffer.Length, false);
                        if (smsData != null)
                        {
                            twrap_sms_type_t smsType = smsData.getType();
                            if (smsType == twrap_sms_type_t.twrap_sms_type_rpdata)
                            {
                                /* === We have received a RP-DATA message === */
                                long   payLength = smsData.getPayloadLength();
                                String SMSC      = message.getSipHeaderValue("P-Asserted-Identity");
                                String SMSCPhoneNumber;
                                String origPhoneNumber = smsData.getOA();

                                /* Destination address */
                                if (origPhoneNumber != null)
                                {
                                    from = UriUtils.GetValidSipUri(origPhoneNumber);
                                }
                                else if ((origPhoneNumber = UriUtils.GetValidPhoneNumber(from)) == null)
                                {
                                    LOG.Error("Invalid destination address");
                                    return(0);
                                }

                                /* SMS Center
                                 * 3GPP TS 24.341 - 5.3.2.4	Sending a delivery report
                                 * The address of the IP-SM-GW is received in the P-Asserted-Identity header in the SIP MESSAGE
                                 * request including the delivered short message.
                                 * */
                                if ((SMSCPhoneNumber = UriUtils.GetValidPhoneNumber(SMSC)) == null)
                                {
                                    SMSC = this.sipService.manager.ConfigurationService.Get(Configuration.ConfFolder.RCS, Configuration.ConfEntry.SMSC, Configuration.DEFAULT_RCS_SMSC);
                                    if ((SMSCPhoneNumber = UriUtils.GetValidPhoneNumber(SMSC)) == null)
                                    {
                                        LOG.Error("Invalid IP-SM-GW address");
                                        return(0);
                                    }
                                }

                                if (payLength > 0)
                                {
                                    /* Send RP-ACK */
                                    RPMessage rpACK = SMSEncoder.encodeACK(smsData.getMR(), SMSCPhoneNumber, origPhoneNumber, false);
                                    if (rpACK != null)
                                    {
                                        long ack_len = rpACK.getPayloadLength();
                                        if (ack_len > 0)
                                        {
                                            buffer = new byte[(int)ack_len];
                                            long len = rpACK.getPayload(buffer, (uint)buffer.Length);

                                            MessagingSession m = new MessagingSession(this.sipService.SipStack);
                                            m.setToUri(SMSC);
                                            m.addHeader("Content-Type", ContentType.SMS_3GPP);
                                            m.addHeader("Content-Transfer-Encoding", "binary");
                                            m.addCaps("+g.3gpp.smsip");
                                            m.send(buffer, (uint)len);
                                            m.Dispose();
                                        }
                                        rpACK.Dispose();
                                    }

                                    /* Get ascii content */
                                    content = new byte[(int)payLength];
                                    smsData.getPayload(content, (uint)content.Length);
                                }
                                else
                                {
                                    /* Send RP-ERROR */
                                    RPMessage rpError = SMSEncoder.encodeError(smsData.getMR(), SMSCPhoneNumber, origPhoneNumber, false);
                                    if (rpError != null)
                                    {
                                        long err_len = rpError.getPayloadLength();
                                        if (err_len > 0)
                                        {
                                            buffer = new byte[(int)err_len];
                                            long len = rpError.getPayload(buffer, (uint)buffer.Length);

                                            MessagingSession m = new MessagingSession(this.sipService.SipStack);
                                            m.setToUri(SMSC);
                                            m.addHeader("Content-Type", ContentType.SMS_3GPP);
                                            m.addHeader("Transfer-Encoding", "binary");
                                            m.addCaps("+g.3gpp.smsip");
                                            m.send(buffer, (uint)len);
                                            m.Dispose();
                                        }
                                        rpError.Dispose();
                                    }
                                }
                            }
                            else
                            {
                                /* === We have received any non-RP-DATA message === */
                                if (smsType == twrap_sms_type_t.twrap_sms_type_ack)
                                {
                                    /* Find message from the history (by MR) an update it's status */
                                    LOG.Debug("RP-ACK");
                                }
                                else if (smsType == twrap_sms_type_t.twrap_sms_type_error)
                                {
                                    /* Find message from the history (by MR) an update it's status */
                                    LOG.Debug("RP-ERROR");
                                }
                            }
                        }
                    }
                    else
                    {
                        /* ==== text/plain or any other  === */
                        content = bytes;
                    }


                    /* Alert the user a,d add the message to the history */
                    if (content != null)
                    {
                        MessagingEventArgs eargs = new MessagingEventArgs(sessionId, MessagingEventTypes.INCOMING, e.getPhrase(), content);
                        eargs
                        .AddExtra(MessagingEventArgs.EXTRA_CODE, e.getCode())
                        .AddExtra(MessagingEventArgs.EXTRA_REMOTE_PARTY, from)
                        .AddExtra(MessagingEventArgs.EXTRA_CONTENT_TYPE, contentType == null ? ContentType.UNKNOWN : contentType);
                        EventHandlerTrigger.TriggerEvent <MessagingEventArgs>(this.sipService.onMessagingEvent, this.sipService, eargs);
                    }
                    break;
                }
                }

                return(0);
            }