Skip to content

Commit 96352cb

Browse files
committed
Change network-protocol classes to return netProtoErr_t instead of bool
Made the network-protocol classes be explicit about error codes. Methods now return either NETPROTO_ERR_NONE or NETPROTO_ERR_UNSPECIFIED, which removes any confusion about the old true/false behavior and whether success or failure looked "backwards." The netProtoErr_t enum still matches the previous bool usage, so existing failure-check logic did not need any changes.
1 parent a4aa404 commit 96352cb

File tree

26 files changed

+1019
-1054
lines changed

26 files changed

+1019
-1054
lines changed

lib/ftp/fnFTP.cpp

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ fnFTP::~fnFTP()
655655
data = nullptr;
656656
}
657657

658-
bool fnFTP::login(const string &_username, const string &_password, const string &_hostname, unsigned short _port)
658+
netProtoErr_t fnFTP::login(const string &_username, const string &_password, const string &_hostname, unsigned short _port)
659659
{
660660
username = _username;
661661
password = _password;
@@ -669,7 +669,7 @@ bool fnFTP::login(const string &_username, const string &_password, const string
669669
{
670670
Debug_printf("Could not log in, errno = %u\r\n", errno);
671671
_statusCode = 421; // service not available
672-
return true;
672+
return NETPROTO_ERR_UNSPECIFIED;
673673
}
674674

675675
Debug_printf("Connected, waiting for 220.\r\n");
@@ -678,7 +678,7 @@ bool fnFTP::login(const string &_username, const string &_password, const string
678678
if (parse_response())
679679
{
680680
Debug_printf("Timed out waiting for 220 banner.\r\n");
681-
return true;
681+
return NETPROTO_ERR_UNSPECIFIED;
682682
}
683683

684684
Debug_printf("Sending USER.\r\n");
@@ -691,13 +691,13 @@ bool fnFTP::login(const string &_username, const string &_password, const string
691691
else
692692
{
693693
Debug_printf("Could not send username. Response was: %s\r\n", controlResponse.c_str());
694-
return true;
694+
return NETPROTO_ERR_UNSPECIFIED;
695695
}
696696

697697
if (parse_response())
698698
{
699699
Debug_printf("Timed out waiting for 331 or 230.\r\n");
700-
return true;
700+
return NETPROTO_ERR_UNSPECIFIED;
701701
}
702702

703703
if (is_positive_intermediate_reply() && is_authentication())
@@ -709,7 +709,7 @@ bool fnFTP::login(const string &_username, const string &_password, const string
709709
if (parse_response())
710710
{
711711
Debug_printf("Timed out waiting for 230.\r\n");
712-
return true;
712+
return NETPROTO_ERR_UNSPECIFIED;
713713
}
714714
}
715715
else
@@ -725,13 +725,13 @@ bool fnFTP::login(const string &_username, const string &_password, const string
725725
else
726726
{
727727
Debug_printf("Could not finish log in. Response was: %s\r\n", controlResponse.c_str());
728-
return true;
728+
return NETPROTO_ERR_UNSPECIFIED;
729729
}
730730

731731
if (parse_response())
732732
{
733733
Debug_printf("Timed out waiting for 200.\r\n");
734-
return true;
734+
return NETPROTO_ERR_UNSPECIFIED;
735735
}
736736

737737
if (is_positive_completion_reply() && is_syntax())
@@ -743,16 +743,16 @@ bool fnFTP::login(const string &_username, const string &_password, const string
743743
Debug_printf("Could not set image type. Ignoring.\r\n");
744744
}
745745

746-
return false;
746+
return NETPROTO_ERR_NONE;
747747
}
748748

749-
bool fnFTP::logout()
749+
netProtoErr_t fnFTP::logout()
750750
{
751751
Debug_printf("fnFTP::logout()\r\n");
752752
if (!control->connected())
753753
{
754754
Debug_printf("Logout called when not connected.\r\n");
755-
return false;
755+
return NETPROTO_ERR_NONE;
756756
}
757757

758758
if (data->connected())
@@ -771,22 +771,22 @@ bool fnFTP::logout()
771771

772772
control->stop();
773773

774-
return false;
774+
return NETPROTO_ERR_NONE;
775775
}
776776

777-
bool fnFTP::reconnect()
777+
netProtoErr_t fnFTP::reconnect()
778778
{
779779
Debug_println("Trying to re-login");
780780
if (control->connected()) logout();
781781
return login(username, password, hostname, control_port);
782782
}
783783

784-
bool fnFTP::open_file(string path, bool stor)
784+
netProtoErr_t fnFTP::open_file(string path, bool stor)
785785
{
786786
if (!control->connected())
787787
{
788788
Debug_printf("fnFTP::open_file(%s) attempted while not logged in. Aborting.\r\n", path.c_str());
789-
return true;
789+
return NETPROTO_ERR_UNSPECIFIED;
790790
}
791791

792792
int retries = 2;
@@ -800,7 +800,7 @@ bool fnFTP::open_file(string path, bool stor)
800800
continue; // successfully reconnected
801801
}
802802
Debug_printf("fnFTP::open_file(%s, %s) could not get data port. Aborting.\n", path.c_str(), stor ? "STOR" : "RETR");
803-
return true;
803+
return NETPROTO_ERR_UNSPECIFIED;
804804
}
805805

806806
// Do command
@@ -816,29 +816,29 @@ bool fnFTP::open_file(string path, bool stor)
816816
if (parse_response())
817817
{
818818
Debug_printf("Timed out waiting for 150 response.\r\n");
819-
return true;
819+
return NETPROTO_ERR_UNSPECIFIED;
820820
}
821821

822822
if (is_positive_preliminary_reply() && is_filesystem_related())
823823
{
824824
_stor = stor;
825825
_expect_control_response = !stor;
826826
Debug_printf("Server began transfer.\r\n");
827-
return false;
827+
return NETPROTO_ERR_NONE;
828828
}
829829
else
830830
{
831831
Debug_printf("Server could not begin transfer. Response was: %s\r\n", controlResponse.c_str());
832-
return true;
832+
return NETPROTO_ERR_UNSPECIFIED;
833833
}
834834
}
835835

836-
bool fnFTP::open_directory(string path, string pattern)
836+
netProtoErr_t fnFTP::open_directory(string path, string pattern)
837837
{
838838
if (!control->connected())
839839
{
840840
Debug_printf("fnFTP::open_directory(%s%s) attempted while not logged in. Aborting.\r\n", path.c_str(), pattern.c_str());
841-
return true;
841+
return NETPROTO_ERR_UNSPECIFIED;
842842
}
843843

844844
int retries = 2;
@@ -852,7 +852,7 @@ bool fnFTP::open_directory(string path, string pattern)
852852
continue; // successfully reconnected
853853
}
854854
Debug_printf("fnFTP::open_directory(%s%s) could not get data port, aborting.\n", path.c_str(), pattern.c_str());
855-
return true;
855+
return NETPROTO_ERR_UNSPECIFIED;
856856
}
857857

858858
// perform LIST
@@ -861,7 +861,7 @@ bool fnFTP::open_directory(string path, string pattern)
861861
if (parse_response())
862862
{
863863
Debug_printf("fnFTP::open_directory(%s%s) Timed out waiting for 150 response.\r\n", path.c_str(), pattern.c_str());
864-
return true;
864+
return NETPROTO_ERR_UNSPECIFIED;
865865
}
866866

867867
Debug_printf("fnFTP::open_directory(%s%s) - %s\r\n", path.c_str(), pattern.c_str(), controlResponse.c_str());
@@ -874,15 +874,15 @@ bool fnFTP::open_directory(string path, string pattern)
874874
else
875875
{
876876
Debug_printf("Didn't get our 150\r\n");
877-
return true;
877+
return NETPROTO_ERR_UNSPECIFIED;
878878
}
879879

880880
uint8_t buf[256];
881881

882882
// if (buf == nullptr)
883883
// {
884884
// Debug_printf("fnFTP::open_directory() - Could not allocate 2048 bytes.\r\n");
885-
// return true;
885+
// return NETPROTO_ERR_UNSPECIFIED;
886886
// }
887887

888888
int tmout_counter = 1 + FTP_TIMEOUT / 50;
@@ -927,21 +927,21 @@ bool fnFTP::open_directory(string path, string pattern)
927927
if (tmout_counter == 0 || (got_response == false && parse_response()))
928928
{
929929
Debug_printf("fnFTP::open_directory(%s%s) Timed out waiting for 226 response.\r\n", path.c_str(), pattern.c_str());
930-
return true;
930+
return NETPROTO_ERR_UNSPECIFIED;
931931
}
932932

933-
return false; // all good.
933+
return NETPROTO_ERR_NONE; // all good.
934934
}
935935

936-
bool fnFTP::read_directory(string &name, long &filesize, bool &is_dir)
936+
netProtoErr_t fnFTP::read_directory(string &name, long &filesize, bool &is_dir)
937937
{
938938
string line;
939939
struct ftpparse parse;
940940

941941
getline(dirBuffer, line);
942942

943943
if (line.empty())
944-
return true;
944+
return NETPROTO_ERR_UNSPECIFIED;
945945

946946
//Debug_printf("fnFTP::read_directory - %s\r\n",line.c_str());
947947
line = line.substr(0, line.size() - 1);
@@ -950,35 +950,35 @@ bool fnFTP::read_directory(string &name, long &filesize, bool &is_dir)
950950
filesize = parse.size;
951951
is_dir = (parse.flagtrycwd == 1);
952952
Debug_printf("Name: \"%s\" size: %lu\r\n", name.c_str(), filesize);
953-
return dirBuffer.eof();
953+
return dirBuffer.eof() ? NETPROTO_ERR_UNSPECIFIED : NETPROTO_ERR_NONE;
954954
}
955955

956-
bool fnFTP::read_file(uint8_t *buf, unsigned short len)
956+
netProtoErr_t fnFTP::read_file(uint8_t *buf, unsigned short len)
957957
{
958958
//Debug_printf("fnFTP::read_file(%p, %u)\r\n", buf, len);
959959
if (!data->connected() && data->available() == 0)
960960
{
961961
Debug_printf("fnFTP::read_file(%p,%u) - data socket not connected, aborting.\r\n", buf, len);
962-
return true;
962+
return NETPROTO_ERR_UNSPECIFIED;
963963
}
964-
return len != data->read(buf, len);
964+
return len != data->read(buf, len) ? NETPROTO_ERR_UNSPECIFIED : NETPROTO_ERR_NONE;
965965
}
966966

967-
bool fnFTP::write_file(uint8_t *buf, unsigned short len)
967+
netProtoErr_t fnFTP::write_file(uint8_t *buf, unsigned short len)
968968
{
969969
//Debug_printf("fnFTP::write_file(%p,%u)\r\n", buf, len);
970970
if (!data->connected())
971971
{
972972
Debug_printf("fnFTP::write_file(%p,%u) - data socket not connected, aborting.\r\n", buf, len);
973-
return true;
973+
return NETPROTO_ERR_UNSPECIFIED;
974974
}
975975

976-
return len != data->write(buf, len);
976+
return len != data->write(buf, len) ? NETPROTO_ERR_UNSPECIFIED : NETPROTO_ERR_NONE;
977977
}
978978

979-
bool fnFTP::close()
979+
netProtoErr_t fnFTP::close()
980980
{
981-
bool res = false;
981+
netProtoErr_t res = NETPROTO_ERR_NONE;
982982
Debug_printf("fnFTP::close()\r\n");
983983
if (_stor)
984984
{
@@ -989,7 +989,7 @@ bool fnFTP::close()
989989
if (parse_response())
990990
{
991991
Debug_printf("Timed out waiting for 226.\r\n");
992-
res = true;
992+
res = NETPROTO_ERR_UNSPECIFIED;
993993
}
994994
}
995995
_stor = false;
@@ -1008,16 +1008,17 @@ int fnFTP::data_available()
10081008
return data->available();
10091009
}
10101010

1011-
bool fnFTP::data_connected()
1011+
netProtoErr_t fnFTP::data_connected()
10121012
{
10131013
if (_expect_control_response && control->available())
10141014
_expect_control_response = parse_response();
1015-
return _expect_control_response || data->connected();
1015+
return (_expect_control_response || data->connected())
1016+
? NETPROTO_ERR_UNSPECIFIED : NETPROTO_ERR_NONE;
10161017
}
10171018

10181019
/** FTP UTILITY FUNCTIONS **********************************************************************/
10191020

1020-
bool fnFTP::parse_response()
1021+
netProtoErr_t fnFTP::parse_response()
10211022
{
10221023
char respBuf[384]; // room for control message incl. file path and file size
10231024
int num_read = 0;
@@ -1032,7 +1033,7 @@ bool fnFTP::parse_response()
10321033
{
10331034
// Timeout
10341035
_statusCode = 421; // service not available
1035-
return true; // error
1036+
return NETPROTO_ERR_UNSPECIFIED; // error
10361037
}
10371038
if (num_read >= 4)
10381039
{
@@ -1053,15 +1054,15 @@ bool fnFTP::parse_response()
10531054
// error - nothing above
10541055
Debug_printf("fnFTP::parse_response() - failed\r\n");
10551056
_statusCode = 501; //syntax error
1056-
return true; // error
1057+
return NETPROTO_ERR_UNSPECIFIED; // error
10571058
}
10581059

10591060
// update control response and status code
10601061
controlResponse = string((char *)respBuf, num_read);
10611062
_statusCode = atoi(controlResponse.substr(0, 3).c_str());
10621063
Debug_printf("fnFTP::parse_response() - %d, \"%s\"\r\n", _statusCode, controlResponse.c_str());
10631064

1064-
return false; // ok
1065+
return NETPROTO_ERR_NONE; // ok
10651066
}
10661067

10671068
int fnFTP::read_response_line(char *buf, int buflen)
@@ -1107,7 +1108,7 @@ int fnFTP::read_response_line(char *buf, int buflen)
11071108
return num_read;
11081109
}
11091110

1110-
bool fnFTP::get_data_port()
1111+
netProtoErr_t fnFTP::get_data_port()
11111112
{
11121113
size_t port_pos_beg, port_pos_end;
11131114

@@ -1121,34 +1122,34 @@ bool fnFTP::get_data_port()
11211122
if (parse_response())
11221123
{
11231124
Debug_printf("Timed out waiting for response.\r\n");
1124-
return true;
1125+
return NETPROTO_ERR_UNSPECIFIED;
11251126
}
11261127

11271128
/*
11281129
if (is_negative_permanent_reply())
11291130
{
11301131
Debug_printf("Server unable to reserve port. Response was: %s\r\n", controlResponse.c_str());
1131-
return true;
1132+
return NETPROTO_ERR_UNSPECIFIED;
11321133
}
11331134
11341135
if (is_negative_transient_reply())
11351136
{
11361137
Debug_printf("Cannot get data port. Response was: %s\r\n", controlResponse.c_str());
1137-
return true;
1138+
return NETPROTO_ERR_UNSPECIFIED;
11381139
}
11391140
11401141
if (is_negative_transient_reply())
11411142
{
11421143
Debug_printf("Cannot get data port. Response was: %s\n", controlResponse.c_str());
1143-
return true;
1144+
return NETPROTO_ERR_UNSPECIFIED;
11441145
}
11451146
*/
11461147

11471148
// accept only 229 response: Entering Extended Passive Mode (|||nnnn|)
11481149
if (_statusCode != 229)
11491150
{
11501151
Debug_printf("Cannot get data port. Response was: %s\n", controlResponse.c_str());
1151-
return true;
1152+
return NETPROTO_ERR_UNSPECIFIED;
11521153
}
11531154

11541155
// At this point, we have a port mapping trapped in (|||1234|), peel it out of there.
@@ -1162,14 +1163,14 @@ bool fnFTP::get_data_port()
11621163
if (!data->connect(hostname.c_str(), data_port, FTP_TIMEOUT))
11631164
{
11641165
Debug_printf("Could not open data port %u, errno = %u\r\n", data_port, errno);
1165-
return true;
1166+
return NETPROTO_ERR_UNSPECIFIED;
11661167
}
11671168
else
11681169
{
11691170
Debug_printf("Data port %u opened.\r\n", data_port);
11701171
}
11711172

1172-
return false;
1173+
return NETPROTO_ERR_NONE;
11731174
}
11741175

11751176
/** FTP VERBS **********************************************************************************/

0 commit comments

Comments
 (0)