Skip to content

Commit ca553e9

Browse files
authored
Merge pull request FreeRDP#11739 from akallabeth/proxy
[core,proxy] fix BIO read methods
2 parents 19c866b + e723f8d commit ca553e9

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

include/freerdp/transport_io.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ extern "C"
3838
* @param userContext user defined context passed by @ref freerdp_set_io_callback_context
3939
* @param data a buffer to read to
4040
* @param bytes the size of the buffer
41-
* @return the number of bytes read or <0 for failures
41+
* @return the number of bytes read. Negative numbers indicate an error
42+
* occurred. \b errno is set accordingly (see man 2 read)
43+
* @bug Before 3.18.0 the function did return \b -1 for transport closed and \b 0 for retry
44+
* events.
4245
* @since version 3.9.0
4346
*/
4447
typedef int (*pTransportLayerRead)(void* userContext, void* data, int bytes);
@@ -48,7 +51,10 @@ extern "C"
4851
* @param userContext user defined context passed by @ref freerdp_set_io_callback_context
4952
* @param data a buffer to write
5053
* @param bytes the size of the buffer
51-
* @return the number of bytes written or <0 for failures
54+
* @return the number of bytes written. Negative numbers indicate an error
55+
* occurred. \b errno is set accordingly (see man 2 send)
56+
* @bug Before 3.18.0 the function did return \b -1 for transport closed and \b 0 for retry
57+
* events.
5258
* @since version 3.9.0
5359
*/
5460
typedef int (*pTransportLayerWrite)(void* userContext, const void* data, int bytes);
@@ -109,14 +115,14 @@ extern "C"
109115
pTransportFkt TLSAccept;
110116
pTransportAttach TransportAttach;
111117
pTransportFkt TransportDisconnect;
112-
pTransportRWFkt ReadPdu; /* Reads a whole PDU from the transport */
113-
pTransportRWFkt WritePdu; /* Writes a whole PDU to the transport */
114-
pTransportRead ReadBytes; /* Reads up to a requested amount of bytes */
118+
pTransportRWFkt ReadPdu; /* Reads a whole PDU from the transport */
119+
pTransportRWFkt WritePdu; /* Writes a whole PDU to the transport */
120+
pTransportRead ReadBytes; /* Reads up to a requested amount of bytes */
115121
pTransportGetPublicKey GetPublicKey; /** @since version 3.2.0 */
116122
pTransportSetBlockingMode SetBlockingMode; /** @since version 3.3.0 */
117123
pTransportConnectLayer ConnectLayer; /** @since 3.9.0 */
118124
pTransportAttachLayer AttachLayer; /** @since 3.9.0 */
119-
UINT64 reserved[64 - 12]; /* Reserve some space for ABI compatibility */
125+
UINT64 reserved[64 - 12]; /* Reserve some space for ABI compatibility */
120126
};
121127
typedef struct rdp_transport_io rdpTransportIo;
122128

libfreerdp/core/proxy.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,6 @@ static BOOL http_proxy_connect(rdpContext* context, BIO* bufferedBio, const char
696696
}
697697
Sleep(10);
698698
}
699-
else
700-
{
701-
/* Error? */
702-
WLog_ERR(TAG, "Failed reading reply from HTTP proxy (BIO_read returned zero)");
703-
goto fail;
704-
}
705699

706700
resultsize += WINPR_ASSERTING_INT_CAST(size_t, status);
707701
}

libfreerdp/core/tcp.c

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,29 +1354,17 @@ static int freerdp_tcp_layer_read(void* userContext, void* data, int bytes)
13541354

13551355
rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
13561356

1357-
int error = 0;
1358-
int status = 0;
1359-
13601357
(void)WSAResetEvent(tcpLayer->hEvent);
1361-
status = _recv((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1358+
const int status = _recv((SOCKET)tcpLayer->sockfd, data, bytes, 0);
13621359

13631360
if (status > 0)
13641361
return status;
13651362

1366-
if (status == 0)
1367-
return -1; /* socket closed */
1368-
1369-
error = WSAGetLastError();
1363+
const int error = WSAGetLastError();
13701364

13711365
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
13721366
(error == WSAEALREADY))
1373-
{
1374-
status = 0;
1375-
}
1376-
else
1377-
{
1378-
status = -1;
1379-
}
1367+
errno = EAGAIN;
13801368

13811369
return status;
13821370
}
@@ -1390,25 +1378,15 @@ static int freerdp_tcp_layer_write(void* userContext, const void* data, int byte
13901378

13911379
rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
13921380

1393-
int error = 0;
1394-
int status = 0;
1395-
1396-
status = _send((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1381+
const int status = _send((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1382+
if (status > 0)
1383+
return status;
13971384

1398-
if (status <= 0)
1399-
{
1400-
error = WSAGetLastError();
1385+
const int error = WSAGetLastError();
14011386

1402-
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1403-
(error == WSAEALREADY))
1404-
{
1405-
status = 0;
1406-
}
1407-
else
1408-
{
1409-
status = -1;
1410-
}
1411-
}
1387+
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1388+
(error == WSAEALREADY))
1389+
errno = EAGAIN;
14121390

14131391
return status;
14141392
}

libfreerdp/core/transport.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,11 +1961,21 @@ static int transport_layer_bio_write(BIO* bio, const char* buf, int size)
19611961

19621962
BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
19631963

1964-
int status = IFCALLRESULT(-1, layer->Write, layer->userContext, buf, size);
1964+
errno = 0;
1965+
const int status = IFCALLRESULT(-1, layer->Write, layer->userContext, buf, size);
19651966

19661967
if (status >= 0 && status < size)
19671968
BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
19681969

1970+
switch (errno)
1971+
{
1972+
case EAGAIN:
1973+
BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
1974+
break;
1975+
default:
1976+
break;
1977+
}
1978+
19691979
return status;
19701980
}
19711981

@@ -1983,11 +1993,17 @@ static int transport_layer_bio_read(BIO* bio, char* buf, int size)
19831993
return -1;
19841994

19851995
BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
1996+
errno = 0;
1997+
const int status = IFCALLRESULT(-1, layer->Read, layer->userContext, buf, size);
19861998

1987-
int status = IFCALLRESULT(-1, layer->Read, layer->userContext, buf, size);
1988-
1989-
if (status == 0)
1990-
BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
1999+
switch (errno)
2000+
{
2001+
case EAGAIN:
2002+
BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
2003+
break;
2004+
default:
2005+
break;
2006+
}
19912007

19922008
return status;
19932009
}

0 commit comments

Comments
 (0)