sal.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * File : sal.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-17 ChenYong First version
  23. */
  24. #ifndef SAL_H__
  25. #define SAL_H__
  26. #include <rtdevice.h>
  27. #ifdef SAL_USING_POSIX
  28. #include <dfs_file.h>
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  34. typedef uint32_t socklen_t;
  35. #endif
  36. /* sal socket magic word */
  37. #define SAL_SOCKET_MAGIC 0x5A10
  38. /* The maximum number of sockets structure */
  39. #ifndef SAL_SOCKETS_NUM
  40. #define SAL_SOCKETS_NUM DFS_FD_MAX
  41. #endif
  42. /* The maximum number of protocol families */
  43. #ifndef SAL_PROTO_FAMILIES_NUM
  44. #define SAL_PROTO_FAMILIES_NUM 4
  45. #endif
  46. /* sal socket offset */
  47. #ifndef SAL_SOCKET_OFFSET
  48. #define SAL_SOCKET_OFFSET 0
  49. #endif
  50. struct proto_ops
  51. {
  52. int (*socket) (int domain, int type, int protocol);
  53. int (*closesocket)(int s);
  54. int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
  55. int (*listen) (int s, int backlog);
  56. int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
  57. int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
  58. int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  59. int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  60. int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
  61. int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
  62. int (*shutdown) (int s, int how);
  63. int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
  64. int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
  65. int (*ioctlsocket)(int s, long cmd, void *arg);
  66. #ifdef SAL_USING_POSIX
  67. int (*poll) (struct dfs_fd *file, struct rt_pollreq *req);
  68. #endif
  69. };
  70. struct sal_socket
  71. {
  72. uint32_t magic; /* sal socket magic word */
  73. int socket; /* sal socket descriptor */
  74. int domain;
  75. int type;
  76. int protocol;
  77. const struct proto_ops *ops; /* socket options */
  78. void *user_data; /* specific sal socket data */
  79. };
  80. struct proto_family
  81. {
  82. char name[RT_NAME_MAX];
  83. int family; /* primary protocol families type */
  84. int sec_family; /* secondary protocol families type */
  85. int (*create)(struct sal_socket *sal_socket, int type, int protocol); /* register socket options */
  86. struct hostent* (*gethostbyname) (const char *name);
  87. int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
  88. void (*freeaddrinfo) (struct addrinfo *ai);
  89. int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  90. };
  91. /* SAL(Socket Abstraction Layer) initialize */
  92. int sal_init(void);
  93. struct sal_socket *sal_get_socket(int sock);
  94. /* protocol family register and unregister operate */
  95. int sal_proto_family_register(const struct proto_family *pf);
  96. int sal_proto_family_unregister(const struct proto_family *pf);
  97. struct proto_family *sal_proto_family_find(const char *name);
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif /* SAL_H__ */