⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpping.c

📁 eCos/RedBoot for勤研ARM AnywhereII(4510) 含全部源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        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) {
            TNR_OFF();
            perror("sendto");
            continue;
        }
        // Wait for a response
        fromlen = sizeof(from);
        len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
        TNR_OFF();
        if (len < 0) {
            perror("recvfrom");
            icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
        } else {
            if (show_icmp(pkt2, len, &from, host)) {
                ok_recv++;
            } else {
                bogus_recv++;
            }
        }
    }
    TNR_OFF();
    diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
}

static void
ping_test(struct bootp *bp)
{
    struct protoent *p;
    struct timeval tv;
    struct sockaddr_in host;
    int s;

    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
        perror("getprotobyname");
        return;
    }
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
    if (s < 0) {
        perror("socket");
        return;
    }
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
    // Set up host address
    host.sin_family = AF_INET;
    host.sin_len = sizeof(host);
    host.sin_addr = bp->bp_siaddr;
    host.sin_port = 0;
    ping_host(s, &host);
    // Now try a bogus host
    // (also, map 76 <-> 191 so that if a pair runs they ping each other)
    host.sin_addr = bp->bp_yiaddr; // *my* address.
//    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) ^ 0xf3);
    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) ^ 2);
    ping_host(s, &host);
    close(s);
}

static void
ping_test_loopback( int lo )
{
    struct protoent *p;
    struct timeval tv;
    struct sockaddr_in host;
    int s;

    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
        perror("getprotobyname");
        return;
    }
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
    if (s < 0) {
        perror("socket");
        return;
    }
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
    // Set up host address
    host.sin_family = AF_INET;
    host.sin_len = sizeof(host);
    host.sin_addr.s_addr = htonl(INADDR_LOOPBACK + (0x100 * lo));
    host.sin_port = 0;
    ping_host(s, &host);
    // Now try a bogus host
    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
    ping_host(s, &host);
    close(s);
}

#ifdef CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
static void 
interface_promisc(const char *intf)
{
  struct ifreq ifr;
  int s;

  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("socket");
    return;
  }

  strcpy(ifr.ifr_name, intf);
  ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_PROMISC;
  if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
    perror("SIOCSIFFLAGS");
  }
  close(s);
}
#endif // CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS

void snmp_do_reinit( void )
{
    diag_printf( "SNMP re-init function\n" );
#ifdef  CYGSEM_SNMPAGENT_TESTS_SNMPv3
    // Initialisation for USM is now invoked from mib_module_inits.h
    //init_usmUser();             /* MIBs to support SNMPv3             */
    //init_usmStats();
    //init_snmpEngine();
    usm_parse_create_usmUser(NULL, "root MD5 md5passwd DES DESpasswd");
#endif //  CYGSEM_SNMPAGENT_TESTS_SNMPv3
}

int create_snmpd_conf (void) {
#ifdef CYGSEM_SNMPAGENT_TESTS_SNMPv3
#ifdef CYGPKG_SNMPLIB_FILESYSTEM_SUPPORT 
#ifdef CYGPKG_FS_RAM
  int c;
  FILE *fd;

  diag_printf ("\nStarting creation of snmpd.conf\n");

  /* Mount RAM-FS */
  if (mount ("", "/", "ramfs") != 0) {
    diag_printf ("File system mount failed; errno=%d \n", errno);
    return -1;
  }
  
  if (mkdir ("/etc", 0) != 0) {
    diag_printf ("mkdir (etc) failed;  errno=%d\n", errno);
    return -1;
  } 

  if (chdir ("/etc") != 0) {
    diag_printf ("... Change-dir (etc) failed; errno=%d\n", errno);
    return -1;
  } else { 
    diag_printf ("chdir-etc done\n");
  }

  if (mkdir ("snmp", 0) != 0) {
    diag_printf ("mkdir failed (snmp);  errno=%d\n", errno);
    return -1;
  } else {
    diag_printf ("mkdir-snmp done\n");
  }

  if (chdir ("snmp") != 0) {
    diag_printf ("... Change-dir (snmp) failed; errno=%d\n", errno);
    return -1;
  } else { 
    diag_printf ("... Change-dir (snmp) done \n");
  }

  /* Open File & Write to it  */
  if ((fd = fopen( "snmpd.conf", "w" )) == NULL) {
    diag_printf ("fopen failed\n");
    return -1;
  }

  fprintf (fd, "#        sec.name     source       community\n");
  fprintf (fd, "com2sec   public     default       crux\n");
  fprintf (fd, "com2sec   root       default       crux\n");
  fprintf (fd, "#                 sec.model   sec.name\n");
  fprintf (fd, "group     public    v1        public\n");
  fprintf (fd, "group     public    v2c       public\n");
  fprintf (fd, "group     public    usm       root\n");
  fprintf (fd, "view      all  included  .1\n");
  fprintf (fd, "access    public    \"\"   any  noauth    exact     all  none none\n");
  fprintf (fd, "\n\n");

  if (fclose (fd)) {
    diag_printf ("fclose failed\n");
    return -1;
  }

  /* Read Back */
  fd = fopen( "/etc/snmp/snmpd.conf", "r" );
  if (fd == NULL) { 
    diag_printf ("fopen failed\n");
    return -1;
  }

  while ((c=fgetc (fd)) != EOF) {
    diag_printf ("%c", c);
  }

  if (fclose (fd))  {
    diag_printf ("fclose failed\n");
    return -1;
  }
#endif
#endif
#endif
  return 0;
}

void
net_test(cyg_addrword_t p)
{
    int i = CYGNUM_SNMPAGENT_TESTS_ITERATIONS;
    int ieth0_up = 0, ieth1_up = 0;
    int j;

    diag_printf("Start PING test\n");
    TNR_INIT();
    init_all_network_interfaces();
#ifdef CYGHWR_NET_DRIVER_ETH0
    ieth0_up = eth0_up;
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    ieth1_up = eth1_up;
#endif
#ifdef CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
#ifdef CYGHWR_NET_DRIVER_ETH0
        if (eth0_up)
            interface_promisc("eth0");
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
        if (eth1_up)
            interface_promisc("eth1");
#endif
#endif // CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
    {
        extern void cyg_net_snmp_init(void);
        extern void (*snmpd_reinit_function)( void );

        snmpd_reinit_function = snmp_do_reinit;

        if (create_snmpd_conf ()) {
          CYG_TEST_FAIL_EXIT("create_snmpd_conf() error\n");
        }
        cyg_net_snmp_init();
    }
    do {
        TNR_ON();
#ifdef CYGHWR_NET_DRIVER_ETH0
        if (eth0_up) {
            ping_test(&eth0_bootp_data);
            cyg_thread_delay(500);
        }
#endif
#if NLOOP > 0
        for ( j = 0; j < NLOOP; j++ ) {
            ping_test_loopback( j );
            cyg_thread_delay(500);
        }
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
        if (eth1_up) {
            ping_test(&eth1_bootp_data);
            cyg_thread_delay(500);
        }
#endif
        TNR_OFF();
        TNR_PRINT_ACTIVITY();

        // If an interface has gone down eg. due to DHCP timing out,
        // re-initialize everything:
        if (0
#ifdef CYGHWR_NET_DRIVER_ETH0
            || ieth0_up != eth0_up
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
            || ieth1_up != eth1_up 
#endif
            ) {
            diag_printf( "Re-initializing the world: eth0 %d/%d eth1 %d/%d!\n",
                         ieth0_up, 0
#ifdef CYGHWR_NET_DRIVER_ETH0
                         | eth0_up
#endif
                         , ieth1_up, 0
#ifdef CYGHWR_NET_DRIVER_ETH1
                         | eth1_up
#endif
                );
            init_all_network_interfaces();
            for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )
                init_loopback_interface( j );

            diag_printf( "Re-initialized the world: eth0 %d/%d eth1 %d/%d!\n",
                         ieth0_up, 0
#ifdef CYGHWR_NET_DRIVER_ETH0
                         | eth0_up
#endif
                         , ieth1_up, 0
#ifdef CYGHWR_NET_DRIVER_ETH1
                         | eth1_up
#endif
                );
        }
    } while ( i-- > 0 );
    CYG_TEST_PASS_FINISH( "Done pinging while SNMP looks on" );
}

void
cyg_start(void)
{
    // Create a main thread, so we can run the scheduler and have time 'pass'
    cyg_thread_create(10,                // Priority - just a number
                      net_test,          // entry
                      0,                 // entry parameter
                      "Network test",    // Name
                      &stack[0],         // Stack
                      STACK_SIZE,        // Size
                      &thread_handle,    // Handle
                      &thread_data       // Thread data structure
            );
    cyg_thread_resume(thread_handle);  // Start it
    cyg_scheduler_start();
}

// EOF snmpping.c

⌨️ 快捷键说明

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