#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
void printAppMenu(char *name)
{
printf("Usage: %s <test#> <arg1> <arg2> ... \n", name);
printf("Test 1: Get lease data for ipaddr in default VRF: %s 1 <ipaddr>\n", name);
printf("Test 2: Get lease data for VRF and ipaddr: %s 2 <vrf-name> <ipaddr>\n", name);
printf("Test 3: Get pool entries: %s 3\n", name);
printf("Test 4: Given a pool name, get lease entries: %s 4 <pool name>\n", name);
printf("Test 5: Given a pool name, get pool type: %s 5 <pool name>\n", name);
printf("Test 6: Display DHCP server statistics: %s 6\n", name);
printf("Test 7: Test DHCP server OpEN API Sanity: %s 7\n", name);
printf("Test 8: Add an excluded IP-address range for a VRF: %s 8 <low-ipaddr> <high-ipaddr> <vrf-name>\n", name);
printf("Test 9: Delete an excluded IP-address range for a VRF: %s 9 <low-ipaddr> <high-ipaddr> <vrf-name>\n", name);
printf("Test 10: Set the VRF name for a DHCP Server Pool: %s 10 <pool-name> <vrf-name>\n", name);
printf("Test 11: Reset the VRF name to default VRF's name for a DHCP Server Pool: %s 11 <pool-name>\n", name);
printf("Test 12: Get excluded addresses range entries: %s 12\n", name);
printf("Test 13: Clear all lease data of a VRF: %s 13 <vrf-name>\n", name);
printf("Test 14: Clear lease data for VRF and ipaddr: %s 14 <vrf-name> <ipaddr>\n", name);
printf("Test 15: Clear all lease data of a Pool: %s 15 <pool-name>\n", name);
printf("Test 16: Clear lease data for Pool and ipaddr: %s 16 <pool-name> <ipaddr>\n", name);
printf("Test 17: Clear all lease data across all VRFs: %s 17\n", name);
return;
}
{
uint32_t mask;
uint32_t remaining;
unsigned char buffer[OPEN_MAC_ADDR_LEN];
hwaddr.
size =
sizeof(buffer);
memset(&ipAddr, 0, sizeof(ipAddr));
inet_pton(AF_INET, ipaddr, (
void*)&(ipAddr.
addr.
ipv4));
ipAddr.addr.ipv4 = htonl(ipAddr.addr.ipv4);
if (ipAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid IP address string '%s', IPv4 address string required\n",
ipaddr);
}
else
{
&ipAddr,
&mask,
&hwaddr,
&clientId,
&poolName,
&remaining);
{
printf("Bad return code trying to get lease data. (result = %d)\n", result);
}
else
{
printf("IP Addr IP Mask hwaddr Pool Name Remaining Client ID\n");
printf("-------------------------------------------------------------------------------------------------------\n");
printf("%-16s %03d.%03d.%03d.%03d %02x:%02x:%02x:%02x:%02x:%02x %-32s%5d %s\n",
ipaddr, (mask & 0xff000000) >> 24, (mask & 0x00ff0000) >> 16,
(mask & 0x0000ff00) >> 8, mask & 0x000000ff,
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
buffer[5], poolName.
name, remaining, clientId.
clientid);
}
}
}
{
uint32_t mask;
uint32_t remaining;
unsigned char buffer[OPEN_MAC_ADDR_LEN];
hwaddr.pstart = buffer;
hwaddr.size = sizeof(buffer);
memset(&ipAddr, 0, sizeof(ipAddr));
inet_pton(AF_INET, ipaddr, (void*)&(ipAddr.addr.ipv4));
ipAddr.addr.ipv4 = htonl(ipAddr.addr.ipv4);
if (ipAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid IP address string '%s', IPv4 address string required\n",
ipaddr);
}
else
{
strncpy(strVrfName.
name, vrfName,
sizeof(strVrfName.name));
&strVrfName,
&ipAddr,
&mask,
&hwaddr,
&clientId,
&poolName,
&remaining);
{
printf("Bad return code trying to get lease data. (result = %d)\n", result);
}
else
{
printf("IP Addr IP Mask hwaddr Pool Name Remaining Client ID\n");
printf("-------------------------------------------------------------------------------------------------------\n");
printf("%-16s %03d.%03d.%03d.%03d %02x:%02x:%02x:%02x:%02x:%02x %-32s%5d %s\n",
ipaddr, (mask & 0xff000000) >> 24, (mask & 0x00ff0000) >> 16,
(mask & 0x0000ff00) >> 8, mask & 0x000000ff,
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
buffer[5], poolName.name, remaining, clientId.clientid);
}
}
}
void clearDhcpServerVrfIpAddrLeaseData(
openapiClientHandle_t *clientHandle,
char *vrfName,
char *ipaddr)
{
memset(&ipAddr, 0, sizeof(ipAddr));
inet_pton(AF_INET, ipaddr, (void*)&(ipAddr.addr.ipv4));
ipAddr.addr.ipv4 = htonl(ipAddr.addr.ipv4);
if (ipAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid IP address string '%s', IPv4 address string required\n", ipaddr);
}
else
{
strncpy(strVrfName.name, vrfName, sizeof(strVrfName.name));
&strVrfName,
&ipAddr);
{
printf("Bad return code trying to clear lease data. (result = %d)\n", result);
}
else
{
printf("Cleared successfully.\n");
}
}
}
{
strncpy(strVrfName.name, vrfName, sizeof(strVrfName.name));
&strVrfName);
{
printf("Bad return code trying to clear lease data. (result = %d)\n", result);
}
else
{
printf("Cleared successfully.\n");
}
}
void clearDhcpServerPoolIpAddrLeaseData(
openapiClientHandle_t *clientHandle,
char *poolName,
char *ipaddr)
{
memset(&ipAddr, 0, sizeof(ipAddr));
inet_pton(AF_INET, ipaddr, (void*)&(ipAddr.addr.ipv4));
ipAddr.addr.ipv4 = htonl(ipAddr.addr.ipv4);
if (ipAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid IP address string '%s', IPv4 address string required\n", ipaddr);
}
else
{
strncpy(strPoolName.name, poolName, sizeof(strPoolName.name));
&strPoolName,
&ipAddr);
{
printf("Bad return code trying to clear lease data. (result = %d)\n", result);
}
else
{
printf("Cleared successfully.\n");
}
}
}
{
strncpy(strPoolName.name, poolName, sizeof(strPoolName.name));
&strPoolName);
{
printf("Bad return code trying to clear lease data. (result = %d)\n", result);
}
else
{
printf("Cleared successfully.\n");
}
}
{
{
printf("Bad return code trying to clear lease data. (result = %d)\n", result);
}
else
{
printf("Cleared successfully.\n");
}
}
{
uint32_t index;
{
{
printf("Non-success return code from openapiDhcpServerPoolVrfNameGet(result = %d).\n", result);
return;
}
printf("Pool-Index Pool-Name VRF-Name \n");
printf("---------------------------------------------------------------\n");
{
printf("%10d %31s %s\n", index, name.name, vrfName.name);
{
printf("Non-success return code from openapiDhcpServerPoolEntryNextGet(result = %d).\n", result);
}
{
{
printf("Non-success return code from openapiDhcpServerPoolVrfNameGet(result = %d).\n", result);
}
}
}
}
else
{
printf("Non-success return code from openapiDhcpServerPoolEntryFirstGet (result = %d).\n", result);
}
}
{
char *ret = "Unknown lease state";
switch(state)
{
ret = "free";
break;
ret = "active";
break;
ret = "offered";
break;
ret = "expired";
break;
ret = "abandoned";
break;
}
return ret;
}
{
uint32_t index;
char ipStr[INET_ADDRSTRLEN];
strncpy(poolName.name, name, sizeof(poolName.name));
&addr, &state);
{
printf("Pool name Lease index IPv4 Addr State\n");
printf("-----------------------------------------------------\n");
{
addr.addr.ipv4=ntohl(addr.addr.ipv4);
inet_ntop(AF_INET, &(addr.addr.ipv4), ipStr, sizeof(ipStr));
printf("%-16s %05d %-16s %s\n", poolName.name, index, ipStr, stateToStr(state));
{
printf("Non-success return code from openapiDhcpServerPoolEntryNextGet(result = %d)\n", result);
}
}
}
else
{
printf("Non-success return code from openapiDhcpServerLeaseEntryFirstGet (result = %d)\n", result);
}
}
{
char *ret = "Unknown type";
switch(type)
{
ret = "inactive";
break;
ret = "dynamic";
break;
ret = "manual";
break;
}
return ret;
}
{
strncpy(poolName.name, name, sizeof(poolName.name));
{
printf("Bad return code from openapiDhcpServerPoolTypeGet. (result = %d)\n", result);
}
else
{
printf("Type for pool %s is %s\n", name, poolTypeToStr(type));
}
}
{
strncpy(poolName.name, pool_name, sizeof(poolName.name));
strncpy(vrfName.name, vrf_name, sizeof(vrfName.name));
{
printf("Bad return code from openapiDhcpServerPoolVrfNameSet. (result = %d)\n", result);
}
else
{
printf("Successfully set. \n");
}
}
{
strncpy(poolName.name, pool_name, sizeof(poolName.name));
{
printf("Bad return code from resetDhcpServerPoolVrfNameToDefaultVrf. (result = %d)\n", result);
}
else
{
printf("Successfully reset. \n");
}
}
{
char startIpStr[INET_ADDRSTRLEN];
char endIpStr[INET_ADDRSTRLEN];
uint32_t index;
&index,
&strVrfName,
&startIpAddr,
&endIpAddr);
{
printf("Index Start-IPaddress End-IPaddress VRF-Name \n");
printf("---------------------------------------------------------------\n");
{
startIpAddr.addr.ipv4=ntohl(startIpAddr.addr.ipv4);
inet_ntop(AF_INET, &(startIpAddr.addr.ipv4), startIpStr, sizeof(startIpStr));
endIpAddr.addr.ipv4=ntohl(endIpAddr.addr.ipv4);
inet_ntop(AF_INET, &(endIpAddr.addr.ipv4), endIpStr, sizeof(endIpStr));
printf("%05d %-16s %-16s %s\n", index, startIpStr, endIpStr, strVrfName.name);
{
printf("Non-success return code from openapiDhcpServerExcludedAddressRangeEntryNextGet(result = %d).\n", result);
}
}
}
else
{
printf("Non-success return code from openapiDhcpServerExcludedAddressRangeEntryFirstGet(result = %d).\n", result);
}
}
void addDhcpServerExcludedAddressRangeForVrf(
openapiClientHandle_t *clientHandle,
char *lowIpAddr,
char *highIpAddr,
char *vrfName)
{
memset(&startIpAddr, 0, sizeof(startIpAddr));
inet_pton(AF_INET, lowIpAddr, (void*)&(startIpAddr.addr.ipv4));
startIpAddr.addr.ipv4 = htonl(startIpAddr.addr.ipv4);
if (startIpAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid low IP address string '%s', IPv4 address string required\n", lowIpAddr);
return;
}
memset(&endIpAddr, 0, sizeof(endIpAddr));
inet_pton(AF_INET, highIpAddr, (void*)&(endIpAddr.addr.ipv4));
endIpAddr.addr.ipv4 = htonl(endIpAddr.addr.ipv4);
if (endIpAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid high IP address string '%s', IPv4 address string required\n", highIpAddr);
return;
}
strncpy(strVrfName.name, vrfName, sizeof(strVrfName.name));
&strVrfName,
&startIpAddr,
&endIpAddr);
{
printf("Bad return code trying to add an excluded-address range. (result = %d).\n", result);
}
else
{
printf("Successfully added. \n");
}
}
void deleteDhcpServerExcludedAddressRangeForVrf(
openapiClientHandle_t *clientHandle,
char *lowIpAddr,
char *highIpAddr,
char *vrfName)
{
memset(&startIpAddr, 0, sizeof(startIpAddr));
inet_pton(AF_INET, lowIpAddr, (void*)&(startIpAddr.addr.ipv4));
startIpAddr.addr.ipv4 = htonl(startIpAddr.addr.ipv4);
if (startIpAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid low IP address string '%s', IPv4 address string required\n", lowIpAddr);
return;
}
memset(&endIpAddr, 0, sizeof(endIpAddr));
inet_pton(AF_INET, highIpAddr, (void*)&(endIpAddr.addr.ipv4));
endIpAddr.addr.ipv4 = htonl(endIpAddr.addr.ipv4);
if (endIpAddr.addr.ipv4 == INADDR_NONE)
{
printf("Invalid high IP address string '%s', IPv4 address string required\n", highIpAddr);
return;
}
strncpy(strVrfName.name, vrfName, sizeof(strVrfName.name));
&strVrfName,
&startIpAddr,
&endIpAddr);
{
printf("Bad return code trying to delete an excluded-address range. (result = %d).\n", result);
}
else
{
printf("Successfully deleted. \n");
}
}
{
{
"Reserved",
"Active Leases",
"Expired Leases",
"Malformed Messages",
"Discover Dropped",
"Discover Received",
"Request Received",
"Decline Received",
"Release Received",
"Inform Received",
"Offer Sent",
"Ack Sent",
"Nack Sent"
};
uint32_t value;
{
{
printf("Bad return code from openapiDhcpServerStatGet for ID %u. (result = %d)\n", id, result);
}
else
{
printf("%-20s: %u\n", statLabel[id], value);
}
}
}
{
uint32_t mask;
uint32_t poolIndex;
uint32_t leaseIndex;
uint32_t val1;
uint32_t remaining;
char buf[OPEN_MAC_ADDR_LEN];
hwaddr.pstart = buf;
hwaddr.size = sizeof(buf);
printf("\nTesting openapiDhcpServerLeaseDataGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL ipaddr. (result = %d)\n", result);
printf("NULL mask. (result = %d)\n", result);
printf("NULL hwaddr. (result = %d)\n", result);
printf("NULL client ID. (result = %d)\n", result);
printf("NULL pool name pointer. (result = %d)\n", result);
printf("NULL remaining pointer. (result = %d)\n", result);
hwaddr.pstart = buf;
hwaddr.size = 1;
printf("hwaddr size too small. (result = %d)\n", result);
hwaddr.pstart = NULL;
hwaddr.size = sizeof(buf);
printf("NULL hwaddr pstart. (result = %d)\n", result);
printf("openapiDhcpServerLeaseDataGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerPoolEntryFirstGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL pool index. (result = %d)\n", result);
printf("NULL pool name. (result = %d)\n", result);
printf("openapiDhcpServerPoolEntryFirstGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerPoolEntryNextGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL pool index. (result = %d)\n", result);
printf("NULL pool name. (result = %d)\n", result);
printf("openapiDhcpServerPoolEntryNextGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerPoolTypeGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL pool name. (result = %d)\n", result);
printf("NULL type. (result = %d)\n", result);
printf("openapiDhcpServerPoolTypeGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerLeaseEntryFirstGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL pool name. (result = %d)\n", result);
printf("NULL lease index. (result = %d)\n", result);
printf("NULL IP addr. (result = %d)\n", result);
printf("NULL state. (result = %d)\n", result);
printf("openapiDhcpServerLeaseEntryFirstGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerLeaseEntryNextGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL pool name. (result = %d)\n", result);
printf("NULL lease index. (result = %d)\n", result);
printf("NULL IP addr. (result = %d)\n", result);
printf("NULL state. (result = %d)\n", result);
printf("openapiDhcpServerLeaseEntryNextGet() sanity successful. \n");
printf("\nTesting openapiDhcpServerStatGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Invalid statistic ID (too low). (result = %d)\n", result);
printf("Invalid statistic ID (too high). (result = %d)\n", result);
printf("NULL statistic output value pointer. (result = %d)\n", result);
printf("openapiDhcpServerStatGet() sanity successful. \n");
}
int main(int argc, char **argv)
{
uint32_t testNum;
char switch_os_revision_string[100];
if (argc < 2)
{
printAppMenu(argv[0]);
exit(1);
}
testNum = atoi(argv[1]);
l7proc_crashlog_register();
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit(2);
}
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting DHCP Server 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 != 3)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerLeaseData(&clientHandle, argv[2]);
break;
case 2:
if (argc != 4)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerVrfIpAddrLeaseData(&clientHandle, argv[2], argv[3]);
break;
case 3:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerPoolEntries(&clientHandle);
break;
case 4:
if (argc != 3)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerLeaseEntries(&clientHandle, argv[2]);
break;
case 5:
if (argc != 3)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerPoolTypeForName(&clientHandle, argv[2]);
break;
case 6:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
displayDhcpServerStats(&clientHandle);
break;
case 7:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
dhcpServerOpENAPIsSanity(&clientHandle);
break;
case 8:
if (argc != 5)
{
printAppMenu(argv[0]);
exit(1);
}
addDhcpServerExcludedAddressRangeForVrf(&clientHandle, argv[2], argv[3], argv[4]);
break;
case 9:
if (argc != 5)
{
printAppMenu(argv[0]);
exit(1);
}
deleteDhcpServerExcludedAddressRangeForVrf(&clientHandle, argv[2], argv[3], argv[4]);
break;
case 10:
if (argc != 4)
{
printAppMenu(argv[0]);
exit(1);
}
setDhcpServerPoolVrfName(&clientHandle, argv[2], argv[3]);
break;
case 11:
if (argc != 3)
{
printAppMenu(argv[0]);
exit(1);
}
resetDhcpServerPoolVrfNameToDefaultVrf(&clientHandle, argv[2]);
break;
case 12:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
getDhcpServerExcludedAddressesRangeEntries(&clientHandle);
break;
case 13:
if (argc != 3)
{
printAppMenu(argv[0]);
exit(1);
}
clearDhcpServerVrfLeaseData(&clientHandle, argv[2]);
break;
case 14:
if (argc != 4)
{
printAppMenu(argv[0]);
exit(1);
}
clearDhcpServerVrfIpAddrLeaseData(&clientHandle, argv[2], argv[3]);
break;
case 15:
if (argc != 3)
{
printAppMenu(argv[0]);
exit(1);
}
clearDhcpServerPoolLeaseData(&clientHandle, argv[2]);
break;
case 16:
if (argc != 4)
{
printAppMenu(argv[0]);
exit(1);
}
clearDhcpServerPoolIpAddrLeaseData(&clientHandle, argv[2], argv[3]);
break;
case 17:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
clearDhcpServerAllLeaseData(&clientHandle);
break;
default:
printAppMenu(argv[0]);
break;
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping DHCP Server API example application");
return 0;
}