VC++ Windows 平台通过QOS2库函数设置Socket DSCP(IP_TOS)参数选项

本体提供的函数实现,只能对于TCP生效,UDP没法生效,看文档没搞明白,有了解UDP怎么设置DSCP的童鞋,可以在评论区给予答复。

用法是这样的,每个TCP在建立链接后,立即创建这个QOSS的实例,当然也可以在链接前,但在连接前有一定限制就是说,你必须在连接前把IP+PORT地址传进去,链接建立后在创建QOS就不需要传入IP+PORT地址。

同时,在关联的TCP链接关闭时需要销毁这个,否则就会产生QOS2内核句柄资源泄漏。

DSCP值26,就等于 IP_TOS: 0x68,即FLASH,IPTOS_LOWDELAY 宏的效果【MACOS/LINUX】上,Windows 平台被微软禁用支援了。

头文件声明:

cpp 复制代码
        class QoSS final
        {
        public:
            QoSS(int fd) noexcept;
            ~QoSS() noexcept;

        public:
            static std::shared_ptr<QoSS>            New(int fd, const boost::asio::ip::address& host, int port) noexcept { return New(fd, host, port, false); }
            static std::shared_ptr<QoSS>            New(int fd) noexcept { return New(fd, boost::asio::ip::address_v4::any(), 0, true); }

        private:
            static std::shared_ptr<QoSS>            New(int fd, const boost::asio::ip::address& host, int port, bool noaddress) noexcept;

        private:
            int                                     fd_ = -1;
            void*                                   h_  = NULL;
            DWORD                                   f_  = 0;
        };

源文件:

cpp 复制代码
        QoSS::QoSS(int fd) noexcept
            : fd_(fd)
            , h_(NULL)
            , f_(NULL)
        {

        }

        QoSS::~QoSS() noexcept
        {
            if (NULL != h_)
            {
                if (f_ != 0)
                {
                    QOSRemoveSocketFromFlow(h_, fd_, f_, 0);
                }

                QOSCloseHandle(h_);
            }
        }

        std::shared_ptr<QoSS> QoSS::New(int fd, const boost::asio::ip::address& host, int port, bool noaddress) noexcept
        {
            if (fd == INVALID_SOCKET)
            {
                return NULL;
            }

            std::shared_ptr<QoSS> qos = make_shared_object<QoSS>(fd);
            if (NULL == qos)
            {
                return NULL;
            }

            QOS_VERSION ver = { 1, 0 };
            if (!QOSCreateHandle(&ver, &qos->h_))
            {
                return NULL;
            }

            if (noaddress)
            {
                if (!QOSAddSocketToFlow(qos->h_, fd, NULL, QOSTrafficTypeControl, QOS_NON_ADAPTIVE_FLOW, &qos->f_))
                {
                    return NULL;
                }
            }
            else
            {
                if (port <= IPEndPoint::MinPort || port > IPEndPoint::MaxPort)
                {
                    return NULL;
                }

                if (!host.is_v4() && !host.is_v6())
                {
                    return NULL;
                }

                if (IPEndPoint::IsInvalid(host))
                {
                    return NULL;
                }

                if (host.is_v4())
                {
                    sockaddr_in in{};
                    in.sin_family = AF_INET;
                    in.sin_port = htons(port);
                    in.sin_addr.s_addr = htonl(host.to_v4().to_uint());

                    if (!QOSAddSocketToFlow(qos->h_, fd, reinterpret_cast<sockaddr*>(&in), QOSTrafficTypeControl, QOS_NON_ADAPTIVE_FLOW, &qos->f_))
                    {
                        return NULL;
                    }
                }
                else
                {
                    sockaddr_in6 in6{};
                    in6.sin6_family = AF_INET6;
                    in6.sin6_port = htons(port);
                    memcpy(&in6.sin6_addr, host.to_v6().to_bytes().data(), sizeof(in6.sin6_addr));

                    if (!QOSAddSocketToFlow(qos->h_, fd, reinterpret_cast<sockaddr*>(&in6), QOSTrafficTypeControl, QOS_NON_ADAPTIVE_FLOW, &qos->f_))
                    {
                        return NULL;
                    }
                }
            }

            // We shift the complete ToS value by 3 to get rid of the 3 bit ECN field
            DWORD dscp = 26;

            // Sets DSCP to the same as Linux
            // This will fail if we're not admin, but we ignore it
            if (!QOSSetFlow(qos->h_, qos->f_, QOSSetOutgoingDSCPValue, sizeof(DWORD), &dscp, 0, NULL))
            {
                return NULL;
            }

            return qos;
        }

参考文档:

https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options#windows-support-for-ip_proto-options

https://learn.microsoft.com/en-us/windows-server/networking/technologies/qos/qos-policy-manage#advanced-qos-settings-dscp-marking-override

https://learn.microsoft.com/en-us/windows-server/networking/technologies/qos/qos-policy-manage#advanced-qos-settings-dscp-marking-override

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/dd874008(v=vs.85)

相关推荐
大今野4 小时前
第 17 章 IP 编址与子网划分
网络·网络协议·tcp/ip·专转本
同步天下-刘经理4 小时前
NTP授时服务器时间抖动根源深度解析
网络
feasibility.6 小时前
电脑一天重启3次、WiFi还消失:蓝屏0x9F背后,是MT7921网卡撞上Windows电源管理
服务器·网络·windows·wifi·电脑·网卡·驱动
顧棟6 小时前
INFINI Gateway 启动过程分析
服务器·网络·gateway
xbzb8 小时前
服务器与网络:从一台裸机到能联网跑服务(5 篇)
服务器·网络·ip·raid·blog.csdn.net·article
xurileidian9 小时前
第 21 篇 · 边玩边学Python · if elif else 与缩进归属
java·服务器·网络
mont evergreen12 小时前
青云宗秘录:libp2p 双节点聊天阵法源码卷
服务器·网络协议·p2p
FlightYe1 天前
音视频修炼之容器与协议(七):HTTP-MP4 / HTTP-FLV
linux·网络·网络协议·http·ffmpeg·音视频
jipinxiaofeihua1 天前
(文章8) 05章-C++泛型编程(下)
linux·运维·网络
回眸&啤酒鸭1 天前
【回眸】AI 大模型驱动 Tessy 单元测试集成效果全景展示
网络·人工智能·单元测试