aboutsummaryrefslogtreecommitdiffstats
path: root/proxy/proxy_int.h
diff options
context:
space:
mode:
Diffstat (limited to 'proxy/proxy_int.h')
-rw-r--r--proxy/proxy_int.h146
1 files changed, 101 insertions, 45 deletions
diff --git a/proxy/proxy_int.h b/proxy/proxy_int.h
index 9d91169..e71d9d6 100644
--- a/proxy/proxy_int.h
+++ b/proxy/proxy_int.h
@@ -14,6 +14,7 @@
#include "proxy_common.h"
#include "sockets.h"
+#include "android_utils.h"
extern int proxy_log;
@@ -24,11 +25,44 @@ proxy_LOG(const char* fmt, ...);
do { if (proxy_log) proxy_LOG(__VA_ARGS__); } while (0)
+/* ProxySelect is used to handle events */
+
+enum {
+ PROXY_SELECT_READ = (1 << 0),
+ PROXY_SELECT_WRITE = (1 << 1),
+ PROXY_SELECT_ERROR = (1 << 2)
+};
+
+typedef struct {
+ int* pcount;
+ fd_set* reads;
+ fd_set* writes;
+ fd_set* errors;
+} ProxySelect;
+
+extern void proxy_select_set( ProxySelect* sel,
+ int fd,
+ unsigned flags );
+
+extern unsigned proxy_select_poll( ProxySelect* sel, int fd );
+
+
/* sockets proxy manager internals */
typedef struct ProxyConnection ProxyConnection;
typedef struct ProxyService ProxyService;
+/* free a given proxified connection */
+typedef void (*ProxyConnectionFreeFunc) ( ProxyConnection* conn );
+
+/* modify the ProxySelect to tell which events to listen to */
+typedef void (*ProxyConnectionSelectFunc) ( ProxyConnection* conn,
+ ProxySelect* sel );
+
+/* action a proxy connection when select() returns certain events for its socket */
+typedef void (*ProxyConnectionPollFunc) ( ProxyConnection* conn,
+ ProxySelect* sel );
+
/* root ProxyConnection object */
struct ProxyConnection {
@@ -42,51 +76,93 @@ struct ProxyConnection {
/* the following is useful for all types of services */
char name[64]; /* for debugging purposes */
- int buffer_pos;
- int buffer_len;
- char* buffer;
- char buffer0[ 1024 ];
- /* rest of data depend on ProxyService */
+ stralloc_t str[1]; /* network buffer (dynamic) */
+ int str_pos; /* see proxy_connection_send() */
+ int str_sent; /* see proxy_connection_send() */
+ int str_recv; /* see proxy_connection_receive() */
+
+ /* connection methods */
+ ProxyConnectionFreeFunc conn_free;
+ ProxyConnectionSelectFunc conn_select;
+ ProxyConnectionPollFunc conn_poll;
+
+ /* rest of data depend on exact implementation */
};
extern void
-proxy_connection_init( ProxyConnection* conn,
- int socket,
- struct sockaddr_in* address,
- ProxyService* service );
+proxy_connection_init( ProxyConnection* conn,
+ int socket,
+ struct sockaddr_in* address,
+ ProxyService* service,
+ ProxyConnectionFreeFunc conn_free,
+ ProxyConnectionSelectFunc conn_select,
+ ProxyConnectionPollFunc conn_poll );
extern void
proxy_connection_done( ProxyConnection* conn );
+/* free the proxy connection object. this will also
+ * close the corresponding socket unless the
+ * 'keep_alive' flag is set to TRUE.
+ */
extern void
proxy_connection_free( ProxyConnection* conn,
+ int keep_alive,
ProxyEvent event );
-/* tries to send data from the connection's buffer to the proxy.
- * returns 1 when all data has been sent (i.e. buffer_pos == buffer_len),
- * 0 if there is still some data to send, or -1 in case of error
+/* status of data transfer operations */
+typedef enum {
+ DATA_ERROR = -1,
+ DATA_NEED_MORE = 0,
+ DATA_COMPLETED = 1
+} DataStatus;
+
+/* try to send data from the connection's buffer to a socket.
+ * starting from offset conn->str_pos in the buffer
+ *
+ * returns DATA_COMPLETED if everything could be written
+ * returns DATA_ERROR for a socket disconnection or error
+ * returns DATA_NEED_MORE if all data could not be sent.
+ *
+ * on exit, conn->str_sent contains the number of bytes
+ * that were really sent. conn->str_pos will be incremented
+ * by conn->str_sent as well.
+ *
+ * note that in case of success (DATA_COMPLETED), this also
+ * performs a proxy_connection_rewind which sets conn->str_pos
+ * to 0.
*/
-extern int
-proxy_connection_send( ProxyConnection* conn );
-
-/* tries to receive data from the connection's buffer from the proxy
- * returns 1 when all data has been received (buffer_pos == buffer_len)
- * returns 0 if there is still some data to receive
- * returns -1 in case of error
+extern DataStatus
+proxy_connection_send( ProxyConnection* conn, int fd );
+
+/* try to read 'wanted' bytes into conn->str from a socket
+ *
+ * returns DATA_COMPLETED if all bytes could be read
+ * returns DATA_NEED_MORE if not all bytes could be read
+ * returns DATA_ERROR in case of socket disconnection or error
+ *
+ * on exit, the amount of data received is in conn->str_recv
*/
-extern int
-proxy_connection_receive( ProxyConnection* conn );
+extern DataStatus
+proxy_connection_receive( ProxyConnection* conn, int fd, int wanted );
-/* tries to receive a line of text from the proxy
+/* tries to receive a line of text from the proxy.
+ * when an entire line is read, the trailing \r\n is stripped
+ * and replaced by a terminating zero. str->n will be the
+ * lenght of the line, exclusing the terminating zero.
* returns 1 when a line has been received
* returns 0 if there is still some data to receive
* returns -1 in case of error
*/
-extern int
-proxy_connection_receive_line( ProxyConnection* conn );
+extern DataStatus
+proxy_connection_receive_line( ProxyConnection* conn, int fd );
+
+/* rewind the string buffer for a new operation */
+extern void
+proxy_connection_rewind( ProxyConnection* conn );
/* base64 encode a source string, returns size of encoded result,
* or -1 if there was not enough room in the destination buffer
@@ -103,38 +179,19 @@ proxy_resolve_server( struct sockaddr_in* addr,
/* a ProxyService is really a proxy server and associated options */
-enum {
- PROXY_SELECT_READ = (1 << 0),
- PROXY_SELECT_WRITE = (1 << 1),
- PROXY_SELECT_ERROR = (1 << 2)
-};
-
/* destroy a given proxy service */
typedef void (*ProxyServiceFreeFunc) ( void* opaque );
/* tries to create a new proxified connection, returns NULL if the service can't
* handle this address */
typedef ProxyConnection* (*ProxyServiceConnectFunc)( void* opaque,
- int socket,
+ int socket_type,
struct sockaddr_in* address );
-/* free a given proxified connection */
-typedef void (*ProxyConnectionFreeFunc) ( ProxyConnection* conn );
-
-/* return flags corresponding to the select() events to wait to a proxified connection */
-typedef unsigned (*ProxyConnectionSelectFunc) ( ProxyConnection* conn );
-
-/* action a proxy connection when select() returns certain events for its socket */
-typedef void (*ProxyConnectionPollFunc) ( ProxyConnection* conn,
- unsigned select_flags );
-
struct ProxyService {
void* opaque;
ProxyServiceFreeFunc serv_free;
ProxyServiceConnectFunc serv_connect;
- ProxyConnectionFreeFunc conn_free;
- ProxyConnectionSelectFunc conn_select;
- ProxyConnectionPollFunc conn_poll;
};
extern int
@@ -142,4 +199,3 @@ proxy_manager_add_service( ProxyService* service );
#endif /* _PROXY_INT_H */
-