From c2db2b6accc7888df514261a7240e7759df95a4c Mon Sep 17 00:00:00 2001 From: The Android Open Source Project Date: Fri, 9 Jan 2009 17:51:21 -0800 Subject: auto import from //branches/cupcake/...@125939 --- proxy/proxy_common.c | 75 +++++++++++++++----------------------------- proxy/proxy_common.h | 7 +++-- proxy/proxy_http.c | 30 ++++++------------ proxy/proxy_http_connector.c | 26 +++++---------- proxy/proxy_http_int.h | 10 +++--- proxy/proxy_http_rewriter.c | 21 +++++-------- proxy/proxy_int.h | 18 +++++------ 7 files changed, 68 insertions(+), 119 deletions(-) (limited to 'proxy') diff --git a/proxy/proxy_common.c b/proxy/proxy_common.c index 0e45481..ffe8e22 100644 --- a/proxy/proxy_common.c +++ b/proxy/proxy_common.c @@ -56,7 +56,7 @@ hex_dump( void* base, int size, const char* prefix ) void proxy_connection_init( ProxyConnection* conn, int socket, - struct sockaddr_in* address, + SockAddress* address, ProxyService* service, ProxyConnectionFreeFunc conn_free, ProxyConnectionSelectFunc conn_select, @@ -74,16 +74,12 @@ proxy_connection_init( ProxyConnection* conn, socket_set_nonblock(socket); { - uint32_t ip = ntohl(address->sin_addr.s_addr); - uint16_t port = ntohs(address->sin_port); - int type = socket_get_type(socket); + SocketType type = socket_get_type(socket); snprintf( conn->name, sizeof(conn->name), - "%s:%d.%d.%d.%d:%d(%d)", - (type == SOCK_STREAM) ? "tcp" : "udp", - (ip >> 24) & 255, (ip >> 16) & 255, - (ip >> 8) & 255, ip & 255, port, - socket ); + "%s:%s(%d)", + (type == SOCKET_STREAM) ? "tcp" : "udp", + sock_address_to_string(address), socket ); /* just in case */ conn->name[sizeof(conn->name)-1] = 0; @@ -133,20 +129,17 @@ proxy_connection_send( ProxyConnection* conn, int fd ) } while (avail > 0) { - int n = send(fd, str->s + conn->str_pos, avail, 0); + int n = socket_send(fd, str->s + conn->str_pos, avail); if (n == 0) { PROXY_LOG("%s: connection reset by peer (send)", conn->name); return DATA_ERROR; } if (n < 0) { - if (socket_errno == EINTR) - continue; - - if (socket_errno == EWOULDBLOCK || socket_errno == EAGAIN) + if (errno == EWOULDBLOCK || errno == EAGAIN) return DATA_NEED_MORE; - PROXY_LOG("%s: error: %s", conn->name, socket_errstr()); + PROXY_LOG("%s: error: %s", conn->name, errno_str); return DATA_ERROR; } conn->str_pos += n; @@ -170,20 +163,17 @@ proxy_connection_receive( ProxyConnection* conn, int fd, int wanted ) int n; stralloc_readyplus( str, wanted ); - n = recv(fd, str->s + str->n, wanted, 0); + n = socket_recv(fd, str->s + str->n, wanted); if (n == 0) { PROXY_LOG("%s: connection reset by peer (receive)", conn->name); return DATA_ERROR; } if (n < 0) { - if (socket_errno == EINTR) - continue; - - if (socket_errno == EWOULDBLOCK || socket_errno == EAGAIN) + if (errno == EWOULDBLOCK || errno == EAGAIN) return DATA_NEED_MORE; - PROXY_LOG("%s: error: %s", conn->name, socket_errstr()); + PROXY_LOG("%s: error: %s", conn->name, errno_str); return DATA_ERROR; } @@ -207,20 +197,17 @@ proxy_connection_receive_line( ProxyConnection* conn, int fd ) for (;;) { char c; - int n = recv(fd, &c, 1, 0); + int n = socket_recv(fd, &c, 1); if (n == 0) { PROXY_LOG("%s: disconnected from server", conn->name ); return DATA_ERROR; } if (n < 0) { - if (socket_errno == EINTR) - continue; - - if (socket_errno == EWOULDBLOCK || socket_errno == EAGAIN) { + if (errno == EWOULDBLOCK || errno == EAGAIN) { PROXY_LOG("%s: blocked", conn->name); return DATA_NEED_MORE; } - PROXY_LOG("%s: error: %s", conn->name, socket_errstr()); + PROXY_LOG("%s: error: %s", conn->name, errno_str); return DATA_ERROR; } @@ -336,10 +323,10 @@ proxy_connection_free( ProxyConnection* conn, int -proxy_manager_add( struct sockaddr_in* address, - int sock_type, - ProxyEventFunc ev_func, - void* ev_opaque ) +proxy_manager_add( SockAddress* address, + SocketType sock_type, + ProxyEventFunc ev_func, + void* ev_opaque ) { int n; @@ -506,22 +493,17 @@ proxy_base64_encode( const char* src, int srclen, } int -proxy_resolve_server( struct sockaddr_in* addr, - const char* servername, - int servernamelen, - int serverport ) +proxy_resolve_server( SockAddress* addr, + const char* servername, + int servernamelen, + int serverport ) { - char name0[64], *name = name0; - int result = -1; - struct hostent* host; + char name0[64], *name = name0; + int result = -1; if (servernamelen < 0) servernamelen = strlen(servername); - memset(addr, 0, sizeof(*addr)); - addr->sin_family = AF_INET; - addr->sin_port = htons(serverport); - if (servernamelen >= sizeof(name0)) { name = qemu_malloc(servernamelen+1); if (name == NULL) @@ -531,18 +513,13 @@ proxy_resolve_server( struct sockaddr_in* addr, memcpy(name, servername, servernamelen); name[servernamelen] = 0; - host = gethostbyname(name); - if (host == NULL) { + if (sock_address_init_resolve( addr, name, serverport, 0 ) < 0) { PROXY_LOG("%s: can't resolve proxy server name '%s'", __FUNCTION__, name); goto Exit; } - addr->sin_addr = *(struct in_addr*)host->h_addr; - { - uint32_t a = ntohl(addr->sin_addr.s_addr); - PROXY_LOG("server name '%s' resolved to %d.%d.%d.%d", name, (a>>24)&255, (a>>16)&255,(a>>8)&255,a&255); - } + PROXY_LOG("server name '%s' resolved to %s", name, sock_address_to_string(addr)); result = 0; Exit: diff --git a/proxy/proxy_common.h b/proxy/proxy_common.h index 57f224d..78eddd8 100644 --- a/proxy/proxy_common.h +++ b/proxy/proxy_common.h @@ -12,11 +12,12 @@ #ifndef _PROXY_COMMON_H_ #define _PROXY_COMMON_H_ +#include "sockets.h" + #ifdef _WIN32 #include #else #include -#include #endif /* types and definitions used by all proxy connections */ @@ -61,8 +62,8 @@ typedef struct { * * returns 0 on success, or -1 if there is no proxy service for this type of connection */ -extern int proxy_manager_add( struct sockaddr_in* address, - int sock_type, +extern int proxy_manager_add( SockAddress* address, + SocketType sock_type, ProxyEventFunc ev_func, void* ev_opaque ); diff --git a/proxy/proxy_http.c b/proxy/proxy_http.c index c3d663c..5c185ca 100644 --- a/proxy/proxy_http.c +++ b/proxy/proxy_http.c @@ -29,34 +29,22 @@ http_service_free( HttpService* service ) static ProxyConnection* -http_service_connect( HttpService* service, - int sock_type, - struct sockaddr_in* address ) +http_service_connect( HttpService* service, + SocketType sock_type, + SockAddress* address ) { - uint32_t addr; - int port; - /* the HTTP proxy can only handle TCP connections */ - if (sock_type != SOCK_STREAM) + if (sock_type != SOCKET_STREAM) return NULL; /* if the client tries to directly connect to the proxy, let it do so */ - if (address->sin_addr.s_addr == service->server_addr.sin_addr.s_addr && - address->sin_port == service->server_addr.sin_port) + if (sock_address_equal( address, &service->server_addr )) return NULL; - addr = ntohl(address->sin_addr.s_addr); - port = ntohs(address->sin_port); - - PROXY_LOG("%s: trying to connect to %d.%d.%d.%d on port %d", - __FUNCTION__, - (addr >> 24) & 255, - (addr >> 16) & 255, - (addr >> 8) & 255, - addr & 255, - port ); + PROXY_LOG("%s: trying to connect to %s", + __FUNCTION__, sock_address_to_string(address)); - if (port == 80) { + if (sock_address_get_port(address) == 80) { /* use the rewriter for HTTP */ PROXY_LOG("%s: using HTTP rewriter", __FUNCTION__); return http_rewriter_connect(service, address); @@ -75,7 +63,7 @@ proxy_http_setup( const char* servername, const ProxyOption* options ) { HttpService* service; - struct sockaddr_in server_addr; + SockAddress server_addr; const ProxyOption* opt_nocache = NULL; const ProxyOption* opt_keepalive = NULL; const ProxyOption* opt_auth_user = NULL; diff --git a/proxy/proxy_http_connector.c b/proxy/proxy_http_connector.c index 1b2ba3e..7bf2f53 100644 --- a/proxy/proxy_http_connector.c +++ b/proxy/proxy_http_connector.c @@ -52,35 +52,25 @@ connection_init( Connection* conn ) HttpService* service = (HttpService*) conn->root->service; ProxyConnection* root = conn->root; stralloc_t* str = root->str; - int ret; - uint32_t address = ntohl(root->address.sin_addr.s_addr); - int port = ntohs(root->address.sin_port); proxy_connection_rewind(root); - stralloc_add_format(str, "CONNECT %d.%d.%d.%d:%d HTTP/" HTTP_VERSION "\r\n", - (address >> 24) & 0xff, (address >> 16) & 0xff, - (address >> 8) & 0xff, address & 0xff, port); + stralloc_add_format(str, "CONNECT %s HTTP/" HTTP_VERSION "\r\n", + sock_address_to_string(&root->address)); stralloc_add_bytes(str, service->footer, service->footer_len); - do { - ret = connect( root->socket, - (struct sockaddr*) &service->server_addr, - sizeof(service->server_addr) ); - } while (ret < 0 && socket_errno == EINTR); - - if (ret == 0) { + if (!socket_connect( root->socket, &service->server_addr )) { /* immediate connection ?? */ conn->state = STATE_SEND_HEADER; PROXY_LOG("%s: immediate connection", root->name); } else { - if (socket_errno == EINPROGRESS || socket_errno == EWOULDBLOCK) { + if (errno == EINPROGRESS || errno == EWOULDBLOCK) { conn->state = STATE_CONNECTING; PROXY_LOG("%s: connecting", root->name); } else { - PROXY_LOG("%s: cannot connect to proxy: %s", root->name, socket_errstr()); + PROXY_LOG("%s: cannot connect to proxy: %s", root->name, errno_str); return -1; } } @@ -183,13 +173,13 @@ connection_poll( ProxyConnection* root, ProxyConnection* -http_connector_connect( HttpService* service, - struct sockaddr_in* address ) +http_connector_connect( HttpService* service, + SockAddress* address ) { Connection* conn; int s; - s = socket(AF_INET, SOCK_STREAM, 0); + s = socket_create_inet( SOCKET_STREAM ); if (s < 0) return NULL; diff --git a/proxy/proxy_http_int.h b/proxy/proxy_http_int.h index d0a6bc0..6daa9cb 100644 --- a/proxy/proxy_http_int.h +++ b/proxy/proxy_http_int.h @@ -18,7 +18,7 @@ /* the HttpService object */ typedef struct HttpService { ProxyService root[1]; - struct sockaddr_in server_addr; /* server address and port */ + SockAddress server_addr; /* server address and port */ char* footer; /* the footer contains the static parts of the */ int footer_len; /* connection header, we generate it only once */ char footer0[512]; @@ -26,13 +26,13 @@ typedef struct HttpService { /* create a CONNECT connection (for port != 80) */ extern ProxyConnection* http_connector_connect( - HttpService* service, - struct sockaddr_in* address ); + HttpService* service, + SockAddress* address ); /* create a HTTP rewriting connection (for port == 80) */ extern ProxyConnection* http_rewriter_connect( - HttpService* service, - struct sockaddr_in* address ); + HttpService* service, + SockAddress* address ); #endif /* _PROXY_HTTP_INT_H */ diff --git a/proxy/proxy_http_rewriter.c b/proxy/proxy_http_rewriter.c index 3e98557..f0bfbbc 100644 --- a/proxy/proxy_http_rewriter.c +++ b/proxy/proxy_http_rewriter.c @@ -364,23 +364,16 @@ rewrite_connection_init( RewriteConnection* conn ) { HttpService* service = (HttpService*) conn->root->service; ProxyConnection* root = conn->root; - int ret; conn->slirp_fd = -1; conn->state = STATE_CONNECTING; - do { - ret = connect( root->socket, - (struct sockaddr*) &service->server_addr, - sizeof(service->server_addr) ); - } while (ret < 0 && socket_errno == EINTR); - - if (ret < 0) { - if (socket_errno == EINPROGRESS || socket_errno == EWOULDBLOCK) { + if (socket_connect( root->socket, &service->server_addr ) < 0) { + if (errno == EINPROGRESS || errno == EWOULDBLOCK) { PROXY_LOG("%s: connecting", conn->root->name); } else { - PROXY_LOG("%s: cannot connect to proxy: %s", root->name, socket_errstr()); + PROXY_LOG("%s: cannot connect to proxy: %s", root->name, errno_str); return -1; } } @@ -401,7 +394,7 @@ rewrite_connection_create_sockets( RewriteConnection* conn ) if (socket_pair( &slirp_1, &conn->slirp_fd ) < 0) { PROXY_LOG("%s: coult not create socket pair: %s", - root->name, socket_errstr()); + root->name, errno_str); return -1; } @@ -1102,13 +1095,13 @@ rewrite_connection_poll( ProxyConnection* root, ProxyConnection* -http_rewriter_connect( HttpService* service, - struct sockaddr_in* address ) +http_rewriter_connect( HttpService* service, + SockAddress* address ) { RewriteConnection* conn; int s; - s = socket(AF_INET, SOCK_STREAM, 0); + s = socket_create_inet( SOCKET_STREAM ); if (s < 0) return NULL; diff --git a/proxy/proxy_int.h b/proxy/proxy_int.h index e71d9d6..9bdac5c 100644 --- a/proxy/proxy_int.h +++ b/proxy/proxy_int.h @@ -67,7 +67,7 @@ typedef void (*ProxyConnectionPollFunc) ( ProxyConnection* conn, /* root ProxyConnection object */ struct ProxyConnection { int socket; - struct sockaddr_in address; /* for debugging */ + SockAddress address; /* for debugging */ ProxyConnection* next; ProxyConnection* prev; ProxyEventFunc ev_func; @@ -95,7 +95,7 @@ struct ProxyConnection { extern void proxy_connection_init( ProxyConnection* conn, int socket, - struct sockaddr_in* address, + SockAddress* address, ProxyService* service, ProxyConnectionFreeFunc conn_free, ProxyConnectionSelectFunc conn_select, @@ -172,10 +172,10 @@ proxy_base64_encode( const char* src, int srclen, char* dst, int dstlen ); extern int -proxy_resolve_server( struct sockaddr_in* addr, - const char* servername, - int servernamelen, - int serverport ); +proxy_resolve_server( SockAddress* addr, + const char* servername, + int servernamelen, + int serverport ); /* a ProxyService is really a proxy server and associated options */ @@ -184,9 +184,9 @@ 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_type, - struct sockaddr_in* address ); +typedef ProxyConnection* (*ProxyServiceConnectFunc)( void* opaque, + SocketType socket_type, + const SockAddress* address ); struct ProxyService { void* opaque; -- cgit v1.1