#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
static void printAppMenu(char *name)
{
printf("Usage: %s <test#> <arg1> <arg2> ... \n", name);
printf("Test 1: Get the DHCP Vendor Option Mode: %s 1 \n", name);
printf("Test 2: Set the DHCP Vendor Option Mode: %s 2 <mode> <actImmediate>\n", name);
printf("Test 3: Get the DHCP Vendor Class Option string: %s 3 \n", name);
printf("Test 4: Set the DHCP Vendor Class Option string: %s 4 <option-string> <actImmediateFlag>\n", name);
printf("Test 5: Get the DHCP server IP address on a management interface: %s 5 <ifNum> <mgmtPortType>\n", name);
printf("Test 6: Get the current state of the DHCP client for specified interface or management port: %s 6 <ifNum> <mgmtPortType>\n", name);
printf("Test 7: Get the transaction ID of the DHCP client for specified interface or management port: %s 7 <ifNum> <mgmtPortType>\n", name);
printf("Test 8: Get the lease time information of the DHCP client for specified interface or management port: %s 8 <ifNum> <mgmtPortType>\n", name);
printf("Test 9: Get the retry count of the DHCP client for specified interface or management port: %s 9 <ifNum> <mgmtPortType>\n", name);
printf("Test 10: Get the generated client identifier on the interface: %s 10 <ifNum> <mgmtPortType>\n", name);
printf("Test 11: Get the DHCP Client Statistics on an interface: %s 11 <ifNum>\n", name);
printf("Test 12: Clear the DHCP Client Statistics on an interface: %s 12 <ifNum>\n", name);
return;
}
{
uint32_t ip4_addr;
uint8_t addr8[16];
int af;
{
af = AF_INET;
ip4_addr = htonl(addr->
addr.
ipv4);
memcpy(addr8, &ip4_addr, sizeof(ip4_addr));
break;
af = AF_INET6;
memcpy(addr8, addr->addr.
ipv6.
u.
addr8,
sizeof(addr8));
break;
default:
return(NULL);
}
return(inet_ntop(af, addr8, buffer, INET6_ADDRSTRLEN));
}
{
switch(result)
{
printf("\nThe DHCP Vendor Option Mode is %u (1-Enabled, 0-Disabled).\n", mode);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the DHCP Vendor Option Mode. (result = %d)\n", result);
break;
}
return;
}
void dhcpVendorClassOptionAdminModeSet(
openapiClientHandle_t *client_handle, uint32_t adminMode, uint32_t flag)
{
if (1 < adminMode)
{
printf("\nERROR: Invalid adminMode value. Expected 0(for Disable) or 1(for Enable).");
return;
}
if (1 == adminMode)
{
}
if (1 < flag)
{
printf("\nERROR: Invalid flag value. Expected 0(for False) or 1(for True).");
return;
}
if (1 == flag)
{
}
switch(result)
{
printf("\nSuccessfully set the given DHCP Vendor Option Mode.\n");
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to set the DHCP Vendor Option Mode. (result = %d)\n", result);
break;
}
return;
}
{
memset(optStr, 0, sizeof(optStr));
bufd.
size =
sizeof(optStr);
switch(result)
{
printf("\nThe DHCP Vendor Class Option string is: %s.\n",(char *)(bufd.pstart));
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the DHCP Vendor Class Option string. (result = %d)\n", result);
break;
}
return;
}
void dhcpVendorClassOptionStringSet(
openapiClientHandle_t *client_handle,
char *optString, uint32_t actFlag)
{
if (1 < actFlag)
{
printf("\nERROR: Invalid flag value. Expected 0(for False) or 1(for True).");
return;
}
if (1 == actFlag)
{
}
memset(optStr, 0, sizeof(optStr));
{
printf("\nERROR: Invalid DHCP Vendor Class Option string.\n");
return;
}
strncpy(optStr, optString, sizeof(optStr) - 1);
bufd.pstart = optStr;
bufd.size = strlen(optStr) + 1;
switch(result)
{
printf("\nSuccessfully set the given DHCP Vendor Class Option string.\n");
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to set the DHCP Vendor Class Option string. (result = %d)\n", result);
break;
}
return;
}
void ipDhcpClientDhcpServerIntfIPAddressGet(
openapiClientHandle_t *client_handle, uint32_t ifNum, uint32_t mgmtType)
{
uint32_t serverAddr = 0;
char ipAddrStr[80] = {0};
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
memset(ipAddrStr, 0x0, sizeof(ipAddrStr));
openIpAddr.addr.ipv4 = serverAddr;
ipAddressFormat(&openIpAddr, ipAddrStr);
printf("\nThe DHCP server IP address on a management interface is: %s.\n", ipAddrStr);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the DHCP server IP address on a management interface (result = %d)\n", result);
break;
}
return;
}
{
OPEN_DHCP_CLIENT_STATE_t clientState;
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
printf("\nThe current state of the DHCP client for specified interface or management port is: %u.\n", clientState);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the current state of the DHCP client for specified interface or management port (result = %d)\n", result);
break;
}
return;
}
void ipDhcpClientTransactionIntfIdGet(
openapiClientHandle_t *client_handle, uint32_t ifNum, uint32_t mgmtType)
{
uint32_t transId = 0;
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
printf("\nThe transaction ID of the DHCP client for specified interface or management port is: %u.\n", transId);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the transaction ID of the DHCP client for specified interface or management port (result = %d)\n", result);
break;
}
return;
}
void ipDhcpClientLeaseTimeIntfInfoGet(
openapiClientHandle_t *client_handle, uint32_t ifNum, uint32_t mgmtType)
{
uint32_t leaseTime = 0;
uint32_t renewalTime = 0;
uint32_t rebindTime = 0;
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
printf("\nThe lease time information of the DHCP client for specified interface or management port is: \n");
printf("\tDHCP client lease time: %u \n",leaseTime);
printf("\tDHCP client renewal time: %u \n",renewalTime);
printf("\tDHCP client rebinding time: %u \n",rebindTime);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the lease time information of the DHCP client for specified interface or management port (result = %d)\n", result);
break;
}
return;
}
void ipDhcpClientIntfRetryCountGet(
openapiClientHandle_t *client_handle, uint32_t ifNum, uint32_t mgmtType)
{
uint32_t retryCount = 0;
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
printf("\nThe retry count of the DHCP client for specified interface or management port is: %u.\n", retryCount);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the retry count of the DHCP client for specified interface or management port (result = %d)\n", result);
break;
}
return;
}
{
memset(clientStr, 0, sizeof(clientStr));
clientIdBufd.pstart = clientStr;
clientIdBufd.size = sizeof(clientStr);
switch(mgmtType)
{
case 0:
mgmtPortType = OPEN_MGMT_SERVICEPORT;
break;
case 1:
mgmtPortType = OPEN_MGMT_NETWORKPORT;
break;
case 2:
mgmtPortType = OPEN_MGMT_IPPORT;
break;
default:
break;
}
{
printf("\nERROR: Invalid management port type value. Expected 0(for ServicePort) or 1(for Network) or 2(for IP-Port).\n");
return;
}
switch(result)
{
printf("\nThe generated client identifier on the given interface is: %s.\n",(char*)(clientIdBufd.pstart));
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the generated client identifier on the interface. (result = %d)\n", result);
break;
}
return;
}
{
uint32_t numDiscoverSent = 0;
uint32_t numOfferRecvd = 0;
uint32_t numRequestSent = 0;
uint32_t numAckRecvd = 0;
uint32_t numNackRecvd = 0;
uint32_t numReleaseSent = 0;
uint32_t numInformSent = 0;
uint32_t numRebindSent = 0;
uint32_t numRenewSent = 0;
result =
openapiDhcpClientStatsGet(client_handle, ifNum, &numDiscoverSent, &numOfferRecvd, &numRequestSent, &numAckRecvd, &numNackRecvd, &numReleaseSent, &numInformSent, &numRebindSent, &numRenewSent);
switch(result)
{
printf("\nThe DHCP Client Statistics on the given interface are: \n");
printf("\tNumber of Discovers sent: %u.\n",numDiscoverSent);
printf("\tNumber of Offers received: %u.\n",numOfferRecvd);
printf("\tNumber of Requests sent: %u.\n",numRequestSent);
printf("\tNumber of Acks received: %u.\n",numAckRecvd);
printf("\tNumber of Nacks received: %u.\n",numNackRecvd);
printf("\tNumber of Releases sent: %u.\n",numReleaseSent);
printf("\tNumber of Informs sent: %u.\n",numInformSent);
printf("\tNumber of Rebinds sent: %u.\n",numRebindSent);
printf("\tNumber of Renews sent: %u.\n",numRenewSent);
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to get the DHCP Client Statistics on an interface. (result = %d)\n", result);
break;
}
return;
}
{
switch(result)
{
printf("\nSuccessfully cleared the DHCP Client Statistics on the given interface.\n");
break;
printf("\nERROR: Invalid argument passed.\n");
break;
default:
printf("\nERROR: Bad return code trying to clear the DHCP Client Statistics on an interface. (result = %d)\n", result);
break;
}
return;
}
int main(int argc, char **argv)
{
uint32_t testNum = 0;
uint32_t ifNum = 0;
uint32_t value = 0;
uint32_t mode = 0;
uint32_t mgmtPortType = 0;
char switch_os_revision_string[100];
int show_help = 1;
if (argc < 2)
{
printAppMenu(argv[0]);
return -1;
}
testNum = atoi(argv[1]);
l7proc_crashlog_register();
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
return -1;
}
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting IP4DHCPClient API example application");
printf("\n");
switch_os_revision.pstart = switch_os_revision_string;
switch_os_revision.size = sizeof(switch_os_revision_string);
printf("Network OS version = %s\n", switch_os_revision_string);
else
printf("Network OS version retrieve error\n");
printf("\n");
switch (testNum)
{
case 1:
if (argc == 2)
{
dhcpVendorClassOptionAdminModeGet(&client_handle);
show_help = 0;
}
break;
case 2:
if (argc == 4)
{
mode = atoi(argv[2]);
value = atoi(argv[3]);
dhcpVendorClassOptionAdminModeSet(&client_handle, mode, value);
show_help = 0;
}
break;
case 3:
if (argc == 2)
{
dhcpVendorClassOptionStringGet(&client_handle);
show_help = 0;
}
break;
case 4:
if (argc == 4)
{
value = atoi(argv[3]);
dhcpVendorClassOptionStringSet(&client_handle, argv[2], value);
show_help = 0;
}
break;
case 5:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientDhcpServerIntfIPAddressGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 6:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientIntfStateGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 7:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientTransactionIntfIdGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 8:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientLeaseTimeIntfInfoGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 9:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientIntfRetryCountGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 10:
if (argc == 4)
{
ifNum = atoi(argv[2]);
mgmtPortType = atoi(argv[3]);
ipDhcpClientIdGeneratedGet(&client_handle, ifNum, mgmtPortType);
show_help = 0;
}
break;
case 11:
if (argc == 3)
{
ifNum = atoi(argv[2]);
dhcpClientStatsGet(&client_handle, ifNum);
show_help = 0;
}
break;
case 12:
if (argc == 3)
{
ifNum = atoi(argv[2]);
dhcpClientStatsClear(&client_handle, ifNum);
show_help = 0;
}
break;
default:
break;
}
if (show_help == 1)
{
printAppMenu(argv[0]);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping IP4DHCPClient API example application");
return 0;
}