eclipse-paho.mqtt 本地编译与调试

下载安装

        下载eclipse-paho.mqtt.c的压缩包传至ubuntu Linux并解压

        下载地址:

GitHub - eclipse/paho.mqtt.c: An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/

        解压后

ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master$ ls
CMakeLists.txt            README.md     buildbak        docs         test_package            version.minor
CODE_OF_CONDUCT.md        about.html    cbuild.bat      edl-v10      travis-build.sh         version.patch
CONTRIBUTING.md           android       cmake           epl-v20      travis-deploy.sh
LICENSE                   appveyor.yml  deploy_rsa.enc  notice.html  travis-install.sh
Makefile                  build         dist            src          travis-setup-deploy.sh
PULL_REQUEST_TEMPLATE.md  build.xml     doc             test         version.major

        进入Mafilefile同级目录并执行make

ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master$ make
mkdir -p build/output/samples
mkdir -p build/output/test
echo OSTYPE is Linux
OSTYPE is Linux
mkdir -p build
sed -e "s/@CLIENT_VERSION@/1.3.9/g" -e "s/@BUILD_TIMESTAMP@/Wed Nov 17 07:46:36 UTC 2021/g" src/VersionInfo.h.in > build/VersionInfo.h
cc -g -fPIC -I/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include -D_GNU_SOURCE -Os -Wall -fvisibility=hidden -Ibuild -DPAHO_MQTT_EXPORTS=1 -o build/output/libpaho-mqtt3c.so.1.3 src/Heap.c src/LinkedList.c src/MQTTProtocolClient.c src/MQTTProtocolOut.c src/SHA1.c src/MQTTProperties.c src/MQTTPersistenceDefault.c src/Messages.c src/MQTTTime.c src/MQTTPacketOut.c src/WebSocket.c src/Clients.c src/OsWrapper.c src/Thread.c src/MQTTPacket.c src/MQTTPersistence.c src/MQTTClient.c src/SocketBuffer.c src/Log.c src/StackTrace.c src/MQTTReasonCodes.c src/utf-8.c src/Socket.c src/Base64.c src/Tree.c -L/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib -shared -Wl,-init,MQTTClient_init -Wl,--start-group -lpthread -lanl -Wl,--end-group -Wl,-soname,libpaho-mqtt3c.so.1
ln -s libpaho-mqtt3c.so.1.3  build/output/libpaho-mqtt3c.so.1
ln -s libpaho-mqtt3c.so.1 build/output/libpaho-mqtt3c.so
cc -g -fPIC -I/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include -D_GNU_SOURCE -Os -Wall -fvisibility=hidden -Ibuild -DPAHO_MQTT_EXPORTS=1 -o build/output/libpaho-mqtt3cs.so.1.3 src/Heap.c src/LinkedList.c src/SSLSocket.c src/MQTTProtocolClient.c src/MQTTProtocolOut.c src/SHA1.c src/MQTTProperties.c src/MQTTPersistenceDefault.c src/Messages.c src/MQTTTime.c src/MQTTPacketOut.c src/WebSocket.c src/Clients.c src/OsWrapper.c src/Thread.c src/MQTTPacket.c src/MQTTPersistence.c src/MQTTClient.c src/SocketBuffer.c src/Log.c src/StackTrace.c src/MQTTReasonCodes.c src/utf-8.c src/Socket.c src/Base64.c src/Tree.c -DOPENSSL -L/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib -shared -Wl,--start-group -lpthread -lanl -ldl -lssl -lcrypto -Wl,--end-group -Wl,-init,MQTTClient_init -Wl,-soname,libpaho-mqtt3cs.so.1 -Wl,-no-whole-archive
In file included from /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include/openssl/comp.h:16,
                 from /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include/openssl/ssl.h:17,
                 from src/SocketBuffer.h:28,
                 from src/SSLSocket.c:31:
src/SSLSocket.c: In function ‘SSLSocket_initialize’:
/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include/openssl/crypto.h:235:57: warning: statement with no effect [-Wunused-value]
  235 | #  define CRYPTO_THREADID_set_callback(threadid_func)   (0)
      |                                                         ^
src/SSLSocket.c:482:3: note: in expansion of macro ‘CRYPTO_THREADID_set_callback’
  482 |   CRYPTO_THREADID_set_callback(SSLThread_id);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from src/Base64.c:43:
src/Base64.c: In function ‘Base64_encodeDecode’:
/home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/include/openssl/bio.h:492:34: warning: value computed is not used [-Wunused-value]
  492 | # define BIO_flush(b)            (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/Base64.c:71:3: note: in expansion of macro ‘BIO_flush’
   71 |   BIO_flush(b_out); /* indicate end of encoding */
      |   ^~~~~~~~~
/usr/bin/ld: skipping incompatible /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib/libssl.so when searching for -lssl
/usr/bin/ld: skipping incompatible /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib/libssl.a when searching for -lssl
/usr/bin/ld: skipping incompatible /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib/libcrypto.so when searching for -lcrypto
/usr/bin/ld: skipping incompatible /home/ubuntu/MQTT_Client_SDK/openssl_1.1.1b/lib/libcrypto.a when searching for -lcrypto
ln -s libpaho-mqtt3cs.so.1.3  build/output/libpaho-mqtt3cs.so.1
ln -s libpaho-mqtt3cs.so.1 build/output/libpaho-mqtt3cs.so

        make执行完后,可以找到build目录下新生成了.so文件和samples test两个目录。

ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master$ cd build
ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build$ ls
VersionInfo.h  output
ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build$ cd output/
ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build/output$ ls
libpaho-mqtt3a.so      libpaho-mqtt3as.so.1    libpaho-mqtt3c.so.1.3   paho_c_version  test
libpaho-mqtt3a.so.1    libpaho-mqtt3as.so.1.3  libpaho-mqtt3cs.so      samples
libpaho-mqtt3a.so.1.3  libpaho-mqtt3c.so       libpaho-mqtt3cs.so.1    samples_in
libpaho-mqtt3as.so     libpaho-mqtt3c.so.1     libpaho-mqtt3cs.so.1.3  samples_out
ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build/output$ cd samples
ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build/output/samples$ ls
MQTTAsync_publish    MQTTClient_publish        MQTTClient_subscribe  paho_c_sub   paho_cs_sub
MQTTAsync_subscribe  MQTTClient_publish_async  paho_c_pub            paho_cs_pub

        将所有.so文件都拷贝到/usr/lib/目录下

ubuntu@ip-172-*-*-20:/usr/lib$ ls -l | grep libpa
-rwxr-xr-x  1 root root 791080 Nov 17 07:33 libpaho-mqtt3as.so.1
-rwxr-xr-x  1 root root 715888 Nov 17 07:37 libpaho-mqtt3cs.so.1

        sample里的文件是可以让我们构造使用场景的。

        将/home/ubuntu/soft/paho.mqtt.c-master/build/output/目录下的samples 拷贝出一份,两份分别取名samples_in, samples_out。

        这样的话我们mqtt-client就已经具备了,但是没有mqtt-server也就是broker是不能正常通信的,因为可以在本机安装一个mosquitto服务端。mosquitto安装方法可以参考以下链接,我是一键就安装好了。

Mosquitto – 人人都懂物联网

mosquitto启动状态如下

ubuntu@ip-172-*-*-20:/usr/lib$ ps -aux | grep mosquit
mosquit+  351105  0.0  0.3  28104  6684 ?        Ssl  05:22   0:06 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ubuntu    360030  0.0  0.0   8160   668 pts/2    S+   09:27   0:00 grep --color=auto mosquit

mosquitto的配置文件位置:

ubuntu@ip-172-*-*-20:/etc/mosquitto$ ls
aclfile.example  ca_certificates  certs  conf.d  mosquitto.conf  pskfile.example  pwfile.example

调试:

调试前修改代码

        sample/MQTTAsync_subscribe.c和sample/MQTTAsync_publish.c里

#define ADDRESS     " tcp://mqtt.eclipse.org:1883"  

        修改为

#define ADDRESS     "tcp://127.0.0.1:1883"   

        同时我增加了日志打印,便于观察。        

        删除build/src/sample目录重新编译。并把编译好的文件MQTTAsync_subscribe,MQTTAsync_publish分别拷贝到sample_in ,sample_out目录下。

调制执行

        打开两个ubuntu的终端。

        第一个终端进入samples_in目录执行./MQTTAsync_subscribe 

ubuntu@ip-172-**-20:~/soft/paho.mqtt.c-master/build/output/samples_in$ ./MQTTAsync_subscribe 
[harry] test async subscribe[harry]Sucess to create client, return code 0
[harry]Success to set callbacks, return code 0
[harry]Sucess to start connect, return code 0
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS0

Press Q to quit
*************以下内容为等待samples_out publish后收到******************
Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: Hello World!

        第二个终端进入samples_out目录执行

ubuntu@ip-172-*-*-20:~/soft/paho.mqtt.c-master/build/output/samples_out$ ./MQTTAsync_publish 
[harry] test async publish[harry]Sucess to create client object, return code 0
[harry]Success to set callback, return code 0
[harry]Success to start connect, return code 0
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 0 delivery confirmed
Successful disconnection

mosquitto日志打印(/var/log/mosquitto/mosquitto.log)为:

1637122985: mosquitto version 1.6.9 starting
1637122985: Config loaded from /etc/mosquitto/mosquitto.conf.
1637122985: Opening ipv4 listen socket on port 1883.
1637122985: Opening ipv6 listen socket on port 1883.
1637124786: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637126548: mosquitto version 1.6.9 starting
1637126548: Config loaded from /etc/mosquitto/mosquitto.conf.
1637126548: Opening ipv4 listen socket on port 1883.
1637126548: Opening ipv6 listen socket on port 1883.
1637128348: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637130149: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637131950: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637133751: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637135291: New connection from 127.0.0.1 on port 1883.
1637135291: New client connected from 127.0.0.1 as ExampleClientSub (p2, c1, k20).
1637135313: Client ExampleClientSub disconnected.
1637135552: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1637136202: New connection from 127.0.0.1 on port 1883.
1637136202: New client connected from 127.0.0.1 as ExampleClientSub (p2, c1, k20).
1637136249: Socket error on client ExampleClientSub, disconnecting.
1637136944: New connection from 127.0.0.1 on port 1883.
1637136945: New client connected from 127.0.0.1 as ExampleClientSub (p2, c1, k20).
1637136955: New connection from 127.0.0.1 on port 1883.
1637136955: New client connected from 127.0.0.1 as ExampleClientPub (p2, c1, k20).
1637136955: Client ExampleClientPub disconnected.

        从打印的日志中可以看出,sample_in向mqtt-server发送了订阅(subscribe)请求后,sample_out向mqtt-sever发送的发布(publish)请求,然后sample_in收到自己想要的订阅消息。

参考资料:

使用开源C语言库连接MQTT Broker_智能边缘平台 IEF_最佳实践_华为云MQTT是一种发布/订阅模式的消息协议,能够在硬件性能低下的远程设备以及网络状况糟糕的情况下工作。本文介绍一种开源的C语言库Eclipse Paho C Client Library连接使用IEF的内置MQTT Broker。git clone https://github.com/eclipse/paho.mqtt.c.git或直接在hhttps://support.huaweicloud.com/bestpractice-ief/ief_04_0007.html

你可能感兴趣的:(test,skills,渗透测试,mqtt,编译,iot,调试)