@@ -457,7 +457,7 @@ lua_add_dhcp_ip(lua_State *L)
457457 char * str ;
458458 int err ;
459459
460- if (optn < 1 || optn > 255 ) {
460+ if (optn < 1 || optn > 254 ) {
461461 logerrx ("%s: option out of range: %lld" , lua_name , optn );
462462 return 0 ;
463463 }
@@ -477,13 +477,68 @@ lua_add_dhcp_ip(lua_State *L)
477477}
478478
479479static int
480- lua_add_dhcp_u32 (lua_State * L )
480+ lua_add_dhcp_uint8 (lua_State * L )
481481{
482482 struct lua_ctx * l = & lua_ctx ;
483483 long long optn = luaL_checkinteger (L , 1 );
484484 long long data = luaL_checkinteger (L , 2 );
485+ uint8_t u8 ;
485486
486- if (optn < 1 || optn > 255 ) {
487+ if (optn < 1 || optn > 254 ) {
488+ logerrx ("%s: option out of range: %lld" , lua_name , optn );
489+ return 0 ;
490+ }
491+ if (data < 0 || data > UINT8_MAX ) {
492+ logerrx ("%s: data out of range: %lld" , lua_name , data );
493+ return 0 ;
494+ }
495+
496+ if (l -> l_p == NULL || l -> l_e == NULL ) {
497+ logerrx ("%s: cannot add options" , lua_name );
498+ return 0 ;
499+ }
500+
501+ u8 = (uint8_t )data ;
502+ DHCP_PUT_U8 (& l -> l_p , l -> l_e , (uint8_t )optn , u8 );
503+ return 0 ;
504+ }
505+
506+ static int
507+ lua_add_dhcp_uint16 (lua_State * L )
508+ {
509+ struct lua_ctx * l = & lua_ctx ;
510+ long long optn = luaL_checkinteger (L , 1 );
511+ long long data = luaL_checkinteger (L , 2 );
512+ uint16_t u16 ;
513+
514+ if (optn < 1 || optn > 254 ) {
515+ logerrx ("%s: option out of range: %lld" , lua_name , optn );
516+ return 0 ;
517+ }
518+ if (data < 0 || data > UINT16_MAX ) {
519+ logerrx ("%s: data out of range: %lld" , lua_name , data );
520+ return 0 ;
521+ }
522+
523+ if (l -> l_p == NULL || l -> l_e == NULL ) {
524+ logerrx ("%s: cannot add options" , lua_name );
525+ return 0 ;
526+ }
527+
528+ u16 = htons (data );
529+ DHCP_PUT_U16 (& l -> l_p , l -> l_e , (uint8_t )optn , u16 );
530+ return 0 ;
531+ }
532+
533+ static int
534+ lua_add_dhcp_uint32 (lua_State * L )
535+ {
536+ struct lua_ctx * l = & lua_ctx ;
537+ long long optn = luaL_checkinteger (L , 1 );
538+ long long data = luaL_checkinteger (L , 2 );
539+ uint32_t u32 ;
540+
541+ if (optn < 1 || optn > 254 ) {
487542 logerrx ("%s: option out of range: %lld" , lua_name , optn );
488543 return 0 ;
489544 }
@@ -497,7 +552,8 @@ lua_add_dhcp_u32(lua_State *L)
497552 return 0 ;
498553 }
499554
500- DHCP_PUT_U32 (& l -> l_p , l -> l_e , (uint8_t )optn , data );
555+ u32 = htonl (data );
556+ DHCP_PUT_U32 (& l -> l_p , l -> l_e , (uint8_t )optn , u32 );
501557 return 0 ;
502558}
503559
@@ -508,7 +564,7 @@ lua_add_dhcp_string(lua_State *L)
508564 long long optn = luaL_checkinteger (L , 1 );
509565 const char * data = luaL_checkstring (L , 2 );
510566
511- if (optn < 1 || optn > 255 ) {
567+ if (optn < 1 || optn > 254 ) {
512568 logerrx ("%s: option out of range: %lld" , lua_name , optn );
513569 return 0 ;
514570 }
@@ -719,7 +775,9 @@ lua_init(struct plugin *p)
719775 { "set_bootp_file" , lua_set_bootp_file },
720776 { "get_option" , lua_get_dhcp_option },
721777 { "add_ip" , lua_add_dhcp_ip },
722- { "add_u32" , lua_add_dhcp_u32 },
778+ { "add_uint8" , lua_add_dhcp_uint8 },
779+ { "add_uint16" , lua_add_dhcp_uint16 },
780+ { "add_uint32" , lua_add_dhcp_uint32 },
723781 { "add_string" , lua_add_dhcp_string },
724782 { NULL , NULL },
725783 };
0 commit comments