[network/libktorrent] src/net: Use enum class for SOCKS enums

Jack Hill <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 8ee131f2d9f21d488c0ac8aa74d347b54b822abb by Jack Hill.
Committed on 14/08/2026 at 21:27.
Pushed by jackh into branch 'master'.

Use enum class for SOCKS enums

I didn't do this the first time around as I assumed it would require
lots of casting to/from Uint8, but actually we just read data directly
into/from the structs themselves.

M  +2    -1    src/net/socks.cpp
M  +22   -21   src/net/socks_p.h

https://invent.kde.org/network/libktorrent/-/commit/8ee131f2d9f21d488c0ac8aa74d347b54b822abb

diff --git a/src/net/socks.cpp b/src/net/socks.cpp
index 31c096b9..33be4f21 100644
--- a/src/net/socks.cpp
+++ b/src/net/socks.cpp
@@ -170,7 +170,7 @@ Socks::State Socks::handleAuthReply()
         return state;
     }
 
-    if (reply.version != socks5::Version::VERSION_5 || reply.method == 0xFF) {
+    if (reply.version != socks5::Version::VERSION_5 || reply.method == socks5::AuthMethod::NO_ACCEPTABLE_METHOD) {
         // Out(SYS_CON|LOG_DEBUG) << "socks5::AuthReply = " << reply.version << " " << reply.method << endl;
         state = FAILED;
         return state;
@@ -184,6 +184,7 @@ Socks::State Socks::handleAuthReply()
         sendUsernamePassword();
         break;
     case socks5::AuthMethod::GSSAPI:
+    case socks5::AuthMethod::NO_ACCEPTABLE_METHOD:
         break;
     }
     return state;
diff --git a/src/net/socks_p.h b/src/net/socks_p.h
index 39b8ec17..393a2c34 100644
--- a/src/net/socks_p.h
+++ b/src/net/socks_p.h
@@ -15,16 +15,16 @@ namespace net
 {
 namespace socks4
 {
-enum Version : bt::Uint8 {
+enum class Version : bt::Uint8 {
     VERSION_4 = 0x04,
 };
 
-enum Command : bt::Uint8 {
+enum class Command : bt::Uint8 {
     CONNECT = 0x01,
     BIND = 0x02,
 };
 
-enum Reply : bt::Uint8 {
+enum class Reply : bt::Uint8 {
     OK = 0x5a,
     FAILED = 0x5b,
     FAILED_2 = 0x5c,
@@ -32,8 +32,8 @@ enum Reply : bt::Uint8 {
 };
 
 struct ConnectRequest {
-    bt::Uint8 version;
-    bt::Uint8 cmd;
+    Version version;
+    Command cmd;
     bt::Uint16 port;
     bt::Uint8 ip[4];
     char user_id[100];
@@ -46,30 +46,30 @@ struct ConnectRequest {
 
 struct ConnectReply {
     bt::Uint8 null_byte;
-    bt::Uint8 reply;
+    Reply reply;
     bt::Uint8 dummy[6];
 };
 } // namespace socks4
 
 namespace socks5
 {
-enum Version : bt::Uint8 {
+enum class Version : bt::Uint8 {
     VERSION_5 = 0x05,
 };
 
-enum AddressType : bt::Uint8 {
+enum class AddressType : bt::Uint8 {
     ADDR_IPV4 = 0x01,
     ADDR_DOMAIN = 0x03,
     ADDR_IPV6 = 0x04,
 };
 
-enum Command : bt::Uint8 {
+enum class Command : bt::Uint8 {
     CONNECT = 0x01,
     BIND = 0x02,
     UDP_ASSOCIATE = 0x03,
 };
 
-enum Reply : bt::Uint8 {
+enum class Reply : bt::Uint8 {
     OK = 0x00, // succeeded
     SERVER_FAILURE = 0x01, // general SOCKS server failure
     NOT_ALLOWED = 0x02, // connection not allowed by ruleset
@@ -81,16 +81,17 @@ enum Reply : bt::Uint8 {
     ADDR_TYPE_NOT_SUPPORTED = 0x08,
 };
 
-enum AuthMethod : bt::Uint8 {
+enum class AuthMethod : bt::Uint8 {
     NONE = 0x00,
     GSSAPI = 0x01,
     USERNAME_PASSWORD = 0x02,
+    NO_ACCEPTABLE_METHOD = 0xFF,
 };
 
 struct AuthRequest {
-    bt::Uint8 version;
+    Version version;
     bt::Uint8 nmethods;
-    bt::Uint8 methods[5];
+    AuthMethod methods[5];
 
     [[nodiscard]] int size() const
     {
@@ -99,15 +100,15 @@ struct AuthRequest {
 };
 
 struct AuthReply {
-    bt::Uint8 version;
-    bt::Uint8 method;
+    Version version;
+    AuthMethod method;
 };
 
 struct ConnectRequest {
-    bt::Uint8 version;
-    bt::Uint8 cmd;
+    Version version;
+    Command cmd;
     bt::Uint8 reserved;
-    bt::Uint8 address_type;
+    AddressType address_type;
     union {
         struct {
             bt::Uint8 ip[4];
@@ -127,10 +128,10 @@ struct ConnectRequest {
 };
 
 struct ConnectReply {
-    bt::Uint8 version;
-    bt::Uint8 reply;
+    Version version;
+    Reply reply;
     bt::Uint8 reserved;
-    bt::Uint8 address_type;
+    AddressType address_type;
 #if 0
     union {
         bt::Uint8 ip_v4[4];
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.