rawiptst.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 1,299 行 · 第 1/3 页

C
1,299
字号
 *
 * PARAM1: void * pio; IN- ptr to GEN_IO object for command-line output
 *
 * RETURNS: an integer indicating success or failure; 
 *          zero indicates success; values <0 indicate failure
 */
int
raw_rihalt(void * pio)
{
   char * arg2;
   long lid;
   unsigned id;
   struct raw_tester * tester;

   /* get argument: tester ID (required) 
    */
   arg2 = nextarg(((GEN_IO)pio)->inbuf);
   if (!arg2 || !*arg2)
   {
      ns_printf(pio, "requires raw IP tester ID\n");
      return -1;
   }
   lid = atol(arg2);
   id = (unsigned)lid;
   if (((long)id) != lid)
   {
      ns_printf(pio, "invalid ID %ld\n", lid);
      return -1;
   }

   /* lock the queue */
   raw_testerq_lock();

   /* look for the tester */
   tester = raw_tester_findid(id);
   if (tester == NULL)
   {
      ns_printf(pio, "ID [%d] not found\n", id);
      raw_testerq_unlock();
      return -1;
   }

   /* if the tester is sending, stop it
    */
   if (tester->sending)
   {
      ns_printf(pio, "Terminating previous send on ID [%d]\n", id);
      tester->sending = 0;
   }
   else
   {
      ns_printf(pio, "ID [%d] not sending\n", id);
   }

   /* unlock the queue */
   raw_testerq_unlock();

   /* and return success */
   return 0;
}

/* FUNCTION: raw_ribind()
 *
 * Tells a tester to bind to a given IP address.
 *
 * PARAM1: void * pio; IN- ptr to GEN_IO object for command-line output
 *
 * RETURNS: an integer indicating success or failure; 
 *          zero indicates success; values <0 indicate failure
 */
int
raw_ribind(void * pio)
{
   char * arg2;
   char * arg3;
   long lid;
   unsigned id;
   unsigned long bindaddr;
   struct raw_tester * tester;
   struct sockaddr_in addr;
   int e;

   /* get argument: tester ID (required) 
    */
   arg2 = nextarg(((GEN_IO)pio)->inbuf);
   if (!arg2 || !*arg2)
   {
      ns_printf(pio, "requires raw IP tester ID\n");
      return -1;
   }
   lid = atol(arg2);
   id = (unsigned)lid;
   if (((long)id) != lid)
   {
      ns_printf(pio, "invalid ID %ld\n", lid);
      return -1;
   }

   /* get second argument: host address to bind to (required)
    */
   arg3 = nextarg(arg2);
   if (!arg3 || !*arg3)
   {
      ns_printf(pio, "requires host address to bind to\n");
      return -1;
   }

   e = in_reshost(arg3, &bindaddr, RH_VERBOSE | RH_BLOCK);
   if (e)
   {
      ns_printf(pio, "Unable to resolve host name \"%s\"\n", arg3);
      return -1;
   }

   /* lock the queue */
   raw_testerq_lock();

   /* look for the tester */
   tester = raw_tester_findid(id);
   if (tester == NULL)
   {
      ns_printf(pio, "ID [%d] not found\n", id);
      raw_testerq_unlock();
      return -1;
   }

   /* bind the tester's socket to the given address
    */
   addr.sin_family = AF_INET;
   addr.sin_port = 0;
   addr.sin_addr.s_addr = bindaddr;
   e = bind(tester->sock, (struct sockaddr *)&addr, sizeof(addr));
   if (e < 0)
   {
      ns_printf(pio, "ID [%d] bind error %d to address %u.%u.%u.%u\n", 
                id, t_errno(tester->sock), PUSH_IPADDR(bindaddr));
   }
   else
   {
      ns_printf(pio, "ID [%d] bound to address %u.%u.%u.%u\n", 
                id, PUSH_IPADDR(bindaddr));
      e = 0;
   }

   /* unlock the queue */
   raw_testerq_unlock();

   /* and return success/failure */
   return e;
}

/* FUNCTION: raw_riconnect()
 *
 * Tells a tester to connect to a given IP address.
 *
 * PARAM1: void * pio; IN- ptr to GEN_IO object for command-line output
 *
 * RETURNS: an integer indicating success or failure; 
 *          zero indicates success; values <0 indicate failure
 */
int
raw_riconnect(void * pio)
{
   char * arg2;
   char * arg3;
   long lid;
   unsigned id;
   unsigned long connaddr;
   struct raw_tester * tester;
   struct sockaddr_in addr;
   int e;

   /* get argument: tester ID (required) 
    */
   arg2 = nextarg(((GEN_IO)pio)->inbuf);
   if (!arg2 || !*arg2)
   {
      ns_printf(pio, "requires raw IP tester ID\n");
      return -1;
   }
   lid = atol(arg2);
   id = (unsigned)lid;
   if (((long)id) != lid)
   {
      ns_printf(pio, "invalid ID %ld\n", lid);
      return -1;
   }

   /* get second argument: host address to connect to (required)
    */
   arg3 = nextarg(arg2);
   if (!arg3 || !*arg3)
   {
      ns_printf(pio, "requires host address to connect to\n");
      return -1;
   }

   e = in_reshost(arg3, &connaddr, RH_VERBOSE | RH_BLOCK);
   if (e)
   {
      ns_printf(pio, "Unable to resolve host name \"%s\"\n", arg3);
      return -1;
   }

   /* lock the queue */
   raw_testerq_lock();

   /* look for the tester */
   tester = raw_tester_findid(id);
   if (tester == NULL)
   {
      ns_printf(pio, "ID [%d] not found\n", id);
      raw_testerq_unlock();
      return -1;
   }

   /* connect the tester's socket to the given address
    */
   addr.sin_family = AF_INET;
   addr.sin_port = 0;
   addr.sin_addr.s_addr = connaddr;
   e = connect(tester->sock, (struct sockaddr *)&addr, sizeof(addr));
   if (e < 0)
   {
      ns_printf(pio, "ID [%d] connect error %d to address %u.%u.%u.%u\n", 
                id, t_errno(tester->sock), PUSH_IPADDR(connaddr));
   }
   else
   {
      if (connaddr == 0)
      {
         ns_printf(pio, "ID [%d] disconnected\n", id);
         tester->conn = 0;
      }
      else
      {
         ns_printf(pio, "ID [%d] connected to address %u.%u.%u.%u\n", 
                   id, PUSH_IPADDR(connaddr));
         tester->conn = 1;
      }
      e = 0;
   }

   /* unlock the queue */
   raw_testerq_unlock();

   /* and return success/failure */
   return e;
}

/* FUNCTION: raw_richeck()
 *
 * Tells a tester to toggle its "verify received data" setting
 *
 * PARAM1: void * pio; IN- ptr to GEN_IO object for command-line output
 *
 * RETURNS: an integer indicating success or failure; 
 *          zero indicates success; values <0 indicate failure
 */
int
raw_richeck(void * pio)
{
   char * arg2;
   long lid;
   unsigned id;
   struct raw_tester * tester;

   /* get argument: tester ID (required) 
    */
   arg2 = nextarg(((GEN_IO)pio)->inbuf);
   if (!arg2 || !*arg2)
   {
      ns_printf(pio, "requires raw IP tester ID\n");
      return -1;
   }
   lid = atol(arg2);
   id = (unsigned)lid;
   if (((long)id) != lid)
   {
      ns_printf(pio, "invalid ID %ld\n", lid);
      return -1;
   }

   /* lock the queue */
   raw_testerq_lock();

   /* look for the tester */
   tester = raw_tester_findid(id);
   if (tester == NULL)
   {
      ns_printf(pio, "ID [%d] not found\n", id);
      raw_testerq_unlock();
      return -1;
   }

   /* if the tester is verifying received data, stop it; else start
    */
   ns_printf(pio, "Turning receive verification %s for ID [%d]\n", 
             (tester->rxcheck) ? "off" : "on",
             id);
   tester->rxcheck = (tester->rxcheck) ? 0 : 1;

   /* unlock the queue */
   raw_testerq_unlock();

   /* and return success */
   return 0;
}

/* FUNCTION: raw_rihdrincl()
 *
 * Tells a tester to toggle its "include IP header" setting
 *
 * PARAM1: void * pio; IN- ptr to GEN_IO object for command-line output
 *
 * RETURNS: an integer indicating success or failure; 
 *          zero indicates success; values <0 indicate failure
 */
int
raw_rihdrincl(void * pio)
{
   char * arg2;
   long lid;
   unsigned id;
   struct raw_tester * tester;
   int hdrincl;
   int optlen;
   int e;

   /* get argument: tester ID (required) 
    */
   arg2 = nextarg(((GEN_IO)pio)->inbuf);
   if (!arg2 || !*arg2)
   {
      ns_printf(pio, "requires raw IP tester ID\n");
      return -1;
   }
   lid = atol(arg2);
   id = (unsigned)lid;
   if (((long)id) != lid)
   {
      ns_printf(pio, "invalid ID %ld\n", lid);
      return -1;
   }

   /* lock the queue */
   raw_testerq_lock();

   /* look for the tester */
   tester = raw_tester_findid(id);
   if (tester == NULL)
   {
      ns_printf(pio, "ID [%d] not found\n", id);
      raw_testerq_unlock();
      return -1;
   }

   /* find out whether the socket has the IP_HDRINCL option set
    */
   optlen = sizeof(hdrincl);
   e = getsockopt(tester->sock, IPPROTO_IP, IP_HDRINCL,
                  &hdrincl, &optlen);
   if (e < 0)
   {
      ns_printf(pio,
                "getsockopt error %d on ID [%d]\n",
                t_errno(tester->sock), tester->id);
      raw_testerq_unlock();
      return -1;
   }

   /* if the tester is verifying received data, stop it; else start
    */
   ns_printf(pio, "Turning IP_HDRINCL %s for ID [%d]\n", 
             (hdrincl) ? "off" : "on", id);
   hdrincl = (hdrincl) ? 0 : 1;
   e = setsockopt(tester->sock, IPPROTO_IP, IP_HDRINCL,
                  &hdrincl, sizeof(hdrincl));
   if (e < 0)
   {
      ns_printf(pio,
                "setsockopt error %d on ID [%d]\n",
                t_errno(tester->sock), tester->id);
      raw_testerq_unlock();
      return -1;
   }

   /* unlock the queue */
   raw_testerq_unlock();

   /* and return success */
   return 0;
}

#ifdef IN_MENUS
/* raw_tester_menu[] - NicheTool command menu for raw IP testing
 */
struct menu_op raw_tester_menu[] =
{
   "rawiptest",   stooges,       "menu to control raw IP testing",
   "ristart",     raw_ristart,   "start a raw IP tester",
   "ridelete",    raw_ridelete,  "delete a raw IP tester",
   "rilist",      raw_rilist,    "list raw IP testers",
   "risend",      raw_risend,    "tell a raw IP tester to start sending",
   "rihalt",      raw_rihalt,    "tell a raw IP tester to stop sending",
   "ribind",      raw_ribind,    "bind a raw IP tester to a host address",
   "riconnect",   raw_riconnect, "connect a raw IP tester to a host address",
   "richeck",     raw_richeck,   "toggle receive data verification",
   "rihdrincl",   raw_rihdrincl, "toggle IP_HDRINCL socket option",
   NULL,
};
#endif /* IN_MENUS */

/* FUNCTION: raw_test_init()
 * 
 * Initialize for raw IP testing operations. Presently this function adds
 * the raw IP testing menu.
 *
 * PARAM1: void
 *
 * RETURNS: 0
 */
int
raw_test_init(void)
{
#ifdef IN_MENUS
   install_menu(raw_tester_menu);
#endif /* IN_MENUS */
   return 0;
}

#endif      /* RAWIPTEST */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?