sal.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-17 ChenYong First version
  9. */
  10. #ifndef SAL_H__
  11. #define SAL_H__
  12. #include <rtdevice.h>
  13. #ifdef SAL_USING_POSIX
  14. #include <dfs_file.h>
  15. #endif
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  20. typedef uint32_t socklen_t;
  21. #endif
  22. /* sal socket magic word */
  23. #define SAL_SOCKET_MAGIC 0x5A10
  24. /* The maximum number of sockets structure */
  25. #ifndef SAL_SOCKETS_NUM
  26. #define SAL_SOCKETS_NUM DFS_FD_MAX
  27. #endif
  28. /* The maximum number of protocol families */
  29. #ifndef SAL_PROTO_FAMILIES_NUM
  30. #define SAL_PROTO_FAMILIES_NUM 4
  31. #endif
  32. /* sal socket offset */
  33. #ifndef SAL_SOCKET_OFFSET
  34. #define SAL_SOCKET_OFFSET 0
  35. #endif
  36. struct proto_ops
  37. {
  38. int (*socket) (int domain, int type, int protocol);
  39. int (*closesocket)(int s);
  40. int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
  41. int (*listen) (int s, int backlog);
  42. int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
  43. int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
  44. int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  45. int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  46. int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
  47. int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
  48. int (*shutdown) (int s, int how);
  49. int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
  50. int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
  51. int (*ioctlsocket)(int s, long cmd, void *arg);
  52. #ifdef SAL_USING_POSIX
  53. int (*poll) (struct dfs_fd *file, struct rt_pollreq *req);
  54. #endif
  55. };
  56. struct sal_socket
  57. {
  58. uint32_t magic; /* sal socket magic word */
  59. int socket; /* sal socket descriptor */
  60. int domain;
  61. int type;
  62. int protocol;
  63. const struct proto_ops *ops; /* socket options */
  64. void *user_data; /* specific sal socket data */
  65. };
  66. struct proto_family
  67. {
  68. char name[RT_NAME_MAX];
  69. int family; /* primary protocol families type */
  70. int sec_family; /* secondary protocol families type */
  71. int (*create)(struct sal_socket *sal_socket, int type, int protocol); /* register socket options */
  72. struct hostent* (*gethostbyname) (const char *name);
  73. int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
  74. void (*freeaddrinfo) (struct addrinfo *ai);
  75. int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  76. };
  77. /* SAL(Socket Abstraction Layer) initialize */
  78. int sal_init(void);
  79. struct sal_socket *sal_get_socket(int sock);
  80. /* protocol family register and unregister operate */
  81. int sal_proto_family_register(const struct proto_family *pf);
  82. int sal_proto_family_unregister(const struct proto_family *pf);
  83. struct proto_family *sal_proto_family_find(const char *name);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* SAL_H__ */