📄 hello_plugs.c
字号:
{
printf("Lookup failed!\n");
nr_plugs_print_error_message("[dns lookup test]",result);
}
else
{
printf("The IP address of %s is ",host);
nr_plugs_print_ip_address_decimal(ip);
printf(".\n");
}
}
return 0;
}
#endif SMALL
// +--------------------------------------------------
// | Arp Scan plug handler and main routine
// |
int r_arp_plug_proc(int arp_plug,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length)
{
ns_plugs_arp_packet *ap = payload; // get payload in proper type
if(ap->op == nm_n2h16(ne_plugs_arp_reply)) // is it a reply we want?
{
// is it a reply within our own subnet (where we care?)
if(((ap->sender_ip_address ^ g.pns.settings.ip_address) & g.pns.settings.subnet_mask) == 0)
{
int x;
x = (ap->sender_ip_address >> 24) & 0xff;
printf("arp_proc: reply from ");
nr_plugs_print_ip_address(ap->sender_ip_address);
printf("\n");
g.arp_scan_replies[x] = x + 0x1000;
}
}
}
int r_arp_scan(int x)
{
int arp_plug;
long i;
ns_plugs_arp_packet a;
int result;
ns_plugs_network_settings settings;
net_32 our_ip_address;
net_32 base_ip_address;
printf("\n\n About to send ARP requests\nfor 256 addresses in subnet.\n");
// Get our current settings for ethernet and base ip address to scan
result = nr_plugs_get_settings(0,&settings);
if(result < 0)
goto go_home;
// Create the Plug for the arp protocol
result = nr_plugs_create(&arp_plug,ne_plugs_arp,0,r_arp_plug_proc,0,ne_plugs_flag_ethernet_broadcast);
if(result)
goto go_home;
// clear the scan reply table
for(i = 0; i < 256; i++)
g.arp_scan_replies[i] = 0;
// set up an arp request to use over & over
a.hardware_type = nm_h2n16(1);
a.protocol_type = nm_h2n16(0x0800);
a.hardware_size = 6;
a.protocol_size = 4;
a.op = nm_h2n16(ne_plugs_arp_request);
a.sender_ethernet_address = settings.ethernet_address;
a.sender_ip_address = settings.ip_address;
a.target_ethernet_address.u32 = 0;
a.target_ethernet_address.l16 = 0;
// Send out all the requests
for(i = 0; i < 256; i++)
{
a.target_ip_address =
(settings.ip_address & nm_h2n32(0xFFFFff00)) + nm_h2n32(i);
nr_plugs_send(arp_plug,&a,sizeof(a),0);
nr_delay(4);
nr_plugs_idle();
}
// Wait some seconds
{
long t0 = nr_timer_milliseconds();
while(nr_timer_milliseconds() - t0 < 2500)
nr_plugs_idle(); // (in case we're in polled mode operation)
}
// ----------------------------------------
// Print out the scan result, in a sort of pretty way
printf("\n");
for(i = 0; i < 256; i++)
{
if(g.arp_scan_replies[i])
printf("%3d ",i);
else
printf(" . ");
if(i % 16 == 15)
printf("\n");
}
printf("\n");
// close down the plug
nr_plugs_destroy(arp_plug);
go_home:
if(result < 0)
nr_plugs_print_error_message("[arp scan test]",result);
return 0;
}
// +--------------------------------------------------
// | Packet Sniffer handler and main routine
// |
int r_eth_plug_proc(int plugHandle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length)
{
g.sniff_count++;
if(g.sniff_pause)
printf(".");
else
{
printf("------------------\n");
printf("Packet Sniffer\n(Press <ESC> to finish sniffing, <SPACE> to pause)\n\n");
// Use convenient packet-printing utility
nr_plugs_print_ethernet_packet(payload,payload_length," [sniff] ");
}
return 0;
}
int r_sniffer(int x)
{
int eth_plug;
int i;
ns_plugs_arp_packet a;
int c;
printf("\n\n About to begin displaying all\nethernet packets received.\n");
// Create the Plug for the eth protocol
nr_plugs_create(ð_plug,
ne_plugs_ethernet,
0,
r_eth_plug_proc,
0,
ne_plugs_flag_ethernet_all); // flag says, "anything the interface gets"
// Wait for an escape key, and give the
// plugs library lots of time to do its stuff.
g.sniff_pause = 0;
g.sniff_count = 0;
while((c = nr_uart_rxchar(0)) != 27)
{
if(c == ' ')
g.sniff_pause = !g.sniff_pause;
nr_plugs_idle();
}
nr_plugs_destroy(eth_plug);
printf("\nSniffed %d packets.\n\n",g.sniff_count);
}
#if !SMALL
// +----------------------------------------------------
// | TCP Telnet routines
// |
typedef struct
{
int connected; // 0:waiting, 1:connected, 2:disconnected
net_32 remote_ip_address;
net_16 remote_port;
long bytes_sent;
long bytes_received;
} s_telnet_status;
int tcp_listen_proc(int plug_handle,
void *context,
host_32 remote_ip_address,
host_16 remote_port)
{
volatile s_telnet_status *status = context;
status->connected = 1;
status->remote_ip_address = remote_ip_address;
status->remote_port = remote_port;
status->bytes_sent = 0;
status->bytes_received = 0;
printf("[tcp_listen_proc] Accepted connection from ");
nr_plugs_print_ip_address(nr_h2n32(remote_ip_address));
printf(" port %d\n",remote_port);
return 0;
}
int tcp_proc(int plug_handle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length)
{
volatile s_telnet_status *status = context;
int i;
if(payload)
{
unsigned char *s = payload;
status->bytes_received += payload_length;
for(i = 0; i < payload_length; i++)
printf("%c",s[i]);
{
char reply[100];
int reply_length = 0;
// |
// | Handle funny telnet requests
// |
for(i = 0; i < payload_length; i++)
{
if(s[i] == 255)
{
i++;
if(s[i] == 253 || s[i] == 254)
{
reply[reply_length++] = 255;
reply[reply_length++] = s[i++] - 2;
reply[reply_length++] = s[i++];
}
}
}
if(reply_length)
nr_plugs_send(plug_handle,reply,reply_length,0);
}
}
else
{
status->connected = 2; // let them know we are disconnected
}
return 0;
}
// +---------------------
// | Once a session is established, either by
// | listening or connecting, we can come
// | here and trade keystrokes until
// | either they disconnect or we get an
// | ESC key.
// |
int r_telnet_session(int tcp_plug, volatile s_telnet_status *status)
{
int still_running;
int result = 0;
still_running = 1;
while(still_running)
{
int c;
if((c = nr_uart_rxchar(0)) > 0)
{
if(c == 27)
still_running = 0;
else
{
nr_plugs_send(tcp_plug,&c,1,0);
status->bytes_sent++;
}
}
if(status->connected == 2)
still_running = 0;
nr_plugs_idle();
}
result = nr_plugs_connect(tcp_plug,0,0,0);
printf("\nConnection closed.\n");
printf("%d bytes sent, %d bytes received.\n\n",status->bytes_sent,status->bytes_received);
go_home:
return result;
}
int r_telnet_accept(int x)
{
long local_port = 23;
int result;
int tcp_plug = 0;
int still_running;
s_telnet_status status = {0,0,0,0,0};
result = r_input_long("Port to listen on",&local_port);
if(result)
goto go_home;
result = nr_plugs_create
(
&tcp_plug,
ne_plugs_tcp,
local_port,
tcp_proc,
&status,
0
);
result = nr_plugs_listen
(
tcp_plug,
tcp_listen_proc,
&status
);
still_running = 1;
// |
// | Wait for connection to be established, then
// | go to the session routine. (Allow ESC to
// | bail out.)
// |
while(status.connected == 0)
{
nr_plugs_idle();
if(nr_uart_rxchar(0) == 27)
{
nr_plugs_idle();
printf("\nAborted.\n");
goto go_home;
}
}
result = r_telnet_session(tcp_plug,&status);
go_home:
if(tcp_plug)
result = nr_plugs_destroy(tcp_plug);
return 0;
}
int r_telnet_connect(int x)
{
static char *default_host = "www.altera.com";
char host[64];
long remote_port = 23;
int result;
int tcp_plug = 0;
int still_running;
s_telnet_status status = {0,0,0,0,0};
host[0] = 0;
result = r_input_string(" Internet host to connect to",default_host,host);
if(result)
goto go_home;
result = r_input_long(" Port to connect to",&remote_port);
if(result)
goto go_home;
result = nr_plugs_create
(
&tcp_plug,
ne_plugs_tcp,
0, // let library choose a port number
tcp_proc,
&status,
0
);
result = nr_plugs_connect
(
tcp_plug,
host,
0,
remote_port
);
if(result)
{
printf("Error connecting to host %s.\n",host);
nr_plugs_print_error_message("[tcp connect test]",result);
goto go_home;
}
result = r_telnet_session(tcp_plug,&status);
go_home:
if(tcp_plug)
result = nr_plugs_destroy(tcp_plug);
return 0;
}
// +---------------------
// | Blink the Link LED for a bit,
// | then return.
// |
int r_blink_link_led(int x)
{
int i;
#ifdef na_lan91c111_0
printf ("\n Link must be already active for the LAN91C111.\n");
#endif
printf("\n\n Blinking the LED...\n");
for(i = 0; i < 10; i++)
{
nr_plugs_set_mac_led(0,0);
nr_plugs_idle();
nr_delay(100);
nr_plugs_set_mac_led(0,1);
nr_plugs_idle();
nr_delay(100);
}
nr_plugs_set_mac_led(0,-1);
printf("\n\n Done blinking the LED.\n");
}
// +---------------------
// | Guess what the next routine does
// |
#endif SMALL
int r_exit_to_monitor(int x)
{
nr_plugs_terminate();
// Disable timer1, in case nr_timer_milliseconds() got used
#ifdef na_timer1
na_timer1->np_timercontrol = np_timercontrol_stop_mask;
na_timer1->np_timerstatus = 0; // clear any last irq
#endif
exit(0);
return 0; // (never happens)
}
int main(void)
{
setbuf(stdout,0);
// Call initialization routines
r_initialize();
// Set up the menus, and run them
r_menu_set_idler((r_menu_proc)nr_plugs_idle);
// Settings menu
r_menu_add_item(k_menu_settings,"Network Settings",show_settings,0); // 1st item is title
r_menu_add_item(k_menu_settings,"Reset All Settings, Attempt DHCP",r_reset_settings,0);
r_menu_add_item(k_menu_settings,"Reinitialize",r_reinitialize,0);
r_menu_add_item(k_menu_settings,"Enter New Settings",r_edit_settings,0);
r_menu_add_item(k_menu_settings,"Save To Flash",r_save_settings,0);
// Remote menu
r_menu_add_item(k_menu_actions,"Network Actions",show_abbreviated_settings,0); // 1st item is title
#if !SMALL
r_menu_add_item(k_menu_actions,"Ping Remote Host",r_ping_host,0);
r_menu_add_item(k_menu_actions,"Ping Remote Host (Debug On)",r_ping_host,1);
r_menu_add_item(k_menu_actions,"Look Up IP Address",r_dns_lookup,0);
#endif
r_menu_add_item(k_menu_actions,"ARP Scan",r_arp_scan,0);
r_menu_add_item(k_menu_actions,"Monitor All Traffic (Sniffer)",r_sniffer,0);
#if !SMALL
r_menu_add_item(k_menu_actions,"Accept Telnet Connection",r_telnet_accept,0);
r_menu_add_item(k_menu_actions,"Open Telnet Connection",r_telnet_connect,0);
r_menu_add_item(k_menu_actions,"Blink Link LED",r_blink_link_led,0);
#endif
// Main menu
r_menu_add_item(0,"Main Menu",0,0);
r_menu_add_link(0,k_menu_settings);
r_menu_add_link(0,k_menu_actions);
r_menu_add_item(0,"Exit to monitor",r_exit_to_monitor,0);
r_menu_run();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -