| 1 | # Network Mask Configuration Guide |
| 2 | |
| 3 | This guide explains how to use the inline network-mask syntax of |
| 4 | `listening_addresses` to get precise subnet-aware transport selection in |
| 5 | multi-NIC setups. |
| 6 | |
| 7 | ## Overview |
| 8 | |
| 9 | `GoveeController` listens on one or more local IP addresses. When multiple |
| 10 | addresses are provided, it must decide which socket to use to talk to a given |
| 11 | device. Two strategies are available: |
| 12 | |
| 13 | - **Heuristic matching** (default, no mask): same `/24`, plus special cases |
| 14 | for `10.0.0.0/8` and `192.168.0.0/16`. |
| 15 | - **Precise subnet matching** (mask provided): the device IP is tested |
| 16 | against the parsed network of each listening interface. |
| 17 | |
| 18 | The mask is supplied **inline in the address string**. There is no separate |
| 19 | `network_masks` parameter. |
| 20 | |
| 21 | ## Syntax |
| 22 | |
| 23 | `listening_addresses` accepts a single string or a list of strings. Each entry |
| 24 | may carry an optional mask in either CIDR or dotted-decimal form: |
| 25 | |
| 26 | ```text |
| 27 | "192.168.1.100" # no mask → heuristic matching |
| 28 | "192.168.1.100/24" # CIDR |
| 29 | "192.168.1.100/255.255.255.0" # dotted netmask |
| 30 | "0.0.0.0" # wildcard (mask is ignored if present) |
| 31 | ``` |
| 32 | |
| 33 | ## Examples |
| 34 | |
| 35 | ### Single interface, no mask |
| 36 | |
| 37 | ```python |
| 38 | from govee_local_api import GoveeController |
| 39 | |
| 40 | controller = GoveeController(listening_addresses="192.168.1.100") |
| 41 | ``` |
| 42 | |
| 43 | ### Single interface with CIDR |
| 44 | |
| 45 | ```python |
| 46 | controller = GoveeController(listening_addresses="192.168.1.100/24") |
| 47 | ``` |
| 48 | |
| 49 | ### Multiple interfaces, mixed notation |
| 50 | |
| 51 | ```python |
| 52 | controller = GoveeController( |
| 53 | listening_addresses=[ |
| 54 | "192.168.1.100/24", # CIDR |
| 55 | "10.0.0.100/255.0.0.0", # dotted netmask |
| 56 | "172.16.1.100/20", # CIDR |
| 57 | ], |
| 58 | ) |
| 59 | ``` |
| 60 | |
| 61 | ### Wildcard plus specific (wildcard is dropped) |
| 62 | |
| 63 | ```python |
| 64 | controller = GoveeController( |
| 65 | listening_addresses=["0.0.0.0", "192.168.1.100/24"], |
| 66 | ) |
| 67 | # Effective listening_addresses: ["192.168.1.100"] |
| 68 | ``` |
| 69 | |
| 70 | When `0.0.0.0` is combined with any specific address, the wildcard entry is |
| 71 | dropped to avoid duplicate packet processing (`0.0.0.0` receives on every |
| 72 | local interface). |
| 73 | |
| 74 | ## Transport selection |
| 75 | |
| 76 | For each outbound message the controller picks a transport as follows: |
| 77 | |
| 78 | 1. If only one transport exists, use it. |
| 79 | 2. Otherwise, for each non-wildcard listening address: |
| 80 | - If it has a parsed network → test `target_ip in network` (precise match). |
| 81 | - If it has no mask → apply the heuristic. |
| 82 | 3. If no match is found, return the first non-wildcard transport. |
| 83 | 4. As a last resort, return the first transport. |
| 84 | |
| 85 | ## Diagnostics |
| 86 | |
| 87 | The controller exposes the parsed configuration for inspection: |
| 88 | |
| 89 | ```python |
| 90 | controller.listening_addresses # ['192.168.1.100', '10.0.0.100'] |
| 91 | controller.networks # [IPv4Network('192.168.1.0/24'), None] |
| 92 | ``` |
| 93 | |
| 94 | `controller.networks[i]` is `None` for entries supplied without a mask and |
| 95 | for `0.0.0.0`. |
| 96 | |
| 97 | ## Invalid masks |
| 98 | |
| 99 | If the mask portion fails to parse, the address is kept and the network is |
| 100 | recorded as `None`; the entry falls back to heuristic matching. The address |
| 101 | itself is **not** validated by the parser — an invalid IP will surface later |
| 102 | as an `OSError` when the socket is bound in `start()`. |