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

receiving CONNECT via multicast socket #142

Closed MQSie closed 5 years ago

MQSie commented 5 years ago

Hello,

I'm trying to use the gateway, but I always get the following error:

$ ./MQTT-SNGateway ... 20190109 224927.119 PahoGateway-01 has been started.

ConfigFile: ./gateway.conf PreDefFile: ./predefinedTopic.conf SensorN/W: UDP Multicast 225.1.1.1:1883 Gateway Port 10000 Broker: 127.0.0.1 : 1883, 8883 RootCApath: (null) RootCAfile: (null) CertKey: (null) PrivateKey: (null)

recved from 127.0.0.1:58514 length = 16

20190109 225340.159 CONNECT <--- GatewayTestClient 04 04 01 00 0A 6D 79 63 6C 69 65 6E 74 69 64 PacketHandleTask gets CONNECT from the client. Client Status = Disconnected 20190109 225340.160 CONNECT ===> GatewayTestClient 10 1D 00 04 4D 51 54 54 04 02 00 0A 00 11 47 61 74 65 77 61 79 54 65 73 74 43 6C 69 65 6E 74 20190109 225340.165 CONNACK <=== GatewayTestClient 20 02 00 00 PacketHandleTask gets CONNACK from the broker. 20190109 225340.166 CONNACK ---> GatewayTestClient 05 00 errno == 22 in UDPPort::sendto sendto 0.0.0.0:0 length = -1

Debugging showed, that the connect message from the client is received via the multicast socket in UDPPort::recv, and thus UDPPort::_clientAddr is not set. The gatway tries to send CONNACK via SensorNetwork::unicast to the client, but with an invalid address since UDPPort::_clientAddr was not previously set.

Sadly my linux udp/ip socket knowledge is not good enough to understand why the connect message is received via the multicast socket. It seems I'm the first to experience this problem, so I assume the problem is on my side. Did I miss to configure something in linux? I get the same result on two different systems (raspberry pi (raspbian) and a linux mint workstation).

The only change to gateway.conf is the broker address, which is located on the same machine. As client I'm using the qos1pub program.

Any help is greatly appriciated

ty4tw commented 5 years ago

Hi MQSie,

Your are using GatewayTester client. Is DHCP available in your testing environment?

MQSie commented 5 years ago

Hi ty4tw,

thanks for the answer.

Yes DHCP is available and GatewayTester is working fine:

$ ./MQTT-SNGatewayTester ... Attempting to Connect the Broker..... sendto 225.1.1.1 :1883 03 01 00 recved 172.24.4.10 :1883 03 01 00 recv SEARCHGW recved 172.24.4.10 :10000 03 02 01 recv GWINFO sendto 172.24.4.10 :10000 17 04 04 01 00 3c 47 61 74 65 77 61 79 54 65 73 74 43 6c 69 65 6e 74 send CONNECT recved 172.24.4.10 :10000 03 05 00

recv CONNACK

Connected to the Broker

Attempting OnConnect..... sendto 172.24.4.10 :10000 11 12 20 00 01 74 79 34 74 77 2f 74 6f 70 69 63 31 send SUBSCRIBE sendto 172.24.4.10 :10000 11 12 20 00 02 74 79 34 74 77 2f 74 6f 70 69 63 32 send SUBSCRIBE recved 172.24.4.10 :10000 08 13 20 00 01 00 01 00 recv SUBACK

Topic "ty4tw/topic1" Id : 1 was Subscribed.

recved 172.24.4.10 :10000 08 13 20 00 02 00 02 00 recv SUBACK Topic "ty4tw/topic2" Id : 2 was Subscribed.

OnConnect complete Test is Ready.

Step0:Subscribe predef topic1 start sendto 172.24.4.10 :10000 07 12 21 00 03 00 01 send SUBSCRIBE recved 172.24.4.10 :10000 08 13 20 00 01 00 03 00 recv SUBACK Topic "(null)" Id : 1 was Subscribed.

Step0:Subscribe predef topic1 complete

Step1:Publish topic1 start sendto 172.24.4.10 :10000 1f 0c 00 00 01 00 00 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 54 6f 70 69 63 31 22 20 0a send PUBLISH Topic "ty4tw/topic1" was Published.

recved 172.24.4.10 :10000 1f 0c 00 00 01 00 00 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 54 6f 70 69 63 31 22 20 0a recv PUBLISH

Topic1 recv. Payload -->publish "ty4tw/Topic1"

<--

Step1:Publish topic1 complete

Step2:Publish topic2 start sendto 172.24.4.10 :10000 1f 0c 20 00 02 00 04 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a send PUBLISH recved 172.24.4.10 :10000 07 0d 00 02 00 04 00 recv PUBACK Topic "ty4tw/topic2" Id : 2 was Published.

Step2:Publish topic2 complete

recved 172.24.4.10 :10000 1f 0c 20 00 02 00 01 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a recv PUBLISH sendto 172.24.4.10 :10000 07 0d 00 02 00 01 00 send PUBACK

Topic2 recv. Payload -->publish "ty4tw/topic2"  <--

Step3:Subscribe topic2 start sendto 172.24.4.10 :10000 07 12 21 00 05 00 0a send SUBSCRIBE recved 172.24.4.10 :10000 08 13 20 00 0a 00 05 03 recv SUBACK

SUBACK Invalid messageId. (null)

Step3:Subscribe topic2 complete

Step4:Publish topic2 start sendto 172.24.4.10 :10000 1f 0c 20 00 02 00 06 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a send PUBLISH recved 172.24.4.10 :10000 07 0d 00 02 00 06 00 recv PUBACK Topic "ty4tw/topic2" Id : 2 was Published.

Step4:Publish topic2 complete

recved 172.24.4.10 :10000 1f 0c 20 00 02 00 02 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a recv PUBLISH sendto 172.24.4.10 :10000 07 0d 00 02 00 02 00 send PUBACK

Topic2 recv. Payload -->publish "ty4tw/topic2"  <--

Step5:Unsubscribe topic2 start sendto 172.24.4.10 :10000 11 14 00 00 07 74 79 34 74 77 2f 74 6f 70 69 63 32 send UNSUBSCRIBE recved 172.24.4.10 :10000 04 15 00 07 recv UNSUBACK Topic "ty4tw/topic2" was Unsubscribed.

Step5:Unsubscribe topic2 complete

Step6:Publish topic2 start sendto 172.24.4.10 :10000 1f 0c 20 00 02 00 08 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a send PUBLISH recved 172.24.4.10 :10000 07 0d 00 02 00 08 00 recv PUBACK Topic "ty4tw/topic2" Id : 2 was Published.

Step6:Publish topic2 complete

recved 172.24.4.10 :10000 1f 0c 20 00 02 00 03 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a recv PUBLISH sendto 172.24.4.10 :10000 07 0d 00 02 00 03 00 send PUBACK

Topic2 recv.

Payload -->publish "ty4tw/topic2"  <--

Step7:subscribe again start sendto 172.24.4.10 :10000 11 12 20 00 09 74 79 34 74 77 2f 74 6f 70 69 63 32 send SUBSCRIBE recved 172.24.4.10 :10000 08 13 20 00 02 00 09 00 recv SUBACK Topic "ty4tw/topic2" Id : 2 was Subscribed.

Step7:subscribe again complete

Step8:Publish topic2 start sendto 172.24.4.10 :10000 1f 0c 20 00 02 00 0a 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a send PUBLISH recved 172.24.4.10 :10000 07 0d 00 02 00 0a 00 recv PUBACK Topic "ty4tw/topic2" Id : 2 was Published.

Step8:Publish topic2 complete

recved 172.24.4.10 :10000 1f 0c 20 00 02 00 04 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 74 6f 70 69 63 32 22 20 0a recv PUBLISH sendto 172.24.4.10 :10000 07 0d 00 02 00 04 00 send PUBACK

Topic2 recv. Payload -->publish "ty4tw/topic2"  <--

Step9:Sleep start sendto 172.24.4.10 :10000 04 18 01 2c send DISCONNECT recved 172.24.4.10 :10000 02 18 recv DISCONNECT

Get into SLEEP mode 59246 [msec].

Step9:Sleep complete

Step10:Publish topic1 start sendto 172.24.4.10 :10000 17 04 04 01 00 3c 47 61 74 65 77 61 79 54 65 73 74 43 6c 69 65 6e 74 send CONNECT recved 172.24.4.10 :10000 03 05 00 recv CONNACK sendto 172.24.4.10 :10000 1f 0c 00 00 01 00 00 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 54 6f 70 69 63 31 22 20 0a send PUBLISH Topic "ty4tw/topic1" was Published.

recved 172.24.4.10 :10000 1f 0c 00 00 01 00 00 70 75 62 6c 69 73 68 20 22 74 79 34 74 77 2f 54 6f 70 69 63 31 22 20 0a recv PUBLISH

Topic1 recv. Payload -->publish "ty4tw/Topic1" <--

Step10:Publish topic1 complete

Step11:Disconnect start sendto 172.24.4.10 :10000 02 18 send DISCONNECT recved 172.24.4.10 :10000 02 18 recv DISCONNECT

Step11:Disconnect complete

######### All tests complete! ###########

qos1pub is not sending SEARCHGW prior CONNECT. Is this the reason, why it is not working?

ty4tw commented 5 years ago

Client can get the IP address and PortNo of the gateway from the GWINFO. In case of the qos1pub, it is necessary to correctly set the gateway IP address and PORT in the host and port variables. where, the IP address is not an address for Multicast but an address for unicast, that is, IP address of the gateway.

int main(int argc, char** argv)
{
    int rc = 0;
    int mysock;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    MQTTSN_topicid topic;
    unsigned char* payload = (unsigned char*)"mypayload";
    int payloadlen = strlen((char*)payload);
    int len = 0;
    int dup = 0;
    int qos = 1;
    int retained = 0;
    short packetid = 1;
    char *host = "127.0.0.1";
    int port = 1883;
    MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

char *host = "127.0.0.1"; <====== Gateway's IP address

int port = 1883; <======== Gateway's Port

MQSie commented 5 years ago

I previously executed qos1pub on the same machine where the gateway was running. Thus "127.0.0.1" should be fine.

Nevertheless, your comment inspired me to use the multicast address for host/port in qos1pub:

char *host = "225.1.1.1";
int port = 10000;

This setup is surprisingly working. Debugging showed, that the gateway receives the messages on the UNICAST socket.

As it stands right now, the gateway receives messages send to UNICAST addresses on the MULTICAST socket, and messages send to MULTICAST addresses on the UNICAST socket.

MQSie commented 5 years ago

I found the error!

The UDP section in the default gateway.conf is wrong:

# UDP GatewayPortNo=10000 MulticastIP=225.1.1.1 MulticastPortNo=1883

but it should be:

# UDP GatewayPortNo=1883 MulticastIP=225.1.1.1 MulticastPortNo=10000

I advise to change the default gateway.conf file, such that future users don't encounter the same problem.

ty4tw commented 5 years ago

I understood what you are doing now.

Broker's port and Multicast Port is a same in your gateway.conf. Try to change the Multicast port from 1883 to any thing else. or change the Borker to iot.eclipse.org:1883

20190109 224927.119 PahoGateway-01 has been started.

ConfigFile: ./gateway.conf
PreDefFile: ./predefinedTopic.conf
SensorN/W: UDP Multicast 225.1.1.1:1883 Gateway Port 10000
Broker: 127.0.0.1 : 1883, 8883
RootCApath: (null)
RootCAfile: (null)
CertKey: (null)
MQSie commented 5 years ago

Yes, it was a port mismatch. Thanks for your help!