Commit e2322af
Fix UdpClient.EnableBroadcast not preventing broadcast sends (#124482)
## Summary
Fixes #118055
`UdpClient.CheckForBroadcast()` unconditionally auto-enables the
`Broadcast` socket option when sending to `IPAddress.Broadcast`, even
when the user has explicitly set `EnableBroadcast = false`. This PR
makes `CheckForBroadcast()` respect the user's explicit choice.
## Changes
- Added `_isBroadcastSetByUser` field to track whether `EnableBroadcast`
has been explicitly set by the user
- Modified `CheckForBroadcast()` to skip auto-enable when the user has
explicitly set `EnableBroadcast`
- Backward-compatible: auto-enable behavior is preserved when users have
not explicitly set the property
**Note:** The pre-existing `_isBroadcast` flag is not reset when the
`Client` socket is replaced via the property setter. The new
`_isBroadcastSetByUser` flag follows this same pattern for consistency.
Resetting state on socket replacement could be a follow-up improvement.
## Testing
- `EnableBroadcast_ExplicitlyDisabled_NotAutoEnabled` — verifies that
explicitly disabling broadcast prevents auto-enable on broadcast send
- `EnableBroadcast_ExplicitlyEnabledThenDisabled_NotAutoEnabled` —
verifies the toggle case (enable → disable) also prevents auto-enable
- `EnableBroadcast_NotExplicitlySet_AutoEnabled` — verifies backward
compatibility: auto-enable still works when the user hasn't set the
property
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent b954bd7 commit e2322af
File tree
2 files changed
+55
-1
lines changed- src/libraries/System.Net.Sockets
- src/System/Net/Sockets
- tests/FunctionalTests
2 files changed
+55
-1
lines changedLines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| 174 | + | |
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
| |||
247 | 248 | | |
248 | 249 | | |
249 | 250 | | |
| 251 | + | |
250 | 252 | | |
251 | 253 | | |
252 | 254 | | |
253 | 255 | | |
254 | 256 | | |
255 | 257 | | |
256 | 258 | | |
257 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
258 | 263 | | |
259 | 264 | | |
260 | 265 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
323 | 372 | | |
324 | 373 | | |
325 | 374 | | |
| |||
0 commit comments