sock.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef GENLIB_NET_SOCK_H
  2. #define GENLIB_NET_SOCK_H
  3. /**************************************************************************
  4. *
  5. * Copyright (c) 2000-2003 Intel Corporation
  6. * All rights reserved.
  7. * Copyright (c) 2012 France Telecom All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither name of Intel Corporation nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  25. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  29. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. **************************************************************************/
  34. /*!
  35. * \defgroup Sock Network Socket Library
  36. *
  37. * @{
  38. *
  39. * \file
  40. */
  41. #include "UpnpInet.h" /* for SOCKET, netinet/in */
  42. #include "UpnpGlobal.h" /* for UPNP_INLINE */
  43. /* The following are not defined under winsock.h */
  44. #ifndef SD_RECEIVE
  45. #define SD_RECEIVE 0x00
  46. #define SD_SEND 0x01
  47. #define SD_BOTH 0x02
  48. #endif
  49. /*! */
  50. typedef struct
  51. {
  52. /*! Handle/descriptor to a socket. */
  53. SOCKET socket;
  54. /*! The following two fields are filled only in incoming requests. */
  55. struct sockaddr_storage foreign_sockaddr;
  56. } SOCKINFO;
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /*!
  61. * \brief Closes the socket if it is different from -1.
  62. *
  63. * \return -1 if an error occurred or if the socket is -1.
  64. */
  65. static UPNP_INLINE int sock_close(
  66. /*! Socket descriptor. */
  67. SOCKET sock)
  68. {
  69. int ret = -1;
  70. if (sock != INVALID_SOCKET)
  71. ret = UpnpCloseSocket(sock);
  72. return ret;
  73. }
  74. /*!
  75. * \brief Assign the passed in socket descriptor to socket descriptor in the
  76. * SOCKINFO structure.
  77. *
  78. * \return Integer:
  79. * \li \c UPNP_E_SUCCESS
  80. * \li \c UPNP_E_OUTOF_MEMORY
  81. * \li \c UPNP_E_SOCKET_ERROR
  82. */
  83. int sock_init(
  84. /*! [out] Socket Information Object. */
  85. SOCKINFO *info,
  86. /*! [in] Socket Descriptor. */
  87. SOCKET sockfd);
  88. /*!
  89. * \brief Calls the sock_init function and assigns the passed in IP address
  90. * and port to the IP address and port in the SOCKINFO structure.
  91. *
  92. * \return Integer:
  93. * \li \c UPNP_E_SUCCESS
  94. * \li \c UPNP_E_OUTOF_MEMORY
  95. * \li \c UPNP_E_SOCKET_ERROR
  96. */
  97. int sock_init_with_ip(
  98. /*! [out] Socket Information Object. */
  99. SOCKINFO* info,
  100. /*! [in] Socket Descriptor. */
  101. SOCKET sockfd,
  102. /*! [in] Remote socket address. */
  103. struct sockaddr *foreign_sockaddr);
  104. /*!
  105. * \brief Shutsdown the socket using the ShutdownMethod to indicate whether
  106. * sends and receives on the socket will be dis-allowed.
  107. *
  108. * After shutting down the socket, closesocket is called to release system
  109. * resources used by the socket calls.
  110. *
  111. * \return Integer:
  112. * \li \c UPNP_E_SOCKET_ERROR on failure.
  113. * \li \c UPNP_E_SUCCESS on success.
  114. */
  115. int sock_destroy(
  116. /*! [in,out] Socket Information Object. */
  117. SOCKINFO* info,
  118. /*! [in] How to shutdown the socket. Used by sockets's shutdown(). */
  119. int ShutdownMethod);
  120. /*!
  121. * \brief Reads data on socket in sockinfo.
  122. *
  123. * \return Integer:
  124. * \li \c numBytes - On Success, no of bytes received.
  125. * \li \c UPNP_E_TIMEDOUT - Timeout.
  126. * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
  127. */
  128. int sock_read(
  129. /*! [in] Socket Information Object. */
  130. SOCKINFO *info,
  131. /*! [out] Buffer to get data to. */
  132. char* buffer,
  133. /*! [in] Size of the buffer. */
  134. size_t bufsize,
  135. /*! [in,out] timeout value. */
  136. int *timeoutSecs);
  137. /*!
  138. * \brief Writes data on the socket in sockinfo.
  139. *
  140. * \return Integer:
  141. * \li \c numBytes - On Success, no of bytes received.
  142. * \li \c UPNP_E_TIMEDOUT - Timeout.
  143. * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
  144. */
  145. int sock_write(
  146. /*! [in] Socket Information Object. */
  147. SOCKINFO *info,
  148. /*! [in] Buffer to send data from. */
  149. const char *buffer,
  150. /*! [in] Size of the buffer. */
  151. size_t bufsize,
  152. /*! [in,out] timeout value. */
  153. int *timeoutSecs);
  154. /*!
  155. * \brief Make socket blocking.
  156. *
  157. * \return 0 if successful, -1 otherwise.
  158. */
  159. int sock_make_blocking(
  160. /* [in] socket. */
  161. SOCKET sock);
  162. /*!
  163. * \brief Make socket non-blocking.
  164. *
  165. * \return 0 if successful, -1 otherwise.
  166. */
  167. int sock_make_no_blocking(
  168. /* [in] socket. */
  169. SOCKET sock);
  170. #ifdef __cplusplus
  171. } /* #extern "C" */
  172. #endif
  173. /* @} Sock Network Socket Library */
  174. #endif /* GENLIB_NET_SOCK_H */