
用ssl ,client's data invalid protocol,这是什么原因?
Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby leeshine » Tue May 05, 2015 3:48 pm
Espressif_Faye wrote:您好,
这表示您的一包 SSL 加密数据大于默认 buffer size :2KB
您可以使用 espconn_secure_set_size 设置增大 SSL buffer size,最大支持到 8KB
client和server 都从2K到8K都试过了,还是会出现这个报错,还有什么问题会导致这个报错吗?修改的位置有什么讲究没?

Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby leeshine » Thu May 07, 2015 3:54 pm
Espressif_Faye wrote:您好,
请问您的 SDK 版本号是多少?麻烦使用最新的 SDK 测试是否能够建立连接。
API 的使用可以参考“2C-ESP8266 编程手册” 中的说明。
如果还是有问题,麻烦提供您的测试代码,以供查证。
sdk用的最新的,APP里的代码用的原来的,原来的版本为0.9.3,原来的程序是正常工作的,ssl测试要提供的东西太多了,底层封起来,报错以后,debug完全没法入手,下面的部分代码,能否帮忙看下用的有没有问题,多谢!


#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "httpclient.h"
#include "cert.h"
#include "private_key.h"
// Suport different Content-Type header
#define HTTP_HEADER_CONTENT_TYPE "application/json"
// Internal state.
typedef struct {
char * path;
int port;
char * post_data;
char * hostname;
char * buffer;
int buffer_size;
http_callback user_callback;
} request_args;
static request_args* req = NULL;
static struct espconn* conn = NULL;
static ICACHE_FLASH_ATTR
http_exit()
{
if(conn != NULL)
{
espconn_delete(conn);
if(conn->proto.tcp != NULL)
{
os_free(conn->proto.tcp);
conn->proto.tcp = NULL;
}
if(conn != NULL)
{
os_free(conn);
conn = NULL;
}
}
if (req->buffer != NULL)
{
// FIXME: make sure this is not a partial response, using the Content-Length header.
const char * version = "HTTP/1.1 ";
if (os_strncmp(req->buffer, version, os_strlen(version)) != 0)
{
PRINTF("Invalid version in %s\n", req->buffer);
return;
}
int http_status = atoi(req->buffer + os_strlen(version));
char * body = (char *)os_strstr(req->buffer, "\r\n\r\n") + 4;
if (req->user_callback != NULL)
{
// Callback is optional.
req->user_callback(body, http_status, req->buffer);
}
}
else
{
req->user_callback(NULL, 0 , NULL);
}
if(req != NULL)
{
//avoid recall this module, memory leak;
if(req->buffer != NULL)
{
os_free(req->buffer);
req->buffer = NULL;
}
if(req->hostname != NULL)
{
os_free(req->hostname);
req->hostname = NULL;
}
if(req->path != NULL)
{
os_free(req->path);
req->path = NULL;
}
if(req->post_data != NULL)
{
os_free(req->post_data);
req->post_data = NULL;
}
if(req != NULL)
{
os_free(req);
req = NULL;
}
}
}
static char * ICACHE_FLASH_ATTR my_strdup(const char * str)
{
if(str == NULL)
{
return NULL;
}
char * new_str = (char *)os_malloc(os_strlen(str) + 1 /*null character*/);
if (new_str == NULL)
return NULL;
os_strcpy(new_str, str);
return new_str;
}
static void ICACHE_FLASH_ATTR receive_callback(void * arg, char * buf, unsigned short len)
{
PRINTF("Receive Response...%d\n", len);
struct espconn * conn = (struct espconn *)arg;
if (req->buffer == NULL)
{
return;
}
// Let's do the equivalent of a realloc().
const int new_size = req->buffer_size + len;
char * new_buffer;
if (new_size > BUFFER_SIZE_MAX || NULL == (new_buffer = (char *)os_malloc(new_size)))
{
PRINTF("Response too long %d\n", new_size);
// TODO: espconn_disconnect(conn) without crashing.
}
os_memcpy(new_buffer, req->buffer, req->buffer_size);
os_memcpy(new_buffer + req->buffer_size - 1 /*overwrite the null character*/, buf, len); // Append new data.
new_buffer[new_size - 1] = '\0'; // Make sure there is an end of string.
os_free(req->buffer);
req->buffer = new_buffer;
req->buffer_size = new_size;
}
static void ICACHE_FLASH_ATTR sent_callback(void * arg)
{
struct espconn * conn = (struct espconn *)arg;
if (req->post_data == NULL)
{
PRINTF("All sent\n");
}
else
{
// The headers were sent, now send the contents.
PRINTF("Sending request body\n");
espconn_secure_sent(conn, (uint8_t *)req->post_data, os_strlen(req->post_data));
os_free(req->post_data);
req->post_data = NULL;
}
}
static void ICACHE_FLASH_ATTR connect_callback(void * arg)
{
PRINTF("Connected\n");
struct espconn * conn = (struct espconn *)arg;
espconn_regist_recvcb(conn, receive_callback);
espconn_regist_sentcb(conn, sent_callback);
char method[5] = "GET";
char post_headers[128] = "";
if (req->post_data != NULL)
{
// If there is data this is a POST request.
os_memcpy(method, "POST", 5);
os_sprintf(post_headers,
"Content-Type:"
HTTP_HEADER_CONTENT_TYPE
"\r\n"
"Content-Length: %d\r\n", os_strlen(req->post_data));
}
char buf[2048];
int len = os_sprintf(buf,
"%s %s HTTP/1.1\r\n"
"Host: %s:%d\r\n"
"Connection: close\r\n"
"User-Agent: ESP8266\r\n"
"%s"
"\r\n",
method, req->path, req->hostname, req->port, post_headers);
espconn_secure_sent(conn, (uint8_t *)buf, len);
PRINTF("Sending request header\n");
}
static void ICACHE_FLASH_ATTR disconnect_callback(void * arg)
{
PRINTF("http disconnected\n");
http_exit();
}
static void ICACHE_FLASH_ATTR reconnect_callback(void *arg, sint8 err)
{
PRINTF("http reconnected, error:%d\n", err);
if(req->buffer != NULL)
{
os_free(req->buffer);
req->buffer = NULL;
}
http_exit();
}
static void ICACHE_FLASH_ATTR dns_callback(const char * hostname, ip_addr_t * addr, void * arg)
{
request_args * req = (request_args *)arg;
if (addr == NULL)
{
PRINTF("DNS failed %s\n", hostname);
if((req->buffer)!= NULL)
{
os_free(req->buffer);
req->buffer = NULL;
}
http_exit();
}
else
{
PRINTF("DNS found %s " IPSTR "\n", hostname, IP2STR(addr));
conn = (struct espconn *)os_malloc(sizeof(struct espconn));
conn->type = ESPCONN_TCP;
conn->state = ESPCONN_NONE;
conn->proto.tcp = (esp_tcp *)os_malloc(sizeof(esp_tcp));
conn->proto.tcp->local_port = espconn_port();
conn->proto.tcp->remote_port = req->port;
os_memcpy(conn->proto.tcp->remote_ip, addr, 4);
espconn_regist_connectcb(conn, connect_callback);
espconn_regist_disconcb(conn, disconnect_callback);
espconn_regist_reconcb(conn, reconnect_callback);
// TODO: consider using espconn_regist_reconcb (for timeouts?)
// cf esp8266_sdk_v0.9.1/examples/at/user/at_ipCmd.c (TCP ARQ retransmission?)
espconn_secure_connect(conn);
}
}
Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby ESP_Faye » Thu May 07, 2015 5:45 pm
请问您连接的 SSL server 域名或者 IP 地址是多少?
SSL 连接可以参考 demo http://bbs.espressif.com/viewtopic.php?f=21&t=389
将 SSL server 的域名或者 IP 地址换成您实际的 SSL server 即可。
建立连接时,根据实际情况决定是否设置 buffer size
Code: Select all
//espconn_secure_set_size(ESPCONN_CLIENT,5120); // set SSL buffer size, if your SSL packet larger than 2048 bytes
espconn_secure_connect(pespconn); // tcp SSL connect
Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby leeshine » Thu May 07, 2015 7:23 pm
Espressif_Faye wrote:您好,
请问您连接的 SSL server 域名或者 IP 地址是多少?
SSL 连接可以参考 demo http://bbs.espressif.com/viewtopic.php?f=21&t=389
将 SSL server 的域名或者 IP 地址换成您实际的 SSL server 即可。
建立连接时,根据实际情况决定是否设置 buffer sizeCode: Select all
//espconn_secure_set_size(ESPCONN_CLIENT,5120); // set SSL buffer size, if your SSL packet larger than 2048 bytes
espconn_secure_connect(pespconn); // tcp SSL connect
我觉得非常非常奇怪


发送的数据,数据都是很短的。
POST /v1/devices/registration HTTP/1.1
Host: api:443
Connection: close
User-Agent: ESP8266
Content-Type:application/json
Content-Length: 208
{
"key":"4518bf0061c642b8637d687aa8c4bb9165",
"device_type":1,
"module":"esp8266",
"version":"1.0"
}
收到的数据:
{"code":0,"message":"","data":{"device_id":4,"key":"7d48d857512d988a39c82fcaee302a6c332c","identifer":"64dfdffd""}
Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby leeshine » Mon May 11, 2015 4:20 pm
Espressif_Faye wrote:您好,
非常抱歉,您提供的代码无法正常编译,能否提供简单的可以复现问题的工程,以供查证?
您连接的 SSL server 域名或者 IP 地址是多少? 我们将连接您的 SSL server 进行测试。
谢谢帮忙,找到问题原因了
我用的http短连接:"Connection: close\r\n"这种情况下会报该错误
而用设置:"Connection: keep-alive\r\n",则不会报这个错。
Re: 用ssl ,client's data invalid protocol,这是什么原因?
Postby 怪我们始终太恋旧 » Wed Aug 15, 2018 4:06 pm
client's data invalid protocol
Error: invalid protocol message
connect err,errno:-61
esp8266作为客户端访问服务器, 访问的网址是https://hao.360.cn, 我已经用espconn_secure_set_size将ssl buffer设置为8192了,还是会出现这种错误。你们能够用esp8266作为client与https://hao.360.cn建立ssl连接,并且GET到正常的数据吗
Who is online
Users browsing this forum: No registered users and 306 guests
Login
Newbies Start Here
Are you new to ESP8266?
Unsure what to do?
Dunno where to start?
Start right here!
Latest SDK
Documentation
Complete listing of the official ESP8266 related documentation release by ESPRESSIF!
Must read here!
- All times are UTC+08:00
- Top
- Delete all board cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. We are the manufacturer of ESP8266EX.