jnetlib.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2006 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: jnetlib.h - JNL main include file (not really necessary).
  6. **
  7. ** For documentation, look at the following files:
  8. ** Generic network initialization: netinc.h
  9. ** DNS: asyncdns.h
  10. ** TCP connections: connection.h
  11. ** HTTP GET connections: httpget.h
  12. ** TCP listen: listen.h
  13. **
  14. ** license:
  15. **
  16. ** This software is provided 'as-is', without any express or implied
  17. ** warranty. In no event will the authors be held liable for any damages
  18. ** arising from the use of this software.
  19. **
  20. ** Permission is granted to anyone to use this software for any purpose,
  21. ** including commercial applications, and to alter it and redistribute it
  22. ** freely, subject to the following restrictions:
  23. **
  24. ** 1. The origin of this software must not be misrepresented; you must not
  25. ** claim that you wrote the original software. If you use this software
  26. ** in a product, an acknowledgment in the product documentation would be
  27. ** appreciated but is not required.
  28. ** 2. Altered source versions must be plainly marked as such, and must not be
  29. ** misrepresented as being the original software.
  30. ** 3. This notice may not be removed or altered from any source distribution.
  31. **
  32. */
  33. #ifndef _JNETLIB_H_
  34. #define _JNETLIB_H_
  35. #include "netinc.h"
  36. #include "../foundation/types.h"
  37. #include "jnetlib_defines.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #ifdef _MSC_VER
  42. #ifdef JNETLIB_EXPORTS
  43. #define JNL_API __declspec(dllexport)
  44. #else
  45. #define JNL_API __declspec(dllimport)
  46. #endif
  47. #elif defined(__ANDROID__) || defined(__APPLE__)
  48. #define JNL_API __attribute__ ((visibility("default")))
  49. #elif defined(__linux__)
  50. #if __GNUC__ >= 4
  51. #define JNL_API __attribute__ ((visibility ("default")))
  52. #else
  53. #define JNL_API
  54. #endif
  55. #else
  56. #error port me
  57. #endif
  58. /* these are reference counted. so make sure to match init/quit calls. */
  59. JNL_API int jnl_init();
  60. JNL_API void jnl_quit();
  61. /* ----- Connection ----- */
  62. JNL_API jnl_connection_t jnl_connection_create( jnl_dns_t dns, size_t sendbufsize, size_t recvbufsize );
  63. JNL_API jnl_connection_t jnl_sslconnection_create( jnl_dns_t dns, size_t sendbufsize, size_t recvbufsize );
  64. JNL_API void jnl_connection_run( jnl_connection_t connection, size_t max_send_bytes, size_t max_receive_bytes, size_t *bytes_sent, size_t *bytes_received );
  65. JNL_API int jnl_connection_get_state( jnl_connection_t connection );
  66. JNL_API size_t jnl_connection_send_bytes_available( jnl_connection_t connection );
  67. JNL_API size_t jnl_connection_receive_bytes_available( jnl_connection_t connection );
  68. JNL_API int jnl_connection_receive_line( jnl_connection_t _connection, void *bytes, size_t size );
  69. JNL_API int jnl_connection_send( jnl_connection_t connection, const void *bytes, size_t size );
  70. JNL_API int jnl_connection_send_string( jnl_connection_t connection, const char *str );
  71. JNL_API size_t jnl_connection_receive( jnl_connection_t connection, void *bytes, size_t size );
  72. JNL_API size_t jnl_connection_send_bytes_in_queue( jnl_connection_t connection );
  73. JNL_API void jnl_connection_release( jnl_connection_t connection );
  74. JNL_API size_t jnl_connection_receive_lines_available( jnl_connection_t connection );
  75. JNL_API void jnl_connection_close( jnl_connection_t _connection, int fast );
  76. JNL_API void jnl_connection_connect( jnl_connection_t connection, const char *hostname, int port );
  77. JNL_API const char *jnl_connection_get_error( jnl_connection_t connection );
  78. JNL_API size_t jnl_connection_peek( jnl_connection_t _connection, void *bytes, size_t size );
  79. /* ----- UDP ----- */
  80. JNL_API int jnl_udp_create_multicast_listener( jnl_udp_t *connection, const char *mcast_ip, unsigned short port );
  81. JNL_API void jnl_udp_release( jnl_udp_t connection );
  82. JNL_API void jnl_udp_run( jnl_udp_t connection, size_t max_send_bytes, size_t max_recv_bytes, size_t *bytes_sent, size_t *bytes_rcvd );
  83. JNL_API size_t jnl_udp_recv_bytes( jnl_udp_t connection, void *buf, size_t len );
  84. JNL_API int jnl_udp_send( jnl_udp_t connection, const void *bytes, size_t size );
  85. JNL_API void jnl_udp_set_peer( jnl_udp_t connection, const char *hostname, unsigned short port );
  86. JNL_API void jnl_udp_set_peer_address( jnl_udp_t connection, struct sockaddr *addr, socklen_t length );
  87. // gets the address of whomever sent the last message
  88. JNL_API int jnl_udp_get_address( jnl_udp_t connection, struct sockaddr **addr, socklen_t *length );
  89. /* ----- HTTP ----- */
  90. /* creation/destruction */
  91. JNL_API jnl_http_t jnl_http_create( int recvbufsize, int sendbufsize );
  92. JNL_API int jnl_http_set_recv_buffer_size( jnl_http_t http, size_t new_size ); /* increases the receive buffer size */
  93. JNL_API jnl_http_t jnl_http_retain( jnl_http_t http );
  94. JNL_API void jnl_http_release( jnl_http_t http );
  95. JNL_API jnl_connection_t jnl_http_get_connection( jnl_http_t http );
  96. /* TODO: replace these with a jnl_http_configure(jnl_http_t http) function */
  97. JNL_API void jnl_http_set_persistent( jnl_http_t http );
  98. JNL_API void jnl_http_allow_compression( jnl_http_t http );
  99. JNL_API void jnl_http_allow_accept_all_reply_codes( jnl_http_t http );
  100. /* run & status stuff */
  101. JNL_API void jnl_http_connect( jnl_http_t http, const char *url, int http_version, const char *method );
  102. JNL_API int jnl_http_run( jnl_http_t http );
  103. JNL_API int jnl_http_get_status( jnl_http_t http );
  104. JNL_API int jnl_http_getreplycode( jnl_http_t http );
  105. JNL_API const char *jnl_http_getreply( jnl_http_t http );
  106. /* reading data */
  107. JNL_API size_t jnl_http_get_bytes( jnl_http_t http, void *buf, size_t len );
  108. JNL_API size_t jnl_http_peek_bytes( jnl_http_t http, void *buf, size_t len );
  109. JNL_API size_t jnl_http_bytes_available( jnl_http_t http );
  110. JNL_API uint64_t jnl_http_content_length( jnl_http_t http );
  111. /* HTTP headers */
  112. JNL_API const char *jnl_http_getheader( jnl_http_t http, const char *header );
  113. JNL_API void jnl_http_addheader( jnl_http_t http, const char *header );
  114. JNL_API void jnl_http_addheadervalue( jnl_http_t http, const char *header, const char *value );
  115. JNL_API void jnl_http_reset_headers( jnl_http_t http );
  116. JNL_API const char *jnl_http_get_all_headers( jnl_http_t http );
  117. /* other information */
  118. JNL_API const char *jnl_http_get_url( jnl_http_t http );
  119. JNL_API void jnl_http_set_proxy( const char *proxy );
  120. /* ----- HTTP Request Parsing ----- */
  121. JNL_API int jnl_http_request_create( jnl_http_request_t *http, jnl_connection_t connection );
  122. JNL_API void jnl_http_request_release( jnl_http_request_t http );
  123. JNL_API int jnl_http_request_run( jnl_http_request_t http );
  124. JNL_API int jnl_htt_request_get_keep_alive( jnl_http_request_t http );
  125. JNL_API const char *jnl_http_request_get_header( jnl_http_request_t http, const char *header );
  126. JNL_API void jnl_http_request_reset( jnl_http_request_t http );
  127. JNL_API void jnl_http_request_addheader( jnl_http_request_t http, const char *header );
  128. JNL_API void jnl_http_request_set_reply_string( jnl_http_request_t http, const char *reply );
  129. JNL_API void jnl_http_request_send_reply( jnl_http_request_t http );
  130. JNL_API const char *jnl_http_request_get_uri( jnl_http_request_t http );
  131. JNL_API const char *jnl_http_request_get_parameter( jnl_http_request_t _http, const char *parameter );
  132. JNL_API jnl_connection_t jnl_http_request_get_connection( jnl_http_request_t http );
  133. JNL_API const char *jnl_http_request_get_method( jnl_http_request_t http );
  134. /* ----- HTTPU Request Parsing ----- */
  135. JNL_API int jnl_httpu_request_create( jnl_httpu_request_t *httpu );
  136. JNL_API void jnl_httpu_request_release( jnl_httpu_request_t httpu );
  137. JNL_API int jnl_httpu_request_process( jnl_httpu_request_t httpu, jnl_udp_t udp );
  138. JNL_API const char *jnl_httpu_request_get_method( jnl_httpu_request_t httpu );
  139. JNL_API const char *jnl_httpu_request_get_uri( jnl_httpu_request_t httpu );
  140. JNL_API const char *jnl_httpu_request_get_header( jnl_httpu_request_t httpu, const char *header );
  141. /* ----- DNS ------ */
  142. JNL_API int jnl_dns_create( jnl_dns_t *dns );
  143. JNL_API void jnl_dns_release( jnl_dns_t dns );
  144. JNL_API int jnl_dns_resolve( jnl_dns_t dns, const char *hostname, unsigned short port, struct addrinfo **addr, int sockettype );
  145. // when you call jnl_dns_resolve_now, you need to call jnl_dns_freeaddrinfo
  146. JNL_API int jnl_dns_resolve_now( const char *hostname, unsigned short port, struct addrinfo **addr, int sockettype );
  147. JNL_API void jnl_dns_freeaddrinfo( struct addrinfo *addr );
  148. JNL_API void jnl_dns_gethostname( char *name, size_t cch );
  149. JNL_API void jnl_dns_ntop( int af, const void *src, char *dst, socklen_t size );
  150. /* listen */
  151. JNL_API int jnl_listen_create( jnl_listen_t *listen, unsigned short port );
  152. JNL_API int jnl_listen_create_from_address( jnl_listen_t *listen, struct addrinfo *addr, size_t index );
  153. JNL_API void jnl_listen_release( jnl_listen_t listen );
  154. JNL_API jnl_connection_t jnl_listen_get_connection( jnl_listen_t listen );
  155. JNL_API unsigned short jnl_listen_get_port( jnl_listen_t listen );
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif//_JNETLIB_H_