/* ****************************************************************************** * INTERPEAK SOURCE FILE * * * INTERPEAK_COPYRIGHT_STRING * * Design and implementation by Zhangqing Luo ****************************************************************************** */ /* **************************************************************************** * 1 DESCRIPTION **************************************************************************** */ /* **************************************************************************** * 2 CONFIGURATION **************************************************************************** */ #include "ipdhcpc_config.h" /* **************************************************************************** * 3 INCLUDE FILES **************************************************************************** */ #ifdef IPDHCPC_INCLUDE_CMD #define IPCOM_USE_CLIB_PROTO #include #include #include #include #include #include #include #include #include #include #include #include #include "ipdhcpc_h.h" /* **************************************************************************** * 4 DEFINES **************************************************************************** */ #define IPDHCPC_MAX_CMD (sizeof(ipdhcpc_cmd_callback)/sizeof(Ipdhcpc_cmd_cb)) /* **************************************************************************** * 5 TYPES **************************************************************************** */ typedef struct Ipdhcpc_cmd_cb_struct { char* cmd; Ip_err(*cmd_cb)(int argc, char *argv[]); } Ipdhcpc_cmd_cb; /* **************************************************************************** * 6 EXTERN PROTOTYPES **************************************************************************** */ IP_EXTERN Ipdhcpc_data ipdhcpc; /* **************************************************************************** * 7 LOCAL PROTOTYPES **************************************************************************** */ IP_STATIC void ipdhcpc_printf (const char *format, /*args*/ ...); IP_STATIC void ipdhcpc_errmsg_print (Ip_err code); IP_STATIC void ipdhcpc_state_print (int state); IP_STATIC void ipdhcpc_cmd_print_usage (void); IP_STATIC Ip_err ipdhcpc_handle_cmd_renew (int argc, char *argv[]); IP_STATIC Ip_err ipdhcpc_handle_cmd_inform (int argc, char *argv[]); IP_STATIC Ip_err ipdhcpc_handle_cmd_interface (int argc, char *argv[]); IP_STATIC Ip_err ipdhcpc_handle_cmd_lease (int argc, char *argv[]); /* **************************************************************************** * 8 DATA **************************************************************************** */ /* *=========================================================================== * ipdhcpc_cmd_cb *=========================================================================== * Description: Ipdhcpc command callback struct. */ IP_STATIC Ipdhcpc_cmd_cb ipdhcpc_cmd_callback[] = { { "renew", ipdhcpc_handle_cmd_renew }, { "inform", ipdhcpc_handle_cmd_inform }, { "lease", ipdhcpc_handle_cmd_lease }, { "interface", ipdhcpc_handle_cmd_interface } }; /* **************************************************************************** * 9 STATIC FUNCTIONS **************************************************************************** */ /* *=========================================================================== * ipdhcpc_printf *=========================================================================== * Description: Format and print the message. * Parameters: message formatted * Returns: - * */ IP_STATIC void ipdhcpc_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) { (void)ipcom_printf(buf); } ipcom_free(buf); } /* *=========================================================================== * ipdhcpc_errmsg_print *=========================================================================== * Description: Prints error messages in clear text. * Parameters: code - error code * Returns: - * */ IP_STATIC void ipdhcpc_errmsg_print(Ip_err code) { switch(code) { case IPCOM_ERR_INVALID_ARG: ipdhcpc_printf("failed :: invalid argument"IP_LF); break; case IPCOM_ERR_INVALID_CONFIG: ipdhcpc_printf("failed :: invalid config"IP_LF); break; case IPCOM_ERR_NOT_FOUND: ipdhcpc_printf("failed :: not found"IP_LF); break; case IPCOM_ERR_NO_MEMORY: ipdhcpc_printf("failed :: out of memory"IP_LF); break; case IPCOM_ERR_ALREADY_INSTALLED: ipdhcpc_printf("failed :: already exists"IP_LF); break; case IPCOM_ERR_NOT_OPENED: ipdhcpc_printf("failed :: ipdhcpc not open"IP_LF); break; case IPCOM_ERR_PERMISSION_DENIED: ipdhcpc_printf("failed :: not allowed"IP_LF); break; case IPCOM_ERR_ALREADY_STARTED: ipdhcpc_printf ("failed::already in processing"); break; default: break; } } /* *=========================================================================== * ipdhcpc_state_print *=========================================================================== * Description: Prints state of the dhcp * Parameters: state code * Returns: - * */ IP_STATIC void ipdhcpc_state_print (int state) { switch(state) { case IPDHCPC_STATE_INIT: ipdhcpc_printf(" State: Init"IP_LF); break; case IPDHCPC_STATE_INIT_REBOOT: ipdhcpc_printf(" State: Init reboot"IP_LF); break; case IPDHCPC_STATE_SELECTING: ipdhcpc_printf(" State: Selecting"IP_LF); break; case IPDHCPC_STATE_REBOOTING: ipdhcpc_printf(" State: Rebooting"IP_LF); break; case IPDHCPC_STATE_REQUESTING: ipdhcpc_printf(" State: Requesting"IP_LF); break; case IPDHCPC_STATE_BOUND: ipdhcpc_printf(" State: Bound"IP_LF); break; case IPDHCPC_STATE_RENEWING: ipdhcpc_printf(" State: Renewing"IP_LF); break; case IPDHCPC_STATE_REBINDING: ipdhcpc_printf(" State: Rebinding"IP_LF); break; case IPDHCPC_STATE_STOPPED: ipdhcpc_printf(" State: Stopped"IP_LF); break; case IPDHCPC_STATE_INFORMING: ipdhcpc_printf(" State: Informing"IP_LF); break; default: break; } } /* *=========================================================================== * ipdhcpc_handle_cmd_renew *=========================================================================== * Description: Handles the renew command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpc_handle_cmd_renew (int argc, char *argv[]) { Ip_err retval = IPCOM_SUCCESS; if (argc<1) IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG); /*got the interface name*/ retval = ipdhcpc_renew_get (argv[0]); ipdhcpc_printf(IP_LF); ipdhcpc_errmsg_print(retval); leave: return retval; } /* *=========================================================================== * ipdhcpc_handle_cmd_inform *=========================================================================== * Description: Handles the inform command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpc_handle_cmd_inform (int argc, char *argv[]) { Ip_err retval = IPCOM_SUCCESS; if (argc<1) IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG); retval = ipdhcpc_inform_get (argv[0], argv[1]); ipdhcpc_printf(IP_LF); ipdhcpc_errmsg_print(retval); leave: return retval; } /* *=========================================================================== * ipdhcpc_handle_cmd_interface *=========================================================================== * Description: Handles the interface command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpc_handle_cmd_interface (int argc, char *argv[]) { Ip_err retval = IPCOM_SUCCESS; Ipdhcpc_if *ift = IP_NULL; (void)argc; if (ipcom_strcmp ("list", argv[0])==0) { char * status = IP_NULL; int i = 0; IPDHCPC_LOCK(); /*show all interfaces status*/ for (ift = ipdhcpc.ift_head; ift != IP_NULL; ift = ift->next) { ipdhcpc_printf (""IP_LF); ipdhcpc_printf ("Details about interface"IP_LF); ipdhcpc_printf (" Name: %s"IP_LF, ift->ifname); ipdhcpc_printf (" Index: %d"IP_LF,ift->ifindex); ipdhcpc_printf (" HWaddr: "); for (;ihlen-1; i++) ipdhcpc_printf ("%x%x:",ift->chaddr[i]>>4, ift->chaddr[i]&0xf); ipdhcpc_printf ("%x%x",ift->chaddr[i]>>4, ift->chaddr[i]&0xf); ipdhcpc_printf (IP_LF); status = ift->isdown?"Down":"Up"; ipdhcpc_printf (" Status: %s"IP_LF,status); status = ift->information_only?"Enabled":"Disabled"; ipdhcpc_printf (" Information Only:%s"IP_LF,status); if (ift->handle != IP_NULL) /*show state*/ ipdhcpc_state_print (ift->handle->priv->state); ipdhcpc_printf (""IP_LF); } IPDHCPC_UNLOCK(); } else retval = IPCOM_ERR_INVALID_ARG; ipdhcpc_printf (IP_LF); ipdhcpc_errmsg_print (retval); return retval; } /* *=========================================================================== * ipdhcpc_handle_cmd_lease *=========================================================================== * Description: Handles the lease command. * Parameters: argc - number of command line arguments * argv - array of command line arguments * * Returns: IPCOM_SUCCESS - the operation was successful * IPCOM_ERR... - failed * */ IP_STATIC Ip_err ipdhcpc_handle_cmd_lease (int argc, char *argv[]) { Ip_err retval = IPCOM_SUCCESS; Ipdhcpc_if *ift = IP_NULL; struct Ip_in_addr addr; (void)argc; if (ipcom_strcmp ("list", argv[0])==0) { IPDHCPC_LOCK(); for (ift = ipdhcpc.ift_head; ift != IP_NULL; ift = ift->next) { if (ift->information_only == IP_TRUE) { ipdhcpc_printf (IP_LF); ipdhcpc_printf (" Interface %s is in INFORMATION_ONLY MODE, no lease from server"IP_LF, ift->ifname); } else if (ift->handle != IP_NULL) { if (ift->handle->priv->state>=IPDHCPC_STATE_BOUND) { ipdhcpc_printf (""IP_LF); ipdhcpc_printf ("Lease:"IP_LF); ipdhcpc_printf (" Index: %d"IP_LF, ift->ifindex); addr.s_addr = ift->handle->info.ipaddr_n; ipdhcpc_printf (" Client address: %s"IP_LF, ipcom_inet_ntoa(addr)); addr.s_addr = ift->handle->info.serveraddr_n; ipdhcpc_printf (" Server address: %s"IP_LF,ipcom_inet_ntoa(addr)); ipdhcpc_printf (" Duration: %ds"IP_LF, ift->lease.tlease); ipdhcpc_printf (""IP_LF); } else { ipdhcpc_printf (IP_LF); ipdhcpc_printf ("Performing dhcp"); ipdhcpc_state_print (ift->handle->priv->state); } } else ipdhcpc_printf ("Not started"IP_LF); } IPDHCPC_UNLOCK(); } else retval = IPCOM_ERR_INVALID_ARG; ipdhcpc_printf (IP_LF); ipdhcpc_errmsg_print (retval); return retval; } /* *=========================================================================== * ipdhcpc_cmd_print_usage *=========================================================================== * Description: Prints the documentation for the command. * Parameters: * Returns: IPCOM_SUCCESS - the operation was successful * * */ IP_STATIC void ipdhcpc_cmd_print_usage(void) { ipdhcpc_printf (IP_LF); ipdhcpc_printf ("NAME" IP_LF); ipdhcpc_printf (" dhcpc - Control Interpeak DHCP client"IP_LF); ipdhcpc_printf (IP_LF); ipdhcpc_printf ("SYNOPSIS" IP_LF); ipdhcpc_printf (" dhcpc renew {name}"IP_LF); ipdhcpc_printf (" dhcpc inform {name} [addr] "IP_LF); ipdhcpc_printf (" dhcpc lease list "IP_LF); ipdhcpc_printf (" dhcpc interface list"IP_LF); ipdhcpc_printf (IP_LF); ipdhcpc_printf ("DESCRIPTION" IP_LF); ipdhcpc_printf (" renew Trigger renew event manually."IP_LF); ipdhcpc_printf (" inform Trigger INFORM message. if no address set, select primary address as default"IP_LF); ipdhcpc_printf (" lease List current data in lease database."IP_LF); ipdhcpc_printf (" interface List interfaces used by the dhcp client."IP_LF); ipdhcpc_printf (IP_LF); ipdhcpc_printf ("EXAMPLES" IP_LF); ipdhcpc_printf (" dhcpc renew eth0"IP_LF); ipdhcpc_printf (" dhcpc inform eth0 128.224.158.82"IP_LF); ipdhcpc_printf (" dhcpc inform eth0"IP_LF); ipdhcpc_printf (" dhcpc lease list"IP_LF); ipdhcpc_printf (" dhcpc interface list"IP_LF); } /* **************************************************************************** * 10 GLOBAL FUNCTIONS **************************************************************************** */ /* *=========================================================================== * ipdhcpc_cmd_dhcpc *=========================================================================== * Description: Shell command used to start,stop and configure the DHCP * server. * Parameters: argc - The number of arguments passed to the command. * argv - The arguments passed to the command. * Returns: 0 = success, * */ IP_GLOBAL int ipdhcpc_cmd_dhcpc(int argc, char **argv) { Ip_err retval; int i; retval = IPCOM_ERR_INVALID_ARG; if(argc < 2) IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG); for(i = 0; i < (int)IPDHCPC_MAX_CMD; i++) { if(ipcom_strcmp(argv[1], ipdhcpc_cmd_callback[i].cmd) == 0) { retval = ipdhcpc_cmd_callback[i].cmd_cb(argc-2, &argv[2]); break; } } leave: if(retval == IPCOM_ERR_INVALID_ARG) ipdhcpc_cmd_print_usage(); return retval; } /* **************************************************************************** * 11 PUBLIC FUNCTIONS **************************************************************************** */ /* **************************************************************************** * END OF FILE **************************************************************************** */ /* #ifdef IPDHCPC_INCLUDE_CMD */ #endif