You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-socket.h,v 1.29 2006/05/01 17:56:32 mrcsys Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifndef NANO_HTTP_SOCKET_H
  25. #define NANO_HTTP_SOCKET_H
  26. #include <sys/types.h>
  27. #include <time.h>
  28. #ifdef HAVE_SSL
  29. #include <openssl/ssl.h>
  30. #endif
  31. #ifdef WIN32
  32. #include <winsock2.h>
  33. #else
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #endif
  37. #include <nanohttp/nanohttp-common.h>
  38. #define HSOCKET_FREE -1
  39. /*
  40. Socket definition
  41. */
  42. typedef struct hsocket_t
  43. {
  44. #ifdef WIN32
  45. SOCKET sock;
  46. #else
  47. int sock;
  48. #endif
  49. struct sockaddr_in addr;
  50. void *ssl;
  51. }
  52. hsocket_t; /* end of socket definition */
  53. #ifdef __cplusplus
  54. extern "C"
  55. {
  56. #endif
  57. /**
  58. Initializes the socket modul. This should be called only
  59. once for an application.
  60. @returns This function should always return H_OK.
  61. */
  62. herror_t hsocket_module_init(int argc, char **argv);
  63. /**
  64. Destroys the socket modul. This should be called after
  65. finishing an application.
  66. */
  67. void hsocket_module_destroy(void);
  68. /**
  69. Initializes a given socket object. This function (or
  70. hsokcet_init_ssl) should
  71. be called for every socket before using it.
  72. @param sock the destination socket to initialize.
  73. @see hsocket_init_ssl
  74. @returns This function should always return H_OK.
  75. */
  76. herror_t hsocket_init(hsocket_t * sock);
  77. /**
  78. Destroys and releases a given socket.
  79. @param sock the socket to destroy
  80. */
  81. void hsocket_free(hsocket_t * sock);
  82. /**
  83. Connects to a given host. The hostname can be an IP number
  84. or a humen readable hostname.
  85. @param sock the destonation socket object to use
  86. @param host hostname
  87. @param port port number to connect to
  88. @param ssl whether to open a SSL connection
  89. @returns H_OK if success. One of the followings if fails:<P>
  90. <BR>HSOCKET_ERROR_CREATE
  91. <BR>HSOCKET_ERROR_GET_HOSTNAME
  92. <BR>HSOCKET_ERROR_CONNECT
  93. */
  94. herror_t hsocket_open(hsocket_t * sock, const char *host, int port,
  95. int ssl);
  96. /**
  97. Close a socket connection.
  98. @param sock the socket to close
  99. */
  100. void hsocket_close(hsocket_t * sock);
  101. /**
  102. Binds a socket to a given port number. After bind you
  103. can call hsocket_listen() to listen to the port.
  104. @param sock socket to use.
  105. @param port port number to bind to
  106. @returns H_OK if success. One of the followings if fails:<P>
  107. <BR>HSOCKET_ERROR_CREATE
  108. <BR>HSOCKET_ERROR_BIND
  109. @see hsocket_listen
  110. */
  111. herror_t hsocket_bind(hsocket_t * sock, int port);
  112. /**
  113. Set the socket to the listen mode. You must bind
  114. the socket to a port with hsocket_bind() before
  115. you can listen to the port.
  116. @param sock the socket to use
  117. @returns H_OK if success. One of the followings if fails:<P>
  118. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  119. <BR>HSOCKET_ERROR_LISTEN
  120. */
  121. herror_t hsocket_listen(hsocket_t * sock);
  122. /**
  123. Accepts an incoming socket request. Note that this function
  124. will not return until a socket connection is ready.
  125. @param sock the socket which listents to a port
  126. @param dest the destination socket which will be created
  127. @returns H_OK if success. One of the followings if fails:<P>
  128. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  129. <BR>HSOCKET_ERROR_ACCEPT
  130. */
  131. herror_t hsocket_accept(hsocket_t * sock, hsocket_t * dest);
  132. /**
  133. Sends data throught the socket.
  134. @param sock the socket to use to send the data
  135. @param bytes bytes to send
  136. @param size size of memory to sent pointed by bytes.
  137. @returns H_OK if success. One of the followings if fails:<P>
  138. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  139. <BR>HSOCKET_ERROR_SEND
  140. */
  141. herror_t hsocket_nsend(hsocket_t * sock, const byte_t * bytes, int size);
  142. /**
  143. Sends a string throught the socket
  144. @param sock the socket to use to send the data
  145. @param str the null terminated string to sent
  146. @returns H_OK if success. One of the followings if fails:<P>
  147. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  148. <BR>HSOCKET_ERROR_SEND
  149. */
  150. herror_t hsocket_send(hsocket_t * sock, const char *str);
  151. int hsocket_select_read(int sock, char *buf, size_t len);
  152. /**
  153. Reads data from the socket.
  154. @param sock the socket to read data from
  155. @param buffer the buffer to use to save the readed bytes
  156. @param size the maximum size of bytes to read
  157. @param force if force is 1 then hsocket_read() will wait until
  158. maximum size of bytes (size parameter) was readed. Otherwise
  159. this function will not wait and will return with the bytes
  160. quequed on the socket.
  161. @returns This function will return -1 if an read error was occured.
  162. Otherwise the return value is the size of bytes readed from
  163. the socket.
  164. */
  165. herror_t hsocket_read(hsocket_t * sock, byte_t * buffer, int size,
  166. int force, int *readed);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif