Not getting error response of sent packet from ESP32C3 LwIP TCP server - esp32

I am facing issue of not receiving error in return of "send()" function in case of TCP socket disconnected at client side. I am using example TCP server(ESP32C3) by using LwIP stack (version v2.1.3).
To debug this issue I have added delay of 50ms after sending data and then send same data again on same socket then I am getting error of -14 which is as expected .
I have modified default TCP server example which is given by ESP-IDF to send 256 bytes continuously.
Any specific time and function for which we need to wait and then check for ACK/NACK from TCP socket of packet which server sent to client?
while (to_write > 0)
{
vTaskDelay(20000 / portTICK_PERIOD_MS); // disconnecting TCP client manually (Hercules.exe)
int written = send(sock, rx_buffer + (len - to_write), 256, 0);
vTaskDelay(50 / portTICK_PERIOD_MS);
written = send(sock, rx_buffer + (len - to_write), 256, 0);
if (written < 0)
{
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
}
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); // not getting error 1st time
errno = 0;
to_write -= written;
}
I have disabled nagle's algorithm .
int val = true;
if (!(setsockopt(sock_internal, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(int)) == ESP_OK))
{
ESP_LOGE(TAG, "failed to set tcp no delay");
}

Related

FTP Arduino issue with ESP8266

Trying to do FTP with my router from an ESP8266 WiFi-board and using the Arduino-IDE, I keep getting the following error message:
331 Password required for anonymous.
My code looks like this:
if (client.connect(server, 21)) { // 21 = FTP server
Serial.println(F("Command connected FIRST TIME"));
} else {
Serial.println(F("Command connection failed FIRST TIME"));
}
eRcv();
Serial.println("OUTPUT BUFFER 1");
Serial.println(outBuf);
client.println(F("USER anonymous"));
eRcv();
Serial.println("OUTPUT BUFFER 2");
Serial.println(outBuf);
client.println(F("PASS anonymous"));
eRcv();
Serial.println("OUTPUT BUFFER 3");
Serial.println(outBuf);
client.println(F("SYST"));
eRcv();
Serial.println("OUTPUT BUFFER 4");
Serial.println(outBuf);
client.println(F("Type I"));
eRcv();
My log looks like that:
WiFi connected; IP address: 192.168.178.33
Command connected FIRST TIME
OUTPUT BUFFER 1
220 FRITZ!Box7490 FTP server ready.
OUTPUT BUFFER 2
331 Password required for anonymous.
As you can see, the error message I receive (i.e. err 331) happens already at cmd nr 2 (i.e. "PASS anonymous2).
The router is set to accept an anonymous FTP (that should not be the problem). The router, of course, is set to allow FTP.
I read something about a "passive mode" (client.println(F("PASV"));) but it seems to me that the "PASS anonymous" should go through independent of PASV-mode ore not. Is this correct ?
Are there any other suggestions of what to do here ?
Much appreciated!
P.S. For completion, the FTP-receive (delivering the "outBuf" from the example-code above) looks like this:
//-------------- FTP receive
byte eRcv() {
byte respCode;
byte thisByte;
long StartTimeoutTime = millis();
while (!client.available() && (millis() - StartTimeoutTime < 1000))
{ // wait for answer with 1 second timeout
delay(1);
}
if (millis() - StartTimeoutTime >= 1000)
{
efail();
return 0;
}
respCode = client.peek();
outCount = 0;
while (client.available()) {
thisByte = client.read();
//Serial.write(thisByte);
if (outCount < 127) {
outBuf[outCount] = thisByte;
outCount++;
outBuf[outCount] = 0;
}
}
if (respCode >= '4') {
efail();
return 0;
}
return 1;
} // eRcv()
Anonymous authentication with FTP still requires that you send a username and a password. Traditionally the username is anonymous and an email address is used as a password. Something like user#test.com works fine. Here is a link to RFC 959, File Transfer Protocol.
From here it looks like you might not be waiting long enough for the server to send the 220 message before you send the USER. After you connect, wait for the server to finish sending its welcome message. Then send your USER, wait for the 331, then send your PASS. The server might also be sending multiple strings for the first message. Try logging into the FTP server with the commandline client for your o/s and see exactly what it's sending you, and adjust your code for that.

For the un-finished 3-way TCP handshake, why the windows OS report the FD_ACCEPT event to the application

Test Scenario
I had written a windows program which I simply called it "simpleServer.exe". This program is just a simulation of a very basic server application. It listens on a port, and wait for incoming messages. The listening Socket was defined to be a TCP Stream Socket. that's all that this program is doing.
I had been deploying this exact same program on 2 different machines, both running on windows 7 professional 64bit. This machine will act as a host. and they are stationed in the same network area.
then, using the program "nmap", I used another machine on the same network, to act as a client. using the "-sS" parameter on "nmap", I do a Syn Scan, to the IP and Port of the listening simpleServer on both machine (one attempt at a time).
(note that the 2 hosts already had "wireshark" started, and is monitoring on tcp packets from the client's IP and to the listening port.)
In the "wireshark" entry, on both machine, I saw the expected tcp packet for Syn Scan:
client ----(SYN)----> host
client <--(SYN/ACK)-- host
client ----(RST)----> host
the above packet exchange suggests that the connection was not established.
But on the "simpleServer.exe", only one of it had "new incoming connection" printed in the logs, while the other instance was not alerted of any new incoming connection, hence no logs at all.
Code Snippets
iRetVal = WSAEventSelect (m_Socket, m_hSocketEvent, FD_ACCEPT);
if (SOCKET_ERROR == iRetVal)
{
if (WSAGetLastError()==WSAENOTSOCK)
{
return E_SOCKET_INVALID;
}
CHKLOGGER (m_pLogger->Log (LOGGER_LOG_ERROR,"GHLSocket::OnAccept() Error while WSAEventSelect(). Error code: ", WSAGetLastError() ));
#if defined GHLSOCKET_DEBUG_VERSION
printf ("Error while WSAEventSelect(). Error code: %ld\n", WSAGetLastError() );
#endif
return E_FAILED_RECV_DATA;
}
// Wait for Network Events to occcur
dwRetVal = WSAWaitForMultipleEvents ( 1,
&m_hSocketEvent,
FALSE,
lTimeout,
TRUE);
if ( WSA_WAIT_TIMEOUT == dwRetVal )
{
return E_TIMEOUT;
goto CleanUp;
}
if ( WSA_WAIT_FAILED == dwRetVal)
{
CHKLOGGER (m_pLogger->Log (LOGGER_LOG_ERROR,"GHLSocket::OnAccept() WSAWaitForMultipleEvents() failed. Error code: ", WSAGetLastError() ));
#if defined GHLSOCKET_DEBUG_VERSION
printf ("Error in WSAWaitForMultipleEvents() failed. Error code: %ld\n", WSAGetLastError() );
#endif
dwReturn = E_FAILED_RECV_DATA;
goto CleanUp;
}
// Parse the Results from the Network Events.
iRetVal = WSAEnumNetworkEvents (m_Socket, m_hSocketEvent, &mEvents);
if (SOCKET_ERROR == iRetVal)
{
CHKLOGGER (m_pLogger->Log (LOGGER_LOG_ERROR,"GHLSocket::OnAccept() Error while WSAEnumNetworkEvents(). Error code: ", WSAGetLastError() ));
#if defined GHLSOCKET_DEBUG_VERSION
printf ("Error while WSAEnumNetworkEvents(). Error code: %ld\n", WSAGetLastError() );
#endif
dwReturn = E_FAILED_RECV_DATA;
goto CleanUp;
}
// ACCEPT event Detected.
if (mEvents.lNetworkEvents & FD_ACCEPT)
{
// Perform accept operation.
*p_SOCKET = accept (m_Socket, NULL,NULL);
}
Help That I Needed
why is the different behavior from the 2 same of the same application on a different machine with the same OS?

Windows TCP socket recv delay

External controller sends 120-bytes message through TCP/IP socket every 30ms.
Application receives this messages through standard tcp/ip socket recv function.
It works perfectly under Linux & OSX (recv returns 120-bytes messages every 30ms).
Under Windows recv returns ~3500 bytes buffer about every 1 sec. Rest of time it returns 0.
Wireshark under Windows shows messages indeed coming every 30ms.
How to make windows tcp socket work properly (without delay) ?
PS: I've played with TCP_NODELAY & TcpAckFrequency already. Wireshark shows everything is ok. So I think it's some Windows optimization, that should be turned off.
Reading--
int WMaster::DataRead(void)
{
if (!open_ok) return 0;
if (!CheckSocket())
{
PrintErrNo();
return 0;
}
iResult = recv(ConnectSocket, (char *)input_buff,sizeof(input_buff),0);
nError=WSAGetLastError();
if(nError==0) return iResult;
if(nError==WSAEWOULDBLOCK) return iResult;
PrintErrNo();
return 0;
}
Initialization-
ConnectSocket = INVALID_SOCKET;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ZeroMemory(&clientService, sizeof(clientService));
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( deviceName.toLatin1().constData() );
clientService.sin_port = htons( port);
iResult = setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag,
sizeof (int));
u_long iMode=1;
iResult=ioctlsocket(ConnectSocket,FIONBIO,&iMode);
iResult = ::connect( ConnectSocket, (SOCKADDR*) &clientService,
sizeof(clientService) );
CheckSocket -
bool WMaster::CheckSocket(void)
{
socklen_t len = sizeof (int);
int retval = getsockopt (ConnectSocket, SOL_SOCKET, SO_ERROR, (char*)(&valopt), &len );
if (retval!=0)
{
open_ok=false;
return false;
};
return true;
}
Consider disabling the Nagle algorithm. 120-bytes is quite small and its possible that data is being buffered before being sent. Another reason I think it is the Nagle Algorithm is that about 33 sends should happen in 1 second. That corresponds with 33*120 = 3960 bytes / sec very similar to the 3500 you are seeing.
Change your dataread function as follows such that WSAGetLastError is only called when there is an error.
int WMaster::DataRead(void)
{
if (!open_ok) return 0;
if (!CheckSocket())
{
PrintErrNo();
return 0;
}
iResult = recv(ConnectSocket, (char *)input_buff,sizeof(input_buff),0);
if(iResult >= 0)
{
return iResult;
}
nError=WSAGetLastError();
if(nError==WSAEWOULDBLOCK) return iResult;
PrintErrNo();
return 0;
}
The fact that you are polling the socket every millisecond may have something to do with your performance problem. But I'd like to see the source to CheckSocket before concluding that as the problem.

Socket programming Update: recv returning -1, error = 10053

I'm implementing a TCP/IP application on Windows 7 that loops around a socket recv() call. For small amount of data (< 5 MB) it works fine, but for large data (>20 MB), the recv fails in between.
Details: My app needs to communicate with HTTP server running , both running on same machine, in this scenerio, tcp app is sending heavy data to HTTP server
It gives error = 2, recv returns 0.
Error 2 means ENOENT, but what does it means?. Does anyone know what this is (in regards to a socket) and how I can get around this?
msgLen = recv(s,msg,BUFFER_SIZE,0);
if(msgLen > 0)
{
// do processing
}
else
{
printf("\n no data received .... msgLen=%d",msgLen);
printf("\n no data received .... errno=%d",errno);
}
Update Code as per comment
msgLen = recv(s,msg,BUFFER_SIZE,0);
if(msgLen > 0)
{
// do processing
}
else if(msgLen == 0)
{
printf("\n sender disconnected");
}
else
{
printf("\n no data received .... msgLen=%d",msgLen);
printf("\n no data received .... errno=%d",WSAGetLastError());
}
The error I get now is:
Firstly, recv = 0 many times, i.e. sender disconnected;
Finally, recv returns -1, and error = 10053.
My TCP/IP application is sending data to HTTP Server. The same works fine with small data, but the issue comes with large amount of data. Is HTTP server getting time out?
When recv() returns 0, it means the other party disconnected gracefully (assuming that your requested buffer size is not 0). recv() only provides an error code when it returns SOCKET_ERROR (-1). On Windows, you have to use WSAGetLastError() to get the error code, not errno, eg:
msgLen = recv(s,msg,BUFFER_SIZE,0);
if(msgLen > 0)
{
// do processing
}
else if (msgLen == 0)
{
printf("\n sender disconnected");
}
else
{
printf("\n no data received .... error=%d",WSAGetLastError());
}
Also keep in mind that if you are using a non-blocking socket, the error code may be WSAEWOULDBLOCK, which is not a fatal error. You can use select() to detect when the socket has data and then attempt the recv() again.

recv() only reads 1 byte (implementing an FTP with winsock)

I'm trying to implement a simple FTP client using winsock. I'm having problems trying to download a file. Here's the code I'm using at the moment:
bool FTPHandler::downloadFile(const char * remoteFilePath, const char * filePath) {
if (!isConnected()) {
setErrorMsg("Not connected, imposible to upload file...");
return false;
}
if (usePasiveMode) {
this->pasivePort = makeConectionPasive();
if (this->pasivePort == -1) {
//error msg will be setted by makeConectionPasive()
return false;
}
} else {
setErrorMsg("Unable to upload file not in pasive mode :S");
return false;
}
char * fileName = new char[500];
getFileName(remoteFilePath,fileName);
// Default name and path := current directory and same name as remote.
if (filePath == NULL) {
filePath = fileName;
}
if (!setDirectory(remoteFilePath)) {
return false;
}
char msg[OTHER_BUF_SIZE];
char serverMsg[SERVER_BUF_SIZE];
sprintf(msg,"%s%s\n",RETR_MSG,fileName);
send(sock, msg, strlen(msg), 0);
SOCKET passSocket;
SOCKADDR_IN passServer;
passSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (passSocket == INVALID_SOCKET) {
WSACleanup();
sprintf(errorMsg,"Error trying to create socket (WSA error code: %d)",WSAGetLastError());
return false;
}
passServer.sin_family = PF_INET;
passServer.sin_port = htons(this->pasivePort);
passServer.sin_addr = *((struct in_addr *)gethostbyname(this->host)->h_addr);
memset(server.sin_zero,0,8);
int errorCode = connect(passSocket, (LPSOCKADDR) &passServer, sizeof(struct sockaddr));
int tries = 0;
while (errorCode == SOCKET_ERROR) {
tries++;
if (tries >= MAX_TRIES) {
closesocket(passSocket);
sprintf(errorMsg,"Error trying to create socket");
WSACleanup();
return false;
}
}
char * buffer = (char *) malloc(CHUNK_SIZE);
ofstream f(filePath);
Sleep(WAIT_TIME);
while (int readBytes = ***recv(passSocket, buffer, CHUNK_SIZE, 0)***>0) {
buffer[readBytes] = '\0';
f.write(buffer,readBytes);
}
f.close();
Sleep(WAIT_TIME);
recv(sock, serverMsg, OTHER_BUF_SIZE, 0);
if (!startWith(serverMsg, FILE_STATUS_OKEY_CODE)) {
sprintf(errorMsg,"Bad response: %s",serverMsg);
return false;
}
return true;
}
That last recv() returns 1 byte several times, and then the method ends and the file that should be around 1Kb is just 23 bytes.
Why isn't recv reading the hole file?
There are all kinds of logic holes and incorrect/missing error handling in this code. You really need to clean up this code in general.
You are passing the wrong sizeof() value to connect(), and not handling an error correctly if connect() fails (your retry loop is useless). You need to use sizeof(sockaddr_in) or sizeof(passServer) instead of sizeof(sockaddr). You are also not initializing passServer correctly.
You are not checking recv() for errors. And in the off-chance that recv() actually read CHUCK_SIZE number of bytes then you have a buffer overflow that will corrupt memory when you write the null byte into the buffer (which you do not need to do) because you are writing it past the boundaries of the buffer.
If connect() fails, or recv() fails with any error other than a server-side initiated disconnect, you are not telling the server to abort the transfer.
Once you tell the server to go into Passive mode, you need to connect to the IP/Port (not just the Port) that the server tells you, before you then send your RETR command.
Don't forget to send the server a TYPE command so it knows what format to send the file bytes in, such as TYPE A for ASCII text and TYPE I for binary data. If you try to transfer a file in the wrong format, you can corrupt the data. FTP's default TYPE is ASCII, not Binary.
And lastly, since you clearly do not seem to know how to program sockets effectively, I suggest you use the FTP portions of the WinInet library instead of WinSock directly, such as the FtpGetFile() function. Let WinInet handle the details of transferring FTP files for you.

Resources