sockets.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright (C) 2016 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef __LWIP_SOCKETS_H__
  13. #define __LWIP_SOCKETS_H__
  14. #include "cfw.h"
  15. #include "lwip/opt.h"
  16. #include "lwip/sockets.h"
  17. #include "lwip/errno.h"
  18. #include "lwip/pbuf.h"
  19. #include "lwip/inet.h"
  20. #include "lwip/ip_addr.h"
  21. #include "lwip/ip4_addr.h"
  22. #include "lwip/dns.h"
  23. #include "lwip/ip4.h"
  24. #include "lwip/icmp.h"
  25. #include "lwip/etharp.h"
  26. #include "lwip/priv/tcpip_priv.h"
  27. #include "lwip/netif.h"
  28. #include "lwip/tcpip.h"
  29. #include "lwip/opt.h"
  30. #include "lwip/etharp.h"
  31. #include "lwip/err.h"
  32. #include "lwip/pbuf.h"
  33. #include "lwip/memp.h"
  34. #include "lwip/ip_addr.h"
  35. #include "lwip/ethip6.h"
  36. extern struct netif *netif_default;
  37. #define CFW_TCPIP_SOCK_STREAM SOCK_STREAM
  38. #define CFW_TCPIP_SOCK_DGRAM SOCK_DGRAM
  39. #define CFW_TCPIP_SOCK_RAW SOCK_RAW
  40. #define CFW_TCPIP_AF_UNSPEC AF_UNSPEC
  41. #define CFW_TCPIP_AF_INET AF_INET
  42. #define CFW_TCPIP_PF_INET PF_INET
  43. #define CFW_TCPIP_PF_UNSPEC PF_UNSPEC
  44. #define CFW_TCPIP_IPPROTO_IP IPPROTO_IP
  45. #define CFW_TCPIP_IPPROTO_ICMP IPPROTO_ICMP
  46. #define CFW_TCPIP_IPPROTO_TCP IPPROTO_TCP
  47. #define CFW_TCPIP_IPPROTO_UDP IPPROTO_UDP
  48. #define ERR_TCPIP_ENODATA ENODATA
  49. #define ERR_TCPIP_EWOULDBLOCK EWOULDBLOCK
  50. #define ERR_TCPIP_IF ERR_IF
  51. #define ERR_TCPIP_MEM ERR_MEM
  52. #define ERR_TCPIP_OK ERR_OK
  53. #define CFW_ERR_TCPIP_CONN_TIMEOUT 13 /* connect time out */
  54. #define INVALID_SOCKET (SOCKET)(~0)
  55. #define SOCKET_ERROR -1
  56. #ifndef GPRS_MTU
  57. #define GPRS_MTU 1500
  58. #endif
  59. #define UDP_FLAGS_RAI_0 0x00
  60. #define UDP_FLAGS_RAI_1 0x01
  61. #define UDP_FLAGS_RAI_2 0x02
  62. #define UDP_FLAGS_EXCEPTIN_MSG 0x04
  63. #ifndef __SOCKET__
  64. typedef int SOCKET;
  65. #define __SOCKET__
  66. #endif
  67. typedef struct in_addr in_addr;
  68. typedef struct in6_addr in6_addr;
  69. #define IPH_VHLTOS_SET(hdr, v, hl, tos) \
  70. do \
  71. { \
  72. IPH_VHL_SET(hdr, v, hl); \
  73. IPH_TOS_SET(hdr, tos); \
  74. } while (0)
  75. typedef struct _CFW_TCPIP_SOCKET_ADDR
  76. {
  77. uint8_t sin_len;
  78. uint8_t sin_family;
  79. uint16_t sin_port;
  80. in_addr sin_addr;
  81. int8_t sin_zero[8];
  82. } CFW_TCPIP_SOCKET_ADDR;
  83. typedef struct _CFW_TCPIP_SOCKET_ADDR6
  84. {
  85. uint8_t sin6_len;
  86. uint8_t sin6_family;
  87. uint16_t sin6_port;
  88. uint32_t sin6_flowinfo;
  89. in6_addr sin6_addr;
  90. uint32_t sin6_scope_id;
  91. } CFW_TCPIP_SOCKET_ADDR6;
  92. typedef struct _CFW_WIFI_DATA
  93. {
  94. uint16_t nDataLength;
  95. uint8_t srcMac[6];
  96. uint8_t destMac[6];
  97. uint8_t *pData;
  98. } CFW_WIFI_DATA;
  99. typedef struct _CFW_WIFI_NETIF
  100. {
  101. uint8_t Mac[6];
  102. // uint8_t gwMac[6];
  103. ip_addr_t ipaddr;
  104. } CFW_WIFI_NETIF;
  105. typedef struct
  106. {
  107. uint8_t ref_count;
  108. uint16_t msg_len;
  109. bool use_dhcp;
  110. uint8_t ip_addr[4];
  111. uint8_t gateway[4];
  112. uint8_t netmask[4];
  113. uint8_t pri_dns_addr[4];
  114. uint8_t sec_dns_addr[4];
  115. } ipaddr_change_req_struct;
  116. typedef struct
  117. {
  118. uint8_t ref_count;
  119. uint16_t msg_len;
  120. uint8_t cause;
  121. uint8_t ip_addr[4];
  122. uint8_t pri_dns_addr[4];
  123. uint8_t sec_dns_addr[4];
  124. uint8_t gateway[4];
  125. uint8_t netmask[4];
  126. } ipaddr_update_ind_struct;
  127. typedef enum
  128. {
  129. CFW_SOCKET_NONE,
  130. CFW_SOCKET_CONNECTING,
  131. CFW_SOCKET_CONNECTED,
  132. CFW_SOCKET_BEARER_LOSING,
  133. CFW_SOCKET_CLOSING,
  134. CFW_SOCKET_COLSED,
  135. } socket_status_t;
  136. typedef struct
  137. {
  138. char *cServer; //ntp server name
  139. int iRetry; //retry times if it fails
  140. bool bAutoUpdate; //long time update or not
  141. int iSyncMode; //sync time mode
  142. osiCallback_t fCallback; //call back function when finish or timedout
  143. uint16_t nSimCid;
  144. void *fCallback_ctx;
  145. } CFW_SNTP_CONFIG;
  146. typedef enum
  147. {
  148. CFW_SNTP_PARAM_INVALID,
  149. CFW_SNTP_SYNCING,
  150. CFW_SNTP_READY
  151. } sntp_status_t;
  152. void CFW_SetPppSendFun(bool (*sendCallBack)(uint8_t *pData, uint16_t uDataSize, uint8_t nDLCI));
  153. void CFW_SetGetSimCidFun(void (*get_simid_cid)(uint8_t *pSimId, uint8_t *pCid, uint8_t nDLCI));
  154. uint32_t CFW_TcpipInetAddr(const char *cp);
  155. int32_t CFW_TcpipAvailableBuffer(SOCKET nSocket);
  156. uint32_t CFW_TcpipGetLastError(void);
  157. SOCKET CFW_TcpipSocket(uint8_t nDomain, uint8_t nType, uint8_t nProtocol);
  158. SOCKET CFW_TcpipSocketEX(uint8_t nDomain, uint8_t nType, uint8_t nProtocol, osiCallback_t func, uint32_t nUserParam);
  159. void CFW_TcpipSocketSetParam(SOCKET nSocket, osiCallback_t func, uint32_t nUserParam);
  160. uint32_t CFW_TcpipSocketConnect(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *pName, uint8_t nNameLen);
  161. int CFW_TcpipSocketSend(SOCKET nSocket, uint8_t *pData, uint16_t nDataSize, uint32_t nFlags);
  162. #if LWIP_RFACK
  163. int CFW_TcpipSocketSendWithSeqno(SOCKET nSocket, uint8_t *pData, uint16_t nDataSize, uint32_t nFlags, uint32_t seqno);
  164. uint32_t CFW_TcpipSocketSendtoWithSeqno(SOCKET nSocket, void *pData, uint16_t nDataSize, uint32_t nFlags, CFW_TCPIP_SOCKET_ADDR *to, int tolen, uint32_t seqno);
  165. #endif
  166. uint32_t CFW_TcpipSocketRecv(SOCKET nSocket, uint8_t *pData, uint16_t nDataSize, uint32_t nFlags);
  167. int CFW_TcpipSocketClose(SOCKET nSocket);
  168. uint32_t CFW_TcpipSocketShutdownOutput(SOCKET nSocket, int how);
  169. uint32_t CFW_TcpipSocketIoctl(SOCKET nSocket, int cmd, void *argp);
  170. uint32_t CFW_TcpipSocketSendto(SOCKET nSocket, void *pData, uint16_t nDataSize, uint32_t nFlags, CFW_TCPIP_SOCKET_ADDR *to, int tolen);
  171. uint32_t CFW_TcpipSocketRecvfrom(SOCKET nSocket, void *pMem, int nLen, uint32_t nFlags, CFW_TCPIP_SOCKET_ADDR *from, int *fromlen);
  172. uint32_t CFW_TcpipSocketBind(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *pName, uint8_t nNameLen);
  173. uint32_t CFW_TcpipSocketListen(SOCKET nSocket, uint32_t backlog);
  174. uint32_t CFW_TcpipSocketAccept(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *addr, uint32_t *addrlen);
  175. uint32_t CFW_TcpipSocketAcceptEx(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *addr, uint32_t *addrlen, osiCallback_t func, uint32_t nUserParam);
  176. uint32_t CFW_TcpipSocketGetsockname(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *pName, int *pNameLen);
  177. uint32_t CFW_TcpipSocketGetpeername(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *pName, int *pNameLen); //
  178. uint32_t CFW_Gethostbyname(char *hostname, ip_addr_t *addr, uint8_t nCid, CFW_SIM_ID nSimID);
  179. uint32_t CFW_GethostbynameEX(char *hostname, ip_addr_t *addr, uint8_t nCid, CFW_SIM_ID nSimID, osiCallback_t func, void *param);
  180. uint32_t CFW_GetallhostbynameEX(char *hostname, ip_addr_t *addr, uint8_t nCid, CFW_SIM_ID nSimID, osiCallback_t func, void *param, int *addr_num);
  181. uint32_t CFW_Gethostbyname_SpecificserverEX(char *hostname, ip_addr_t *addr, uint8_t nCid, CFW_SIM_ID nSimID, osiCallback_t func, void *param, char *dns_server, u8_t addrtype);
  182. int CFW_TcpipSocketConnectEx(SOCKET nSocket, CFW_TCPIP_SOCKET_ADDR *pName, uint8_t nNameLen, CFW_SIM_ID nSimID);
  183. int CFW_TcpipSocketGetsockopt(SOCKET nSocket, int level, int optname, void *optval, int *optlen);
  184. int CFW_TcpipSocketSetsockopt(SOCKET nSocket, int level, int optname, const void *optval, int optlen);
  185. uint16_t CFW_TcpipGetRecvAvailable(SOCKET nSocket);
  186. int32_t CFW_TcpipSocketGetAckedSize(SOCKET nSocket);
  187. int32_t CFW_TcpipSocketGetSentSize(SOCKET nSocket);
  188. socket_status_t CFW_TcpipSocketGetStatus(SOCKET nSocket);
  189. int32_t CFW_TcpipSocketGetMss(SOCKET nSocket);
  190. void CFW_TcpipSocketDataNotify(SOCKET nSocket);
  191. int32_t CFW_TcpipSocketGetRecvAvailSize(SOCKET nSocket);
  192. CFW_SNTP_CONFIG *CFW_SntpInit(void);
  193. sntp_status_t CFW_SntpStart(CFW_SNTP_CONFIG *sntpconfig);
  194. void CFW_SntpStop(CFW_SNTP_CONFIG *sntpconfig);
  195. #define UPLOAD 1
  196. #define DOWNLOAD 2
  197. #define LWIP_DATA 1
  198. #define PPP_DATA 2
  199. #define RNDIS_DATA 3
  200. bool CFW_get_Netif_dataCount(struct netif *netif, uint16_t uType, uint16_t uDataType, uint32_t *loadsize);
  201. bool CFW_get_Netif_dataCountBySimCid(uint16_t simID, uint16_t CID, uint16_t uType, uint16_t uDataType, uint32_t *loadsize);
  202. #if LWIP_IPV4
  203. #define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) \
  204. do \
  205. { \
  206. (sin)->sin_len = sizeof(struct sockaddr_in); \
  207. (sin)->sin_family = AF_INET; \
  208. (sin)->sin_port = lwip_htons((port)); \
  209. inet_addr_from_ip4addr(&(sin)->sin_addr, ipaddr); \
  210. memset((sin)->sin_zero, 0, SIN_ZERO_LEN); \
  211. } while (0)
  212. #define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) \
  213. do \
  214. { \
  215. inet_addr_to_ip4addr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \
  216. (port) = lwip_ntohs((sin)->sin_port); \
  217. } while (0)
  218. #endif /* LWIP_IPV4 */
  219. #if LWIP_IPV6
  220. #define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) \
  221. do \
  222. { \
  223. (sin6)->sin6_len = sizeof(struct sockaddr_in6); \
  224. (sin6)->sin6_family = AF_INET6; \
  225. (sin6)->sin6_port = lwip_htons((port)); \
  226. (sin6)->sin6_flowinfo = 0; \
  227. inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); \
  228. (sin6)->sin6_scope_id = ip6_addr_zone(ipaddr); \
  229. } while (0)
  230. #define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) \
  231. do \
  232. { \
  233. inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \
  234. if (ip6_addr_has_scope(ip_2_ip6(ipaddr), IP6_UNKNOWN)) \
  235. { \
  236. ip6_addr_set_zone(ip_2_ip6(ipaddr), (u8_t)((sin6)->sin6_scope_id)); \
  237. } \
  238. (port) = lwip_ntohs((sin6)->sin6_port); \
  239. } while (0)
  240. #endif /* LWIP_IPV6 */
  241. #if LWIP_IPV4 && LWIP_IPV6
  242. void sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port);
  243. #define IS_SOCK_ADDR_LEN_VALID(namelen) (((namelen) == sizeof(struct sockaddr_in)) || \
  244. ((namelen) == sizeof(struct sockaddr_in6)))
  245. #define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || \
  246. ((name)->sa_family == AF_INET6))
  247. #define SOCK_ADDR_TYPE_MATCH(name, sock) \
  248. ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \
  249. (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type))))
  250. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \
  251. do \
  252. { \
  253. if (((ipaddr) && IP_IS_ANY_TYPE_VAL(*ipaddr)) || IP_IS_V6(ipaddr)) \
  254. { \
  255. IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6 *)(void *)(sockaddr), ip_2_ip6(ipaddr), port); \
  256. } \
  257. else \
  258. { \
  259. IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in *)(void *)(sockaddr), ip_2_ip4(ipaddr), port); \
  260. } \
  261. } while (0)
  262. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) sockaddr_to_ipaddr_port(sockaddr, ipaddr, &(port))
  263. #define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6))
  264. #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */
  265. #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6))
  266. #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6)
  267. #define SOCK_ADDR_TYPE_MATCH(name, sock) 1
  268. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \
  269. IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6 *)(void *)(sockaddr), ip_2_ip6(ipaddr), port)
  270. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \
  271. SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6 *)(const void *)(sockaddr), ipaddr, port)
  272. #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type)
  273. #else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */
  274. #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in))
  275. #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET)
  276. #define SOCK_ADDR_TYPE_MATCH(name, sock) 1
  277. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \
  278. IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in *)(void *)(sockaddr), ip_2_ip4(ipaddr), port)
  279. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \
  280. SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in *)(const void *)(sockaddr), ipaddr, port)
  281. #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type)
  282. #endif /* LWIP_IPV6 */
  283. #endif /* __LWIP_SOCKETS_H__ */