📄 net.h
字号:
typedef void (*udp_handler_t)(udp_socket_t *skt, char *buf, int len,
ip_route_t *src_route, word src_port);
typedef struct _tcp_socket {
struct _tcp_socket *next;
int state; /* connection state */
#define _CLOSED 0
#define _LISTEN 1
#define _SYN_RCVD 2
#define _SYN_SENT 3
#define _ESTABLISHED 4
#define _CLOSE_WAIT 5
#define _LAST_ACK 6
#define _FIN_WAIT_1 7
#define _FIN_WAIT_2 8
#define _CLOSING 9
#define _TIME_WAIT 10
ip_route_t his_addr; /* address of other end of connection */
word our_port;
word his_port;
word data_bytes; /* number of data bytes in pkt */
char reuse; /* SO_REUSEADDR, no 2MSL */
timer_t timer;
pktbuf_t pkt; /* dedicated xmit packet */
pktbuf_t *rxlist; /* list of unread incoming data packets */
char *rxptr; /* pointer to next byte to read */
int rxcnt; /* bytes left in current read packet */
dword ack;
dword seq;
char pktbuf[ETH_MAX_PKTLEN];
} tcp_socket_t;
/*
* Our address.
*/
extern enet_addr_t __local_enet_addr;
extern ip_addr_t __local_ip_addr;
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
extern ip_addr_t __local_ip_gate;
extern ip_addr_t __local_ip_mask;
#endif
/*
* Set a timer. Caller is responsible for providing the timer_t struct.
*/
extern void __timer_set(timer_t *t, unsigned long delay,
tmr_handler_t handler, void *user_data);
/*
* Cancel the given timer.
*/
extern void __timer_cancel(timer_t *t);
/*
* Poll timer list for timer expirations.
*/
extern void __timer_poll(void);
/*
* Initialize the free list.
*/
extern void __pktbuf_init(void);
/*
* simple pktbuf allocation.
* allocates at least nbytes for packet data including ethernet header.
*/
extern pktbuf_t *__pktbuf_alloc(int nbytes);
/*
* return a pktbuf for reuse.
*/
extern void __pktbuf_free(pktbuf_t *pkt);
/*
* Dump packet structures (debug, in case of running out)
*/
extern void __pktbuf_dump(void);
/*
* Install handlers for ethernet packets.
* Returns old handler.
*/
extern pkt_handler_t __eth_install_listener(int eth_type,
pkt_handler_t handler);
extern void __eth_remove_listener(int eth_type);
/*
* Non-blocking poll of ethernet link. Processes all pending
* input packets.
*/
extern void __enet_poll(void);
/*
* Send an ethernet packet.
*/
extern void __enet_send(pktbuf_t *pkt, enet_addr_t *dest, int eth_type);
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
/*
* return true if addr is on local subnet
*/
extern int __ip_addr_local(ip_addr_t *addr);
#endif
/*
* Handle incoming ARP packets.
*/
extern void __arp_handler(pktbuf_t *pkt);
/*
* Find the ethernet address of the machine with the given
* ip address.
* Return true and fills in 'eth_addr' if successful, false
* if unsuccessful.
*/
extern int __arp_request(ip_addr_t *ip_addr, enet_addr_t *eth_addr);
/*
* Lookup an address from the local ARP cache. If not found,
* then call 'arp_request' to find it. [Basically just a cached
* version of 'arp_request']
*/
extern int __arp_lookup(ip_addr_t *host, ip_route_t *rt);
/*
* Do a one's complement checksum.
* The data being checksum'd is in network byte order.
* The returned checksum is in network byte order.
*/
extern unsigned short __sum(word *w, int len, int init_sum);
/*
* Compute a partial checksum for the UDP/TCP pseudo header.
*/
extern int __pseudo_sum(ip_header_t *ip);
/*
* Handle IP packets coming from the polled ethernet interface.
*/
extern void __ip_handler(pktbuf_t *pkt, enet_addr_t *src_enet_addr);
/*
* Send an IP packet.
*
* The IP data field should contain pkt->pkt_bytes of data.
* pkt->[udp|tcp|icmp]_hdr points to the IP data field. Any
* IP options are assumed to be already in place in the IP
* options field. Returns 0 for success.
*/
extern int __ip_send(pktbuf_t *pkt, int protocol, ip_route_t *dest);
/*
* Abort connection.
*/
extern void __tcp_abort(tcp_socket_t *s, unsigned long delay);
/*
* Handle incoming ICMP packets.
*/
extern void __icmp_handler(pktbuf_t *pkt, ip_route_t *r);
extern int __icmp_install_listener(icmp_handler_t handler);
extern void __icmp_remove_listener(void);
/*
* Handle incoming UDP packets.
*/
extern void __udp_handler(pktbuf_t *pkt, ip_route_t *r);
/*
* Install a handler for incoming udp packets.
* Caller provides the udp_socket_t structure.
* Returns zero if successful, -1 if socket is already used.
*/
extern int __udp_install_listener(udp_socket_t *s, word port,
udp_handler_t handler);
/*
* Remove the handler for the given socket.
*/
extern void __udp_remove_listener(word port);
/*
* Send a UDP packet.
*/
extern int __udp_send(char *buf, int len, ip_route_t *dest_ip,
word dest_port, word src_port);
// Send a UDP packet
extern int __udp_sendto(char *buf, int len,
struct sockaddr_in *server, struct sockaddr_in *local);
// Receive a UDP packet
extern int __udp_recvfrom(char *buf, int len,
struct sockaddr_in *from, struct sockaddr_in *local,
struct timeval *timeout);
/*
* TCP poll function. Should be called as often as possible.
*/
extern void __tcp_poll(void);
/*
* Initiate outgoing connection, waiting for at most timeout seconds.
*/
extern int __tcp_open(tcp_socket_t *s, struct sockaddr_in *host,
word port, int timeout, int *err);
/*
* Set up a listening socket on the given port.
* Does not block.
*/
extern int __tcp_listen(tcp_socket_t *s, word port);
/*
* SO_REUSEADDR, no 2MSL.
*/
extern void __tcp_so_reuseaddr(tcp_socket_t *s);
/*
* Block while waiting for all outstanding socket data to
* be transmitted.
*/
extern void __tcp_drain(tcp_socket_t *s);
/*
* Initiate connection close.
*/
extern void __tcp_close(tcp_socket_t *s);
/*
* Wait until connection has fully closed.
*/
extern void __tcp_close_wait(tcp_socket_t *s);
/*
* Read up to 'len' bytes without blocking.
* Returns number of bytes read.
* If connection is closed, returns -1.
*/
extern int __tcp_read(tcp_socket_t *s, char *buf, int len);
/*
* Write up to 'len' bytes without blocking.
* Returns number of bytes written.
* If connection is closed, returns -1.
*/
extern int __tcp_write(tcp_socket_t *s, char *buf, int len);
/*
* Write up to 'len' bytes, blocking until sent (not ACK'd).
* Returns number of bytes written.
* If connection is closed, returns -1.
*/
extern int __tcp_write_block(tcp_socket_t *s, char *buf, int len);
/*
* The following are a higher-level tcp socket interface.
*/
/*
* Initialize a socket for given port.
*/
extern void __skt_init(tcp_socket_t *s, unsigned short port);
/*
* Return true if socket connection is closed.
*/
#define __skt_is_closed(s) (((tcp_socket_t *)(s))->state == _CLOSED)
/*
* Block while listening for an incoming connection.
*/
extern void __skt_wait_for_connect(tcp_socket_t *s);
/*
* Read up to 'len' bytes from the given socket.
* Returns number of bytes read.
* Doesn't block.
*/
extern int __skt_read(tcp_socket_t *s, char *buf, int len);
/*
* Write 'len' bytes to the given socket.
* Returns number of bytes written.
* May not write all data if connection closes.
*/
extern int __skt_write(tcp_socket_t *s, char *buf, int len);
// Initialize the network stack - logical driver layer, etc.
extern void net_init(void);
// Test for new network I/O connections
extern void net_io_test(bool is_idle);
// Conversion between IP addresses and printable strings
extern bool inet_aton(const char *, in_addr_t *);
extern char *inet_ntoa(in_addr_t *);
// FIXME
/* #define NET_SUPPORT_RARP 1 */
#define NET_SUPPORT_ICMP 1
#define NET_SUPPORT_UDP 1
#define NET_SUPPORT_TCP 1
#ifdef BSP_LOG
#define BSPLOG(x) { int old_console = start_console(); x; end_console(old_console); }
#define bsp_log diag_printf
#else
#define BSPLOG(x)
#endif
#endif // _NET_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -