1、背景
由于项目有需求在一个现有的产品上增加MQTT通信的功能,且出于安全考虑,MQTT要走TLS,采用单向认证的方式。
2、方案选择
由于是在现有的产品上新增功能,那么为了减少总的成本,故选择只动应用软件的来实现需求。
MQTT的功能直接选择PahoMqtt这个第三方库来实现,因为以前用过,比较熟悉。由于只想动应用软件,那么只能选择他的embedded-c分支,这样才可以直接集成代码,而不需要编译成so放到固件里,同时也减少程序体积的增加。
embedded-c分支不支持TLS,那么就要想办法自己给embedded-c实现TLS的功能,经过考虑,现有的产品里有openssl库,故使用openssl来给embedded-c添加TLS的支持。
最终方案为:paho.mqtt.embedded-c实现MQTT的连接、断开和收\发,使用openssl来给embedded-c添加TLS的支持。
3、实现代码
step1:先去github把源码下载下来
step2:把对应的代码文件集成到项目里。
这里只需要MQTTPacket/src目录下的所有代码、MQTTClient-C/src/linux目录下的所有代码、MQTTClient-C/src目录下的MQTTClient.h和MQTTClient.c这两个文件就行了。具体内容如下图:
到这一步,paho.mqtt.embedded-c基本的就集成完了,对应的调用例子可以参考MQTTClient/samples/linux/main.cpp和MQTTClient-C/samples/linux/stdoutsub.c这两个文件,这里就不多赘述了。
step3:使用openssl给embedded-c增加TLS单向认证的支持
1、修改MQTTLinux.h文件
主要是修改点有两点:1、struct Network的内容,增加ssl的支持;2、新增两条NetworkConnect函数,一条用来走TCP,一条用来走TLS。(这里我选择保留最原始的NetworkConnect函数,方便以后debug的时候对比用。)
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
*******************************************************************************/
#if !defined(__MQTT_LINUX_)
#define __MQTT_LINUX_
#if defined(WIN32_DLL) || defined(WIN64_DLL)
#define DLLImport __declspec(dllimport)
#define DLLExport __declspec(dllexport)
#elif defined(LINUX_SO)
#define DLLImport extern
#define DLLExport __attribute__ ((visibility ("default")))
#else
#define DLLImport
#define DLLExport
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
typedef struct Timer
{
struct timeval end_time;
} Timer;
void TimerInit(Timer*);
char TimerIsExpired(Timer*);
void TimerCountdownMS(Timer*, unsigned int);
void TimerCountdown(Timer*, unsigned int);
int TimerLeftMS(Timer*);
typedef struct Network
{
/* socket的描述符 */
int my_socket;
/* 读取MQTT消息的函数 */
int (*mqttread) (struct Network*, unsigned char*, int, int);
/* 发送MQTT消息的函数 */
int (*mqttwrite) (struct Network*, unsigned char*, int, int);
/* 使用SSL时的描述符 */
SSL *ssl;
/* 是否使用SSL 0:否 1:是