sal.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <dfs_file.h>
  27. #include <rtdevice.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  32. typedef uint32_t socklen_t;
  33. #endif
  34. /* sal socket magic word */
  35. #define SAL_SOCKET_MAGIC 0x5A10
  36. /* The maximum number of sockets structure */
  37. #define SAL_SOCKETS_NUM DFS_FD_MAX
  38. /* The maximum number of protocol families */
  39. #ifndef SAL_PROTO_FAMILIES_NUM
  40. #define SAL_PROTO_FAMILIES_NUM 4
  41. #endif
  42. /* sal socket offset */
  43. #ifndef SAL_SOCKET_OFFSET
  44. #define SAL_SOCKET_OFFSET 0
  45. #endif
  46. struct proto_ops
  47. {
  48. int (*socket) (int domain, int type, int protocol);
  49. int (*closesocket)(int s);
  50. int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
  51. int (*listen) (int s, int backlog);
  52. int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
  53. int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
  54. int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  55. int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  56. int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
  57. int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
  58. int (*shutdown) (int s, int how);
  59. int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
  60. int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
  61. int (*ioctlsocket)(int s, long cmd, void *arg);
  62. int (*poll) (struct dfs_fd *file, struct rt_pollreq *req);
  63. };
  64. struct sal_socket
  65. {
  66. uint32_t magic; /* sal socket magic word */
  67. int socket; /* sal socket descriptor */
  68. int domain;
  69. int type;
  70. int protocol;
  71. const struct proto_ops *ops; /* socket options */
  72. void *user_data; /* specific sal socket data */
  73. };
  74. struct proto_family
  75. {
  76. char name[RT_NAME_MAX];
  77. int family; /* primary protocol families type */
  78. int sec_family; /* secondary protocol families type */
  79. int (*create)(struct sal_socket *sal_socket, int type, int protocol); /* register socket options */
  80. struct hostent* (*gethostbyname) (const char *name);
  81. int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
  82. void (*freeaddrinfo) (struct addrinfo *ai);
  83. int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  84. };
  85. /* SAL(Socket Abstraction Layer) initialize */
  86. int sal_init(void);
  87. struct sal_socket *sal_get_socket(int sock);
  88. /* protocol family register and unregister operate */
  89. int sal_proto_family_register(const struct proto_family *pf);
  90. int sal_proto_family_unregister(const struct proto_family *pf);
  91. struct proto_family *sal_proto_family_find(const char *name);
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif /* SAL_H__ */