#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
#define PRINTSANITYRESULTS(result, test, msg) \
if ((result==OPEN_E_NONE) && (test)) { printf("Sanity Success - %s\n", msg); } \
else { printf("Sanity Failure - %s\n", msg); }
#define PRINTBADRESULT(result, msg) \
if (result!=OPEN_E_NONE) { printf("Test Failure - %s (err %d)\n", msg, result); }
#define XX_MAX_STRING_LENGTH 256
#define XX_PROTO_MAX 10
uint32_t *interface)
{
uint32_t intf = 0;
if (!clientHandle || !interface)
{
}
{
}
{
*interface = intf;
}
return result;
}
{
uint32_t i;
uint32_t protoId = 0;
uint32_t nextProtoId = 0;
for (i = 0; i < XX_PROTO_MAX; i++)
{
if (result ==
OPEN_E_NONE && (strlen(protoName->
pstart)>0)) {
break; }
protoId = nextProtoId;
}
return result;
}
uint32_t asn)
{
uint32_t tmp;
PRINTBADRESULT(result, "openapiBgpLocalASSet");
{
PRINTBADRESULT(result, "openapiBgpLocalASGet");
}
PRINTSANITYRESULTS(result, asn==tmp, __func__);
return;
}
uint32_t time)
{
uint32_t tmp;
PRINTBADRESULT(result, "openapiBgpGlobalHoldTimeConfiguredSet");
{
PRINTBADRESULT(result, "openapiBgpGlobalHoldTimeConfiguredGet");
}
PRINTSANITYRESULTS(result, time==tmp, __func__);
return;
}
uint32_t time)
{
uint32_t tmp;
PRINTBADRESULT(result, "openapiBgpGlobalKeepAliveConfiguredSet");
{
PRINTBADRESULT(result, "openapiBgpGlobalKeepAliveConfiguredGet");
}
PRINTSANITYRESULTS(result, time==tmp, __func__);
return;
}
char *routerId)
{
uint32_t tmp;
memset(&ipAddr, 0, sizeof(ipAddr));
if (inet_pton(AF_INET, routerId, (
void*)&(ipAddr.
addr.
ipv4)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
PRINTBADRESULT(result, "openapiBgpLocalIdSet");
}
{
PRINTBADRESULT(result, "openapiBgpLocalIdGet");
}
PRINTSANITYRESULTS(result, ipAddr.addr.ipv4==tmp, __func__);
return;
}
uint32_t pref)
{
uint32_t tmp;
PRINTBADRESULT(result, "openapiBgpLocalPrefSet");
{
PRINTBADRESULT(result, "openapiBgpLocalPrefGet");
}
PRINTSANITYRESULTS(result, pref==tmp, __func__);
return;
}
uint32_t extPref,
uint32_t intPref,
uint32_t locPref)
{
uint32_t tmp;
PRINTBADRESULT(result, "openapiIpRouterPreferenceSet IBGP");
{
PRINTBADRESULT(result, "openapiIpRouterPreferenceGet IBGP");
}
PRINTSANITYRESULTS(result, intPref==tmp, __func__);
PRINTBADRESULT(result, "openapiIpRouterPreferenceSet EBGP");
{
PRINTBADRESULT(result, "openapiIpRouterPreferenceGet EBGP");
}
PRINTSANITYRESULTS(result, extPref==tmp, __func__);
PRINTBADRESULT(result, "openapiIpRouterPreferenceSet Local BGP");
{
PRINTBADRESULT(result, "openapiIpRouterPreferenceGet Local BGP");
}
PRINTSANITYRESULTS(result, locPref==tmp, __func__);
return;
}
bool log)
{
bool tmp;
PRINTBADRESULT(result, "openapiBgpLogNeighborChangesSet");
{
PRINTBADRESULT(result, "openapiBgpLogNeighborChangesGet");
}
PRINTSANITYRESULTS(result, log==tmp, __func__);
return;
}
bool isIbgp)
{
uint32_t maxPaths;
uint32_t tmpPaths;
PRINTBADRESULT(result, "openapiBgpMaxPathsSet");
{
PRINTBADRESULT(result, "openapiBgpMaxPathsGet");
}
PRINTSANITYRESULTS(result, maxPaths==tmpPaths, __func__);
return;
}
bool normalMode,
bool useRm,
char *ipStr,
uint32_t pfxLen,
char *rmNameStr)
{
char str[XX_MAX_STRING_LENGTH];
memset(&ipAddr, 0, sizeof(ipAddr));
if (inet_pton(AF_INET, ipStr, (void*)&(ipAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, ipStr, (
void*)&(ipAddr.addr.
ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
strcpy(str, rmNameStr);
rmName.pstart = str;
rmName.
size = strlen(str)+1;
normalMode,
useRm,
ipAddr,
pfxLen,
&rmName);
PRINTBADRESULT(result, "openapiBgpNetworkAddDelete");
PRINTSANITYRESULTS(result, 1==1, __func__);
}
return;
}
bool add,
bool matchSet,
uint32_t matchBits,
bool metricSet,
bool metricValueSet,
uint32_t metricValue,
bool routeMapSet,
char *rmNameStr)
{
char str[XX_MAX_STRING_LENGTH];
strcpy(str, rmNameStr);
rmName.pstart = str;
rmName.size = strlen(str);
protoName.size++;
protoName.pstart = malloc(protoName.size);
result = getFirstRoutingProtocolName(clientHandle, &protoName);
{
add,
&protoName,
matchSet,
matchBits,
metricSet,
metricValueSet,
metricValue,
routeMapSet,
af,
&rmName);
}
PRINTBADRESULT(result, "openapiBgpRedistributionSet");
PRINTSANITYRESULTS(result, 1==1, __func__);
free(protoName.pstart);
return;
}
char *peerIpStr,
uint32_t scopeId,
bool activate,
bool defValue)
{
bool tmp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
af,
activate,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerActivateSet");
}
{
remoteAddr,
scopeId,
af,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerActivateGet");
}
PRINTSANITYRESULTS(result, activate==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
bool defValue)
{
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
status,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerAdminStatusSet");
}
{
remoteAddr,
scopeId,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerAdminStatusGet");
}
PRINTSANITYRESULTS(result, status==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
uint32_t interval,
bool defValue)
{
uint32_t tmp;
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
af,
interval,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerAdvertisementIntervalSet");
}
{
remoteAddr,
scopeId,
af,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerAdvertisementIntervalGet");
}
PRINTSANITYRESULTS(result, interval==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
uint32_t time,
bool defValue)
{
uint32_t tmp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
time,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerHoldTimeConfiguredSet");
}
{
remoteAddr,
scopeId,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerHoldTimeConfiguredGet");
}
PRINTSANITYRESULTS(result, time==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
uint32_t time,
bool defValue)
{
uint32_t tmp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
time,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerKeepAliveConfiguredSet");
}
{
remoteAddr,
scopeId,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerKeepAliveConfiguredGet");
}
PRINTSANITYRESULTS(result, time==tmp, __func__);
return;
}
uint32_t asn,
char *peerIpStr,
uint32_t scopeId)
{
uint32_t tmp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
asn);
PRINTBADRESULT(result, "openapiBgpPeerRemoteASSet");
}
{
remoteAddr,
scopeId,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerRemoteASGet");
}
PRINTSANITYRESULTS(result, asn==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
bool defValue)
{
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
af,
enable,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerNextHopSelfModeSet");
}
{
remoteAddr,
scopeId,
af,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerNextHopSelfModeGet");
}
PRINTSANITYRESULTS(result, enable==tmp, __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
uint32_t threshold,
bool warningOnly,
bool defValue)
{
uint32_t pfxLimit;
uint32_t pTemp;
uint32_t tTemp;
bool bTemp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
af,
pfxLimit,
threshold,
warningOnly,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerPfxLimitSet");
}
{
remoteAddr,
scopeId,
af,
&pTemp,
&tTemp,
&bTemp);
PRINTBADRESULT(result, "openapiBgpPeerPfxLimitGet");
}
PRINTSANITYRESULTS(result, (pfxLimit==pTemp && threshold==tTemp && warningOnly==bTemp), __func__);
return;
}
char *peerIpStr,
uint32_t scopeId,
uint32_t source,
bool defValue)
{
uint32_t tmp;
memset(&remoteAddr, 0, sizeof(remoteAddr));
if (inet_pton(AF_INET, peerIpStr, (void*)&(remoteAddr.addr.ipv4)) > 0)
{
}
else if (inet_pton(AF_INET6, peerIpStr, (void*)&(remoteAddr.addr.ipv6)) > 0)
{
}
else
{
printf("Bad return code trying to convert ip address.\n");
}
{
remoteAddr,
scopeId,
source,
defValue);
PRINTBADRESULT(result, "openapiBgpPeerUpdateSourceSet");
}
{
remoteAddr,
scopeId,
&tmp);
PRINTBADRESULT(result, "openapiBgpPeerUpdateSourceGet");
}
PRINTSANITYRESULTS(result, source==tmp, __func__);
return;
}
int main (int argc, char **argv)
{
uint32_t source = 0;
l7proc_crashlog_register();
if ((result =
{
printf ("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit (2);
}
{
sleep (1);
}
L7PROC_LOGF (L7PROC_LOG_SEVERITY_INFO, 0, "Starting BGP API example application");
printf ("\n");
printf ("Begin Sanity tests...\n");
printf ("Establish BGP and global parameters...\n");
testLocalAS(&clientHandle, 1);
testLocalId(&clientHandle, "1.1.1.1");
testLocalPref(&clientHandle, 1234567890);
testDistance(&clientHandle, 10, 20, 30);
testGlobalHoldTime(&clientHandle, 65535);
testGlobalKeepAlive(&clientHandle, 65535);
testLogNeighbor(&clientHandle, true);
testNetwork(&clientHandle, true, true, "20.10.0.0", 24, "my-map1");
testNetwork(&clientHandle, false, true, "20.10.0.0", 24, "my-map1");
testRedistribution(&clientHandle, true, true, matchBits, true, true,
printf ("Create a ipv4 test peer...\n");
testPeerRemoteAS(&clientHandle, 1, "2.2.2.2", 0);
testPeerAdminStatus(&clientHandle,
"2.2.2.2", 0,
OPEN_BGP_START,
true);
testPeerKeepAlive(&clientHandle, "2.2.2.2", 0, 600, true);
testPeerHoldTime(&clientHandle, "2.2.2.2", 0, 700, true);
testPeerActivate(&clientHandle,
OPEN_AF_INET6,
"2.2.2.2", 0,
true,
true);
testPeerAdvertisement(&clientHandle,
OPEN_AF_INET,
"2.2.2.2", 0, 200,
true);
testPeerPfxLimit(&clientHandle,
OPEN_AF_INET,
"2.2.2.2", 0, 20,
true,
true);
printf ("Create a ipv6 test peer...\n");
testPeerRemoteAS(&clientHandle, 1, "2222::", 0);
testPeerAdminStatus(&clientHandle,
"2222::", 0,
OPEN_BGP_STOP,
true);
testPeerKeepAlive(&clientHandle, "2222::", 0, 600, true);
testPeerHoldTime(&clientHandle, "2222::", 0, 700, true);
testPeerActivate(&clientHandle,
OPEN_AF_INET6,
"2222::", 0,
true,
true);
testPeerAdvertisement(&clientHandle,
OPEN_AF_INET6,
"2222::", 0, 222,
true);
testPeerPfxLimit(&clientHandle,
OPEN_AF_INET6,
"2222::", 0, 60,
true,
true);
if (getFirstRoutingInterface(&clientHandle, &source) ==
OPEN_E_NONE)
{
testPeerRemoteAS(&clientHandle, 1, "fe80::1", source);
testPeerUpdateSource(&clientHandle, "2.2.2.2", 0, source, true);
}
else
{
printf ("Sanity tests are incomplete because no routing interfaces are available!\n");
printf ("Please configure a routing interface and re-run test.\n\n");
}
printf ("Complete.\n");
L7PROC_LOGF (L7PROC_LOG_SEVERITY_INFO, 0, "Stopping BGP API example application");
return 0;
}