-
Notifications
You must be signed in to change notification settings - Fork 888
Description
Bug Description
Bandwidth limiting functionality in 3proxy fails or behaves incorrectly for high bandwidth values. The bandwidth either becomes unlimited or gets capped at unexpected values when using rates significantly above typical values.
Root Cause
The issue is located in the bandlimitfunc function in auth.c (line ~570). The problem occurs due to integer overflow/precision loss in the timing calculation:
((nbytesin+32)/64)*(((64*8*1000000)/param->bandlims[i]->rate))
When rate is high, the expression (6481000000)/param->bandlims[i]->rate can result in very small values or zero due to integer division, breaking the bandwidth limiting algorithm.
Mathematical analysis shows the critical threshold is around:
6481000000 = 512,000,000
For rates approaching or exceeding this value, integer division causes precision loss or zero results
Steps to Reproduce
- Configure 3proxy with high bandwidth limits (e.g., bandlimin 300000000 for 300 Mbps)
- Test actual bandwidth usage
- Observe that bandwidth limiting either doesn't work or works incorrectly
Expected Behavior
Bandwidth should be properly limited to the specified rate regardless of the value (within reasonable limits).
Confirmed Solution
Replace integer arithmetic with floating-point calculations to maintain precision:
param->bandlims[i]->nexttime = msec + nsleeptime + ((nbytesin > 512)?
(unsigned)((double)(nbytesin+32)/64.0 * (64.0*8.0*1000000.0)/(double)param->bandlims[i]->rate) :
(unsigned)((double)(nbytesin+1) * 8.0*1000000.0/(double)param->bandlims[i]->rate));
The same fix should be applied to the bandlimsout section of the code.
Environment
- 3proxy version: git master (commit: dc4e8d3, 2025-05-06)
- OS: Ubuntu 24.04.1 LTS
- Bandwidth rates tested: High values (300+ Mbps)
Additional Notes
- This issue affects both inbound (bandlimin) and outbound (bandlimout) bandwidth limiting
- The floating-point solution has been tested and resolves the issue
- The fix maintains backward compatibility with existing configurations