贝壳物联之模拟第三方及用户登录

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <fcntl.h>

#include <openssl/ssl.h>

#define DOMAIN  "www.bigiot.net"
#define PORT    443
#define IP      "121.42.180.30"

int s;
int ret;
struct sockaddr_in bigiot_addr;
char buf[1024];

int main()
{
	SSL_CTX *ctx;
	SSL *ssl;
	const SSL_METHOD *meth;

	int recv_len,len;
	char sendData[40960] = {0};
	char recvData[40960] = {0};

	SSL_library_init();
	OpenSSL_add_all_algorithms();
	SSL_load_error_strings();
	meth = TLSv1_2_client_method();
	ctx = SSL_CTX_new(meth);
	if(ctx == NULL)
	{
		exit(0);
	}

	s = socket(AF_INET, SOCK_STREAM, 0);
	if (s < 0) {
		printf("create socket error!\n");

		exit(-1);
	} else {
		printf("socket ok!\n");
	}

	bigiot_addr.sin_family = AF_INET;
	bigiot_addr.sin_port = htons(PORT);

	struct hostent *h;
	h = gethostbyname(DOMAIN);
	if (NULL == h) {
		printf("gethostbyname error!\n");

		exit(-2);
	} else {
		printf("get ok!\n");
	}

	char *ip = inet_ntoa(*((struct in_addr *)h->h_addr_list[0]));
	bigiot_addr.sin_addr.s_addr = inet_addr(ip);

	ret = connect(s, (const struct sockaddr *)&bigiot_addr, sizeof(bigiot_addr));
	if (ret < 0) {
		printf("connect error!\n");
		exit(-2);
	} else {
		printf("connect ok!\n");
	}

	ssl = SSL_new(ctx);
	SSL_set_fd(ssl,s);
	if(SSL_connect(ssl) == -1)
	{
		exit(0);
	}
#if 0
	snprintf(sendData,
	sizeof(sendData) - 1,
	"POST /oauth/say HTTP/1.1\r\n"
	"Host: www.bigiot.net\r\n"
	"Content-Type: application/x-www-form-urlencoded\r\n"
	"Content-Length: 70\r\n"
	"\r\n"
	"access_token=01be744fab9f461eda77e9eee94cd736b2a2aff3&id=D17992&c=play\r\n"
	);
#else
	snprintf(sendData,
	sizeof(sendData) - 1,
	"GET /oauth/dev?access_token=01be744fab9f461eda77e9eee94cd736b2a2aff3&id=0 HTTP/1.1\r\n"
	"Host: www.bigiot.net\r\n"
	"Content-Type: application/x-www-form-urlencoded\r\n"
	"\r\n"
	);
#endif

	printf("%s\n", sendData);

	SSL_write(ssl,sendData,strlen(sendData));

	len = 0;
	recv_len = 0;
	do
	{
		len+=recv_len;
		recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);
	}while(recv_len > 0);
	SSL_shutdown(ssl);
	SSL_free(ssl);
	close(s);
	SSL_CTX_free(ctx);
	printf(recvData);
}

Makefile:

all:                                                                         
    gcc main.c -o test -lssl -lcrypto
    ./test
 
clean:
    rm -rf test
fatal error: openssl/ssl.h: No such file or directory 

sudo apt-get install libssl-dev

用户登录:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <fcntl.h>

#include <openssl/ssl.h>

#define DOMAIN  "www.bigiot.net"
#define PORT    8585

int s;
int ret;
struct sockaddr_in bigiot_addr;
char buf[1024];

int main()
{
	SSL_CTX *ctx;
	SSL *ssl;
	const SSL_METHOD *meth;

	int recv_len,len;
	char sendData[40960] = {0};
	char recvData[40960] = {0};

	SSL_library_init();
	OpenSSL_add_all_algorithms();
	SSL_load_error_strings();
	meth = TLSv1_2_client_method();
	ctx = SSL_CTX_new(meth);
	if(ctx == NULL)
	{
		exit(0);
	}

	s = socket(AF_INET, SOCK_STREAM, 0);
	if (s < 0) {
		printf("create socket error!\n");

		exit(-1);
	} else {
		printf("socket ok!\n");
	}

	bigiot_addr.sin_family = AF_INET;
	bigiot_addr.sin_port = htons(PORT);

	struct hostent *h;
	h = gethostbyname(DOMAIN);
	if (NULL == h) {
		printf("gethostbyname error!\n");

		exit(-2);
	} else {
		printf("get ok!\n");
	}

	char *ip = inet_ntoa(*((struct in_addr *)h->h_addr_list[0]));
	bigiot_addr.sin_addr.s_addr = inet_addr(ip);

	ret = connect(s, (const struct sockaddr *)&bigiot_addr, sizeof(bigiot_addr));
	if (ret < 0) {
		printf("connect error!\n");
		exit(-2);
	} else {
		printf("connect ok!\n");
	}
	
	ssl = SSL_new(ctx);
	SSL_set_fd(ssl,s);
	if(SSL_connect(ssl) == -1)
	{
		exit(0);
	}
	
	recv_len = 0;
	recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);

	if (recv_len) {
		printf(recvData);
	}
	char *login = "{\"M\":\"login\",\"ID\":\"xxxx\",\"K\":\"xxxxxx\"}\n";
	printf("%s\n", login);
	SSL_write(ssl,login,strlen(login));
	
	recv_len = 0;
	recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);

	if (recv_len) {
		printf(recvData);
	}

	recv_len = 0;
	recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);

	if (recv_len) {
		printf(recvData);
	}
	char *say = "{\"M\":\"say\",\"ID\":\"Dxxxx\",\"C\":\"play\"}\n";

	printf("%s\n", say);
	SSL_write(ssl,say,strlen(say));
	
	len = 0;
	recv_len = 0;
	do
	{
		len+=recv_len;
		recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);
	}while(recv_len > 0);

	printf(recvData);

	while (1) {

	len = 0;
	recv_len = 0;
	do
	{
		len+=recv_len;
		recv_len = SSL_read(ssl,recvData+len,sizeof(recvData)-len-1);
	}while(recv_len > 0);

	if (len > 0)
		printf(recvData);

	}
	
	SSL_shutdown(ssl);
	SSL_free(ssl);
	close(s);
	SSL_CTX_free(ctx);
}
#!/bin/bash

echo "bigiot api test!"

curl "https://www.bigiot.net/oauth/say" -d "access_token=4b73fa972b7983e0c0b1b2e93c9181ce17c24c35&id=D10768&c=play&&sign="

sleep 1s

curl "https://www.bigiot.net/oauth/say" -d "access_token=4b73fa972b7983e0c0b1b2e93c9181ce17c24c35&id=D10768&c=stop&&sign="

echo "success!"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值