NwEgtPingMain.c 9.03 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1 2 3 4 5
/*----------------------------------------------------------------------------*
 *                    Copyright (C) 2010 Amit Chawre.                         *
 *----------------------------------------------------------------------------*/


6
/**
Cedric Roux's avatar
 
Cedric Roux committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * @file NwEgtPingMain.c
 * @brief This is a program demostrating usage of nw-gtpv2c library for eGTP ping.
*/

#include <stdio.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
#include "NwEvt.h"
#include "NwLog.h"
#include "NwGtpv2c.h"

#include "NwMiniLogMgrEntity.h"
#include "NwMiniTmrMgrEntity.h"
#include "NwMiniUdpEntity.h"
#include "NwMiniUlpEntity.h"

#ifndef NW_ASSERT
#define NW_ASSERT assert
26
#endif
Cedric Roux's avatar
 
Cedric Roux committed
27 28 29 30 31

static
NwCharT* gLogLevelStr[] = {"EMER", "ALER", "CRIT",  "ERRO", "WARN", "NOTI", "INFO", "DEBG"};


32
typedef struct NwEgtPingS {
33 34 35 36
  uint8_t                         localIpStr[20];
  uint8_t                         targetIpStr[20];
  uint32_t                        pingInterval;
  uint32_t                        pingCount;
Cedric Roux's avatar
 
Cedric Roux committed
37 38 39 40 41 42 43 44 45 46 47 48
  NwGtpv2cNodeUlpT              ulpObj;
  NwGtpv2cNodeUdpT              udpObj;
} NwEgtPingT;

static NwGtpv2cNodeUlpT              ulpObj;
static NwGtpv2cNodeUdpT              udpObj;

static NwEgtPingT               egtPing;

void nwEgtPingHandleSignal(int sigNum)
{
  printf("\n--- %s ("NW_IPV4_ADDR") EGTPING statistics --- ", egtPing.targetIpStr, NW_IPV4_ADDR_FORMAT(inet_addr(egtPing.targetIpStr)));
49 50
  printf("\n%u requests sent, %u response received, %d%% packet loss \n\n", udpObj.packetsSent, udpObj.packetsRcvd,
         (udpObj.packetsSent ? 100 * (udpObj.packetsSent - udpObj.packetsRcvd ) / udpObj.packetsSent : 0) );
Cedric Roux's avatar
 
Cedric Roux committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  exit(sigNum);
}

NwRcT
nwEgtPingHelp()
{
  printf("Usage: egtping [-i interval] [-c count] [-l local-ip] ");
  printf("\n               [-t3 t3-time] [-n3 n3-count] destination");
  printf("\n");
  printf("\n       -i <interval>     : Interval between two echo request messages. (Default: 1 sec)");
  printf("\n       -c <count>        : Stop after sending count pings. (Default: Infinite)");
  printf("\n       -t <t3-time>      : GTP T3 timeout value. (Default: 2 sec)");
  printf("\n       -n <n3-count>     : GTP N3 count value. (Default: 2 sec)");
  printf("\n       -l <local-ip>     : Local IP adddress to use. (Default: All local IPs)");
  printf("\n       -h                : Show this message.");
  printf("\n");
  printf("\n");
}

NwRcT
nwEgtPingParseCmdLineOpts(int argc, char* argv[])
{
  NwRcT rc = NW_OK;
  int i = 0;
  i++;

  egtPing.pingInterval  = 1;
  egtPing.pingCount     = 0xffffffff;

  if(argc < 2)
    return NW_FAILURE;

  if( (argc == 2) &&
      ((strcmp("--help", argv[i]) == 0)
85
       || (strcmp(argv[i], "-h") == 0)))
Cedric Roux's avatar
 
Cedric Roux committed
86 87
    return NW_FAILURE;

88
  while( i < argc - 1) {
Cedric Roux's avatar
 
Cedric Roux committed
89
    NW_LOG(NW_LOG_LEVEL_DEBG, "Processing cmdline arg %s", argv[i]);
90

Cedric Roux's avatar
 
Cedric Roux committed
91
    if((strcmp("--local-ip", argv[i]) == 0)
92
        || (strcmp(argv[i], "-l") == 0)) {
Cedric Roux's avatar
 
Cedric Roux committed
93
      i++;
94

Cedric Roux's avatar
 
Cedric Roux committed
95 96 97 98 99
      if(i >= (argc - 1))
        return NW_FAILURE;

      strcpy(egtPing.localIpStr, (argv[i]));

100 101
    } else if((strcmp("--interval", argv[i]) == 0)
              || (strcmp(argv[i], "-i") == 0)) {
Cedric Roux's avatar
 
Cedric Roux committed
102
      i++;
103

Cedric Roux's avatar
 
Cedric Roux committed
104 105 106 107 108
      if(i >= (argc - 1))
        return NW_FAILURE;

      egtPing.pingInterval = atoi(argv[i]);

109 110
    } else if((strcmp("--count", argv[i]) == 0)
              || (strcmp(argv[i], "-c") == 0)) {
Cedric Roux's avatar
 
Cedric Roux committed
111
      i++;
112

Cedric Roux's avatar
 
Cedric Roux committed
113 114 115 116
      if(i >= (argc - 1))
        return NW_FAILURE;

      egtPing.pingCount = atoi(argv[i]);
117 118
    } else if((strcmp("--help", argv[i]) == 0)
              || (strcmp(argv[i], "-h") == 0)) {
Cedric Roux's avatar
 
Cedric Roux committed
119
      rc = NW_FAILURE;
120
    } else {
Cedric Roux's avatar
 
Cedric Roux committed
121 122
      return NW_FAILURE;
    }
123

Cedric Roux's avatar
 
Cedric Roux committed
124 125 126 127 128 129 130 131 132
    i++;
  }

  strcpy(egtPing.targetIpStr, (argv[i]));

  return rc;
}

/*---------------------------------------------------------------------------
133
 *                T H E      M A I N      F U N C T I O N
Cedric Roux's avatar
 
Cedric Roux committed
134 135 136 137
 *--------------------------------------------------------------------------*/

int main(int argc, char* argv[])
{
138
  NwRcT                         rc;
139 140
  uint32_t                        logLevel;
  uint8_t*                        logLevelStr;
Cedric Roux's avatar
 
Cedric Roux committed
141 142 143 144 145 146 147 148 149 150 151

  NwGtpv2cStackHandleT          hGtpv2cStack = 0;

  NwGtpv2cUlpEntityT            ulp;
  NwGtpv2cUdpEntityT            udp;
  NwGtpv2cTimerMgrEntityT       tmrMgr;
  NwGtpv2cLogMgrEntityT         logMgr;

  printf("EGTPING 0.1, Copyright (C) 2011 Amit Chawre.\n");
  rc = nwEgtPingParseCmdLineOpts(argc, argv);

152
  if(rc != NW_OK) {
Cedric Roux's avatar
 
Cedric Roux committed
153 154 155 156 157 158
    rc = nwEgtPingHelp();
    exit(rc);
  }

  logLevelStr = getenv ("NW_LOG_LEVEL");

159
  if(logLevelStr == NULL) {
Cedric Roux's avatar
 
Cedric Roux committed
160
    logLevel = NW_LOG_LEVEL_INFO;
161
  } else {
Cedric Roux's avatar
 
Cedric Roux committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if(strncmp(logLevelStr, "EMER",4) == 0)
      logLevel = NW_LOG_LEVEL_EMER;
    else if(strncmp(logLevelStr, "ALER",4) == 0)
      logLevel = NW_LOG_LEVEL_ALER;
    else if(strncmp(logLevelStr, "CRIT",4) == 0)
      logLevel = NW_LOG_LEVEL_CRIT;
    else if(strncmp(logLevelStr, "ERRO",4) == 0)
      logLevel = NW_LOG_LEVEL_ERRO ;
    else if(strncmp(logLevelStr, "WARN",4) == 0)
      logLevel = NW_LOG_LEVEL_WARN;
    else if(strncmp(logLevelStr, "NOTI",4) == 0)
      logLevel = NW_LOG_LEVEL_NOTI;
    else if(strncmp(logLevelStr, "INFO",4) == 0)
      logLevel = NW_LOG_LEVEL_INFO;
    else if(strncmp(logLevelStr, "DEBG",4) == 0)
      logLevel = NW_LOG_LEVEL_DEBG;
  }

  /*---------------------------------------------------------------------------
   *  Initialize event library
   *--------------------------------------------------------------------------*/

  NW_EVT_INIT();

  /*---------------------------------------------------------------------------
187
   *  Initialize Log Manager
Cedric Roux's avatar
 
Cedric Roux committed
188 189 190 191 192 193 194 195
   *--------------------------------------------------------------------------*/
  nwMiniLogMgrInit(nwMiniLogMgrGetInstance(), logLevel);

  /*---------------------------------------------------------------------------
   *  Initialize Gtpv2c Stack Instance
   *--------------------------------------------------------------------------*/
  rc = nwGtpv2cInitialize(&hGtpv2cStack);

196
  if(rc != NW_OK) {
Cedric Roux's avatar
 
Cedric Roux committed
197 198 199 200 201 202 203
    NW_LOG(NW_LOG_LEVEL_ERRO, "Failed to create gtpv2c stack instance. Error '%u' occured", rc);
    exit(1);
  }

  rc = nwGtpv2cSetLogLevel(hGtpv2cStack, logLevel);

  /*---------------------------------------------------------------------------
204
   * Set up Ulp Entity
Cedric Roux's avatar
 
Cedric Roux committed
205 206 207 208 209 210 211 212 213 214 215
   *--------------------------------------------------------------------------*/
  rc = nwGtpv2cUlpInit(&ulpObj, hGtpv2cStack, egtPing.localIpStr);
  NW_ASSERT(NW_OK == rc);

  ulp.hUlp = (NwGtpv2cUlpHandleT) &ulpObj;
  ulp.ulpReqCallback = nwGtpv2cUlpProcessStackReqCallback;

  rc = nwGtpv2cSetUlpEntity(hGtpv2cStack, &ulp);
  NW_ASSERT(NW_OK == rc);

  /*---------------------------------------------------------------------------
216
   * Set up Udp Entity
Cedric Roux's avatar
 
Cedric Roux committed
217 218 219 220 221 222 223 224 225 226 227
   *--------------------------------------------------------------------------*/
  rc = nwGtpv2cUdpInit(&udpObj, hGtpv2cStack, egtPing.localIpStr);
  NW_ASSERT(NW_OK == rc);

  udp.hUdp = (NwGtpv2cUdpHandleT) &udpObj;
  udp.udpDataReqCallback = nwGtpv2cUdpDataReq;

  rc = nwGtpv2cSetUdpEntity(hGtpv2cStack, &udp);
  NW_ASSERT(NW_OK == rc);

  /*---------------------------------------------------------------------------
228
   * Set up Log Entity
Cedric Roux's avatar
 
Cedric Roux committed
229 230 231 232 233 234 235 236 237
   *--------------------------------------------------------------------------*/
  tmrMgr.tmrMgrHandle = 0;
  tmrMgr.tmrStartCallback = nwTimerStart;
  tmrMgr.tmrStopCallback = nwTimerStop;

  rc = nwGtpv2cSetTimerMgrEntity(hGtpv2cStack, &tmrMgr);
  NW_ASSERT(NW_OK == rc);

  /*---------------------------------------------------------------------------
238
   * Set up Log Entity
Cedric Roux's avatar
 
Cedric Roux committed
239 240 241 242 243 244 245 246 247 248 249
   *--------------------------------------------------------------------------*/
  logMgr.logMgrHandle   = (NwGtpv2cLogMgrHandleT) nwMiniLogMgrGetInstance();
  logMgr.logReqCallback  = nwMiniLogMgrLogRequest;

  rc = nwGtpv2cSetLogMgrEntity(hGtpv2cStack, &logMgr);
  NW_ASSERT(NW_OK == rc);

  /*---------------------------------------------------------------------------
   *  Send Message Request to Gtpv2c Stack Instance
   *--------------------------------------------------------------------------*/
  NW_LOG(NW_LOG_LEVEL_NOTI, "EGTPING %s ("NW_IPV4_ADDR")", egtPing.targetIpStr, NW_IPV4_ADDR_FORMAT(inet_addr(egtPing.targetIpStr)));
250 251 252 253 254 255
  rc = nwGtpv2cUlpPing(&ulpObj,
                       inet_addr(egtPing.targetIpStr),
                       egtPing.pingCount,
                       egtPing.pingInterval,
                       2,
                       3);
Cedric Roux's avatar
 
Cedric Roux committed
256 257 258
  NW_ASSERT(NW_OK == rc);

  /*---------------------------------------------------------------------------
259
   * Install signal handler
Cedric Roux's avatar
 
Cedric Roux committed
260 261 262 263
   *--------------------------------------------------------------------------*/
  signal(SIGINT, nwEgtPingHandleSignal);

  /*---------------------------------------------------------------------------
264
   * Event loop
Cedric Roux's avatar
 
Cedric Roux committed
265 266 267 268 269 270 271 272 273
   *--------------------------------------------------------------------------*/

  NW_EVT_LOOP();
  NW_LOG(NW_LOG_LEVEL_ERRO, "Exit from eventloop, no events to process!");

  /*---------------------------------------------------------------------------
   *  Destroy Gtpv2c Stack Instance
   *--------------------------------------------------------------------------*/
  rc = nwGtpv2cFinalize(hGtpv2cStack);
274 275

  if(rc != NW_OK) {
Cedric Roux's avatar
 
Cedric Roux committed
276 277 278 279 280
    NW_LOG(NW_LOG_LEVEL_ERRO, "Failed to finalize gtpv2c stack instance. Error '%u' occured", rc);
  }

  return rc;
}