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

📄 plugs.h

📁 altera epxa1的例子程序
💻 H
字号:
// | 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_

//MR #include "nios.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 6
#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

// +-------------------------------------
// | 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.

//MR
//#define arm_packed
//#define nm_packed __attribute__ ((packed,aligned(2)))
// for armcc
#define arm_packed __packed
#define nm_packed
//for msvc
//#define arm_packed
//#define nm_packed
//#pragma pack(1)

// +-------------------------------------
// | 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

//MR typedef struct
typedef arm_packed 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

//MR typedef struct
typedef arm_packed struct
	{
	net_48 ethernet_address;
	short pad;
	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.
// |

#define nk_plugs_settings_count 4

typedef struct
	{
	int settings_index;  // 0..3
	ns_plugs_network_settings settings[4];
	} ns_plugs_persistent_network_settings;

#ifdef nasys_main_flash
 #if __nios32__
	#define nasys_plugs_persistent_network_settings \
			((ns_plugs_persistent_network_settings *)(nasys_main_flash + 0x6000))
 #else
	#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;

//MR typedef struct
typedef arm_packed struct
	{
	net_48 destination_address;
	net_48 source_address;
	net_16 type;
//MR  unsigned char payload[0];	// | generic payload
	unsigned char payload[1];	// | generic payload
	} nm_packed ns_plugs_ethernet_packet;

//MR typedef struct
typedef arm_packed 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;

//MR typedef struct
typedef arm_packed 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;
//MR	unsigned char payload[0];	// | generic payload
	unsigned char payload[1];	// | generic payload
	} nm_packed ns_plugs_ip_packet;

//MR typedef struct
typedef arm_packed struct
	{
	net_8 type;
	net_8 code;
	net_16 checksum;
//MR	unsigned char payload[0];
	unsigned char payload[1];
	} nm_packed ns_plugs_icmp_packet;		// | (always built on ip)

//MR typedef struct
typedef arm_packed struct
	{
	net_16 source_port;
	net_16 destination_port;
	net_16 length;			// | header + payload length
	net_16 checksum;			// | optional for udp -- 0=unused
//MR	unsigned char payload[0];	// | generic payload
	unsigned char payload[1];	// | generic payload
	} nm_packed ns_plugs_udp_packet;		// | (always built on ip)

//MR typedef struct
typedef arm_packed 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;
//MR	unsigned char payload[0];
	unsigned char payload[1];
	} 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, one 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_debug_rx = 0x0100,   // Show all received packets
	ne_plugs_flag_debug_tx = 0x0200,   // Show all transmitted packets
	ne_plugs_flag_add_adapter = 0x1000 // (Append additional adapter)
	};

// +---------------------------------
// | 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
	};

// +-----------------------------------
// | 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 driver
// |
// | The driver description is a structure which
// | contains pointers to a small handful of routines

// | Routine that the mac driver 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_network_settings *s);
typedef int (*nr_plugs_adapter_set_led_proc)
		(void *hardware_base_address,int led_onoff);
typedef int (*nr_plugs_adapter_set_loopback_proc)
		(void *hardware_base_address,int loopback_onoff);
typedef int (*nr_plugs_adapter_check_for_events_proc)
		(void *hardware_base_address,nr_plugs_adapter_dispatch_packet_proc proc,void *context);
typedef int (*nr_plugs_adapter_tx_frame_proc)
		(void *hardware_base_address,char *ethernet_frame,int frame_length);
typedef int (*nr_plugs_adapter_dump_registers_proc)
		(void *hardware_base_address);

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;
	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,
		ns_plugs_adapter_description *adapter_description
		);

int nr_plugs_terminate
		(
		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_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
		);

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

// +----------------------------------------------------------
// | 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
	);

//MR
#undef nm_packed
#undef arm_packed
//for msvc
//#pragma pack

#endif // _plugs_

// end of file

⌨️ 快捷键说明

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