socket.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #ifndef GMSSL_SOCKET_H
  10. #define GMSSL_SOCKET_H
  11. #include <string.h>
  12. #include <stdint.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifdef WIN32
  17. #pragma comment (lib, "Ws2_32.lib")
  18. #pragma comment (lib, "Mswsock.lib")
  19. #pragma comment (lib, "AdvApi32.lib")
  20. #include <winsock2.h>
  21. typedef SOCKET tls_socket_t;
  22. typedef int tls_ret_t;
  23. typedef int tls_socklen_t;
  24. #define tls_socket_send(sock,buf,len,flags) send(sock,buf,(int)(len),flags)
  25. #define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,(int)(len),flags)
  26. #define tls_socket_close(sock) closesocket(sock)
  27. #else
  28. #include <fcntl.h>
  29. #include <netdb.h>
  30. #include <arpa/inet.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <unistd.h>
  35. typedef int tls_socket_t;
  36. typedef ssize_t tls_ret_t;
  37. typedef socklen_t tls_socklen_t;
  38. #define tls_socket_send(sock,buf,len,flags) send(sock,buf,len,flags)
  39. #define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,len,flags)
  40. #define tls_socket_close(sock) close(sock)
  41. #endif
  42. int tls_socket_lib_init(void);
  43. int tls_socket_lib_cleanup(void);
  44. int tls_socket_create(tls_socket_t *sock, int af, int type, int protocl);
  45. int tls_socket_connect(tls_socket_t sock, const struct sockaddr_in *addr);
  46. int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr);
  47. int tls_socket_listen(tls_socket_t sock, int backlog);
  48. int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t *conn_sock);
  49. #ifdef __cplusplus
  50. }
  51. #endif
  52. #endif