基于MQTT协议的云端proxy远程登陆
这篇文件是建立在一下两篇文章基础上完成的 很多重复的内容不会在这章提到
telnet协议相关
MQTT协议相关
从这里开始就假设对MQTT和telnet协议的实现已经有一定的基础了
这张时序图是整个程序的时序 数据流向也是按章箭头的流向来的
一下为实现代码 部分的if 0是关掉了调试用信息
其中的 telnet.c telnet.h utils.c utils.h都已经在之前的文章中贴出来了 这次主要是控制部分
cloud_terninal.c
#include
#include
#include
#include
#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60
#define MSG_MAX_SIZE 512
#define TOPIC_NUM 3
bool session = true;
const static char* topic[TOPIC_NUM] =
{
"Gai爷:",
"ycy ",
"CCYY "
};
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf(">%s %s", message->topic, (char *)message->payload);
//printf("--sdfs----%ld-----------------\n",strlen((char *)message->payload));
}else{
printf("%s (null)\n", message->topic);
}
fflush(stdout);
}
void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
int i;
if(!result){
/* Subscribe to broker information topics on successful connect. */
mosquitto_subscribe(mosq, NULL, "CCYY ", 2);
}else{
fprintf(stderr, "Connect failed\n");
}
}
void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i
my_task.c
#include
#include
#include
#include
#include"mosquitto.h"
#include"utils.h"
#include"telnet.h"
//#define IP_ADDRESS "127.0.0.1"
#define IP_PORT 23
#define SERV_PORT 3333
#define MAXLINE 1024
#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60
#define MSG_MAX_SIZE 512
#define TOPIC_NUM 3
#define Key_to_Fun_Size 3
typedef struct sockaddr SA;
typedef void (*Fun)() ;
typedef struct key_word_to_fun
{
char key_word[MSG_MAX_SIZE];
Fun fun;
void* arg1;
}Key_to_Fun;
static void log_in_telnet();
bool session = true;
uint32 SOCKFD = 0;
char IP_ADDRESS[] = "xxx.xxx.xxx.xxx";
const static char* topic[TOPIC_NUM] =
{
"Gai爷:",
"ycy ",
"CCYY "
};
static Key_to_Fun tab_Key_to_Fun[Key_to_Fun_Size] =
{
{"telnet",log_in_telnet,0},
{"login",log_in_telnet,0},
{"password",log_in_telnet,0},
//{NULL,NULL}
};
uint32 max(uint32 a, uint32 b)
{
return (a>b?a:b);
}
void ERR_EXIT(char* s)
{
perror(s);
exit(EXIT_FAILURE);
}
void INFO_PRINT(char* s)
{
printf("%s",s);
}
uint32 match_string(char *src,char* dst,char *pattern)
{
//char *pattern1 = "^telnet";
char errbuf[MAXLINE];
//char match[MAXLINE];
regex_t reg;
int err,nm = 10;
regmatch_t pmatch[nm];
if(regcomp(®,pattern,REG_EXTENDED) < 0)
{
regerror(err,®,errbuf,sizeof(errbuf));
printf("err:%s\n",errbuf);
return 0;
}
err = regexec(®,src,nm,pmatch,0);
if(err == REG_NOMATCH)
{
printf("no match\n");
return 0;
}
else if(err)
{
regerror(err,®,errbuf,sizeof(errbuf));
printf("err:%s\n",errbuf);
return 0;
}
for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++)
{
int len = pmatch[i].rm_eo - pmatch[i].rm_so;
if(len)
{
memset(dst,'\0',MAXLINE);
memcpy(dst,src + pmatch[i].rm_so,len);
printf("%s\n",dst);
}
}
return 1;
}
static void log_in_telnet()
{
uint32 sockfd,isReady=0;
struct sockaddr_in servaddr;
uint32 hname[128];
sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(IP_PORT);
servaddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
printf("servaddr: IP is %s, Port is %d\n",inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
while(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))){};
SOCKFD = sockfd;
printf("connect has been ready\n");
}
static void search_key_word_to_fun(struct mosquitto *mosq,char *s)
{
int i = 0,j = 0;
char temp[MSG_MAX_SIZE] = {};
char match[MAXLINE];
char pattern[] = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
for(i =0 ;i< Key_to_Fun_Size;i++)
{
if(match_string(s,match,tab_Key_to_Fun[i].key_word))
{
if((i==0)&& (SOCKFD != 0))
{
printf("telnet has connect!! error command !!\n");
return ;
}
else if((i==0)&& (SOCKFD == 0))
{
if(match_string(s,match,pattern))
{
printf("---match------%s----------\n",match);
for(j =0 ; match[j] != '\0' ;j++)
{
IP_ADDRESS[j] = match[j];
}
IP_ADDRESS[j] = '\0';
tab_Key_to_Fun[i].fun();
}
else
{
/* do nothing */
mosquitto_publish(mosq,NULL,"CCYY ",29,"you should put into your ip\n",0,0);
}
}
else
{
tab_Key_to_Fun[i].fun();
}
return ;
}
}
if((SOCKFD)&&(i != 0))
{
writen(SOCKFD,s,strlen(s));
}
}
static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf(">%s %s", message->topic, (char *)message->payload);
search_key_word_to_fun(mosq,(char *)message->payload);
//MQTT发啥 就往telnet发啥
}else{
printf("%s (null)\n", message->topic);
}
//fflush(stdout);
}
static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
int i;
if(!result){
/* Subscribe to broker information topics on successful connect. */
mosquitto_subscribe(mosq, NULL, "ycy ", 2);
}else{
fprintf(stderr, "Connect failed\n");
}
}
static void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i0)
{
/* do nothing */
}
else
{
ERR_EXIT("some error occurred ");
}
//printf("FD_ISSET(fileno(fp),&rset)----%d--\n",n);
//memset(buf,0,MAXLINE);
telnet_client_send_msg(sockfd,(char *)buf,n);
//send_message_to_cloud(sockfd,(char *)buf,n);
}
}
}
void test()
{
//unsigned char s[] = 15042b58;
//printf("+++++++++++++++++++++++++++++++\n",s);
printf("+++++++++++++++++++++++++++++++\n");
putchar(0x15);
putchar(0x04);
putchar(0x2b);
putchar(0x58);
printf("+++++++++++++++++++++++++++++++\n");
}
int main()
{
struct mosquitto *mosq = NULL;
char buff[MSG_MAX_SIZE];
test();
//libmosquitto 库初始化
mosquitto_lib_init();
//创建mosquitto客户端
mosq = mosquitto_new(NULL,session,NULL);
if(!mosq){
printf("create client failed..\n");
mosquitto_lib_cleanup();
return 1;
}
//设置回调函数,需要时可使用
//mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
//连接服务器
if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
fprintf(stderr, "Unable to connect.\n");
return 1;
}
//开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS)
{
printf("mosquitto loop error\n");
return 1;
}
while(!SOCKFD){};
wait_message_from_telnet(mosq,stdin,SOCKFD);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
makefile
#不是系统默认库 要记得添加连接选项
all:My_task
@echo ""
@echo "This is My_task compile......."
@echo ""
My_task: utils.o telnet.o my_task.o Cloud
gcc -g -o My_task my_task.o utils.o telnet.o -lmosquitto -lpthread
Cloud: utils.o telnet.o cloud_terminal.o
gcc -g -o Cloud cloud_terminal.c -lmosquitto -lpthread
my_task.o:my_task.c utils.h telnet.h
gcc -g -c my_task.c
cloud_terminal.o:cloud_terminal.c utils.h telnet.h
gcc -g -c cloud_terminal.c
utils.o:utils.c utils.h
gcc -g -c utils.c
telnet.o:telnet.c telnet.h
gcc -g -c telnet.c
clean :
-rm my_task.o cloud_terminal.o utils.o telnet.o Cloud My_task
运行情况:
为了方便调试 在mytask的程序中我也打印出来登录信息
=====================================================================================================
以上的程序有一点不足之处是默认登录我自己的IP 不能从外界获取IP 一下的程序再这个基础上有所改动 可以使用“telnet 127.0.0.1”这种命令登录进去。
添加的部分是使用了正则表达式,包含头文件#include
我在这里遇见的问题是
我想匹配“xxx.xxx.xxx.xxx”使用的可以识别模式串是char pattern[] = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}";这个是没有问题的
但是最开始我使用的是char pattern[] = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"; 以上这两种用法在python中是相同的结果 但是第二种情况在C语言中不适用
my_task.c
#include
#include
#include
#include
#include"mosquitto.h"
#include"utils.h"
#include"telnet.h"
//#define IP_ADDRESS "127.0.0.1"
#define IP_PORT 23
#define SERV_PORT 3333
#define MAXLINE 1024
#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60
#define MSG_MAX_SIZE 512
#define TOPIC_NUM 3
#define Key_to_Fun_Size 3
typedef struct sockaddr SA;
typedef void (*Fun)() ;
typedef struct key_word_to_fun
{
char key_word[MSG_MAX_SIZE];
Fun fun;
void* arg1;
}Key_to_Fun;
static void log_in_telnet();
bool session = true;
uint32 SOCKFD = 0;
char IP_ADDRESS[] = "xxx.xxx.xxx.xxx";
const static char* topic[TOPIC_NUM] =
{
"Gai爷:",
"ycy ",
"CCYY "
};
static Key_to_Fun tab_Key_to_Fun[Key_to_Fun_Size] =
{
{"telnet",log_in_telnet,0},
{"login",log_in_telnet,0},
{"password",log_in_telnet,0},
//{NULL,NULL}
};
uint32 max(uint32 a, uint32 b)
{
return (a>b?a:b);
}
void ERR_EXIT(char* s)
{
perror(s);
exit(EXIT_FAILURE);
}
void INFO_PRINT(char* s)
{
printf("%s",s);
}
uint32 match_string(char *src,char* dst,char *pattern)
{
//char *pattern1 = "^telnet";
char errbuf[MAXLINE];
//char match[MAXLINE];
regex_t reg;
int err,nm = 10;
regmatch_t pmatch[nm];
if(regcomp(®,pattern,REG_EXTENDED) < 0)
{
regerror(err,®,errbuf,sizeof(errbuf));
printf("err:%s\n",errbuf);
return 0;
}
err = regexec(®,src,nm,pmatch,0);
if(err == REG_NOMATCH)
{
printf("no match\n");
return 0;
}
else if(err)
{
regerror(err,®,errbuf,sizeof(errbuf));
printf("err:%s\n",errbuf);
return 0;
}
for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++)
{
int len = pmatch[i].rm_eo - pmatch[i].rm_so;
if(len)
{
memset(dst,'\0',MAXLINE);
memcpy(dst,src + pmatch[i].rm_so,len);
printf("%s\n",dst);
}
}
return 1;
}
static void log_in_telnet()
{
uint32 sockfd,isReady=0;
struct sockaddr_in servaddr;
uint32 hname[128];
sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(IP_PORT);
servaddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
printf("servaddr: IP is %s, Port is %d\n",inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
while(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))){};
SOCKFD = sockfd;
printf("connect has been ready\n");
}
static void search_key_word_to_fun(struct mosquitto *mosq,char *s)
{
int i = 0,j = 0;
char temp[MSG_MAX_SIZE] = {};
char match[MAXLINE];
char pattern[] = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
for(i =0 ;i< Key_to_Fun_Size;i++)
{
if(match_string(s,match,tab_Key_to_Fun[i].key_word))
{
if((i==0)&& (SOCKFD != 0))
{
printf("telnet has connect!! error command !!\n");
return ;
}
else if((i==0)&& (SOCKFD == 0))
{
if(match_string(s,match,pattern))
{
printf("---match------%s----------\n",match);
for(j =0 ; match[j] != '\0' ;j++)
{
IP_ADDRESS[j] = match[j];
}
IP_ADDRESS[j] = '\0';
tab_Key_to_Fun[i].fun();
}
else
{
/* do nothing */
mosquitto_publish(mosq,NULL,"CCYY ",29,"you should put into your ip\n",0,0);
}
}
else
{
tab_Key_to_Fun[i].fun();
}
return ;
}
}
if((SOCKFD)&&(i != 0))
{
writen(SOCKFD,s,strlen(s));
}
}
static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf(">%s %s", message->topic, (char *)message->payload);
search_key_word_to_fun(mosq,(char *)message->payload);
//MQTT发啥 就往telnet发啥
}else{
printf("%s (null)\n", message->topic);
}
//fflush(stdout);
}
static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
int i;
if(!result){
/* Subscribe to broker information topics on successful connect. */
mosquitto_subscribe(mosq, NULL, "ycy ", 2);
}else{
fprintf(stderr, "Connect failed\n");
}
}
static void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i0)
{
/* do nothing */
}
else
{
ERR_EXIT("some error occurred ");
}
//printf("FD_ISSET(fileno(fp),&rset)----%d--\n",n);
//memset(buf,0,MAXLINE);
writen(sockfd,(char *)buf,n);
//send_message_to_cloud(sockfd,(char *)buf,n);
}
}
}
int main()
{
struct mosquitto *mosq = NULL;
char buff[MSG_MAX_SIZE];
//libmosquitto 库初始化
mosquitto_lib_init();
//创建mosquitto客户端
mosq = mosquitto_new(NULL,session,NULL);
if(!mosq){
printf("create client failed..\n");
mosquitto_lib_cleanup();
return 1;
}
//设置回调函数,需要时可使用
//mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
//连接服务器
if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
fprintf(stderr, "Unable to connect.\n");
return 1;
}
//开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS)
{
printf("mosquitto loop error\n");
return 1;
}
while(!SOCKFD){};
wait_message_from_telnet(mosq,stdin,SOCKFD);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
一下是程序运行情况
总结:
1.gdb调试方法十分有效 应当加强掌握
2.在调试过程中出现过程序不稳定的情况 但是很偶然 根据那几次的分析结果 可能是读写缓冲区的问题 比如发来的数据很快并且多 这时候读数据的速度跟不上 就会出现显示出现问题 这个方面我不是很了解 以后要了解一下这方面的知识
以上