tw_cli_ping.c.svn-base
资源名称:cli.rar [点击查看]
上传用户:hujq123
上传日期:2016-04-10
资源大小:153k
文件大小:6k
源码类别:
Telnet服务器
开发平台:
Unix_Linux
- /*
- * Copyright (C) 2002 Koninklijke Philips Electronics N.V.,
- * All Rights Reserved.
- *
- * This source code and any compilation or derivative thereof is the
- * proprietary information of Koninklijke Philips Electronics N.V.
- * and is confidential in nature.
- * Under no circumstances is this software to be exposed to or placed
- * under an Open Source License of any type without the expressed
- * written permission of Koninklijke Philips Electronics N.V.
- *
- *###########################################################
- */
- /*!
- * file tw_cli_ping.c
- *
- * brief -
- *
- */
- /*-----------------------------------------------------------*
- *
- * %version: 4 %
- * instance: DS_6
- * %date_created: Fri Feb 21 08:04:56 2003 %
- *
- *###########################################################
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <socket_app.h>
- #include <tm_common.h>
- #include <tw_cli.h>
- #include <ctype.h>
- #include <sys_conf.h>
- #include <arpa/inet.h>
- /* #define PING_DEBUG */
- #define MAX_PING_PARAMS 8
- #define MAX_PING_TIMEOUT 400
- #define MAX_PING_SIZE 528
- typedef char PingParam_t[64];
- static PingParam_t PingParamList[MAX_PING_PARAMS];
- static char PingBuffer[MAX_PING_SIZE];
- static int CliCoProc_Ping (Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliRco_Ping =
- {
- "ping",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "Send ICMP Echo Request to host",
- NULL,
- NULL,
- CliCoProc_Ping,
- };
- static void GetDottedIP(char *strbuf, UInt8 *IPAddr)
- {
- int i;
- UInt8 byteval;
- for (i = 0; (i < 4); i++)
- {
- byteval = 0;
- while (isdigit(*strbuf))
- {
- byteval = (byteval * 10) + (*strbuf - '0');
- strbuf++;
- }
- IPAddr[i] = byteval;
- if (*strbuf == '.')
- {
- strbuf++;
- }
- }
- }
- /************************************************************************
- *
- * iadk_ping - Send an echo request to a host.
- *
- * Globals:
- * None
- *
- * Returns:
- * int - 0 if no error, -1 otherwise.
- *
- * Description:
- * Send an echo request to a host. Each TCP/IP stack seems to have
- * their own favorite API for this so we combine them here.
- *
- * Notes:
- * None
- *
- * History:
- * 10/25/01 FD Created.
- *
- ************************************************************************/
- int iadk_ping (UInt8 *IPAddress, char *RequestBuffer,
- UInt32 RequestLength,
- UInt32 RequestTimeout,
- UInt32 *Resptime,
- UInt32 *EchoResult)
- {
- int result;
- *EchoResult = 1;
- result = tw_ping (IPAddress, RequestBuffer, RequestLength,
- RequestTimeout,
- Resptime);
- if (result < 0)
- result = -1;
- else
- result = 0;
- return result;
- }
- static int CliCoProc_Ping(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- int i;
- int sts;
- int FieldCount;
- int ParamIndex;
- char *param;
- int PingCount;
- UInt32 dstaddr;
- struct hostent *h;
- PingParam_t hostName;
- int PingSize;
- UInt32 RespTime;
- UInt32 EchoResult;
- unsigned long interPingGap = KC_TICKS2SEC;
- FieldCount = sscanf(CmdStr + Ptr,"%s %s %s %s %s %s %s %s",
- PingParamList[0],
- PingParamList[1],
- PingParamList[2],
- PingParamList[3],
- PingParamList[4],
- PingParamList[5],
- PingParamList[6],
- PingParamList[7]);
- #ifdef PING_DEBUG
- printf("nPING: Param Count = %dn", FieldCount-1);
- #endif
- /*
- First we'll set up all of our defaults, then let's process
- all these options
- */
- PingCount = 3;
- PingSize = 64;
- for (i = 0; (i < FieldCount); i++)
- {
- param = (char *)&PingParamList[i];
- #ifdef PING_DEBUG
- printf("Parameter[%d] = [%s]n", i, param);
- #endif
- if (param[0] == '-')
- {
- if (param[1] == 'c')
- {
- PingCount = atoi(PingParamList[++i]);
- /* i += 1; */
- }
- if (param[1] == 's')
- {
- PingSize = atoi(PingParamList[++i]);
- }
- if (param[1] == 't')
- {
- interPingGap = (unsigned long) atoi(PingParamList[++i]);
- }
- continue;
- }
- /* we got a hostname, we done */
- strcpy(hostName,param);
- break;
- }
- if (PingSize > MAX_PING_SIZE)
- {
- PingSize = MAX_PING_SIZE;
- }
- Cli_Printf(pSess,
- "pinging %s %d times with packet of size %d with %d ticks betweenn",
- hostName,PingCount,PingSize,interPingGap);
- for (i = 0; (i < PingCount); i++)
- {
- if ((dstaddr = inet_addr(hostName)) == (unsigned int)-1)
- {
- /*
- h = gethostbyname(hostName);
- if(h)
- {
- memcpy(&dstaddr, h->h_addr, sizeof(dstaddr));
- }
- else*/
- {
- Cli_Printf(pSess,"PING: bad hostnamen");
- return 0;
- }
- }
- sts = iadk_ping((UInt8 *)&dstaddr, &PingBuffer[0], PingSize,
- MAX_PING_TIMEOUT, &RespTime, &EchoResult);
- Cli_Printf(pSess, "sts=%d, EchoResult=%dn", sts, EchoResult);
- if (EchoResult)
- {
- if (sts > 0)
- {
- unsigned char *p = (unsigned char *) &dstaddr;
- Cli_Printf(pSess, "Reply from %d.%d.%d.%d bytes = %d "
- "time = %d msn", p[0], p[1], p[2], p[3],
- sts, RespTime);
- }
- else if (sts == 0)
- {
- Cli_Printf(pSess, "Request Timed Outn");
- }
- }
- tm_wkafter(interPingGap);
- }
- return 0;
- }