Ejemplo n.º 1
0
int lsif(char *ifname) {
    struct snmp_pdu *pdu, *resp;
    oid tmp_oid[MAX_OID_LEN];
    size_t tmp_oid_len;
    oid ifname_oid[] = { 1,3,6,1,2,1,2,2,1,2 };
    int stat, next;
    char *tmp;
    char *ifstat[3] = { "unkn", "up", "down" };
    char ifalias[32];
    
    if(!ifname) {
        prsnmpstr(".1.3.6.1.2.1.1.1.0");
        putchar('\n');
        printf("Hostname: ");
        prsnmpstr(".1.3.6.1.2.1.1.5.0");
        printf("  Location: ");
        prsnmpstr(".1.3.6.1.2.1.1.6.0");
        putchar('\n');

    }

    memmove(tmp_oid, ifname_oid, sizeof(ifname_oid));
    tmp_oid_len=sizeof(ifname_oid);
    next=1;
    while(next) {
        pdu=snmp_pdu_create(SNMP_MSG_GETNEXT);
        snmp_add_null_var(pdu, tmp_oid, tmp_oid_len/sizeof(oid)); 
        stat=snmp_synch_response(ses, pdu, &resp);
        if(stat == STAT_SUCCESS && resp->errstat == SNMP_ERR_NOERROR) {
            if(memcmp(ifname_oid, resp->variables->name, sizeof(ifname_oid)) == 0) {
                tmp=malloc((resp->variables->val_len+1) * sizeof(char));
                memcpy(tmp, resp->variables->val.string, resp->variables->val_len);
                tmp[resp->variables->val_len]=0;
                if(ifname) {
                   if(strcasecmp(ifname, tmp)==0) {
                        //printf("%s found at index %lu:\n", tmp, resp->variables->name[resp->variables->name_length-1]);
                        return resp->variables->name[resp->variables->name_length-1];
                    } 
                }
                else {
                    printf("%3lu : \"%s\" [%s/%s]", 
                        resp->variables->name[resp->variables->name_length-1], 
                        tmp, 
                        ifstat[ifstatus(OID_ADM, resp->variables->name[resp->variables->name_length-1])],
                        ifstat[ifstatus(OID_OPR, resp->variables->name[resp->variables->name_length-1])]
                    );
                    if(extended) {
                        prifalias(resp->variables->name[resp->variables->name_length-1]);
                        snprintf(ifalias, sizeof(ifalias), "%s.%lu", "1.3.6.1.2.1.31.1.1.1.18.0", resp->variables->name[resp->variables->name_length-1]);
                        prsnmpstr(ifalias);
                    }
                    putchar('\n');
                }
                memmove((char *)tmp_oid, (char *)resp->variables->name, resp->variables->name_length * sizeof(oid));
                tmp_oid_len=resp->variables->name_length * sizeof(oid);
                free(tmp);
                if(resp) snmp_free_pdu(resp);
            }
            else 
                next=0;
        }
        else
            perr(resp);
    }
    if(resp) 
        snmp_free_pdu(resp);
    if(ifname) {
        fprintf(stderr, "Unable to find \"%s\"! Use 'list' to display all interfaces.\n", ifname);
        if(strcmp(gargv[optind+2], ifname)) {
            fprintf(stderr, "Did \"%s\" unnecessarily expand from \"%s\"? Prefix with @:\n%s %s %s @%s\n",
            ifname, gargv[optind+2], gargv[0], gargv[optind], gargv[optind+1], gargv[optind+2]);
        }
        snmp_close(ses);
        SOCK_CLEANUP;
        exit(1);
    }
    return 0;
}
Ejemplo n.º 2
0
SSPEC_RESOURCETABLE_END


void main()
{
	auto int if_status;
	auto unsigned long t;
	auto char buffer[100];

	MY_CTS_BIT_SETUP	// set up the CTS handshake input
	MY_RTS_BIT_SETUP	// set up the RTS handshake output

	sock_init();

	//configure PPP for dialing in to ISP and bring it up
	ifconfig(MY_PPP_INTERFACE,
				IFS_PPP_INIT,
				IFS_PPP_SPEED, DIALUP_SPEED,
				IFS_PPP_RTSPIN, MY_RTS_PORT, &MY_RTS_PORT_SHADOW, MY_RTS_BIT,
				IFS_PPP_CTSPIN, MY_CTS_PORT, MY_CTS_BIT,
				IFS_PPP_FLOWCONTROL, DIALUP_FLOWCONTROL,
				IFS_PPP_SENDEXPECT, DIALUP_SENDEXPECT,
				IFS_PPP_HANGUP, "ATH #ok",
				IFS_PPP_MODEMESCAPE, 1,
				IFS_PPP_ACCEPTIP, 1,
				IFS_PPP_ACCEPTDNS, 1,
				IFS_PPP_REMOTEAUTH, DIALUP_NAME, DIALUP_PASSWORD,
				IFS_UP,
				IFS_END);

	while (IF_COMING_UP == (if_status = ifpending(MY_PPP_INTERFACE)) ||
	       IF_COMING_DOWN == if_status)
	{
		tcp_tick(NULL);
	}
	if(ifstatus(MY_PPP_INTERFACE))
	{
		printf("PPP established\n");
	}
	else
	{
		printf("PPP failed\n");
	}
	printf("IP address is %s\n", inet_ntoa( buffer, gethostid()));

	http_init();

/*
 *  tcp_reserveport causes the web server to maintain pending requests
 * whenever there is not a listen socket available
 *
 */

   tcp_reserveport(80);

/*
 *  http_handler needs to be called to handle the active http servers.
 */

   while (1) {
      http_handler();
   }

	ifconfig(MY_PPP_INTERFACE, IFS_DOWN, IFS_END);

	//wait while PPP terminates
	tcp_tick(NULL);
	while (IF_COMING_DOWN == (if_status = ifpending(MY_PPP_INTERFACE)) ||
	       IF_COMING_UP == if_status)
	{
		tcp_tick(NULL);
	}

}
Ejemplo n.º 3
0
int main()
{
	auto int if_status;

	printf("Multiple Interface Echo Test\n");
	usage();
	printf("\nPress any key to proceed.\n");
	while (!kbhit());
	getchar();
	printf("Initializing TCP/IP stack...\n");

	for (i = 0; i < IF_MAX; i++)
		ifconfig(i, IFG_IPADDR, oldip+i,
#ifdef USE_IF_CALLBACK
					IFS_IF_CALLBACK, updowncb,
#endif
					IFS_END);

	sock_init();

	// Wait for the interface to come up
		while (IF_COMING_UP == (if_status = ifpending(IF_DEFAULT)) ||
		       IF_COMING_DOWN == if_status)
	{
		tcp_tick(NULL);
	}

	printf("...done.\n");

	memset(state, 0, sizeof(state));
	socknum = -1;
	seq = 1;
	ping_ip = 0;
	act_ip = 0;
	asock = s + (NUM_SOCKS - 1);
	astate = state + (NUM_SOCKS - 1);
	*astate = STATE_DONT_USE;

	if(!udp_extopen(&usock, IF_ANY, ECHO_PORT, -1L, 0, echo_handler, 0, 0)) {
		printf("udp_extopen failed!\n");
		exit(0);
	}


	for (;;) {
		socknum++;
		if (socknum == NUM_SOCKS)
			socknum = 0;

		tcp_tick(NULL);

		if (state[socknum] > STATE_CLOSED &&
		    !sock_alive(s+socknum)) {
		   if (socknum == NUM_LISTEN_SOCKS) {
		   	printf("Active socket closed\n");
				state[socknum] = STATE_DONT_USE;
		   }
		   else {
				printf("Socket %d closed\n", socknum);
				state[socknum] = STATE_CLOSED;
			}
			sock_perror(s+socknum, NULL);
		}

		for (i = 0; i < IF_MAX; i++) {
			ifconfig(i, IFG_IPADDR, &ip, IFS_END);
			if (oldip[i] != ip) {
				printf("IPaddr on i/f %d changed from %08lx to %08lx\n",
					i, oldip[i], ip);
				oldip[i] = ip;
				ifconfig(i, IFS_ICMP_CONFIG_RESET, IFS_END);
			}
		}

		if (kbhit()) {
			kb = getchar();
			if (kb == '?')
				usage();
			else if (kb == 'i')
				ip_print_ifs();
			else if (kb == 'r')
				router_printall();
			else if (kb == 'c')
				arpcache_printall();
			else if (kb == 'p') {
				printf("Press an interface number [TCP/IP now blocked]\n");
				while (!kbhit());
				kb = getchar();
				if (isdigit(kb)) {
					ifconfig(kb - '0', IFG_ROUTER_DEFAULT, &ping_ip, IFS_END);
					if (ping_ip) {
						printf("Pinging router %08lX...\n", ping_ip);
						_send_ping(ping_ip, seq++, 1, IPTOS_DEFAULT, &ping_id);
					}
					else
						printf("No router for interface %d\n", kb - '0');
				}
				else
					printf("No interface selected.\n");
			}
			else if (kb == 'a') {
				if (act_ip && *astate != STATE_DONT_USE) {
					printf("Closing active connection to %s...\n", inet_ntoa(buf, act_ip));
					sock_close(asock);
					while (tcp_tick(asock));
				}
				*astate = STATE_DONT_USE;
				printf("Enter a host name or IP address [TCP/IP now blocked]\n");
				gets(buf);
				printf("Resolving...\n");
				act_ip = resolve(buf);
				if (act_ip) {
					printf("Enter a port number to connect to (0-65535)\n");
					gets(buf);
					aport = (word)atol(buf);
					printf("Opening to %s:%u...\n", inet_ntoa(buf, act_ip), aport);
					*astate = STATE_ACTOPEN;
				}
				else
					printf("Could not resolve %s to IP address.\n", buf);

			}
			else if (isdigit(kb)) {
				// Toggle interface status
				kb -= '0';
	  			if (!(1u<<kb & IF_SET)) {
            	printf("Not a valid interface\n");
					continue;
            }
				if (ifstatus(kb)) {
					printf("Bringing interface %d down...\n", kb);
					ifconfig(kb, IFS_DOWN, IFS_END);
				}
				else {
					printf("Bringing interface %d up...\n", kb);
					ifconfig(kb, IFS_UP, IFS_END);
				}
			}
		}

		if (ping_ip) {
			if (_chk_ping(ping_ip , &ping_id) != 0xffffffffL) {
				printf("Got ping response from %08lX\n", ping_ip);
				ping_ip = 0;
			}
		}

		switch (state[socknum]) {
			case STATE_CLOSED:
				if (!tcp_extlisten(s + socknum, IF_ANY, ECHO_PORT, 0, 0, t_handler, 0, 0, 0)) {
					printf("Listen failed - socket %d\n", socknum);
					state[socknum] = STATE_DONT_USE;
					break;
				}
				printf("Listening on socket %d...\n", socknum);
				state[socknum] = STATE_LISTEN;
				break;
			case STATE_ACTOPEN:
				if (!tcp_extopen(s + socknum, IF_ANY, 0, act_ip, aport, t_handler, 0, 0)) {
					printf("Active open failed\n");
					state[socknum] = STATE_DONT_USE;
					break;
				}
				state[socknum] = STATE_LISTEN;
				break;
			case STATE_LISTEN:
				if (sock_established(s + socknum)) {
					printf("Connection %d estab.\n", socknum);
					state[socknum] = STATE_ESTAB;
				}
				break;
			case STATE_ESTAB:
				if ((rb = sock_fastread(s + socknum, buf, sizeof(buf))) > 0) {
					sock_fastwrite(s + socknum, buf, rb);
					if (!strncmp(buf, "quit", 4)) {
						printf("Peer on socket %d requests close\n", socknum);
						sock_close(s + socknum);
						state[socknum] = STATE_CLOSING;
					}
				}
				else if (rb < 0) {
					printf("Connection %d closed by peer.\n", socknum);
					sock_close(s + socknum);
					state[socknum] = STATE_CLOSING;
				}
				break;
			default:
				break;
		}
	}
}