@@ -61,7 +61,11 @@ func lookupNodeForAgent(agentCredID int) int {
6161 name = fmt .Sprintf ("node-%d" , agentCredID )
6262 }
6363
64- meshIP := nextMeshIP ()
64+ meshIP , err := nextMeshIP ()
65+ if err != nil {
66+ slog .Error ("mesh full, cannot auto-create node" , "agent_id" , agentCredID )
67+ return 0
68+ }
6569 _ , err = store .Exec (
6670 "INSERT INTO node (label, wg_pubkey, allowed_ips, agent_credential_id) VALUES (?,?,?,?)" ,
6771 name , "pending" , meshIP , agentCredID ,
@@ -77,12 +81,12 @@ func lookupNodeForAgent(agentCredID int) int {
7781}
7882
7983// nextMeshIP finds the next available IP in the 10.0.0.0/24 mesh.
80- // 10.0.0.1 is reserved for the server.
81- func nextMeshIP () string {
84+ // 10.0.0.1 is reserved for the server. Returns an error if all 253 addresses are exhausted.
85+ func nextMeshIP () ( string , error ) {
8286 var maxOctet int
8387 rows , err := store .Query ("SELECT allowed_ips FROM node" )
8488 if err != nil {
85- return "10.0.0.2/32"
89+ return "10.0.0.2/32" , nil
8690 }
8791 defer rows .Close ()
8892
@@ -108,9 +112,9 @@ func nextMeshIP() string {
108112 next = 2 // 10.0.0.1 is the server
109113 }
110114 if next > 254 {
111- next = 254
115+ return "" , fmt . Errorf ( "mesh full: all 253 addresses in use" )
112116 }
113- return fmt .Sprintf ("10.0.0.%d/32" , next )
117+ return fmt .Sprintf ("10.0.0.%d/32" , next ), nil
114118}
115119
116120// handleWGStatus processes a wg.status message from a node agent.
0 commit comments