eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
315 stars 178 forks source link

Unable to connect to AWS IoT broker #163

Closed ghiasum closed 5 years ago

ghiasum commented 5 years ago

Hello. I am unable to connect to aws mqtt broker, keep getting connection timed out.

0190820 201655.465 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74 Error: BrokerSendTask: mg-client can't connect to the broker. errno=110 Connection timed out

gateway.conf

BrokerName=....iot.us-east-1.amazonaws.com BrokerPortNo=1883 BrokerSecurePortNo=8883

ClientAuthentication=NO AggregatingGateway=NO QoS-1=NO Forwarder=NO

PredefinedTopic=NO

RootCAfile=./aws_cafile.crt RootCApath=./ CertKey=./aws_cert.crt PrivateKey=./aws_private_key.pem

GatewayID=1 GatewayName=PahoGateway-01 KeepAlive=900

LoginID=test

Password=test

GatewayPortNo=1884 MulticastIP=225.1.1.1 MulticastPortNo=1883

GatewayUDP6Bind=FFFF:FFFE::1 GatewayUDP6Port=1884 GatewayUDP6Broadcast=FF02::1 GatewayUDP6If=wpan0

ShearedMemory=NO;

ty4tw commented 5 years ago

Hi Muhammad,

I think AWS IoT requires secure connection.

RGDs. Tomoaki YAMAGUCHI

2019年8月21日(水) 5:26 Muhammad Ghias notifications@github.com:

Hello. I am unable to connect to aws mqtt broker, keep getting connection timed out.

0190820 201655.465 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74 Error: BrokerSendTask: mg-client can't connect to the broker. errno=110 Connection timed out

gateway.conf

BrokerName=....iot.us-east-1.amazonaws.com BrokerPortNo=1883 BrokerSecurePortNo=8883

ClientAuthentication=NO AggregatingGateway=NO QoS-1=NO Forwarder=NO

PredefinedTopic=NO

RootCAfile=./aws_cafile.crt RootCApath=./ CertKey=./aws_cert.crt PrivateKey=./aws_private_key.pem

GatewayID=1 GatewayName=PahoGateway-01 KeepAlive=900

LoginID=test

Password=test

UDP

GatewayPortNo=1884 MulticastIP=225.1.1.1 MulticastPortNo=1883 UDP6

GatewayUDP6Bind=FFFF:FFFE::1 GatewayUDP6Port=1884 GatewayUDP6Broadcast=FF02::1 GatewayUDP6If=wpan0 LOG

ShearedMemory=NO;

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt-sn.embedded-c/issues/163?email_source=notifications&email_token=AB3X2BCULKZLQGS7BVU5ARLQFRHODA5CNFSM4IN4ONJ2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HGK7ZHQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AB3X2BF3P3B67A5FHAMSBSLQFRHODANCNFSM4IN4ONJQ .

ty4tw commented 5 years ago

You have to set the client List config file to enable the secure connection.

ghiasum commented 5 years ago

Hi Tomoaki, after adding client to client List I am now getting the following errors

Error: BrokerSendTask: PahoGateway-01 can't connect to the broker. errno=110 Connection timed out SSL_connect() error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number Error: BrokerSendTask: PahoGateway-01-S can't connect to the broker. errno=0 Success

Any thoughts on how to resolve this? My openssl version is 1.0.1f

ty4tw commented 5 years ago

Hi,

You are still using dangerous version of OpenSSL. Upgrade OpenSSL to 1.1.1, then you can connect.

Tomoaki YAMAGUCHI

2019年8月22日(木) 4:03 Muhammad Ghias notifications@github.com:

Hi ty4tw, after adding client to client List I am now getting the following errors

Error: BrokerSendTask: PahoGateway-01 can't connect to the broker. errno=110 Connection timed out SSL_connect() error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number Error: BrokerSendTask: PahoGateway-01-S can't connect to the broker. errno=0 Success

Any thoughts on how to resolve this? My openssl version is 1.0.1f

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt-sn.embedded-c/issues/163?email_source=notifications&email_token=AB3X2BCR35HUDYDH22OGZK3QFWGPVA5CNFSM4IN4ONJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD42ZLUI#issuecomment-523605457, or mute the thread https://github.com/notifications/unsubscribe-auth/AB3X2BAZYG2RKYYK5VMOWPDQFWGPVANCNFSM4IN4ONJQ .

ghiasum commented 5 years ago

Hi Tomoaki, thanks for the help. I was able to get past that error using OpenSSL 1.1.1. Now i am encountering a different issue. I am testing with the mqtt-sn tools found here https://github.com/njh/mqtt-sn-tools . I have a set a sleep delay for client of 3600s before disconnect. The first time i publish a message it makes its way to the aws broker. After about 30 seconds or so the gateway outputs "Error: BrokerRecvTask can't receive a packet from the broker errno=0 mg-client" After this anything i publish does not make its way to the broker. Restarting the gateway allows me to publish one more message and then i have the same issue. Any thoughts on how to fix this?

20190822 204519.225 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74 PacketHandleTask gets CONNECT from the client. Client Status = Disconnected 20190822 204519.252 CONNECT ===> mg-client 10 15 00 04 4D 51 54 54 04 02 00 0A 00 09 6D 67 2D 63 6C 69 65 6E 74 20190822 204519.579 CONNACK <=== mg-client 20 02 00 00 PacketHandleTask gets CONNACK from the broker. 20190822 204519.579 CONNACK ---> mg-client 03 05 00 sendto 209.20.46.26:6148 length = 3 recved from 209.20.46.26:6148 length = 41

20190822 204519.632 REGISTER 0001 <--- mg-client 29 0A 00 00 00 01 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C PacketHandleTask gets REGISTER 0001 from the client. Client Status = Active 20190822 204519.632 REGACK 0001 ---> mg-client 07 0B 00 01 00 01 00 sendto 209.20.46.26:6148 length = 7 recved from 209.20.46.26:6148 length = 9

20190822 204519.684 PUBLISH 0002 <--- mg-client 09 0C 00 00 01 00 02 36 30 PacketHandleTask gets PUBLISH 0002 from the client. Client Status = Active 20190822 204519.684 PUBLISH ===> mg-client 30 27 00 23 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C 36 30 recved from 209.20.46.26:6148 length = 4 20190822 204519.684 DISCONNECT <--- mg-client 04 18 0E 10 PacketHandleTask gets DISCONNECT from the client. Client Status = Asleep 20190822 204519.684 DISCONNECT ---> mg-client 02 18 sendto 209.20.46.26:6148 length = 2 Error: BrokerRecvTask can't receive a packet from the broker errno=0 mg-client recved from 209.20.46.26:6800 length = 15

20190822 204543.469 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74 PacketHandleTask gets CONNECT from the client. Client Status = Active 20190822 204543.469 CONNACK ---> mg-client 03 05 00 sendto 209.20.46.26:6800 length = 3 recved from 209.20.46.26:6800 length = 41

20190822 204543.522 REGISTER 0001 <--- mg-client 29 0A 00 00 00 01 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C PacketHandleTask gets REGISTER 0001 from the client. Client Status = Active 20190822 204543.523 REGACK 0001 ---> mg-client 07 0B 00 01 00 01 00 sendto 209.20.46.26:6800 length = 7 recved from 209.20.46.26:6800 length = 9

20190822 204543.576 PUBLISH 0002 <--- mg-client 09 0C 00 00 01 00 02 36 30 PacketHandleTask gets PUBLISH 0002 from the client. Client Status = Active recved from 209.20.46.26:6800 length = 4 20190822 204543.576 DISCONNECT <--- mg-client 04 18 0E 10 PacketHandleTask gets DISCONNECT from the client. Client Status = Asleep 20190822 204543.576 DISCONNECT ---> mg-client 02 18 sendto 209.20.46.26:6800 length = 2 20190822 204543.584 PUBLISH ===> mg-client 30 27 00 23 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C 36 30 Error: BrokerRecvTask can't receive a packet from the broker errno=0 mg-client recved from 209.20.46.26:6735 length = 15

20190822 204547.973 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74 PacketHandleTask gets CONNECT from the client. Client Status = Active 20190822 204547.973 CONNACK ---> mg-client 03 05 00 sendto 209.20.46.26:6735 length = 3 recved from 209.20.46.26:6735 length = 41

20190822 204548.014 REGISTER 0001 <--- mg-client 29 0A 00 00 00 01 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C PacketHandleTask gets REGISTER 0001 from the client. Client Status = Active 20190822 204548.014 REGACK 0001 ---> mg-client 07 0B 00 01 00 01 00 sendto 209.20.46.26:6735 length = 7 recved from 209.20.46.26:6735 length = 9

20190822 204548.054 PUBLISH 0002 <--- mg-client 09 0C 00 00 01 00 02 36 30 PacketHandleTask gets PUBLISH 0002 from the client. Client Status = Active recved from 209.20.46.26:6735 length = 4 20190822 204548.054 DISCONNECT <--- mg-client 04 18 0E 10 PacketHandleTask gets DISCONNECT from the client. Client Status = Asleep 20190822 204548.054 DISCONNECT ---> mg-client 02 18 sendto 209.20.46.26:6735 length = 2 20190822 204548.062 PUBLISH ===> mg-client 30 27 00 23 62 6C 75 65 6D 6F 62 69 6C 65 2F 31 32 33 34 31 32 33 34 2F 63 6F 6E 66 69 67 2F 69 6E 74 65 72 76 61 6C 36 30 Error: BrokerRecvTask can't receive a packet from the broker errno=0 mg-client

ty4tw commented 5 years ago

Hi, Thank you for your contribution. If you don't DISCONNECT for sleep, how's going on? Is every thing OK or still you get the same error?

ty4tw commented 5 years ago

I found the client sent invalid packet. QoS0 PUBLISH packet has MsgId 0x0002.
MsgId of PUBLISH QoS0 sholud be 0x0000. (specification page 14)

20190822 204543.576 PUBLISH 0002 <--- mg-client 09 0C 00 00 01 00 02 36 30

PacketHandleTask gets PUBLISH 0002 from the client.

Could you check the following code to reject the invalid packet.

file MQTTSNGWPublishHandler.cpp line 91

        if( !topic && qos == 3 )
        {
            WRITELOG("%s Invalid TopicId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
            return nullptr;
        }

Insert following 6 lines to line 97

        if ( ( qos == 0 || qos == 3 ) && msgId > 0 )
        {
            WRITELOG("%s Invalid MsgId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
            return nullptr;
        }
        if( !topic && msgId && qos > 0 && qos < 3 )
        {
            /* Reply PubAck with INVALID_TOPIC_ID to the client */
            MQTTSNPacket* pubAck = new MQTTSNPacket();
            pubAck->setPUBACK( topicid.data.id, msgId, MQTTSN_RC_REJECTED_INVALID_TOPIC_ID);
            Event* ev1 = new Event();
            ev1->setClientSendEvent(client, pubAck);
            _gateway->getClientSendQue()->post(ev1);
            return nullptr;
        }
        if ( topic )
        {
            pub.topic = (char*)topic->getTopicName()->data();
            pub.topiclen = topic->getTopicName()->length();
        }
ghiasum commented 5 years ago

Yes that seems to be an error in the packet, the msg id should be zero. Your code for works to reject the invalid packet. Sending a packet with msg id zero does not solve the original problem though.

After some more investigation I believe what is happening is that after sn client disconnects with sleep the gateway still gets a keep alive packet from the broker. Since the broker doesn't receive a response to this it disconnects the client.

Next time around when the MQTT-SN client connects again the gateway does not send connect message to broker and tries to publish but since the broker has already disconnected the client the communication doesn't work anymore. Hope that makes sense.

ty4tw commented 5 years ago

Hi,

thank you for your investigation. It helps me a lot.

20190822 204519.225 CONNECT <--- mg-client 0F 04 04 01 00 0A 6D 67 2D 63 6C 69 65 6E 74

This client sets it's Keep Alive duration to 10 secs by CONNECT. Therefore it must send PINGREQ or any message with in every 10 secs. other wise Broker disconnects the client. In your case, the client can't send PINGREQ with in 10 secs because it is sleeping.

To avoid this, Keep Alive Duration must be setten longer than sleep interval.

ghiasum commented 5 years ago

OK, i thought it was the broker that was responsible for keeping the connection alive but i see in the spec that it is the client. Setting the keep alive duration longer than sleep interval seems to work. Thanks for you help!