#c #openssl #tls1.2
Вопрос:
У меня есть сетевая библиотека, которая отправляет и получает данные с помощью OpenSSL.
void recvFromSite(char buf[], int maxsize){
if(url.protocol == "http"){
if (recv(sock, buf, maxsize, 0) == -1) throw "Error receving message";
} else if(url.protocol == "https"){
fd_set fds;
timeval timeout;
int sslSock;
int recv, count =0;
int totalRecv = 0;
while(true){
recv = SSL_read(ssl, buf, maxsize);
if(recv > 0){
totalRecv = recv;
std::cout << "Bytes reccieved: " << recv << " Total bytes: " << totalRecv << "n";
} else {
count ;
int err = SSL_get_error(ssl, recv);
switch(err){
case SSL_ERROR_NONE:
//No real error, do nothing
std::cout << "No error " << count << "n" ;
continue;
case SSL_ERROR_ZERO_RETURN:
//Peer disconected
std::cout << "Connection broken " << count <<"n";
break;
case SSL_ERROR_WANT_READ:
sslSock = SSL_get_rfd(ssl);
FD_ZERO(amp;fds);
FD_SET(sslSock, amp;fds);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
err = select(sslSock 1,amp;fds, nullptr, nullptr, amp;timeout);
if(err > 0){
continue;
} else if(err == 0){
std::cout << "Connection timed out " << count <<"n";
break;
} else {
std::cout << "Unknown error" << count <<"n";
break;
}
case SSL_ERROR_WANT_WRITE:
sslSock = SSL_get_wfd(ssl);
FD_ZERO(amp;fds);
FD_SET(sslSock, amp;fds);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
err = select(sslSock 1,nullptr, amp;fds, nullptr, amp;timeout);
if(err > 0){
continue;
} else if(err == 0){
std::cout << "Connection timed outn";
break;
} else {
std::cout << "Unknown errorn";
break;
}
case SSL_ERROR_SYSCALL:
std::cout << "Error in system" << count << "n";
break;
case SSL_ERROR_SSL:
std::cout << "Error in SSL" << count << "n";
break;
default:
std::cout << "Unknown error reccievedn";
break;
}
break;
}
}
}
}
Когда я попытаюсь запросить Google (или любую другую домашнюю страницу), SSL_read()
он вернет только фрагмент домашней страницы, прежде чем выбросить SSL_error_syscall
. Вот результат:
Bytes reccieved: 1000 Total bytes: 1000
Bytes reccieved: 396 Total bytes: 1396
Bytes reccieved: 1000 Total bytes: 2396
Bytes reccieved: 396 Total bytes: 2792
Bytes reccieved: 1000 Total bytes: 3792
Bytes reccieved: 396 Total bytes: 4188
Bytes reccieved: 1000 Total bytes: 5188
Bytes reccieved: 396 Total bytes: 5584
Bytes reccieved: 1000 Total bytes: 6584
Bytes reccieved: 396 Total bytes: 6980
Bytes reccieved: 1000 Total bytes: 7980
Bytes reccieved: 396 Total bytes: 8376
Bytes reccieved: 1000 Total bytes: 9376
Bytes reccieved: 396 Total bytes: 9772
Bytes reccieved: 1000 Total bytes: 10772
Bytes reccieved: 396 Total bytes: 11168
Bytes reccieved: 1000 Total bytes: 12168
Bytes reccieved: 396 Total bytes: 12564
Bytes reccieved: 1000 Total bytes: 13564
Bytes reccieved: 396 Total bytes: 13960
Bytes reccieved: 1000 Total bytes: 14960
Bytes reccieved: 396 Total bytes: 15356
Bytes reccieved: 1000 Total bytes: 16356
Bytes reccieved: 396 Total bytes: 16752
Bytes reccieved: 1000 Total bytes: 17752
Bytes reccieved: 396 Total bytes: 18148
Bytes reccieved: 1000 Total bytes: 19148
Bytes reccieved: 396 Total bytes: 19544
Bytes reccieved: 1000 Total bytes: 20544
Bytes reccieved: 396 Total bytes: 20940
Bytes reccieved: 828 Total bytes: 21768
Bytes reccieved: 225 Total bytes: 21993
Bytes reccieved: 1000 Total bytes: 22993
Bytes reccieved: 396 Total bytes: 23389
Bytes reccieved: 1000 Total bytes: 24389
Bytes reccieved: 396 Total bytes: 24785
Bytes reccieved: 1000 Total bytes: 25785
Bytes reccieved: 396 Total bytes: 26181
Bytes reccieved: 1000 Total bytes: 27181
Bytes reccieved: 396 Total bytes: 27577
Bytes reccieved: 1000 Total bytes: 28577
Bytes reccieved: 396 Total bytes: 28973
Bytes reccieved: 1000 Total bytes: 29973
Bytes reccieved: 396 Total bytes: 30369
Bytes reccieved: 1000 Total bytes: 31369
Bytes reccieved: 396 Total bytes: 31765
Bytes reccieved: 1000 Total bytes: 32765
Bytes reccieved: 396 Total bytes: 33161
Bytes reccieved: 1000 Total bytes: 34161
Bytes reccieved: 396 Total bytes: 34557
Bytes reccieved: 1000 Total bytes: 35557
Bytes reccieved: 396 Total bytes: 35953
Bytes reccieved: 1000 Total bytes: 36953
Bytes reccieved: 396 Total bytes: 37349
Bytes reccieved: 1000 Total bytes: 38349
Bytes reccieved: 396 Total bytes: 38745
Bytes reccieved: 1000 Total bytes: 39745
Bytes reccieved: 396 Total bytes: 40141
Bytes reccieved: 1000 Total bytes: 41141
Bytes reccieved: 396 Total bytes: 41537
Bytes reccieved: 1000 Total bytes: 42537
Bytes reccieved: 396 Total bytes: 42933
Bytes reccieved: 1000 Total bytes: 43933
Bytes reccieved: 396 Total bytes: 44329
Bytes reccieved: 1000 Total bytes: 45329
Bytes reccieved: 396 Total bytes: 45725
Bytes reccieved: 1000 Total bytes: 46725
Bytes reccieved: 396 Total bytes: 47121
Bytes reccieved: 1000 Total bytes: 48121
Bytes reccieved: 396 Total bytes: 48517
Bytes reccieved: 182 Total bytes: 48699
Error in system1
=====Webpage=============
,x22sbplx22:16,x22sbprx22:16,x22scdx22:10,x22stokx22:x22blGf9wuC_Q0LaJOrsNCmVd9P35gx22,x22uhdex22:false}}';google.pmc=JSON.parse(pmc);})();</script> </body></html>2sbitx22:x22Search by imagex22,x22srchx22:x22Google Searchx22},x22ovrx22:{},x22pqx22:x22x22,x22refpdx22:true,x22rfsx22:[],x22sbasx22:x220 3px 8px 0 rgba(0,0,0,0.2),0 0 0 1px rgba(0,0,0,0.08)x22){google.jl={attn:false,blt:'none',chnk:0,dw:false,emtn:0,end:0,ine:false,lls:'default',pdt:0,rep:0,sif:true,snet:true,strt:0,ubm:false,uwp:true};})();(function(){var pmc='{x22dx22:{},x22sb_hex22:{x22agenx22:true,x22cgenx22:true,x22clientx22:x22heirloom-hpx22,x22dhx22:true,x22dhqtx22:true,x22dsx22:x22x22,x22ffqlx22:x22enx22,x22flx22:true,x22hostx22:x22google.comx22,x22isbhx22:28,x22jsonpx22:true,x22msgsx22:{x22ciblx22:x22Clear Searchx22,x22dymx22:x22Did you mean:x22,x22lckyx22:x22I\u0026#39;m Feeling Luckyx22,x22lmlx22:x22Learn morex22,x22osktq
Полученные байты показывают, сколько байтов было получено в каждом пакете, а текст ниже веб-страницы является фактической веб-страницей.
Почему SSL_read()
он ведет себя так странно?
Комментарии:
1. Если
SSL_read()
сообщаетSSL_ERROR_SYSCALL
, используйтеerrno
или эквивалент, чтобы получить системную ошибку. ЕслиSSL_read()
есть сообщенияSSL_ERROR_SSL
, вместо этого проверьте очередь ошибок OpenSSL. Что это за сообщения об ошибках? В любом случае, вам нужно остановить цикл, если вы получите любую из этих ошибок.