www.pudn.com > test11.rar > plugs.h, change:2008-07-31,size:15752b


// | file: plugs.h 
// | 
// | Types and routines for the 
// | the "plugs" embedded tcp/ip stack 
// | library. 
// | 
// | David Van Brink / 2001 / Altera Corporation 
// | 
 
#ifndef _plugs_ 
#define _plugs_ 
 
#include "excalibur.h" 
 
// ++==================================== 
// || 
// || CONFIGURATION 
// || The following constants can be 
// || modified to control the build. 
// || This can be used to tune footprint. 
// || Override them in your gcc options, 
// || or (more dangerously) modify them here. 
 
// Include code to allow debug printing? 
// Set to 0 for "no", and to 2 for "even more printing" 
#ifndef PLUGS_DEBUG 
#define PLUGS_DEBUG 1 
#endif 
 
// Maximum number of plugs (CANNOT be more than 32!) 
#ifndef PLUGS_PLUG_COUNT 
#define PLUGS_PLUG_COUNT 10 
#endif 
 
// Maximum number of adapters 
#ifndef PLUGS_ADAPTER_COUNT 
#define PLUGS_ADAPTER_COUNT 2 
#endif 
 
// DNS access built-in? 
#ifndef PLUGS_DNS 
#define PLUGS_DNS 1 
#endif 
 
// PING responder built-in? 
#ifndef PLUGS_PING 
#define PLUGS_PING 1 
#endif 
 
// TCP support? 
#ifndef PLUGS_TCP 
#define PLUGS_TCP 1 
#endif 
 
// IRQ support? 
#ifndef PLUGS_IRQ 
#define PLUGS_IRQ 1 
#endif 
 
// DHCP support? 
#ifndef PLUGS_DHCP 
#define PLUGS_DHCP 1 
#endif 
 
// +------------------------------------- 
// | To overlay C structures on raw received 
// | ethernet packets, we must ensure that 
// | there's no padding in the struct. 
// | (Aligning down to halfwords is fine, network 
// | structures are all 16-bit-word based.) 
// | For GCC, this is done as follows. 
 
#define nm_packed __attribute__ ((packed,aligned(2))) 
 
// +------------------------------------- 
// | Primitive network types 
// | Defined as structs so you cannot 
// | accidentally use them as ints 
// | (except for net_8). 
 
typedef unsigned char host_8;	// | host 8 
typedef unsigned short host_16;	// | host 16 
typedef unsigned long host_32;	// | host 32 
 
typedef unsigned char net_8;	// | network 8 
typedef unsigned short net_16;	// | network 16 
typedef unsigned long net_32;	// | network 32 
 
typedef struct 
	{ 
	net_32 u32;				// | upper 32 bits of 48 bit address 
	net_16 l16;				// | lower 16 bits of 48 bit address 
	} nm_packed net_48; 
 
typedef net_48 host_48; 
 
// +------------------------------------- 
// | Configuration block with various 
// | network info 
 
typedef struct 
	{ 
	net_48 ethernet_address; 
	short flags; 
	net_32 ip_address; 
	net_32 nameserver_ip_address; 
	net_32 subnet_mask; 
	net_32 gateway_ip_address; 
	} nm_packed ns_plugs_network_settings; 
 
// +------------------------------------- 
// | Structure and location of flash-based 
// | network settings in flash... 
// | If you pass 0 for network settings pointer, 
// | you get default index of existing settings. 
// | If you pass -1, -2, &c for 
// | the network settings pointer, initialize 
// | to that flash based value. 
// | 
 
typedef struct 
	{ 
	int signature; // must be 0x00005afe for network settings 
	ns_plugs_network_settings settings; 
	} ns_plugs_persistent_network_settings; 
 
#ifdef nasys_main_flash 
 #if __nios32__ 
    #if nasys_main_flash_size == 0x100000 // apex board 1meg flash 
	#define nasys_plugs_persistent_network_settings \ 
			((ns_plugs_persistent_network_settings *)(nasys_main_flash + 0x6000)) 
    #else // last 64k of 8meg stratix/cyclone flash... 
	#define nasys_plugs_persistent_network_settings \ 
			((ns_plugs_persistent_network_settings *)(nasys_main_flash + 0x7f0000)) 
    #endif 
 #else // nios 16, just use beginning of flash memory 
	#define nasys_plugs_persistent_network_settings \ 
			((ns_plugs_persistent_network_settings *)(nasys_main_flash)) 
 #endif 
#endif 
 
// +------------------------------------- 
// | Definitions of each supported packet type 
 
typedef struct 
	{ 
	void *header; 
	int length; 
	} ns_plugs_packet; 
 
typedef struct 
	{ 
	net_48 destination_address; 
	net_48 source_address; 
	net_16 type; 
	unsigned char payload[0];	// | generic payload 
	} nm_packed ns_plugs_ethernet_packet; 
 
typedef struct 
	{ 
	net_16 hardware_type;		// | 1 = ethernet 
	net_16 protocol_type; 
	net_8 hardware_size;		// | 6 for ethernet 
	net_8 protocol_size;		// | 4 for IP 
	net_16 op;			// | 1: req, 2: rep, 3: rarp req, 4: rarp rep 
	net_48 sender_ethernet_address; 
	net_32 sender_ip_address; 
	net_48 target_ethernet_address; 
	net_32 target_ip_address; 
	} nm_packed ns_plugs_arp_packet; 
 
typedef struct 
	{ 
	net_8 version_header_length;	// | 0x45 for no options 
	net_8 tos;					// | zero 
	net_16 length;				// | header + payload length 
	net_16 identifier;			// | bump for each packet 
	net_16 flags_fragment;		// | fragment miscellany 
	net_8 time_to_live;		// | send with 128, usually 
	net_8 protocol;			// | protocol of payload 
	net_16 header_checksum;		// | checksum of header only (!payload) 
	net_32 source_ip_address; 
	net_32 destination_ip_address; 
	unsigned char payload[0];	// | generic payload 
	} nm_packed ns_plugs_ip_packet; 
 
typedef struct 
	{ 
	net_8 type; 
	net_8 code; 
	net_16 checksum; 
	unsigned char payload[0]; 
	} nm_packed ns_plugs_icmp_packet;		// | (always built on ip) 
 
typedef struct 
	{ 
	net_16 source_port; 
	net_16 destination_port; 
	net_16 length;			// | header + payload length 
	net_16 checksum;			// | optional for udp -- 0=unused 
	unsigned char payload[0];	// | generic payload 
	} nm_packed ns_plugs_udp_packet;		// | (always built on ip) 
 
typedef struct 
	{ 
	net_16 source_port; 
	net_16 destination_port; 
	net_32 sequence_number; 
	net_32 acknowledgement_number; 
	net_16 header_length_flags; 
	net_16 window_size; 
	net_16 checksum; 
	net_16 urgent_pointer; 
	unsigned char payload[0]; 
	} nm_packed ns_plugs_tcp_packet; 
 
#define nm_ip2h(a,b,c,d) ( \ 
		(((unsigned long)(a)&0xff)<<24) \ 
		| (((unsigned long)(b)&0xff)<<16) \ 
		| (((unsigned long)(c)&0xff)<<8) \ 
		| (((unsigned long)(d)&0xff)<<0) ) 
 
#define nm_ip2n(a,b,c,d) ( \ 
		(((unsigned long)(a)&0xff)<<0) \ 
		| (((unsigned long)(b)&0xff)<<8) \ 
		| (((unsigned long)(c)&0xff)<<16) \ 
		| (((unsigned long)(d)&0xff)<<24) ) 
 
// +----------------------------------- 
// | Protocol numbers for plugs in general 
enum 
	{ 
	ne_plugs_ethernet = 1, 
	ne_plugs_arp, 
	ne_plugs_ip, 
	ne_plugs_icmp, 
	ne_plugs_udp, 
	ne_plugs_tcp_raw, 
	ne_plugs_tcp, 
	ne_plugs_last_protocol 
	}; 
 
// +----------------------------------- 
// | Protocol numbers that are part of packets 
enum 
	{ 
	// | 
	// | "type" field for ethernet packet 
	// | 
 
	ne_plugs_ethernet_arp = 0x0806, 
	ne_plugs_ethernet_ip = 0x0800, 
 
	// |  
	// | "op" field for arp packet 
	// | 
 
	ne_plugs_arp_request = 1, 
	ne_plugs_arp_reply = 2, 
 
	// | 
	// | "type" field for icmp packet 
	// | 
 
	ne_plugs_icmp_ping_request = 8, 
	ne_plugs_icmp_ping_reply = 0, 
 
	// | 
	// | "protocol" field for ip packet 
	// | 
 
	ne_plugs_ip_icmp = 1, 
	ne_plugs_ip_udp = 17, 
	ne_plugs_ip_tcp = 6, 
 
	// | 
	// | "port" field for udp packet 
	// | 
 
	ne_plugs_udp_dns = 53 
	}; 
 
// +----------------------------------- 
// | Flags found in tcp header 
 
enum 
	{ 
	ne_plugs_flag_tcp_fin = 1, 
	ne_plugs_flag_tcp_syn = 2, 
	ne_plugs_flag_tcp_rst = 4, 
	ne_plugs_flag_tcp_psh = 8, 
	ne_plugs_flag_tcp_ack = 16, 
	ne_plugs_flag_tcp_urg = 32, 
	ne_plugs_flag_tcp_mask = 63 
	}; 
 
// +----------------------------------- 
// | Flags for nr_plugs_send and ns_plugs_plug.flags 
// | Also, for nr_plugs_initialize. 
 
enum 
	{ 
    ne_plugs_flag_adapter_index_mask = 0x000f, // low four bits specify which adapter for plug 
    ne_plugs_flag_ethernet_broadcast = 0x0010, 
    ne_plugs_flag_ethernet_all       = 0x0020, 
    ne_plugs_flag_ip_all             = 0x0040, // sort of like promiscuous mode, for ip address 
    ne_plugs_flag_debug_rx           = 0x0100, // Show all received packets 
    ne_plugs_flag_debug_tx           = 0x0200, // Show all transmitted packets 
    ne_plugs_flag_tcp_manual_flow    = 0x0400, // Ask TCP sender to back-off. 
 
    ne_plugs_flag_add_adapter        = 0x1000  // (Append additional adapter) 
    }; 
 
// +--------------------------------- 
// | Flags for ns_plugs_network_settings.flags or nr_plugs_initialize 
#if PLUGS_DHCP 
enum 
    { 
    ne_plugs_flag_dhcp = 0x2000   // Use only the Ethernet address; rest from dhcp 
    }; 
#endif // PLUGS_DHCP 
 
// +--------------------------------- 
// | Error Return Values 
enum 
	{ 
	ne_plugs_error_first = -100, 
	ne_plugs_error, 
	ne_plugs_error_not_initialized, 
	ne_plugs_error_feature_disabled, 
	ne_plugs_error_too_many_plugs, 
	ne_plugs_error_unwanted_reply, 
	ne_plugs_error_dns_not_found, 
	ne_plugs_error_dns_timed_out, 
	ne_plugs_error_arp, 
	ne_plugs_error_arp_timed_out, 
	ne_plugs_error_tcp_connection_refused, 
	ne_plugs_error_tcp_timed_out, 
    ne_plugs_error_dhcp_failed 
	}; 
 
// +----------------------------------- 
// | Macros for flipping bytes between 
// | host and network byte ordering. 
 
 
#define nm_n2h16(_n16_) ((host_16)( ((_n16_) >> 8) + ((_n16_) << 8) )) 
#define nm_h2n16(_h16_) nm_n2h16(_h16_) 
 
#define nm_n2h32(_n32_) ((host_32)( \ 
		  (((_n32_) >> 24) & 0x000000ff) \ 
		| (((_n32_) >>  8) & 0x0000ff00) \ 
		| (((_n32_) <<  8) & 0x00ff0000) \ 
		| (((_n32_) << 24) & 0xff000000) \ 
		)) 
#define nm_h2n32(_h32_) nm_n2h32(_h32_) 
 
// +---------------------------------------------------------- 
// | Description of a network adapter 
// | 
// | The adapter description is a structure which 
// | contains pointers to a small handful of routines 
 
// | Every routine in the adapter is passed a pointer 
// | to a structure with some storage for its own 
// | use. If it's not big enough, the adapter may 
// | certainly call malloc() and put a pointer in it. 
// | 
// | (This is a new field introduced in the NEDK 2.0 
// | kit, which supports the lan91c111 part out of the 
// | box.) 
// | 
 
typedef struct 
	{ 
	long d0; 
	long d1; 
	long d2; 
	long d3; 
	} ns_plugs_adapter_storage; 
 
// | Routine that the mac adapter calls with a received packet, if any 
 
typedef int (*nr_plugs_adapter_dispatch_packet_proc) 
		( 
		void *raw_packet, 
		int raw_packet_length, 
		void *context 
		); 
 
typedef int (*nr_plugs_adapter_reset_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		ns_plugs_network_settings *s 
		); 
typedef int (*nr_plugs_adapter_set_led_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		int led_onoff 
		); 
typedef int (*nr_plugs_adapter_set_loopback_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		int loopback_onoff 
		); 
typedef int (*nr_plugs_adapter_check_for_events_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		nr_plugs_adapter_dispatch_packet_proc proc, 
		void *context 
		); 
typedef int (*nr_plugs_adapter_tx_frame_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		char *ethernet_frame, 
		int frame_length 
		); 
typedef int (*nr_plugs_adapter_dump_registers_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage 
		); 
typedef int (*nr_plugs_adapter_set_promiscuous_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		int promiscuous_onoff 
		); 
typedef int (*nr_plugs_adapter_set_irq_proc) 
		( 
		void *hardware_base_address, 
		ns_plugs_adapter_storage *adapter_storage, 
		int irq_onoff 
		); 
 
typedef struct 
	{ 
	nr_plugs_adapter_reset_proc reset_proc; 
	nr_plugs_adapter_set_led_proc set_led_proc; 
	nr_plugs_adapter_set_loopback_proc set_loopback_proc; 
	nr_plugs_adapter_check_for_events_proc check_for_events_proc; 
	nr_plugs_adapter_tx_frame_proc tx_frame_proc; 
	nr_plugs_adapter_dump_registers_proc dump_registers_proc; 
	nr_plugs_adapter_set_promiscuous_proc set_promiscuous_proc; 
	nr_plugs_adapter_set_irq_proc set_irq_proc; 
	char *adapter_name; 
	} ns_plugs_adapter_description; 
 
// +---------------------------------------------------------- 
// | A plug's callback proc gets the appropriate 
// | payload, as well as an array with all subprotocols. 
 
typedef int (*nr_plugs_receive_callback_proc) 
		( 
		int plug_handle, 
		void *context, 
		ns_plugs_packet *p, 
		void *payload, 
		int payload_length 
		); 
 
// +---------------------------------------------------------- 
// | A tcp plug's "listen" proc, where it may choose to 
// | accept or reject an incoming request 
 
typedef int (*nr_plugs_listen_callback_proc) 
		( 
		int plug_handle, 
		void *context, 
		host_32 remote_ip_address, 
		host_16 remote_port 
		); 
 
// +---------------------------------------------------------- 
// | High level public routines (called by your application) 
 
int nr_plugs_initialize 
		( 
		long flags, 
		ns_plugs_network_settings *network_settings, 
		void *adapter_base_address, 
		int adapter_irq, 
		ns_plugs_adapter_description *adapter_description 
		); 
 
int nr_plugs_terminate 
		( 
		void 
		); 
 
int nr_plugs_force_timeout 
        ( 
        void 
        ); 
 
int nr_plugs_get_settings 
		( 
		int adapter_index, 
		ns_plugs_network_settings *network_settings_out 
		); 
 
int nr_plugs_set_mac_loopback 
		( 
		int adapter_index, 
		int loopback_onoff 
		); 
 
int nr_plugs_set_mac_led 
		( 
		int adapter_index, 
		int led_onoff  // -1 means "default behavior" 
		); 
 
int nr_plugs_create 
		( 
		int *plug_handle_out, 
		int protocol, 
		host_16 port, 
		nr_plugs_receive_callback_proc callback, 
		void *callback_context, 
		long flags 
		); 
 
int nr_plugs_destroy 
		( 
		int plug_handle 
		); 
	 
int nr_plugs_send 
		( 
		int plug_handle, 
		void *data, 
		int data_length, 
		long flags 
		); 
 
int nr_plugs_tcp_ready_for_next 
		( 
                 int plug_handle, 
                 int window_size 
		); 
 
int nr_plugs_send_to 
		( 
		int plug_handle, 
		void *data, 
		int data_length, 
		long flags, 
		net_32 ip_address,		// | net order 
		net_16 port		// | net order 
		); 
 
int nr_plugs_connect 
		( 
		int plug_handle, 
		char *remote_name, 
		host_32 remote_ip_address, 
		host_16 remote_port 
		); 
 
unsigned int nr_plugs_get_tcp_remote_window_size  
               ( 
                int plug_handle 
                ); 
 
int nr_plugs_listen 
		( 
		int plug_handle, 
		nr_plugs_listen_callback_proc callback, 
		void *callback_context 
		); 
 
 
int nr_plugs_ip_to_ethernet 
		( 
		int adapter_index, 
		net_32 ip_address, 
		net_48 *ethernet_address_out, 
		long flags 
		); 
 
int nr_plugs_name_to_ip 
		( 
		char *remote_name, 
		net_32 *remote_ip_address_out 
		); 
 
int nr_plugs_idle(void);	// if not interrupts or timer, call this often 
 
int nr_plugs_get_interruptee_pc (void);  
 
int nr_plugs_interrupts_enabled (void); 
 
int nr_plugs_idle_exclusive (int); 
// +---------------------------------------------------------- 
// | Network/Host Byte Ordering Routines 
// | 
 
 
#define nr_h2n16(n) nr_n2h16((n)) 
#define nr_h2n32(n) nr_n2h32((n)) 
 
host_16 nr_n2h16(net_16 n); 
host_32 nr_n2h32(net_32 n); 
 
// +---------------------------------------------------------- 
// | Printing Routines for packets & types 
// | 
 
void nr_plugs_print_error_message(char *caption, int error); 
void nr_plugs_print_ip_address(net_32 ip_address); // prints hex.hex... 
void nr_plugs_print_ip_address_decimal(net_32 ip_address); // prints hex.hex... 
 
void nr_plugs_print_ethernet_address(net_48 *ethernet_address); // prints hex:hex... 
 
void nr_plugs_print_ethernet_packet 
	( 
	ns_plugs_ethernet_packet *p, 
	int length, 
	char *title 
	); 
 
void nr_plugs_print_icmp_packet 
	( 
	ns_plugs_icmp_packet *p, 
	int length, 
	char *title 
	); 
 
#undef nm_packed 
 
#endif // _plugs_ 
 
// end of file