Refactor checking socket type into a function

This commit is contained in:
Juliusz Sosinowicz
2022-08-04 11:35:27 +02:00
parent 3278210e1c
commit ebcfa31993

View File

@@ -372,6 +372,20 @@ static int sockAddrEqual(
return 0; return 0;
} }
static int isDGramSock(int sfd)
{
int type = 0;
XSOCKLENT length = sizeof(int); /* optvalue 'type' is of size int */
if (getsockopt(sfd, SOL_SOCKET, SO_TYPE, &type, &length) == 0 &&
type != SOCK_DGRAM) {
return 0;
}
else {
return 1;
}
}
/* The receive embedded callback /* The receive embedded callback
* return : nb bytes read, or error * return : nb bytes read, or error
*/ */
@@ -482,11 +496,7 @@ int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
return recvd; return recvd;
} }
else if (recvd == 0) { else if (recvd == 0) {
int type; if (!isDGramSock(sd)) {
XSOCKLENT length = sizeof(int); /* optvalue 'type' is of size int */
if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &type, &length) == 0 &&
type != SOCK_DGRAM) {
/* Closed TCP connection */ /* Closed TCP connection */
recvd = WOLFSSL_CBIO_ERR_CONN_CLOSE; recvd = WOLFSSL_CBIO_ERR_CONN_CLOSE;
} }
@@ -530,13 +540,10 @@ int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
int sent; int sent;
const SOCKADDR_S* peer = NULL; const SOCKADDR_S* peer = NULL;
XSOCKLENT peerSz = 0; XSOCKLENT peerSz = 0;
int type;
XSOCKLENT length = sizeof(int); /* optvalue 'type' is of size int */
WOLFSSL_ENTER("EmbedSendTo()"); WOLFSSL_ENTER("EmbedSendTo()");
if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &type, &length) == 0 && if (!isDGramSock(sd)) {
type != SOCK_DGRAM) {
/* Probably a TCP socket. peer and peerSz MUST be NULL and 0 */ /* Probably a TCP socket. peer and peerSz MUST be NULL and 0 */
} }
else if (!dtlsCtx->connected) { else if (!dtlsCtx->connected) {