/* ****************************************************************************** * INTERPEAK SOURCE FILE * * Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipdhcpr_cmd_dhcpr.c,v $ $Revision: 1.13.10.1 $ * $Source: /home/interpeak/CVSRoot/ipdhcpr/src/ipdhcpr_cmd_dhcpr.c,v $ * $Author: dgocheno $ * $State: Exp $ $Locker: $ * * INTERPEAK_COPYRIGHT_STRING * Design and implementation by Jonas Rendel ****************************************************************************** */ /* **************************************************************************** * 1 DESCRIPTION **************************************************************************** */ /* **************************************************************************** * 2 CONFIGURATION **************************************************************************** */ #include "ipdhcpr_config.h" /* **************************************************************************** * 3 INCLUDE FILES **************************************************************************** */ #ifdef IPDHCPR_INCLUDE_CMD #define IPCOM_USE_CLIB_PROTO #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ipdhcpr.h" #include "ipdhcpr_h.h" /* **************************************************************************** * 4 DEFINES **************************************************************************** */ #define IPDHCPR_MAX_CMD (sizeof(ipdhcpr_cmd_callback)/sizeof(Ipdhcpr_cmd_cb)) #define IPDHCPR_MAX_SUBCMD (sizeof(ipdhcpr_subcmd)/sizeof(char*)) #define IPDHCPR_SUBCMD_LST (0) #define IPDHCPR_SUBCMD_ADD (1) #define IPDHCPR_SUBCMD_DEL (2) #define IPDHCPR_SUBCMD_ENA (3) #define IPDHCPR_SUBCMD_DIS (4) #define IPDHCPR_SUBCMD_OPT82 (5) /* **************************************************************************** * 5 TYPES **************************************************************************** */ typedef struct Ipdhcpr_cmd_cb_struct { char* cmd; Ip_err (*cmd_cb) (int argc, char *argv[], int subcmd); } Ipdhcpr_cmd_cb; /* **************************************************************************** * 6 EXTERN PROTOTYPES **************************************************************************** */ IP_EXTERN Ipdhcpr_data ipdhcprc; /* **************************************************************************** * 7 LOCAL PROTOTYPES **************************************************************************** */ IP_STATIC void ipdhcpr_server_print (void *obj, void *data); IP_STATIC void ipdhcpr_interface_print(void *obj, void *data); IP_STATIC void ipdhcpr_errmsg_print(Ip_err retval); IP_STATIC Ip_err ipdhcpr_handle_cmd_server (int argc, char *argv[], int subcmd); IP_STATIC Ip_err ipdhcpr_handle_cmd_interface (int argc, char *argv[], int subcmd); IP_STATIC Ip_err ipdhcpr_handle_cmd_option82 (int argc, char *argv[], int subcmd); /* **************************************************************************** * 8 DATA **************************************************************************** */ /* *=========================================================================== * ipdhcpr_subcmd *=========================================================================== * Description: Text info representing the valid dhcpr subcommands * */ IP_STATIC char* ipdhcpr_subcmd[] = { "list", "add", "delete", "enable", "disable", "option82" }; /* *=========================================================================== * ipdhcpr_cmd_cb *=========================================================================== * Description: Ipdhcpr command callback struct. */ IP_STATIC Ipdhcpr_cmd_cb ipdhcpr_cmd_callback[] = { { "server", ipdhcpr_handle_cmd_server }, { "interface", ipdhcpr_handle_cmd_interface }, { "option82", ipdhcpr_handle_cmd_option82 } }; /* **************************************************************************** * 9 STATIC FUNCTIONS **************************************************************************** */ IP_STATIC void ipdhcpr_printf(const char *format, /*args*/ ...) { char *buf; va_list ap; Ip_s32 retval; buf = ipcom_malloc(IPCOM_STDIO_BUFSIZE); ip_assert(buf); va_start(ap, format); retval = ipcom_vsnprintf(buf, IPCOM_STDIO_BUFSIZE, format, ap); retval = IP_MIN(retval, (int) IPCOM_STDIO_BUFSIZE); va_end(ap); if(retval > 0) { if(ipdhcprc.preconfiguring == IP_TRUE) { if(buf[0] != '\n') IPCOM_LOG0(INFO,buf); } else (void)ipcom_printf(buf); } ipcom_free(buf); } /* *=========================================================================== * ipdhcpr_cmd_print_usage *=========================================================================== * Description: Prints the documentation for the command. * Parameters: * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR_FAILED - * */ IP_STATIC Ip_err ipdhcpr_cmd_print_usage(void) { ipdhcpr_printf(IP_LF); ipdhcpr_printf("NAME" IP_LF); ipdhcpr_printf(" dhcpr - Control Interpeak DHCP/BOOTP relay agent" IP_LF); ipdhcpr_printf(IP_LF); ipdhcpr_printf("SYNOPSIS" IP_LF); ipdhcpr_printf(" dhcpr server {list | add {addr} | delete {addr} |" IP_LF); ipdhcpr_printf(" option82 {addr} {{enable auth {in raw hex format}} |" IP_LF); ipdhcpr_printf(" {disable auth}}}" IP_LF); ipdhcpr_printf(" dhcpr interface {list | enable {name} | disable {name} |" IP_LF); ipdhcpr_printf(" option82 {name} {{circuit-id {in raw hex format}} |" IP_LF); ipdhcpr_printf(" {remote-id {in raw hex format}} |" IP_LF); ipdhcpr_printf(" disable}}" IP_LF); ipdhcpr_printf(" dhcpr option82 {list | enable {auth | drop} | disable {auth | drop}}" IP_LF); ipdhcpr_printf(IP_LF); ipdhcpr_printf("DESCRIPTION" IP_LF); ipdhcpr_printf(" The dhcpr program starts, stops and configures the Interpeak DHCP/BOOTP" IP_LF); ipdhcpr_printf(" relay agent." IP_LF); ipdhcpr_printf(IP_LF); ipdhcpr_printf(" server Show or change the configured list of servers called by" IP_LF); ipdhcpr_printf(" by the DHCP/BOOTP relay agent." IP_LF); ipdhcpr_printf(" interface List or enable/disable interfaces used by the dhcp relay agent." IP_LF); ipdhcpr_printf(" option82 Enable/disable DHCP relay agent Authentication and the drop" IP_LF); ipdhcpr_printf(" policy if incoming packet has no Authentication suboption." IP_LF); ipdhcpr_printf(IP_LF); #if 0 ipdhcpr_printf("EXAMPLES" IP_LF); #endif return IPCOM_SUCCESS; } /* *=========================================================================== * ipdhcpr_cmd_server *=========================================================================== * Description: Handles the server command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * subcmd - sub command descriptor. * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpr_handle_cmd_server(int argc, char *argv[], int subcmd) { Ip_err retval; Ip_u32 addr; switch(subcmd) { case IPDHCPR_SUBCMD_LST: ipdhcpr_printf(" Registered servers"IP_LF); retval = ipdhcpr_server_for_each(ipdhcpr_server_print, IP_NULL); ipdhcpr_printf(IP_LF); break; case IPDHCPR_SUBCMD_ADD: if(argc != 1) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); if(ipcom_inet_pton(IP_AF_INET, argv[0], &addr) != 1) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_CONFIG); retval = ipdhcpr_server_add(addr); break; case IPDHCPR_SUBCMD_DEL: if(argc != 1) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); if(ipcom_inet_pton(IP_AF_INET, argv[0], &addr) != 1) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_CONFIG); retval = ipdhcpr_server_delete(addr); break; /* dhcpr server option82 {addr} {{enable auth {in raw hex format}} | {disable auth}} */ case IPDHCPR_SUBCMD_OPT82: { Ip_bool auth_en = IP_FALSE; char *auth_key = IP_NULL; if ((argc < 3) || (argc > 4)) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); if (ipcom_inet_pton(IP_AF_INET, argv[0], &addr) != 1) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_CONFIG); if (ipcom_strcmp("enable", argv[1]) == 0) { if (4 != argc) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); auth_key = argv[3]; auth_en = IP_TRUE; } else { if (ipcom_strcmp("disable", argv[1]) != 0) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); } if (ipcom_strcmp("auth", argv[2]) != 0) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); retval = ipdhcpr_server_option82(addr, auth_en, auth_key); break; } default: IPDHCPR_LEAVE(IPCOM_ERR_FAILED); } leave: ipdhcpr_printf(IP_LF); ipdhcpr_errmsg_print(retval); return(retval); } /* *=========================================================================== * ipdhcpr_cmd_interface *=========================================================================== * Description: Handles the interface command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * subcmd - sub command descriptor. * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpr_handle_cmd_interface (int argc, char *argv[], int subcmd) { Ip_err retval; switch(subcmd) { case IPDHCPR_SUBCMD_LST: retval = ipdhcpr_interface_for_each(ipdhcpr_interface_print, IP_NULL); break; case IPDHCPR_SUBCMD_ENA: if (argc < 1) /* check that an interface was specified */ retval = IPCOM_ERR_INVALID_ARG; else retval = ipdhcpr_interface_status_set(argv[0], IP_TRUE); break; case IPDHCPR_SUBCMD_DIS: if (argc < 1) /* check that an interface was specified */ retval = IPCOM_ERR_INVALID_ARG; else retval = ipdhcpr_interface_status_set(argv[0], IP_FALSE); break; /* dhcpr interface option82 {name} {{circuit-id {in raw hex format}} | * {remote-id {in raw hex format}}} * 0 */ case IPDHCPR_SUBCMD_OPT82: { char *circuit_id = IP_NULL; char *remote_id = IP_NULL; if ((argc < 2) || (argc > 5)) { retval = IPCOM_ERR_INVALID_ARG; break; } if ((0 != ipcom_strcmp("circuit-id", argv[1])) && (0 != ipcom_strcmp("remote-id", argv[1])) && (0 != ipcom_strcmp("disable", argv[1]))) { retval = IPCOM_ERR_INVALID_ARG; break; } if (0 == ipcom_strcmp("circuit-id", argv[1])) { if (argc < 3) /* exclude only configure with 'circuit-id' */ { retval = IPCOM_ERR_INVALID_ARG; break; } circuit_id = argv[2]; if (argc == 5) { if (0 == ipcom_strcmp("remote-id", argv[3])) { remote_id = argv[4]; } else { retval = IPCOM_ERR_INVALID_ARG; break; } } } else /* remote-id */ { if (0 == ipcom_strcmp("remote-id", argv[1])) { if (argc < 3) /* exclude only configure with 'remote-id' */ { retval = IPCOM_ERR_INVALID_ARG; break; } remote_id = argv[2]; if (argc == 5) { if (0 == ipcom_strcmp("circuit-id", argv[3])) { circuit_id = argv[4]; } else { retval = IPCOM_ERR_INVALID_ARG; break; } } } } retval = ipdhcpr_interface_option82(argv[0], circuit_id, remote_id); break; } default: retval = IPCOM_ERR_INVALID_ARG; } ipdhcpr_printf(IP_LF); ipdhcpr_errmsg_print(retval); return retval; } /* *=========================================================================== * ipdhcpr_handle_cmd_option82 *=========================================================================== * Description: Handles the OPTION82 command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * subcmd - sub command descriptor. * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpr_handle_cmd_option82 (int argc, char *argv[], int subcmd) { Ip_err retval = 0; switch(subcmd) { case IPDHCPR_SUBCMD_LST: ipdhcpr_printf(" DHCP relay agent authentication is %s"IP_LF, ipdhcprc.enable_ra_auth ? "enabled" : "disabled"); ipdhcpr_printf(" DHCP relay agent drop policy is %s"IP_LF, ipdhcprc.enable_drop ? "enabled" : "disabled"); break; case IPDHCPR_SUBCMD_ENA: { if (1 == argc) { if (0 == ipcom_strcmp(argv[0], "auth")) { retval = ipdhcpr_option82_auth_set(IP_TRUE); } else { if (0 == ipcom_strcmp(argv[0], "drop")) retval = ipdhcpr_option82_drop_set(IP_TRUE); else retval = IPCOM_ERR_INVALID_ARG; } } else { retval = IPCOM_ERR_INVALID_ARG; } break; } case IPDHCPR_SUBCMD_DIS: { if (1 == argc) { if (0 == ipcom_strcmp(argv[0], "auth")) { retval = ipdhcpr_option82_auth_set(IP_FALSE); } else { if (0 == ipcom_strcmp(argv[0], "drop")) retval = ipdhcpr_option82_drop_set(IP_FALSE); else retval = IPCOM_ERR_INVALID_ARG; } } else { retval = IPCOM_ERR_INVALID_ARG; } break; } default: retval = IPCOM_ERR_INVALID_ARG; } ipdhcpr_printf(IP_LF); ipdhcpr_errmsg_print(retval); return retval; } /* *=========================================================================== * ipdhcpr_subnet_print *=========================================================================== * Description: Callback function used for printing server information * Parameters: obj - Pointer to server descriptor * data - not used * * Returns: - * */ IP_STATIC void ipdhcpr_server_print( void *obj, void *data) { Ipdhcpr_server *s =(Ipdhcpr_server*)obj; char buf[20]; (void)data; (void)ipcom_inet_ntop(IP_AF_INET, &s->saddr, buf, sizeof(buf)); ipdhcpr_printf(" %s :: shareKey : %s, firstPacket : %s, curSendRDCount : %u, lastRecvRDCount : %u"IP_LF, buf, s->shareKey ? s->shareKey : "null", s->notFirstPacket ? "FALSE" : "TRUE", s->curSendRDCount, s->lastRecvRDCount); } /* *=========================================================================== * ipdhcpr_interface_print *=========================================================================== * Description: Callback function used for printing interface information. * Parameters: obj - Pointer to interface descriptor * data - not used. * * Returns: - * */ IP_STATIC void ipdhcpr_interface_print(void *obj, void *data) { Ipdhcpr_if *i = (Ipdhcpr_if*)obj; char buf [20]; (void)data; (void)ipcom_inet_ntop(IP_AF_INET, &i->ifaddr, buf, sizeof(buf)); ipdhcpr_printf(" Interface %s (%s) :: %s, circuit-ID : %s, remote-ID : %s"IP_LF, i->ifname, buf, i->enabled == IP_TRUE ? "enabled" : "disabled", i->circuitID ? i->circuitID : "", i->remoteID ? i->remoteID : ""); } /* *=========================================================================== * ipdhcpr_errmsg_print *=========================================================================== * Description: Prints error messages in clear text. * Parameters: code - error code * Returns: - * */ IP_STATIC void ipdhcpr_errmsg_print(Ip_err code) { switch(code) { case IPCOM_ERR_INVALID_ARG: ipdhcpr_printf("failed :: invalid argument"IP_LF); break; case IPCOM_ERR_INVALID_CONFIG: ipdhcpr_printf("failed :: invalid config"IP_LF); break; case IPCOM_ERR_NOT_FOUND: ipdhcpr_printf("failed :: not found"IP_LF); break; case IPCOM_ERR_NO_MEMORY: ipdhcpr_printf("failed :: out of memory"IP_LF); break; case IPCOM_ERR_ALREADY_INSTALLED: ipdhcpr_printf("failed :: already exists"IP_LF); break; case IPCOM_ERR_NOT_OPENED: ipdhcpr_printf("failed :: ipdhcpr not open"IP_LF); break; default: break; } } /* **************************************************************************** * 10 GLOBAL FUNCTIONS **************************************************************************** */ /* *=========================================================================== * ipdhcpr_cmd_dhcpr *=========================================================================== * Description: Shell command used to start,stop and configure the DHCP * relay agent. * Parameters: argc - The number of arguments passed to the command. * argv - The arguments passed to the command. * Returns: 0 = success, * */ IP_GLOBAL int ipdhcpr_cmd_dhcpr(int argc, char **argv) { Ip_err retval; int i, s; retval = IPCOM_ERR_INVALID_ARG; if(argc < 2) IPDHCPR_LEAVE(IPCOM_ERR_INVALID_ARG); for(i = 0; i < IPDHCPR_MAX_CMD; i++) { if(ipcom_strcmp(argv[1], ipdhcpr_cmd_callback[i].cmd) == 0) { for(s = IPDHCPR_MAX_SUBCMD; s > 0 && argc > 2; s--) if(ipcom_strcmp(argv[2], ipdhcpr_subcmd[s - 1]) == 0) break; retval = ipdhcpr_cmd_callback[i].cmd_cb(argc-3, &argv[3], s - 1); break; } } leave: if(retval == IPCOM_ERR_INVALID_ARG) (void) ipdhcpr_cmd_print_usage(); return retval; } #endif /* #ifdef IPDHCPR_INCLUDE_CMD */ /* **************************************************************************** * 11 PUBLIC FUNCTIONS **************************************************************************** */ /* **************************************************************************** * END OF FILE **************************************************************************** */