routeping.inl

来自「开放源码实时操作系统源码.」· INL 代码 · 共 641 行 · 第 1/2 页

INL
641
字号
//==========================================================================
//
//      autotest/current/tests/routeping.inl
//
//      Simple pinging test, the server pings me and I ping her.
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//####BSDCOPYRIGHTBEGIN####
//
// -------------------------------------------
//
// Portions of this software may have been derived from OpenBSD or other sources,
// and are covered by the appropriate copyright disclaimers included herein.
//
// -------------------------------------------
//
//####BSDCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    hmt,gthomas
// Contributors: hmt,gthomas
// Date:         2000-10-23
// Purpose:      
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <pkgconf/system.h>

#include <cyg/infra/testcase.h>         // testing infrastructure

#include <pkgconf/net.h>

#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
#include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
#endif

#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
#  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
# endif
#endif


// Fill in the blanks if necessary
#ifndef TNR_OFF
# define TNR_OFF()
#endif
#ifndef TNR_ON
# define TNR_ON()
#endif
#ifndef TNR_INIT
# define TNR_INIT()
#endif
#ifndef TNR_PRINT_ACTIVITY
# define TNR_PRINT_ACTIVITY()
#endif

// ------------------------------------------------------------------------

#include <errno.h>
#include <network.h>

#include "autohost.inl"

// ------------------------------------------------------------------------

externC int sscanf( char * /* str */, const char * /* format */, ... );

// ------------------------------------------------------------------------

static int route_add_s( struct sockaddr_in *target,
                        struct sockaddr_in *gw,
                        int maskbits,
                        int metric )
{
    struct ecos_rtentry route;
    struct sockaddr_in mask;

    int s;

    memcpy( &mask, gw, sizeof(*gw) );
    maskbits--;
    mask.sin_addr.s_addr = htonl( (0xfffffffful ^ ((0x80000000ul >> maskbits)-1)) );

    memset( &route, 0, sizeof(route) );

    memcpy( &route.rt_dst    ,  target, sizeof(*target) );
    memcpy( &route.rt_gateway,  gw    , sizeof(*gw) );
    memcpy( &route.rt_genmask, &mask  , sizeof(mask) ); 

    route.rt_flags = RTF_UP|RTF_GATEWAY;
    route.rt_metric = metric;

    route.rt_dev = NULL;

    diag_printf("INFO:<Route - dst: %s",
                inet_ntoa(((struct sockaddr_in *)&route.rt_dst)->sin_addr));
    diag_printf(", mask: %s",
                inet_ntoa(((struct sockaddr_in *)&route.rt_genmask)->sin_addr));
    diag_printf(", gateway: %s>\n",
                inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));

    s = socket( AF_INET, SOCK_DGRAM, 0 );
    if (s < 0) {
        perror( "socket" );
        return false;
    }
    if (ioctl(s, SIOCADDRT, &route)) {
        perror("SIOCADDRT");
        close(s);
        return false;
    }
    diag_printf( "PASS:<Route added OK>\n" );
    close(s);
    return true;
}



int route_add( char *target,
                      char *gw,
                      int maskbits,
                      int metric )
{
    struct sockaddr_in t_s, gw_s;
    int ints[4];

    memset( &t_s,  0, sizeof(t_s)  );
    memset( &gw_s, 0, sizeof(gw_s) );

    t_s.sin_len    = gw_s.sin_len    = sizeof(t_s);
    t_s.sin_family = gw_s.sin_family = AF_INET;

    if ( 4 != sscanf( target, "%d.%d.%d.%d", ints, ints+1, ints+2, ints+3 ) )
        CYG_TEST_FAIL( "sscanf of target IP addr" );
    else
        t_s.sin_addr.s_addr = htonl( 
            (ints[0] << 24) |
            (ints[1] << 16) |
            (ints[2] <<  8) |
            (ints[3]      )  );
    
    if ( 4 != sscanf( gw, "%d.%d.%d.%d", ints, ints+1, ints+2, ints+3 ) )
        CYG_TEST_FAIL( "sscanf of target IP addr" );
    else
        gw_s.sin_addr.s_addr = htonl( 
            (ints[0] << 24) |
            (ints[1] << 16) |
            (ints[2] <<  8) |
            (ints[3]      )  );

    return route_add_s( &t_s, &gw_s, maskbits, metric );
}

// ------------------------------------------------------------------------

#define NUM_PINGS 16
#define MAX_PACKET 4096
#define MIN_PACKET   64
#define MAX_SEND   4000

#define PACKET_ADD  ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
#define nPACKET_ADD  1 

static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];

#define UNIQUEID 0x1234

// Compute INET checksum
int
inet_cksum(u_short *addr, int len)
{
    register int nleft = len;
    register u_short *w = addr;
    register u_short answer;
    register u_int sum = 0;
    u_short odd_byte = 0;

    /*
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
     *  we add sequential 16 bit words to it, and at the end, fold
     *  back all the carry bits from the top 16 bits into the lower
     *  16 bits.
     */
    while( nleft > 1 )  {
        sum += *w++;
        nleft -= 2;
    }

    /* mop up an odd byte, if necessary */
    if( nleft == 1 ) {
        *(u_char *)(&odd_byte) = *(u_char *)w;
        sum += odd_byte;
    }

    /*
     * add back carry outs from top 16 bits to low 16 bits
     */
    sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
    sum += (sum >> 16);                     /* add carry */
    answer = ~sum;                          /* truncate to 16 bits */
    return (answer);
}

static int
errshorts, errinvalids, errbadids, errwronghosts, errsendtos, errrecvfroms;
#define ERRRESET() \
errshorts=errinvalids=errbadids=errwronghosts=errsendtos=errrecvfroms=0

static int
check_icmp(unsigned char *pkt, int len, 
          struct sockaddr_in *from, struct sockaddr_in *to)
{
    cyg_tick_count_t *tp, tv;
    struct ip *ip;
    struct icmp *icmp;
    tv = cyg_current_time();
    ip = (struct ip *)pkt;
    if (len < sizeof(*ip))
        if (ip->ip_v != IPVERSION) {
        errshorts++;
        return 0;
    }
    icmp = (struct icmp *)(pkt + sizeof(*ip));
    len -= (sizeof(*ip) + 8);
    tp = (cyg_tick_count_t *)&icmp->icmp_data;
    if (icmp->icmp_type != ICMP_ECHOREPLY) {
        errinvalids++;
        return 0;
    }
    if (icmp->icmp_id != UNIQUEID) {
        errbadids++;
        return 0;
    }
    if (from->sin_addr.s_addr != to->sin_addr.s_addr) {
        errwronghosts++;
        return 0;
    }
    return 1;
}

// expect is
// 0 - require failure
// 1 - require success
// 2 - don't care

#define FAILURE_REQUIRED 0
#define SUCCESS_REQUIRED 1
#define NOTHING_REQUIRED 2

static void
ping_host(int s, struct sockaddr_in *host, int expect )
{
    struct icmp *icmp = (struct icmp *)pkt1;
    int icmp_len = MIN_PACKET;
    int seq, ok_recv, bogus_recv;
    cyg_tick_count_t *tp;
    long *dp;
    struct sockaddr_in from;
    int i, len, fromlen;

    ERRRESET();
    ok_recv = 0;
    bogus_recv = 0;
    TNR_OFF();
    diag_printf("INFO:<PING server %s ....>\n", inet_ntoa(host->sin_addr));
    for (seq = 0;  seq < NUM_PINGS;  seq++, icmp_len += PACKET_ADD ) {
        TNR_ON();
        cyg_thread_delay( 47 ); // Half a second...
        // Build ICMP packet
        icmp->icmp_type = ICMP_ECHO;
        icmp->icmp_code = 0;
        icmp->icmp_cksum = 0;
        icmp->icmp_seq = seq;
        icmp->icmp_id = 0x1234;
        // Set up ping data
        tp = (cyg_tick_count_t *)&icmp->icmp_data;
        *tp++ = cyg_current_time();
        dp = (long *)tp;
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
            *dp++ = i;
        }
        // Add checksum
        icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
        // Send it off
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {

⌨️ 快捷键说明

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