Socket.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation and documentation
  15. * Ian Craggs - async client updates
  16. *******************************************************************************/
  17. #if !defined(SOCKET_H)
  18. #define SOCKET_H
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. #if defined(_WIN32) || defined(_WIN64)
  22. #include <errno.h>
  23. #include <winsock2.h>
  24. #include <ws2tcpip.h>
  25. #define MAXHOSTNAMELEN 256
  26. #define poll WSAPoll
  27. #if !defined(SSLSOCKET_H)
  28. #undef EAGAIN
  29. #define EAGAIN WSAEWOULDBLOCK
  30. #undef EINTR
  31. #define EINTR WSAEINTR
  32. #undef EINPROGRESS
  33. #define EINPROGRESS WSAEINPROGRESS
  34. #undef EWOULDBLOCK
  35. #define EWOULDBLOCK WSAEWOULDBLOCK
  36. #undef ENOTCONN
  37. #define ENOTCONN WSAENOTCONN
  38. #undef ECONNRESET
  39. #define ECONNRESET WSAECONNRESET
  40. #undef ETIMEDOUT
  41. #define ETIMEDOUT WAIT_TIMEOUT
  42. #endif
  43. #define ioctl ioctlsocket
  44. #define socklen_t int
  45. #else
  46. #define INVALID_SOCKET SOCKET_ERROR
  47. #include <sys/socket.h>
  48. #if !defined(_WRS_KERNEL)
  49. #include <sys/param.h>
  50. #include <sys/time.h>
  51. #include <sys/select.h>
  52. #include <poll.h>
  53. #include <sys/uio.h>
  54. #else
  55. #include <selectLib.h>
  56. #endif
  57. #include <netinet/in.h>
  58. #include <netinet/tcp.h>
  59. #include <arpa/inet.h>
  60. #include <netdb.h>
  61. #include <stdio.h>
  62. #include <unistd.h>
  63. #include <errno.h>
  64. #include <fcntl.h>
  65. #include <unistd.h>
  66. #define ULONG size_t
  67. #define SOCKET int
  68. #endif
  69. #include "mutex_type.h" /* Needed for mutex_type */
  70. /** socket operation completed successfully */
  71. #define TCPSOCKET_COMPLETE 0
  72. #if !defined(SOCKET_ERROR)
  73. /** error in socket operation */
  74. #define SOCKET_ERROR -1
  75. #endif
  76. /** must be the same as SOCKETBUFFER_INTERRUPTED */
  77. #define TCPSOCKET_INTERRUPTED -22
  78. #define SSL_FATAL -3
  79. #if !defined(INET6_ADDRSTRLEN)
  80. #define INET6_ADDRSTRLEN 46 /** only needed for gcc/cygwin on windows */
  81. #endif
  82. #if !defined(max)
  83. #define max(A,B) ( (A) > (B) ? (A):(B))
  84. #endif
  85. #include "LinkedList.h"
  86. /*
  87. * Network write buffers for an MQTT packet
  88. */
  89. typedef struct
  90. {
  91. int count; /**> number of buffers/buflens/frees */
  92. char** buffers; /**> array of byte buffers */
  93. size_t* buflens; /**> array of lengths of buffers */
  94. int* frees; /**> array of flags indicating whether each buffer needs to be freed */
  95. uint8_t mask[4]; /**> websocket mask used to mask the buffer data, if any */
  96. } PacketBuffers;
  97. /**
  98. * Structure to hold all socket data for the module
  99. */
  100. typedef struct
  101. {
  102. List* connect_pending; /**< list of sockets for which a connect is pending */
  103. List* write_pending; /**< list of sockets for which a write is pending */
  104. #if defined(USE_SELECT)
  105. fd_set rset, /**< socket read set (see select doc) */
  106. rset_saved; /**< saved socket read set */
  107. int maxfdp1; /**< max descriptor used +1 (again see select doc) */
  108. List* clientsds; /**< list of client socket descriptors */
  109. ListElement* cur_clientsds; /**< current client socket descriptor (iterator) */
  110. fd_set pending_wset; /**< socket pending write set for select */
  111. #else
  112. unsigned int nfds; /**< no of file descriptors for poll */
  113. struct pollfd* fds_read; /**< poll read file descriptors */
  114. struct pollfd* fds_write;
  115. struct {
  116. int cur_fd; /**< index into the fds_saved array */
  117. unsigned int nfds; /**< number of fds in the fds_saved array */
  118. struct pollfd* fds_write;
  119. struct pollfd* fds_read;
  120. } saved;
  121. #endif
  122. } Sockets;
  123. void Socket_outInitialize(void);
  124. void Socket_outTerminate(void);
  125. SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int* rc);
  126. int Socket_getch(SOCKET socket, char* c);
  127. char *Socket_getdata(SOCKET socket, size_t bytes, size_t* actual_len, int* rc);
  128. int Socket_putdatas(SOCKET socket, char* buf0, size_t buf0len, PacketBuffers bufs);
  129. int Socket_close(SOCKET socket);
  130. #if defined(__GNUC__) && defined(__linux__)
  131. /* able to use GNU's getaddrinfo_a to make timeouts possible */
  132. int Socket_new(const char* addr, size_t addr_len, int port, SOCKET* socket, long timeout);
  133. #else
  134. int Socket_new(const char* addr, size_t addr_len, int port, SOCKET* socket);
  135. #endif
  136. int Socket_noPendingWrites(SOCKET socket);
  137. char* Socket_getpeer(SOCKET sock);
  138. void Socket_addPendingWrite(SOCKET socket);
  139. void Socket_clearPendingWrite(SOCKET socket);
  140. typedef void Socket_writeContinue(SOCKET socket);
  141. void Socket_setWriteContinueCallback(Socket_writeContinue*);
  142. typedef void Socket_writeComplete(SOCKET socket, int rc);
  143. void Socket_setWriteCompleteCallback(Socket_writeComplete*);
  144. typedef void Socket_writeAvailable(SOCKET socket);
  145. void Socket_setWriteAvailableCallback(Socket_writeAvailable*);
  146. #endif /* SOCKET_H */