NetSim Source Code Help
Loading...
Searching...
No Matches
Ping.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi kant suman
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include <signal.h>
16#include "CLI.h"
17#include "../IP/IP.h"
18
19#pragma comment(lib,"libIP.lib")
20
22{
23 if (command->length - index < 2)
24 {
25 send_message(info, "USAGE: ping deviceName or ping IPAddress\n");
26 return false;
27 }
28
29 if (_stricmp(command->commands[index], "ping"))
30 return false;
31
32 if (!isCommandAsDeviceName(command->commands[index + 1]))
33 {
34 if (!isValidIPAddress(command->commands[index + 1]))
35 {
36 send_message(info, "%s is not a valid IP address or valid device name.\n",
37 command->commands[index + 1]);
38 return false;
39 }
40 }
41 return true;
42}
43
44bool resp(ptrCLIENTINFO info,char* msg, bool isMore)
45{
46 if(info->clientType != CLIENTTYPE_STRING)
47 send_message(info, msg);
48 if (info->clientType == CLIENTTYPE_SOCKET && !isMore)
49 {
50 info->CLIENT.sockClient.iswait = false;
51 WakeByAddressSingle(&info->CLIENT.sockClient.iswait);
52 }
53 else if(info->multResp)
54 {
55 int len = (int)strlen(msg);
56 info->multResp(info->multRespArg,
57 _strdup(msg),
58 len,
59 isMore);
60 }
61 return !isMore;
62}
63
65{
66 NETSIM_ID dest;
67 if (isCommandAsDeviceName(command->commands[index + 1]))
68 {
69 dest = fn_NetSim_Stack_GetDeviceId_asName(command->commands[index + 1]);
70 }
71 else
72 {
73 NETSIM_IPAddress ip = STR_TO_IP(command->commands[index + 1],
74 STR_GET_IP_TYPE(command->commands[index + 1]));
75 NETSIM_ID in;
77 if (!dest)
78 {
79 send_message(info, "%s IP is not associated with any device.\n",
80 command->commands[index + 1]);
81 return;
82 }
83 }
84 IP_DEVVAR* ipVar = GET_IP_DEVVAR(d);
85 if (ipVar && !ipVar->isICMP)
86 {
87 send_message(info, "ICMP is not configured for device %d.\n", d);
88 return;
89 }
90
91 ipVar = GET_IP_DEVVAR(dest);
92 if (ipVar && !ipVar->isICMP)
93 {
94 send_message(info, "ICMP is not configured for device %d.\n", dest);
95 return;
96 }
97
98 ICMP_StartPingRequest(d, dest, 4, resp, info);
99 if (info->clientType == CLIENTTYPE_SOCKET)
100 {
101 info->CLIENT.sockClient.iswait = true;
102 WaitOnAddress(&info->CLIENT.sockClient.iswait,
104 sizeof info->CLIENT.sockClient.iswait,
105 INFINITE);
106 }
107 else
108 {
109 info->isMultResp = true;
110 }
111}
unsigned int NETSIM_ID
Definition: Animation.h:45
bool isCommandAsDeviceName(char *name)
@ CLIENTTYPE_SOCKET
Definition: CLI.h:26
@ CLIENTTYPE_STRING
Definition: CLI.h:27
void send_message(ptrCLIENTINFO info, char *msg,...)
#define GET_IP_DEVVAR(d)
Definition: IP.h:49
void * ICMP_StartPingRequest(NETSIM_ID src, NETSIM_ID dest, UINT count, bool(*resp)(void *, char *, bool), void *arg)
bool isValidIPAddress(char *ip)
NETSIM_IPAddress STR_TO_IP(char *ipStr, int type)
int STR_GET_IP_TYPE(char *ip)
#define _stricmp
Definition: Linux.h:127
#define _strdup(x)
Definition: Linux.h:126
#define INFINITE
Definition: Linux.h:82
void execute_ping_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
Definition: Ping.c:64
bool validate_ping_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
Definition: Ping.c:21
bool resp(ptrCLIENTINFO info, char *msg, bool isMore)
Definition: Ping.c:44
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asName(const char *name)
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asIP(NETSIM_IPAddress ip, NETSIM_ID *nInterfaceId)
Structure to store the device ip details.
Definition: IP.h:226
bool isICMP
Definition: IP.h:236
CLIENTTYPE clientType
Definition: CLI.h:71
union stru_clientInfo::client CLIENT
bool(* multResp)(void *, char *, int, bool)
Definition: CLI.h:85
void * multRespArg
Definition: CLI.h:86
bool isMultResp
Definition: CLI.h:84
bool dontUseMe
Definition: CLI.h:60
SOCKCLIENTINFO sockClient
Definition: CLI.h:74