ajankovic / smpp

Go library for SMPP 3.4
MIT License
24 stars 21 forks source link

How to handle session shutdown in invalid pdu case?? #5

Open vanipolnedi opened 4 years ago

vanipolnedi commented 4 years ago

Hi, when a message with invalid PDU is submitted, session is shutting down but we want resp,err to be returned without session shutdown. How to handle it??

Thanks in advance

ajankovic commented 4 years ago

Could you provide more details about this problem? What PDU are you sending that comes out as invalid?

When you use sess.Send() there is no logic that would trigger session shutdown. This can only happen if the receiving side decided to close it. For example they can send unbind response or they closed the tcp connection due to failure on their side. So your PDU could be valid but they could crash because of it and session will be closed because tcp was broken. We can't return resp, err in that situation.

vanipolnedi commented 4 years ago

we are getting Invalid PDU error while decode in serve function (below code in session.go):

`
h, p, err := sess.dec.Decode()

if err != nil {
    if err == io.EOF {
        sess.conf.Logger.InfoF("decoding pdu: %s %+v", sess, err)
    } else {
        sess.conf.Logger.ErrorF("decoding pdu: %s %+v", sess, err)
    }
    sess.shutdown()
    return
}`

Internal logs : 2020/02/08 18:58:07 INFO: received request: (SMSC:test:39362819-31AB-8C20) BindTransmitterID&{SystemID:test Password:test SystemType:SMPP InterfaceVersion:52 AddrTon:1 AddrNpi:1 AddressRange:} 2020/02/08 18:58:12 INFO: sent response: (SMSC:test:39362819-31AB-8C20) BindTransmitterRespID &{SystemID:test Options:} 2020/02/08 18:58:25 ERRO: decoding pdu: (SMSC:test:39362819-31AB-8C20) smpp: pdu length over upper limit 2020/02/08 18:58:25 INFO: session closed: (SMSC:test:39362819-31AB-8C20)

ajankovic commented 4 years ago

Decoding errors are caused by malformed byte sequence. If bytes are malformed in one decoding loop continuing session means that you will be dealing with unpredictable input from that point forward.

For example the error that you are getting is telling you that PDU header is set to the length that is considered to be too long and this behaviour is invalid according to the SMPP specs. Decoding errors are unrecoverable because we no longer can be sure what we are reading from the wire so we have to restart the session. Otherwise we will just be going in the loop and decode invalid PDUs and it will turn into an uncontrollable mess.

There are recoverable errors, like PDUs received in invalid session state, which doesn't result in closed session.

If the sender is not behaving properly you have to solve this with them. If you are sure they are sending proper PDU then please provide byte sequence so we can debug potential problems with decoding. But I don't think this is the reason to change the behaviour of the session.

vanipolnedi commented 4 years ago

If client sends invalid PDU, is it possible to notify that client has sent invalid PDU (like sending generic_nack PDU to client ) and then close session ??