?? hello_plugs.c
字號:
// ex:set tabstop=4:
// +--------------------
// | Includes
// |
#include "excalibur.h"
#include "hello_plugs_menu.h"
#include "plugs.h"
#if __nios16__
#define SMALL 1
#endif
// Set __adapter__ to the current design...
#include "plugs_example_designs.h"
// +--------------------
// | Types
enum
{
k_menu_settings = 1,
k_menu_actions, // ping & nslookup
};
// +---------------------------------
// | Global State
// |
// | (If you must have globals, put them
// | in one struct for tidiness! And name
// | the one instance "g".)
typedef struct
{
ns_plugs_persistent_network_settings pns;
int arp_scan_replies[256]; // matrix of who has responded
int sniff_pause; // if set, prints dots instead of packets
int sniff_count; // total packets sniffed in session
} ns_globals;
static ns_globals g; // = {0};
// +----------------------------------------
// | Local Prototypes
// |
static int r_reset_settings(int x);
static int r_reinitialize(int x);
static int show_abbreviated_settings(int x);
// +----------------------------------------
// | Initialization and Utilities
// |
// +----------------------------------------
// | r_get_settings_from_flash
// |
// | If there's flash memory, get the settings from
// | the conventional location in flash. If the
// | index looks crazy, or there's no flash, then
// | reset the settings to our default values.
// |
void r_get_settings_from_flash(void)
{
int i;
#ifdef nasys_main_flash
g.pns = *nasys_plugs_persistent_network_settings;
if(g.pns.signature != 0x00005afe)
r_reset_settings(0);
#else
r_reset_settings(0);
#endif
}
int r_save_settings(int x)
{
#ifdef nasys_main_flash
printf("erasing flash\n");
nr_flash_erase_sector((void *)-1,(void *)nasys_plugs_persistent_network_settings);
printf("writing flash at 0x%08x\n",nasys_plugs_persistent_network_settings);
g.pns.signature = 0x00005afe;
nr_flash_write_buffer((void *)-1,(void *)nasys_plugs_persistent_network_settings,(void *)&g.pns,sizeof(g.pns));
#endif
}
int r_set_settings(void)
{
int result;
nr_plugs_terminate();
result = nr_plugs_initialize(0, &g.pns.settings,
__adapter__,
__adapter_irq__,
__adapter_struct_addr__);
if(result < 0)
goto go_home;
// after initializing, pull the settings back into our globals
result = nr_plugs_get_settings(0,&g.pns.settings);
if(result)
goto go_home;
go_home:
if(result < 0)
nr_plugs_print_error_message("[r_set_settings]",result);
return result;
}
void r_initialize(void)
{
nr_zerorange((char *)(&g),sizeof(g));
r_get_settings_from_flash();
r_set_settings(); // initializes plugs, too
}
// ++=================================================
// || Network Settings Menu Procs
// ||
// Routines
static int show_ip_address(ns_plugs_network_settings *ns)
{
printf(" ip address: ");
nr_plugs_print_ip_address_decimal(ns->ip_address);
if(ns->flags & ne_plugs_flag_dhcp)
printf(" (obtained via dhcp)");
printf("\n");
}
static int show_settings(int x)
{
#if SMALL
return show_abbreviated_settings(x);
#else
ns_plugs_network_settings ns;
nr_plugs_get_settings(0,&ns); // show the settings plugs library reports, regardless what WE set
printf("\n Network Settings \n\n");
printf(" ethernet address: ");
nr_plugs_print_ethernet_address(&ns.ethernet_address);
printf("\n");
show_ip_address(&ns);
printf(" nameserver ip address: ");
nr_plugs_print_ip_address_decimal(ns.nameserver_ip_address);
printf("\n");
printf(" subnet mask: ");
nr_plugs_print_ip_address_decimal(ns.subnet_mask);
printf("\n");
printf(" gateway ip address: ");
nr_plugs_print_ip_address_decimal(ns.gateway_ip_address);
printf("\n");
printf("\n");
#endif SMALL
}
static int show_abbreviated_settings(int x)
{
ns_plugs_network_settings ns;
nr_plugs_get_settings(0,&ns); // show the settings plugs library reports, regardless what WE set
show_ip_address(&ns);
printf("\n");
}
static int input_ip_address(char *prompt, net_32 *ip_inout)
{
int result;
char s[64];
nr_plugs_ip_to_string(*ip_inout,s);
result = r_input_string(prompt,0,s);
nr_plugs_string_to_ip(s,ip_inout);
return result;
}
static int r_input_long(char *prompt, long *x_inout)
{
int result;
char s[64];
nr_plugs_long_to_string(*x_inout,s);
result = r_input_string(prompt,0,s);
*x_inout = nr_plugs_string_to_long(s);
return result;
}
static int r_edit_settings(int x)
{
char s[64];
ns_plugs_network_settings *settings;
int result;
settings = &g.pns.settings;
nr_plugs_ethernet_to_string(&settings->ethernet_address,s);
result = r_input_string(" ethernet address",0,s);
if(result < 0)
goto go_home;
nr_plugs_string_to_ethernet(s,&settings->ethernet_address);
// |
// | use dhcp [y/n]
// |
{
int dhcp_flag = settings->flags & ne_plugs_flag_dhcp;
s[0] = dhcp_flag ? 'Y' : 'N';
s[1] = 0;
ask_about_dhcp:
result = r_input_string(" use dhcp",0,s);
if(result < 0)
goto go_home;
if(s[0] == 'Y' || s[0] == 'y')
settings->flags |= ne_plugs_flag_dhcp;
else if(s[0] == 'N' || s[0] == 'n')
settings->flags &= ~ne_plugs_flag_dhcp;
else
goto ask_about_dhcp;
}
// |
// | no dhcp? do it the hard way
// |
if(!(settings->flags & ne_plugs_flag_dhcp))
{
result = input_ip_address(" ip address",&settings->ip_address);
if(result < 0)
goto go_home;
result = input_ip_address(" nameserver ip address",&settings->nameserver_ip_address);
if(result < 0)
goto go_home;
result = input_ip_address(" subnet mask",&settings->subnet_mask);
if(result < 0)
goto go_home;
result = input_ip_address(" gateway ip address",&settings->gateway_ip_address);
if(result < 0)
goto go_home;
}
printf("\n");
go_home:
if(result >= 0)
r_set_settings();
return result;
}
static int r_reset_settings(int x)
{
ns_plugs_network_settings settings = {0,0,0,0,0,0,0};
settings.ethernet_address.u32=0x11121314;
settings.ethernet_address.l16=0x1516 + nr_timer_milliseconds();
settings.nameserver_ip_address = nm_ip2n(10,0,0,1);
settings.subnet_mask = nm_ip2n(255,255,255,0);
settings.gateway_ip_address = nm_ip2n(10,0,0,255);
// dhcp on first setting. It's good.
settings.flags = ne_plugs_flag_dhcp;
settings.ip_address = nm_ip2n(10,0,0,51);
g.pns.settings = settings;
r_reinitialize(0);
return 0;
}
static int r_reinitialize(int x)
{
printf("Reinitializing...\n");
r_set_settings();
return 0;
}
// ++=================================================
// || Network Action Menu Items
// ||
#if !SMALL
// +-----------------------------------------------------
// | Ping Proc and Pinger
// Utility to do unaligned long-int read from packet
unsigned long get_bytes_32(void *address)
{
unsigned char *w = address;
return ((long)w[0] << 24) + ((long)w[1] << 16) + ((long)w[2] << 8) + w[3];
}
// Utility to do unaligned long-int write to packet
void put_bytes_32(void *address,unsigned long x)
{
unsigned char *w = address;
w[0] = x >> 24;
w[1] = x >> 16;
w[2] = x >> 8;
w[3] = x;
}
// +----------------------------------
// | ping_proc -- gets called for icmp packets
// | from the ip address being pinged.
int ping_proc(int plug_handle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length)
{
ns_plugs_icmp_packet *icmpp = payload;
ns_plugs_ip_packet *ipp = p[ne_plugs_ip].header;
long time;
// |
// | We don't expect them to be pinging us
// | back or anything, and this plug will
// | filter for only the remote host
// | we're pinging, but if they do send
// | a ping-request, we'll print a message
// | mentioning it.
// |
// | We mostly expect the first case: ping reply to our ping.
// |
if(icmpp->type == ne_plugs_icmp_ping_reply)
{
time = get_bytes_32(icmpp->payload);
time = nr_timer_milliseconds() - time;
printf(" [ping_proc] got ping reply from ");
nr_plugs_print_ip_address(ipp->source_ip_address);
printf(" (%d msec)\n",time);
}
else if(icmpp->type == ne_plugs_icmp_ping_request)
{
printf("ping_proc: got ping request from ");
nr_plugs_print_ip_address(ipp->source_ip_address);
printf("!\n");
}
else
printf("ping_proc: got icmp type %d\n",icmpp->type);
}
#define k_ping_count 4
int r_ping_host(int x) // both PING menu items come here, debug with x=1.
{
int ping_plug;
static char *default_host = "www.altera.com";
char host[64];
char *hostp;
net_32 ip;
int result;
char data[64];
ns_plugs_icmp_packet *ping_request = (ns_plugs_icmp_packet *)data;
// overlay it on data, for nonzero payload
int len;
int i;
host[0] = 0;
// x = 1 means have debuggin turned on
x = x ? (ne_plugs_flag_debug_rx | ne_plugs_flag_debug_tx) : 0;
while(1)
{
printf("\n\n");
result = r_input_string(" Internet host to ping by name (<ESC> to finish)",
default_host,host);
if(result < 0)
break;
// Allocate the plug of icmp type
result = nr_plugs_create(&ping_plug,
ne_plugs_icmp,
0, // port number, does not matter here
ping_proc,
0,
x); // flags set depending on menu choice
if(result)
{
printf(" Could not create ping plugs.\n");
nr_plugs_print_error_message("[ping test]",result);
continue;
}
// |
// | First, see if, perchance, it was a
// | dot-separated numerical string
// |
if(host[0] >= '0' && host[0] <= '9')
{
nr_plugs_string_to_ip(host,&ip);
ip = nr_n2h32(ip);
hostp = 0;
}
else
{
ip = 0;
hostp = host;
}
// Connect to remote host by name
result = nr_plugs_connect(ping_plug,hostp,ip,0); // talk to remote host
if(result)
{
printf(" Lookup failure on %s.\n",host);
nr_plugs_print_error_message("[ping test]",result);
// Delete the plug, since we can't connect, and we'll allocate again
nr_plugs_destroy(ping_plug);
continue;
}
printf("Connected!\n\n");
// | Construct the ping request
ping_request->type = ne_plugs_icmp_ping_request;
ping_request->code = 0;
ping_request->payload[0] = 0;
ping_request->payload[1] = 3;
ping_request->payload[2] = 4;
ping_request->payload[3] = 8;
// "length" is packet + data
len = sizeof(ns_plugs_icmp_packet) + 4;
for(i = 0; i < k_ping_count; i++)
{
printf("Sending ping %d of %d.\n",i+1,k_ping_count);
// | Pass the time as payload
put_bytes_32(ping_request->payload,nr_timer_milliseconds());
nr_plugs_send(ping_plug,(void *)data,len,0);
// Wait 1 second, pumping the ether all the time
{
long t0 = nr_timer_milliseconds();
while(nr_timer_milliseconds() - t0 < 1000)
nr_plugs_idle();
}
}
// All done pinging, delete the plug
result = nr_plugs_destroy(ping_plug);
}
}
// +--------------------------------------------------
// | DNS lookup routine
// |
int r_dns_lookup(int x)
{
static char *default_host = "www.altera.com";
char host[64];
net_32 ip;
int result;
host[0] = 0;
printf("\nEnter host names to look up, <ESC> when finished.\n\n");
while(1)
{
printf("\n\n");
result = r_input_string(" Internet host to look up by name",default_host,host);
if(result < 0)
break;
result = nr_plugs_name_to_ip(host,&ip);
if(result)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -