@@ -223,10 +223,12 @@ static void add(const char *subnet, struct lyd_node *cfg)
223223 start = lydx_get_cattr (node , "start-address" );
224224 end = lydx_get_cattr (node , "end-address" );
225225
226- fprintf (fp , "\n# Subnet pool %s - %s\n" , start , end );
227- fprintf (fp , "dhcp-range=%s%sset:%s,%s,%s,%s\n" ,
228- ifname ?: "" , ifname ? "," : "" , tag ,
229- start , end , lydx_get_cattr (node , "lease-time" ));
226+ if (start && end ) {
227+ fprintf (fp , "\n# Subnet pool %s - %s\n" , start , end );
228+ fprintf (fp , "dhcp-range=%s%sset:%s,%s,%s,%s\n" ,
229+ ifname ?: "" , ifname ? "," : "" , tag ,
230+ start , end , lydx_get_cattr (node , "lease-time" ));
231+ }
230232 }
231233
232234err :
@@ -400,6 +402,7 @@ static int cand(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
400402 "router" ,
401403 "dns-server" ,
402404 };
405+ sr_val_t * subnets = NULL ;
403406 size_t i , cnt = 0 ;
404407
405408 if (event != SR_EV_UPDATE && event != SR_EV_CHANGE )
@@ -413,6 +416,37 @@ static int cand(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
413416 srx_set_item (session , & inferred , 0 , fmt , opt [i ]);
414417 }
415418
419+ /* Infer pool: .100 to .250 for /24 networks */
420+ if (sr_get_items (session , CFG_XPATH "/subnet/subnet" , 0 , 0 , & subnets , & cnt ) == 0 ) {
421+ for (i = 0 ; i < cnt ; i ++ ) {
422+ const char * pool_xpathfmt = CFG_XPATH "/subnet[subnet='%s']/pool" ;
423+ const char * host_xpathfmt = CFG_XPATH "/subnet[subnet='%s']/host" ;
424+ const char * subnet = subnets [i ].data .string_val ;
425+ sr_val_t pool_val = { .type = SR_STRING_T };
426+ char start_addr [16 ], end_addr [16 ];
427+ unsigned int a , b , c , d , len ;
428+ size_t pool_cnt = 0 , host_cnt = 0 ;
429+
430+ if (sscanf (subnet , "%u.%u.%u.%u/%u" , & a , & b , & c , & d , & len ) != 5 || len != 24 )
431+ continue ;
432+
433+ /* Don't auto-infer if pool or static hosts already exist */
434+ if (!srx_nitems (session , & pool_cnt , pool_xpathfmt , subnet ) && pool_cnt )
435+ continue ;
436+ if (!srx_nitems (session , & host_cnt , host_xpathfmt , subnet ) && host_cnt )
437+ continue ;
438+
439+ snprintf (start_addr , sizeof (start_addr ), "%u.%u.%u.100" , a , b , c );
440+ snprintf (end_addr , sizeof (end_addr ), "%u.%u.%u.250" , a , b , c );
441+
442+ pool_val .data .string_val = start_addr ;
443+ srx_set_item (session , & pool_val , 0 , CFG_XPATH "/subnet[subnet='%s']/pool/start-address" , subnet );
444+ pool_val .data .string_val = end_addr ;
445+ srx_set_item (session , & pool_val , 0 , CFG_XPATH "/subnet[subnet='%s']/pool/end-address" , subnet );
446+ }
447+ sr_free_values (subnets , cnt );
448+ }
449+
416450 return 0 ;
417451}
418452
0 commit comments