net.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * \file net.h
  3. *
  4. * \brief MD5 message digest algorithm (hash function)
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_NET_H
  28. #define POLARSSL_NET_H
  29. #include <string.h>
  30. #define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0040 /**< Failed to get an IP address for the given hostname. */
  31. #define POLARSSL_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
  32. #define POLARSSL_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
  33. #define POLARSSL_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
  34. #define POLARSSL_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
  35. #define POLARSSL_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
  36. #define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
  37. #define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
  38. #define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
  39. #define POLARSSL_ERR_NET_WANT_READ -0x0052 /**< Connection requires a read call. */
  40. #define POLARSSL_ERR_NET_WANT_WRITE -0x0054 /**< Connection requires a write call. */
  41. #define POLARSSL_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * \brief Initiate a TCP connection with host:port
  47. *
  48. * \param fd Socket to use
  49. * \param host Host to connect to
  50. * \param port Port to connect to
  51. *
  52. * \return 0 if successful, or one of:
  53. * POLARSSL_ERR_NET_SOCKET_FAILED,
  54. * POLARSSL_ERR_NET_UNKNOWN_HOST,
  55. * POLARSSL_ERR_NET_CONNECT_FAILED
  56. */
  57. int net_connect( int *fd, const char *host, int port );
  58. /**
  59. * \brief Create a listening socket on bind_ip:port.
  60. * If bind_ip == NULL, all interfaces are binded.
  61. *
  62. * \param fd Socket to use
  63. * \param bind_ip IP to bind to, can be NULL
  64. * \param port Port number to use
  65. *
  66. * \return 0 if successful, or one of:
  67. * POLARSSL_ERR_NET_SOCKET_FAILED,
  68. * POLARSSL_ERR_NET_BIND_FAILED,
  69. * POLARSSL_ERR_NET_LISTEN_FAILED
  70. */
  71. int net_bind( int *fd, const char *bind_ip, int port );
  72. /**
  73. * \brief Accept a connection from a remote client
  74. *
  75. * \param bind_fd Relevant socket
  76. * \param client_fd Will contain the connected client socket
  77. * \param client_ip Will contain the client IP address
  78. *
  79. * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
  80. * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
  81. * non-blocking and accept() is blocking.
  82. */
  83. int net_accept( int bind_fd, int *client_fd, void *client_ip );
  84. /**
  85. * \brief Set the socket blocking
  86. *
  87. * \param fd Socket to set
  88. *
  89. * \return 0 if successful, or a non-zero error code
  90. */
  91. int net_set_block( int fd );
  92. /**
  93. * \brief Set the socket non-blocking
  94. *
  95. * \param fd Socket to set
  96. *
  97. * \return 0 if successful, or a non-zero error code
  98. */
  99. int net_set_nonblock( int fd );
  100. /**
  101. * \brief Portable usleep helper
  102. *
  103. * \param usec Amount of microseconds to sleep
  104. *
  105. * \note Real amount of time slept will not be less than
  106. * select()'s timeout granularity (typically, 10ms).
  107. */
  108. void net_usleep( unsigned long usec );
  109. /**
  110. * \brief Read at most 'len' characters. If no error occurs,
  111. * the actual amount read is returned.
  112. *
  113. * \param ctx Socket
  114. * \param buf The buffer to write to
  115. * \param len Maximum length of the buffer
  116. *
  117. * \return This function returns the number of bytes received,
  118. * or a non-zero error code; POLARSSL_ERR_NET_WANT_READ
  119. * indicates read() is blocking.
  120. */
  121. int net_recv( void *ctx, unsigned char *buf, size_t len );
  122. /**
  123. * \brief Write at most 'len' characters. If no error occurs,
  124. * the actual amount read is returned.
  125. *
  126. * \param ctx Socket
  127. * \param buf The buffer to read from
  128. * \param len The length of the buffer
  129. *
  130. * \return This function returns the number of bytes sent,
  131. * or a non-zero error code; POLARSSL_ERR_NET_WANT_WRITE
  132. * indicates write() is blocking.
  133. */
  134. int net_send( void *ctx, const unsigned char *buf, size_t len );
  135. /**
  136. * \brief Gracefully shutdown the connection
  137. *
  138. * \param fd The socket to close
  139. */
  140. void net_close( int fd );
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* net.h */