#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
char clientDesc[64] = {'\0'};
static void printAppMenu(char *name)
{
printf("Usage: %s <clentNum> <numOfNotifyEvents> <eventType1> <timeout1> <eventCount1> <eventType2> <timeout2> <eventCount2> ... &\n", name);
printf("Usage: Only 1 or 2 can be used for clientNum.\n");
printf("Usage: Mininum of one event and its data should be specified.\n");
printf("Usage: It is preferrable to run this example in background using '&'\n");
printf("Test 1: Run a sample code to simulate a event notification client : %s <clientNum> <numOfNotifyEvents> <eventType1> <timeout1> <eventCount1> &\n", name);
return;
}
{
int i = 0, j = 0;
uint32_t ip;
char ipAddrStr[24] = {0};
unsigned char *mac;
printf ("\n \"%s\" received the below event data:\n", clientDesc);
printf (
"\nEvent Type - %u", data->
eventType);
{
printf ("\r\n");
{
{
printf ("IP info change on %s\n",
}
else
{
printf (
"IP info change on IP port interface-%d \n", ipInfo->
intIfNum);
}
if (0 == inet_ntop (AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)))
{
printf ("Bad return code trying to convert IP.\n");
}
else
{
printf ("IP Address : %s\n", ipAddrStr);
}
if (0 == inet_ntop (AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)))
{
printf ("Bad return code trying to convert IP.\n");
}
else
{
printf ("Network mask : %s\n", ipAddrStr);
}
if (0 == inet_ntop (AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)))
{
printf ("Bad return code trying to convert IP.\n");
}
else
{
printf ("Gateway : %s\n", ipAddrStr);
}
printf ("DNS servers : %s\n", ipAddrStr);
for (j = 0;
{
if (0 == inet_ntop (AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)))
{
printf ("Bad return code trying to convert IP.\n");
}
else
{
printf (" %s\n", ipAddrStr);
}
}
printf (
"Hostname : %s\n", ipInfo->
hostName);
break;
printf ("ChangeType %u \r\n",(arpEvent+i)->eventChangeType);
printf ("Arp entry Type, Age :");
{
printf("%s, n/a\n", "Local");
}
{
printf("%s, %d\n", "Gateway", (arpEvent+i)->arpEntry.age);
}
{
printf("%s, n/a\n", "Static");
}
{
printf("%s, n/a\n", "Kernel");
}
else
{
printf("%s, %d\n", "Dynamic", (arpEvent+i)->arpEntry.age);
}
ip = htonl ((arpEvent+i)->arpEntry.ipAddr.addr.ipv4);
if (inet_ntop(AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)) == 0)
{
printf("Bad return code trying to convert IP.\n");
}
printf ("IP Address : %s \n",ipAddrStr);
printf ("VRF ID %u\n",(arpEvent+i)->arpEntry.vrfId);
mac = (arpEvent+i)->arpEntry.macAddr;
printf ("MAC %02X:%02X:%02X:%02X:%02X:%02X \r\n",
mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
printf ("VlanId %u\n", (arpEvent+i)->arpEntry.vlanId);
printf ("IfNum %u\n", (arpEvent+i)->arpEntry.intIfNum);
printf ("Hits %u\n", (arpEvent+i)->arpEntry.hits);
break;
printf ("ChangeType %u \r\n",(ipv4Route+i)->eventChangeType);
printf ("VRF ID %u\n",(ipv4Route+i)->vrfId);
ip = htonl ((ipv4Route+i)->routeDest);
if (inet_ntop(AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)) == 0)
{
printf("Bad return code trying to convert IP.\n");
}
printf ("Route destination : %s \n",ipAddrStr);
ip = htonl ((ipv4Route+i)->routeMask);
if (inet_ntop(AF_INET, (void*)&ip, ipAddrStr, sizeof(ipAddrStr)) == 0)
{
printf("Bad return code trying to convert IP.\n");
}
printf ("Route mask : %s \n",ipAddrStr);
printf ("Route protocol %u\n", (ipv4Route+i)->routeProto);
printf ("Route preference %u\n", (ipv4Route+i)->routePref);
break;
printf ("IfNum %u \r\n",(portInfo+i)->ifNum);
printf ("PortState %u \r\n",(portInfo+i)->portState);
printf ("LinkState %u \r\n",(portInfo+i)->linkState);
printf ("FlowControl %u \r\n",(portInfo+i)->flowControl);
printf ("DisableReason %u \r\n",(portInfo+i)->disableReason);
printf ("IfSpeed %u \r\n",(portInfo+i)->ifSpeed);
printf ("Autoneg %u \r\n",(portInfo+i)->autoneg);
printf ("Duplex %u \r\n",(portInfo+i)->duplex);
printf ("MdixMode %u \r\n",(portInfo+i)->mdixMode);
printf ("Active medium %u \r\n",(portInfo+i)->medium);
break;
printf ("ChangeType %u \r\n",(lldpNeighbor+i)->eventChangeType);
printf ("IfNum %u \r\n",(lldpNeighbor+i)->ifNum);
printf ("RemIndex %u \r\n",(lldpNeighbor+i)->remIndex);
printf ("Timestamp %u \r\n",(lldpNeighbor+i)->timestamp);
printf ("SrcMac %02X:%02X:%02X:%02X:%02X:%02X \r\n",(lldpNeighbor+i)->srcMac[0],
(lldpNeighbor+i)->srcMac[1],(lldpNeighbor+i)->srcMac[2],(lldpNeighbor+i)->srcMac[3],
(lldpNeighbor+i)->srcMac[4],(lldpNeighbor+i)->srcMac[5]);
break;
printf ("IfNum %u \r\n",(poePortStatus+i)->ifNum);
printf ("State %u \r\n",(poePortStatus+i)->state);
printf ("DetectionStatus %u \r\n",(poePortStatus+i)->detectionStatus);
printf ("Status %u \r\n",(poePortStatus+i)->status);
printf ("PowerLimit %u \r\n",(poePortStatus+i)->powerLimit);
printf ("MaxPower %u \r\n",(poePortStatus+i)->maxPower);
printf ("PdClass %u \r\n",(poePortStatus+i)->pdClass);
printf ("PdType %u \r\n",(poePortStatus+i)->pdType);
break;
printf ("ChangeType %u \r\n",(fdbEvent+i)->eventChangeType);
printf ("IfNum %u \r\n",(fdbEvent+i)->ifNum);
printf ("VlanId %u \r\n",(fdbEvent+i)->vlanId);
printf (
"FDB event entry type %u \r\n",(fdbEvent+i)->
entryType);
printf ("Mac %02X:%02X:%02X:%02X:%02X:%02X \r\n",(fdbEvent+i)->mac[0],
(fdbEvent+i)->mac[1],(fdbEvent+i)->mac[2],(fdbEvent+i)->mac[3],
(fdbEvent+i)->mac[4],(fdbEvent+i)->mac[5]);
break;
default:
printf (
"Invalid event type %u.", data->
eventType);
}
printf ("\r\n");
}
fflush(stdout);
}
void *dataThread (void *p)
{
{
printf ("\nFailed to receive event notifications\n");
pthread_exit (NULL);
}
return NULL;
}
int main(int argc, char **argv)
{
char switch_os_revision_string[100];
unsigned int numOfNotifyEvents = 0;
int clientNum = 0;
int argIndex = 0, i = 0;
pthread_t tid;
int errNum = 0;
if (argc < 6)
{
printAppMenu(argv[0]);
return -1;
}
l7proc_crashlog_register();
clientNum = atoi (argv[1]);
if ((1 != clientNum) &&
(2 != clientNum))
{
printf ("\nInvalid client number. Client number should be 1 or 2.\n");
printAppMenu(argv[0]);
return -1;
}
numOfNotifyEvents = atoi (argv[2]);
{
printf ("\nInvalid number of events. Supported range is 1-7.\n");
printAppMenu(argv[0]);
return -1;
}
if (((3*(numOfNotifyEvents)) + 3) != argc)
{
printf ("\nMissing some arguments.\n");
printAppMenu(argv[0]);
return -1;
}
argIndex = 3;
for (i = 0; i < numOfNotifyEvents; i++)
{
eventNotifyCfg[i].
eventType = atoi (argv[argIndex]);
eventNotifyCfg[i].
timeout = atoi (argv[argIndex + 1]);
eventNotifyCfg[i].
eventCount = atoi (argv[argIndex + 2]);
argIndex += 3;
}
snprintf (clientDesc, sizeof (clientDesc),
"event_notify_example_%d", clientNum);
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
return -1;
}
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting event_notify 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");
clientDescBuf.pstart = clientDesc;
clientDescBuf.size = strlen (clientDesc);
{
printf ("\nFailed to register for event notifications\n");
return 0;
}
errNum = pthread_create (&tid, NULL, dataThread, (void *)&client_handle);
if (0 != errNum)
{
printf ("Unable to create a thread for handling data with reason %s\n",
strerror (errNum));
return 0;
}
buf.pstart = eventNotifyCfg;
numOfNotifyEvents, &buf);
{
printf ("\nFailed to register for events with client.\n");
return 0;
}
errNum = pthread_join (tid, NULL);
if (0 != errNum)
{
printf ("Unable to wait on thread with reason %s\n", strerror (errNum));
}
return 0;
}