Giter VIP home page Giter VIP logo

Comments (6)

ty4tw avatar ty4tw commented on July 24, 2024

Thank you, I forget to add code (//NOP should be ToDo). The list is used when clients return REGACK.
I will fix this.

void PacketHandleTask::run()
{
case MQTTSN_REGISTER:
_mqttsnPublish->handleRegister(client, snPacket);
break;
case MQTTSN_REGACK:
// NOP
break;

from paho.mqtt-sn.embedded-c.

ty4tw avatar ty4tw commented on July 24, 2024

Try develop branch.

from paho.mqtt-sn.embedded-c.

jue89 avatar jue89 commented on July 24, 2024

Wow, that was fast! Nice! Thank you a lot for your quick response and patch!

But I still have issues with double-frees. They make the program cashing. I'm using the UDP6 sensor net driver. Can you reproduce this?

*** Error in `/home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway': double free or corruption (out): 0x00007fbcbc000a20 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fbccac127e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fbccac1b37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fbccac1f53c]
/home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway[0x405ee3]
/home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway[0x40c0be]
/home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway[0x40c209]
/home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway[0x414aae]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7fbccbdd26ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fbccaca23dd]
======= Memory map: ========
<-SNIP->
 ***************************************************************************
 * MQTT-SN Transparent Gateway
 * Part of Project Paho in Eclipse
 * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)
 *
 * Author : Tomoaki YAMAGUCHI
 * Version: 1.0.0
 ***************************************************************************

20171220 152626.733 PahoGateway-01 has been started.

 ConfigFile: /tmp/paho-i4sPL1/gateway.conf
 SensorN/W:  GatewayUDP6Bind: ::1 Gateway Port: 12345 Broadcast Address: ::1 Interface: lo
 Broker:     localhost : 18883, (null)
 RootCApath: (null)
 RootCAfile: (null)
 CertKey:    (null)
 PrivateKey: (null)

20171220 152627.879   CONNECT           <---  test-client                         04 04 01 07 08 74 65 73 74 2D 63 6C 69 65 6E 74
20171220 152627.880   CONNECT           --->  test-client                         10 17 00 04 4D 51 54 54 04 02 07 08 00 0B 74 65 73 74 2D 63 6C 69 65 6E 74
20171220 152628.233   CONNACK           <---  test-client                         20 02 00 00
20171220 152628.233   CONNACK           --->  test-client                         05 00
unicast sendto ::1%lo, port: 41234 length = 3

20171220 152628.237   SUBSCRIBE   002B  <---  test-client                         12 20 00 2B 74 65 73 74 2F 2B
20171220 152628.238   SUBSCRIBE   002B  --->  test-client                         82 0B 00 2B 00 06 74 65 73 74 2F 2B 01
20171220 152628.243   SUBACK      002B  <---  test-client                         90 03 00 2B 01
20171220 152628.243   SUBACK      002B  --->  test-client                         13 20 00 01 00 2B 00
unicast sendto ::1%lo, port: 41234 length = 8

20171220 152628.246   PUBLISH     0000  <---  test-client                         30 10 00 08 74 65 73 74 2F 73 75 62 66 6F 6F 62 61 72
20171220 152628.246   REGISTER    0001  --->  test-client                         0A 00 02 00 01 74 65 73 74 2F 73 75 62
unicast sendto ::1%lo, port: 41234 length = 14
20171220 152628.246   PUBLISH     0000  --->  test-client                         0C 00 00 02 00 00 66 6F 6F 62 61 72
unicast sendto ::1%lo, port: 41234 length = 13
20171220 152628.247   REGACK      0001  <---  test-client                         0B 00 02 00 01 00
20171220 152628.247   ADVERTISE         --->  Clients                             00

After getting this error i tried using valgrind. Now REGISTER seems to cause segmentation faults.

==25977== Thread 2:
==25977== Conditional jump or move depends on uninitialised value(s)
==25977==    at 0x425ED6: MQTTSNSerialize_register (in /home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway)
==25977==    by 0x40D4B6: MQTTSNGW::MQTTSNPacket::setREGISTER(unsigned short, unsigned short, MQTTSNString*) (MQTTSNGWPacket.cpp:168)
==25977==    by 0x4051AF: MQTTSNGW::MQTTGWPublishHandler::handlePublish(MQTTSNGW::Client*, MQTTSNGW::MQTTGWPacket*) (MQTTGWPublishHandler.cpp:133)
==25977==    by 0x40EB3C: MQTTSNGW::PacketHandleTask::run() (MQTTSNGWPacketHandleTask.cpp:202)
==25977==    by 0x40EBE8: MQTTSNGW::PacketHandleTask::EXECRUN() (MQTTSNGWPacketHandleTask.h:45)
==25977==    by 0x414AAD: MQTTSNGW::Thread::_run(void*) (Threading.cpp:487)
==25977==    by 0x4E416B9: start_thread (pthread_create.c:333)
==25977==    by 0x5FAB3DC: clone (clone.S:109)
==25977==
==25977== Use of uninitialised value of size 8
==25977==    at 0x4C30F62: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25977==    by 0x425EE0: MQTTSNSerialize_register (in /home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway)
==25977==    by 0x40D4B6: MQTTSNGW::MQTTSNPacket::setREGISTER(unsigned short, unsigned short, MQTTSNString*) (MQTTSNGWPacket.cpp:168)
==25977==    by 0x4051AF: MQTTSNGW::MQTTGWPublishHandler::handlePublish(MQTTSNGW::Client*, MQTTSNGW::MQTTGWPacket*) (MQTTGWPublishHandler.cpp:133)
==25977==    by 0x40EB3C: MQTTSNGW::PacketHandleTask::run() (MQTTSNGWPacketHandleTask.cpp:202)
==25977==    by 0x40EBE8: MQTTSNGW::PacketHandleTask::EXECRUN() (MQTTSNGWPacketHandleTask.h:45)
==25977==    by 0x414AAD: MQTTSNGW::Thread::_run(void*) (Threading.cpp:487)
==25977==    by 0x4E416B9: start_thread (pthread_create.c:333)
==25977==    by 0x5FAB3DC: clone (clone.S:109)
==25977==
==25977== Invalid read of size 1
==25977==    at 0x4C30F62: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25977==    by 0x425EE0: MQTTSNSerialize_register (in /home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway)
==25977==    by 0x40D4B6: MQTTSNGW::MQTTSNPacket::setREGISTER(unsigned short, unsigned short, MQTTSNString*) (MQTTSNGWPacket.cpp:168)
==25977==    by 0x4051AF: MQTTSNGW::MQTTGWPublishHandler::handlePublish(MQTTSNGW::Client*, MQTTSNGW::MQTTGWPacket*) (MQTTGWPublishHandler.cpp:133)
==25977==    by 0x40EB3C: MQTTSNGW::PacketHandleTask::run() (MQTTSNGWPacketHandleTask.cpp:202)
==25977==    by 0x40EBE8: MQTTSNGW::PacketHandleTask::EXECRUN() (MQTTSNGWPacketHandleTask.h:45)
==25977==    by 0x414AAD: MQTTSNGW::Thread::_run(void*) (Threading.cpp:487)
==25977==    by 0x4E416B9: start_thread (pthread_create.c:333)
==25977==    by 0x5FAB3DC: clone (clone.S:109)
==25977==  Address 0x1103 is not stack'd, malloc'd or (recently) free'd
==25977==
==25977==
==25977== Process terminating with default action of signal 11 (SIGSEGV)
==25977==  Access not within mapped region at address 0x1103
==25977==    at 0x4C30F62: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25977==    by 0x425EE0: MQTTSNSerialize_register (in /home/jue/paho.mqtt-sn.embedded-c/MQTTSNGateway/Build/MQTT-SNGateway)
==25977==    by 0x40D4B6: MQTTSNGW::MQTTSNPacket::setREGISTER(unsigned short, unsigned short, MQTTSNString*) (MQTTSNGWPacket.cpp:168)
==25977==    by 0x4051AF: MQTTSNGW::MQTTGWPublishHandler::handlePublish(MQTTSNGW::Client*, MQTTSNGW::MQTTGWPacket*) (MQTTGWPublishHandler.cpp:133)
==25977==    by 0x40EB3C: MQTTSNGW::PacketHandleTask::run() (MQTTSNGWPacketHandleTask.cpp:202)
==25977==    by 0x40EBE8: MQTTSNGW::PacketHandleTask::EXECRUN() (MQTTSNGWPacketHandleTask.h:45)
==25977==    by 0x414AAD: MQTTSNGW::Thread::_run(void*) (Threading.cpp:487)
==25977==    by 0x4E416B9: start_thread (pthread_create.c:333)
==25977==    by 0x5FAB3DC: clone (clone.S:109)
==25977==  If you believe this happened as a result of a stack
==25977==  overflow in your program's main thread (unlikely but
==25977==  possible), you can try to increase the size of the
==25977==  main thread stack using the --main-stacksize= flag.
==25977==  The main thread stack size used in this run was 8388608.

 ***************************************************************************
 * MQTT-SN Transparent Gateway
 * Part of Project Paho in Eclipse
 * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)
 *
 * Author : Tomoaki YAMAGUCHI
 * Version: 1.0.0
 ***************************************************************************

20171220 151841.615 PahoGateway-01 has been started.

 ConfigFile: /tmp/paho-Zrcllw/gateway.conf
 SensorN/W:  GatewayUDP6Bind: ::1 Gateway Port: 12345 Broadcast Address: ::1 Interface: lo
 Broker:     localhost : 18883, (null)
 RootCApath: (null)
 RootCAfile: (null)
 CertKey:    (null)
 PrivateKey: (null)

20171220 151842.072   CONNECT           <---  test-client                         04 04 01 07 08 74 65 73 74 2D 63 6C 69 65 6E 74
20171220 151842.192   CONNECT           --->  test-client                         10 17 00 04 4D 51 54 54 04 02 07 08 00 0B 74 65 73 74 2D 63 6C 69 65 6E 74
20171220 151842.682   CONNACK           <---  test-client                         20 02 00 00
20171220 151842.694   CONNACK           --->  test-client                         05 00
unicast sendto ::1%lo, port: 41234 length = 3

20171220 151842.719   SUBSCRIBE   002B  <---  test-client                         12 20 00 2B 74 65 73 74 2F 2B
20171220 151842.734   SUBSCRIBE   002B  --->  test-client                         82 0B 00 2B 00 06 74 65 73 74 2F 2B 01
20171220 151842.735   SUBACK      002B  <---  test-client                         90 03 00 2B 01
20171220 151842.741   SUBACK      002B  --->  test-client                         13 20 00 01 00 2B 00
unicast sendto ::1%lo, port: 41234 length = 8

20171220 151842.744   PUBLISH     0000  <---  test-client                         30 10 00 08 74 65 73 74 2F 73 75 62 66 6F 6F 62 61 72
==25977==
==25977== HEAP SUMMARY:
==25977==     in use at exit: 96,523 bytes in 44 blocks
==25977==   total heap usage: 236 allocs, 192 frees, 230,013 bytes allocated
==25977==
==25977== LEAK SUMMARY:
==25977==    definitely lost: 152 bytes in 2 blocks
==25977==    indirectly lost: 0 bytes in 0 blocks
==25977==      possibly lost: 1,440 bytes in 5 blocks
==25977==    still reachable: 94,931 bytes in 37 blocks
==25977==         suppressed: 0 bytes in 0 blocks
==25977== Rerun with --leak-check=full to see details of leaked memory
==25977==
==25977== For counts of detected and suppressed errors, rerun with: -v
==25977== Use --track-origins=yes to see where uninitialised values come from
==25977== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

Furthermore, as you can see in the first log output, the PUBLISH is fired right after the REGISTER packet. Shouldn't we wait for the REGACK?

I think this portion of code should be altered:

/* send REGISTER */
Event* evrg = new Event();
evrg->setClientSendEvent(client, regPacket);
_gateway->getClientSendQue()->post(evrg);
/* send PUBLISH */
topicId.data.id = id;
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos,
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload,
pub.payloadlen);
client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId);

For testing I am generating packets using https://github.com/ithinuel/mqttsn-packet with a tiny wrapper around it doing UDP stuff and asserting the received frames from MQTTSNGateway:

const SUBTOPIC = 'test/+';
const MSGTOPIC = 'test/sub';
const MSG = 'foobar';

// Connect
mqttsn.send({
	cmd: 'connect',
	will: false,
	cleanSession: true,
	duration: 1800,
	clientId: 'test-client'
});
await mqttsn.recv({
	cmd: 'connack',
	returnCode: 'Accepted'
});

// Subscribe to topic
mqttsn.send({
	cmd: 'subscribe',
	dup: false,
	qos: 1,
	msgId: 43,
	topicIdType: 'normal',
	topicName: SUBTOPIC
});
await mqttsn.recv({
	cmd: 'suback',
	msgId: 43,
	qos: 1,
	returnCode: 'Accepted'
});

// Send message
mqtt.publish(MSGTOPIC, MSG);
const register = await mqttsn.recv({
	cmd: 'register',
	topicName: MSGTOPIC
});
mqttsn.send({
	cmd: 'regack',
	returnCode: 'Accepted',
	msgId: register.msgId,
	topicId: register.topicId
});
await mqttsn.recv({
	cmd: 'publish',
	dup: false,
	qos: 0,
	retain: false,
	topicIdType: 'normal',
	topicId: register.topicId,
	payload: MSG
});

Sry for the huge amount of information, logs and code. If you need further details, please contact me!

from paho.mqtt-sn.embedded-c.

ty4tw avatar ty4tw commented on July 24, 2024

Hi, thx a lot.
You are right.
SUBSCRIBE jesj/+ ----> GW
SUBACK 0001 <------- GW

PUBLISH test/suRf <------ GW
REGISTER test/sub <---- GW
REGACK ----> GW
PUBLISH <------ GW

a flow should be above. purpose of the WaitRegAck list is waiting REGACK.

Could you try this code,

/* send PUBLISH /
topicId.data.id = id;
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos,
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t
) pub.payload,
pub.payloadlen);
client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId);
return; <============= Insert this
}

I have one question. why test/sub topic is sent from the broker?
the client doesn't SUBSCRIBE it.

from paho.mqtt-sn.embedded-c.

jue89 avatar jue89 commented on July 24, 2024

Hi!

Could you try this code,

/* send PUBLISH /
topicId.data.id = id;
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos,
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t) pub.payload,
pub.payloadlen);
client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId);
return; <============= Insert this
}

Nice! That fixed the order of the packets. The gateway now waits for the REGACK. Debug output:

20171221 131221.436   CONNECT           <---  test-client                         04 04 01 07 08 74 65 73 74 2D 63 6C 69 65 6E 74
20171221 131221.436   CONNECT           --->  test-client                         10 17 00 04 4D 51 54 54 04 02 07 08 00 0B 74 65 73 74 2D 63 6C 69 65 6E 74
20171221 131221.775   CONNACK           <---  test-client                         20 02 00 00
20171221 131221.775   CONNACK           --->  test-client                         05 00
unicast sendto ::1%lo, port: 41234 length = 3

20171221 131221.779   SUBSCRIBE   002B  <---  test-client                         12 20 00 2B 74 65 73 74 2F 2B
20171221 131221.779   SUBSCRIBE   002B  --->  test-client                         82 0B 00 2B 00 06 74 65 73 74 2F 2B 01
20171221 131221.785   SUBACK      002B  <---  test-client                         90 03 00 2B 01
20171221 131221.785   SUBACK      002B  --->  test-client                         13 20 00 01 00 2B 00
unicast sendto ::1%lo, port: 41234 length = 8

20171221 131221.789   PUBLISH     0000  <---  test-client                         30 10 00 08 74 65 73 74 2F 73 75 62 66 6F 6F 62 61 72
20171221 131221.789   REGISTER    0001  --->  test-client                         0A 00 02 00 01 74 65 73 74 2F 73 75 62
unicast sendto ::1%lo, port: 41234 length = 14
20171221 131221.790   REGACK      0001  <---  test-client                         0B 00 02 00 01 00
20171221 131221.790   PUBLISH     0000  --->  test-client                         0C 00 00 02 00 00 66 6F 6F 62 61 72

I have one question. why test/sub topic is sent from the broker?
the client doesn't SUBSCRIBE it.

The client subscribed to test/+ and + is a wildcard. Hence, it receives messages also from topic test/sub.

from paho.mqtt-sn.embedded-c.

ty4tw avatar ty4tw commented on July 24, 2024

Hi, Juergen
Thank you for your contribution.

from paho.mqtt-sn.embedded-c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.