Socket.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2014 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  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 <sys/types.h>
  20. #if defined(WIN32) || defined(WIN64)
  21. #include <winsock2.h>
  22. #include <ws2tcpip.h>
  23. #define MAXHOSTNAMELEN 256
  24. #if !defined(SSLSOCKET_H)
  25. #undef EAGAIN
  26. #define EAGAIN WSAEWOULDBLOCK
  27. #undef EINTR
  28. #define EINTR WSAEINTR
  29. #undef EINPROGRESS
  30. #define EINPROGRESS WSAEINPROGRESS
  31. #undef EWOULDBLOCK
  32. #define EWOULDBLOCK WSAEWOULDBLOCK
  33. #undef ENOTCONN
  34. #define ENOTCONN WSAENOTCONN
  35. #undef ECONNRESET
  36. #define ECONNRESET WSAECONNRESET
  37. #undef ETIMEDOUT
  38. #define ETIMEDOUT WAIT_TIMEOUT
  39. #endif
  40. #define ioctl ioctlsocket
  41. #define socklen_t int
  42. #else
  43. #define INVALID_SOCKET SOCKET_ERROR
  44. #include <sys/socket.h>
  45. #if !defined(_WRS_KERNEL)
  46. #include <sys/param.h>
  47. #include <sys/time.h>
  48. #include <sys/select.h>
  49. #include <sys/uio.h>
  50. #else
  51. #include <selectLib.h>
  52. #endif
  53. #include <netinet/in.h>
  54. #include <netinet/tcp.h>
  55. #include <arpa/inet.h>
  56. #include <netdb.h>
  57. #include <stdio.h>
  58. #include <unistd.h>
  59. #include <errno.h>
  60. #include <fcntl.h>
  61. #include <unistd.h>
  62. #define ULONG size_t
  63. #endif
  64. /** socket operation completed successfully */
  65. #define TCPSOCKET_COMPLETE 0
  66. #if !defined(SOCKET_ERROR)
  67. /** error in socket operation */
  68. #define SOCKET_ERROR -1
  69. #endif
  70. /** must be the same as SOCKETBUFFER_INTERRUPTED */
  71. #define TCPSOCKET_INTERRUPTED -22
  72. #define SSL_FATAL -3
  73. #if !defined(INET6_ADDRSTRLEN)
  74. #define INET6_ADDRSTRLEN 46 /** only needed for gcc/cygwin on windows */
  75. #endif
  76. #if !defined(max)
  77. #define max(A,B) ( (A) > (B) ? (A):(B))
  78. #endif
  79. #include "LinkedList.h"
  80. /*BE
  81. def FD_SET
  82. {
  83. 128 n8 "data"
  84. }
  85. def SOCKETS
  86. {
  87. FD_SET "rset"
  88. FD_SET "rset_saved"
  89. n32 dec "maxfdp1"
  90. n32 ptr INTList "clientsds"
  91. n32 ptr INTItem "cur_clientsds"
  92. n32 ptr INTList "connect_pending"
  93. n32 ptr INTList "write_pending"
  94. FD_SET "pending_wset"
  95. }
  96. BE*/
  97. /**
  98. * Structure to hold all socket data for the module
  99. */
  100. typedef struct
  101. {
  102. fd_set rset, /**< socket read set (see select doc) */
  103. rset_saved; /**< saved socket read set */
  104. int maxfdp1; /**< max descriptor used +1 (again see select doc) */
  105. List* clientsds; /**< list of client socket descriptors */
  106. ListElement* cur_clientsds; /**< current client socket descriptor (iterator) */
  107. List* connect_pending; /**< list of sockets for which a connect is pending */
  108. List* write_pending; /**< list of sockets for which a write is pending */
  109. fd_set pending_wset; /**< socket pending write set for select */
  110. } Sockets;
  111. void Socket_outInitialize(void);
  112. void Socket_outTerminate(void);
  113. int Socket_getReadySocket(int more_work, struct timeval *tp);
  114. int Socket_getch(int socket, char* c);
  115. char *Socket_getdata(int socket, size_t bytes, size_t* actual_len);
  116. int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees);
  117. void Socket_close(int socket);
  118. int Socket_new(char* addr, int port, int* socket);
  119. int Socket_noPendingWrites(int socket);
  120. char* Socket_getpeer(int sock);
  121. void Socket_addPendingWrite(int socket);
  122. void Socket_clearPendingWrite(int socket);
  123. typedef void Socket_writeComplete(int socket);
  124. void Socket_setWriteCompleteCallback(Socket_writeComplete*);
  125. #endif /* SOCKET_H */