/******************************************************************************
版权所有：	
文件名称：	rt_socket.h
文件版本：	01.01
创建作者：	sunxi
创建日期：	2020-06-18
功能说明：	实时微系统网络接口.
其它说明：
修改记录：
*/
#ifndef _RT_SOCKET_H
#define _RT_SOCKET_H
/*------------------------------- 头文件 --------------------------------------
*/
#if 1
#include <linux/net.h>
#include <linux/in.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#endif

/*------------------------------- 宏定义 --------------------------------------
*/

#define 	RT_SOCKET_FD
#ifdef RT_SOCKET_FD
#define 	RT_SOCKET_ERR				(-1)
#define 	RT_SOCKET_ACCEPT_RST		(-EAGAIN)
#define 	RT_SOCKET_EBADF			(-EBADF)
typedef	int 	SOCKET;
#else
#define 	RT_SOCKET_ERR				NULL
#define 	RT_SOCKET_ACCEPT_RST		NULL
#define	RT_SOCKET_EBADF			NULL	
typedef	struct socket * SOCKET;
#endif



#define 	RT_LINK_TIMEOUT		3		// 以秒为单位

/*------------------------------ 类型结构 -------------------------------------
*/


/*------------------------------ 函数声明 -------------------------------------
*/
extern int g_net_num; // 正常的网口数量

int rt_socket_init(void);

// 打开关闭
SOCKET rt_socket (int domain, int type, int protocol);
int rt_socket_close (SOCKET s);

//连接
SOCKET rt_accept (SOCKET s, struct sockaddr * addr, unsigned int * addr_len);
int rt_bind (SOCKET s, struct sockaddr * addr, unsigned int len);
int rt_connect (SOCKET s,  struct sockaddr * addr, unsigned int len);
int rt_listen (SOCKET s, int n) ;
int rt_shutdown (SOCKET s, int how) ;

//接收发送
int rt_send(SOCKET s,  void *buf, int n, int flags);
int rt_recv (SOCKET s, void *buf, int n, int flags);
int rt_sendto (SOCKET s,  void *buf, int n,int flags,  struct sockaddr * addr, unsigned int addr_len);
int rt_recvfrom (SOCKET s, void * buf, int n, int flags, struct sockaddr * addr, unsigned int * addr_len);
//int rt_sendmsg (SOCKET s,  struct msghdr *message,int flags);
//int rt_recvmsg (SOCKET s, struct msghdr *message, int flags);

//控制
int rt_getsockname (SOCKET s, struct sockaddr * addr,	unsigned int * len);
int rt_getpeername (SOCKET s, struct sockaddr * addr,unsigned int * len);
int rt_getsockopt (SOCKET s, int level, int optname,void * optval,unsigned int * optlen);
int rt_setsockopt (SOCKET s, int level, int optname,  void *optval, unsigned int optlen);
int rt_kernel_sock_ioctl(SOCKET s, int cmd, uint32_t arg);
int rt_sockconnected(SOCKET s) ;
int rt_net_set_KeepAlive(SOCKET s, int interval) ;
//int rt_sockioctl(SOCKET s, int cmd, uint32_t arg);

int rt_if_up(int index);
int rt_if_down(int index);
int rt_if_sz2mac(char *sz, unsigned char *mac);
int rt_if_mac_set(int index, unsigned char *mac);
int rt_if_mac_get(int index, unsigned char *mac);
int rt_if_ip_set(int index, unsigned char * ip);
int rt_if_ip_get(int index, unsigned char *ip);
int rt_if_mask_set(int index, unsigned char * mask);
int rt_if_mask_get(int index, unsigned char *mask);
int rt_route_add(int index,unsigned char * destination,unsigned char *gateway);
int rt_route_del(int index,unsigned char * destination,unsigned char *gateway);
int net_if_get(int if_index,unsigned char *mac,unsigned char *ip,unsigned char *mask);
int net_gateway_set(unsigned char *gateway);
int rt_get_net_linkstatus(int if_index);
int rt_get_netcard_id_from_socket(SOCKET s); // 物理网口索引
int net_route_set(unsigned char *routenet, unsigned char *routenetmask, unsigned char *routegate);
int net_if_set2(int eth_id, int if_index,unsigned char *mac,unsigned char *ip,unsigned char *mask);
int rt_if_ip_get2(int eth_id, int index, unsigned char *ip);
int rt_if_down2(int eth_id, int index);
int net_if_set(int if_index,unsigned char *mac,unsigned char *ip,unsigned char *mask);


int net_if_test(void);
int socket_test(void);
#endif //_RT_SOCKET_H

/*------------------------------ 文件结束 -------------------------------------
*/

