#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
#define MAX_STR_LEN 100
#define LOCALHOST_IP "127.0.0.1"
static void printAppMenu(char *name)
{
printf("Usage: user_sessions <test#> <arg1> <arg2> ... \n");
printf("Test 1: Create a session: %s 1 <username> <password> \n", name);
printf("Test 2: Validate a session: %s 2 <sid> \n", name);
printf("Test 3: End a session: %s 3 <sid>\n", name);
printf("Test 4: Get the login session username: %s 4 <sid>\n", name);
printf("Test 5: Get the login session remote IP address: %s 5 <sid>\n", name);
printf("Test 6: Get the login session idle time: %s 6 <sid>\n", name);
printf("Test 7: Get the login session session time: %s 7 <sid>\n", name);
printf("Test 8: Get the login session type: %s 8 <sid>\n", name);
printf("Test 9: Run error checking tests: %s 9\n", name);
return;
}
{
char str1[MAX_STR_LEN];
char str2[MAX_STR_LEN];
char str3[MAX_STR_LEN];
char str4[MAX_STR_LEN]= LOCALHOST_IP;
char str5[MAX_STR_LEN];
uint32_t accessLevel;
snprintf(str1, MAX_STR_LEN, "%s", userName);
unameDesc.
size = strnlen(str1, MAX_STR_LEN) + 1;
snprintf(str2, MAX_STR_LEN, "%s", passWord);
pwdDesc.pstart = str2;
pwdDesc.size = strnlen(str2, MAX_STR_LEN) + 1;
snprintf(str3, MAX_STR_LEN,"%s", "HTTP");
loginType.pstart = str3;
loginType.size = strnlen(str3, MAX_STR_LEN) + 1;
inetaddr.pstart = str4;
inetaddr.size = strnlen(str4, MAX_STR_LEN) + 1;
sessionId.pstart = str5;
sessionId.size = sizeof(str5);
&loginType,&inetaddr, &accessLevel, &sessionId)) !=
OPEN_E_NONE)
{
printf("Bad return code trying to create session. (result = %d)\n", result);
}
else
{
printf("User session created successfully\n");
printf("session Id:%s access level = %d\n", str5, accessLevel);
}
return;
}
{
char str1[MAX_STR_LEN];
char str2[MAX_STR_LEN];
char str3[MAX_STR_LEN] = LOCALHOST_IP;
uint32_t accessLevel;
snprintf(str1, MAX_STR_LEN, "%s", sid);
session.pstart = str1;
session.size = strnlen(str1, MAX_STR_LEN) + 1;
snprintf(str2, MAX_STR_LEN, "%s", "HTTP");
loginType.pstart = str2;
loginType.size = strnlen(str2, MAX_STR_LEN) + 1;
inetaddr.pstart = str3;
inetaddr.size = strnlen(str3, MAX_STR_LEN) + 1;
{
printf("Bad return code trying to validate session. (result = %d)\n", result);
}
else
{
printf("Session is valid, access level = %d\n", accessLevel);
}
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, sid, (sizeof(str) - 1));
session.pstart = str;
session.size = strlen(str)+1;
{
printf("Session end failed, (result = %d)\n", result);
}
else
{
printf("Session ended.\n");
}
return;
}
{
char nameStr[OPEN_LOGIN_SIZE];
name.pstart = nameStr;
name.size = sizeof(nameStr);
{
printf("Bad return code trying to get login user name. (result = %d)\n", result);
}
else
{
printf("login user name: %s\n", nameStr);
}
return;
}
{
uint32_t ip4_addr;
uint8_t addr8[INET6_ADDRSTRLEN];
int af;
{
af = AF_INET;
ip4_addr = 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, (void *)addr8, buffer, sizeof(addr8)));
}
{
char ipAddrStr[80];
{
printf("Bad return code trying to get login session remote IP address. (result = %d)\n", result);
}
else
{
if (ipAddressFormat(&addr, ipAddrStr) != NULL)
{
printf("remote IP address: %s\n", ipAddrStr);
}
else
{
printf("unable to convert IP address to string\n");
}
}
return;
}
{
uint32_t idleTime;
{
printf("Bad return code trying to get login session idle time. (result = %d)\n", result);
}
else
{
printf("login session idle time seconds: %d\n", idleTime);
}
return;
}
{
uint32_t sessionTime;
{
printf("Bad return code trying to get login session time. (result = %d)\n", result);
}
else
{
printf("login session time: %d seconds\n", sessionTime);
}
return;
}
{
char *ret = "unknown";
if (type == OPEN_LOGIN_TYPE_UNKNWN)
{
ret = "OPEN_LOGIN_TYPE_UNKNWN";
}
else if (type == OPEN_LOGIN_TYPE_SERIAL)
{
ret = "OPEN_LOGIN_TYPE_SERIAL";
}
else if (type == OPEN_LOGIN_TYPE_TELNET)
{
ret = "OPEN_LOGIN_TYPE_TELNET";
}
else if (type == OPEN_LOGIN_TYPE_SSH )
{
ret = "OPEN_LOGIN_TYPE_SSH ";
}
else if (type == OPEN_LOGIN_TYPE_HTTP )
{
ret = "OPEN_LOGIN_TYPE_HTTP ";
}
else if (type == OPEN_LOGIN_TYPE_HTTPS)
{
ret = "OPEN_LOGIN_TYPE_HTTPS";
}
else if (type == OPEN_LOGIN_TYPE_SNMP)
{
ret = "OPEN_LOGIN_TYPE_SNMP";
}
else if (type == OPEN_LOGIN_TYPE_PING)
{
ret = "OPEN_LOGIN_TYPE_PING";
}
else if (type == OPEN_LOGIN_TYPE_UDS)
{
ret = "OPEN_LOGIN_TYPE_UDS";
}
return ret;
}
{
{
printf("Bad return code trying to get login session type. (result = %d)\n", result);
}
else
{
printf("login session type: %s\n", typeToName(type));
}
return;
}
{
char nameBuf[OPEN_LOGIN_SIZE];
uint32_t timeVal;
printf("Testing Login Session OpEN APIs sanity:\n\n");
buf.size = sizeof(nameBuf);
buf.pstart = nameBuf;
printf("Testing openapiLoginSessionUserGet():\n");
printf("Unlikely session ID:(result = %d)\n", result);
printf("NULL client handle:(result = %d)\n", result);
printf("NULL name:(result = %d)\n", result);
buf.size = sizeof(nameBuf) - 1;
printf("Buffer size too smnall:(result = %d)\n", result);
buf.size = sizeof(nameBuf);
buf.pstart = (void *) NULL;
printf("NULL buffer pstart:(result = %d)\n", result);
printf("Testing openapiLoginSessionRemoteIpAddrGet():\n");
printf("Unlikely session ID:(result = %d)\n", result);
printf("NULL client handle:(result = %d)\n", result);
printf("NULL addr:(result = %d)\n", result);
printf("Testing openapiLoginSessionIdleTimeGet():\n");
printf("Unlikely session ID:(result = %d)\n", result);
printf("NULL client handle:(result = %d)\n", result);
printf("NULL time:(result = %d)\n", result);
printf("Testing openapiLoginSessionTimeGet():\n");
printf("Unlikely session ID:(result = %d)\n", result);
printf("NULL client handle:(result = %d)\n", result);
printf("NULL time:(result = %d)\n", result);
printf("Testing openapiLoginSessionTypeGet():\n");
printf("Unlikely session ID:(result = %d)\n", result);
printf("NULL client handle:(result = %d)\n", result);
printf("NULL type:(result = %d)\n", result);
}
int main(int argc, char **argv)
{
uint32_t testNum;
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 User session 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 == 4)
{
createSession(&clientHandle, argv[2], argv[3]);
show_help = 0;
}
break;
case 2:
if (argc == 3)
{
validateSession(&clientHandle, argv[2]);
show_help = 0;
}
break;
case 3:
if (argc == 3)
{
endSession(&clientHandle, argv[2]);
show_help = 0;
}
break;
case 4:
if (argc == 3)
{
getLoginSessionName(&clientHandle, atoi(argv[2]));
show_help = 0;
}
break;
case 5:
if (argc == 3)
{
getLoginSessionRemoteIPAddr(&clientHandle, atoi(argv[2]));
show_help = 0;
}
break;
case 6:
if (argc == 3)
{
getLoginSessionIdleTime(&clientHandle, atoi(argv[2]));
show_help = 0;
}
break;
case 7:
if (argc == 3)
{
getLoginSessionTime(&clientHandle, atoi(argv[2]));
show_help = 0;
}
break;
case 8:
if (argc == 3)
{
getLoginSessionType(&clientHandle, atoi(argv[2]));
show_help = 0;
}
break;
case 9:
if (argc == 2)
{
runSanity(&clientHandle);
show_help = 0;
}
break;
default:
break;
}
if (show_help == 1)
{
printAppMenu(argv[0]);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping User sessions API example application");
return 0;
}