sal.h 3.8 KB

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