simplify fetch_from_buf; cull idle dnsworkers.

svn:r354
This commit is contained in:
Roger Dingledine 2003-06-25 00:31:41 +00:00
parent ad917e7788
commit dbf3435cde
7 changed files with 32 additions and 59 deletions

View File

@ -135,19 +135,13 @@ int write_to_buf(char *string, int string_len,
int fetch_from_buf(char *string, int string_len, int fetch_from_buf(char *string, int string_len,
char **buf, int *buflen, int *buf_datalen) { char **buf, int *buflen, int *buf_datalen) {
/* if there are string_len bytes in buf, write them onto string, /* There must be string_len bytes in buf; write them onto string,
* then memmove buf back (that is, remove them from buf). * then memmove buf back (that is, remove them from buf).
* *
* If there are not enough bytes on the buffer to fill string, return -1.
*
* Return the number of bytes still on the buffer. */ * Return the number of bytes still on the buffer. */
assert(string && buf && *buf && buflen && buf_datalen); assert(string && buf && *buf && buflen && buf_datalen);
assert(string_len <= *buf_datalen); /* make sure we don't ask for too much */
/* this is the point where you would grow the buffer, if you want to */
if(string_len > *buf_datalen) /* we want too much. sorry. */
return -1;
memcpy(string,*buf,string_len); memcpy(string,*buf,string_len);
*buf_datalen -= string_len; *buf_datalen -= string_len;

View File

@ -568,9 +568,7 @@ repeat_connection_package_raw_inbuf:
cell.length = amount_to_process; cell.length = amount_to_process;
} }
if(connection_fetch_from_buf(cell.payload+RELAY_HEADER_SIZE, connection_fetch_from_buf(cell.payload+RELAY_HEADER_SIZE, cell.length, conn);
cell.length, conn) < 0)
return -1;
circ = circuit_get_by_conn(conn); circ = circuit_get_by_conn(conn);
if(!circ) { if(!circ) {
@ -695,9 +693,7 @@ int connection_process_cell_from_inbuf(connection_t *conn) {
if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */ if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn) < 0) { connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn);
return -1;
}
#if 0 #if 0
printf("Cell header crypttext: "); printf("Cell header crypttext: ");

View File

@ -19,8 +19,7 @@ int ap_handshake_process_socks(connection_t *conn) {
if(conn->inbuf_datalen < sizeof(socks4_t)) /* basic info available? */ if(conn->inbuf_datalen < sizeof(socks4_t)) /* basic info available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf((char *)&socks4_info,sizeof(socks4_t),conn) < 0) connection_fetch_from_buf((char *)&socks4_info,sizeof(socks4_t),conn);
return -1;
log_fn(LOG_DEBUG,"Successfully read socks info."); log_fn(LOG_DEBUG,"Successfully read socks info.");
@ -67,8 +66,7 @@ int ap_handshake_process_socks(connection_t *conn) {
ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT); ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
return -1; return -1;
} }
if(connection_fetch_from_buf(tmpbuf,amt,conn) < 0) connection_fetch_from_buf(tmpbuf,amt,conn);
return -1;
conn->read_username = 1; conn->read_username = 1;
log_fn(LOG_DEBUG,"Successfully read username."); log_fn(LOG_DEBUG,"Successfully read username.");
} }
@ -82,8 +80,7 @@ int ap_handshake_process_socks(connection_t *conn) {
ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT); ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
return -1; return -1;
} }
if(connection_fetch_from_buf(tmpbuf,amt,conn) < 0) connection_fetch_from_buf(tmpbuf,amt,conn);
return -1;
conn->dest_addr = strdup(tmpbuf); conn->dest_addr = strdup(tmpbuf);
log_fn(LOG_NOTICE,"successfully read dest addr '%s'", log_fn(LOG_NOTICE,"successfully read dest addr '%s'",

View File

@ -372,9 +372,7 @@ or_handshake_client_process_auth(connection_t *conn) {
if(conn->inbuf_datalen < 128) /* entire response available? */ if(conn->inbuf_datalen < 128) /* entire response available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf(cipher,128,conn) < 0) { connection_fetch_from_buf(cipher,128,conn);
return -1;
}
log(LOG_DEBUG,"or_handshake_client_process_auth() : Received auth."); log(LOG_DEBUG,"or_handshake_client_process_auth() : Received auth.");
/* decrypt response */ /* decrypt response */
@ -483,9 +481,7 @@ or_handshake_server_process_auth(connection_t *conn) {
if(conn->inbuf_datalen < 128) /* entire response available? */ if(conn->inbuf_datalen < 128) /* entire response available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf(cipher,128,conn) < 0) { connection_fetch_from_buf(cipher,128,conn);
return -1;
}
log(LOG_DEBUG,"or_handshake_server_process_auth() : Received auth."); log(LOG_DEBUG,"or_handshake_server_process_auth() : Received auth.");
/* decrypt response */ /* decrypt response */
@ -632,9 +628,7 @@ or_handshake_server_process_nonce(connection_t *conn) {
if(conn->inbuf_datalen < 128) /* entire response available? */ if(conn->inbuf_datalen < 128) /* entire response available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf(cipher,128,conn) < 0) { connection_fetch_from_buf(cipher,128,conn);
return -1;
}
log(LOG_DEBUG,"or_handshake_server_process_nonce() : Received auth."); log(LOG_DEBUG,"or_handshake_server_process_nonce() : Received auth.");
/* decrypt response */ /* decrypt response */

View File

@ -186,9 +186,7 @@ int directory_handle_command(connection_t *conn) {
return 0; /* not yet */ return 0; /* not yet */
} }
if(connection_fetch_from_buf(buf,strlen(getstring),conn) < 0) { connection_fetch_from_buf(buf,strlen(getstring),conn);
return -1;
}
if(strncasecmp(buf,getstring,strlen("GET / HTTP/"))) { if(strncasecmp(buf,getstring,strlen("GET / HTTP/"))) {
log_fn(LOG_DEBUG,"Command doesn't seem to be a get. Closing,"); log_fn(LOG_DEBUG,"Command doesn't seem to be a get. Closing,");
@ -224,10 +222,7 @@ int directory_handle_reading(connection_t *conn) {
if(amt < 0) /* not there yet */ if(amt < 0) /* not there yet */
return 0; return 0;
headers = tor_malloc(amt+1); headers = tor_malloc(amt+1);
if(connection_fetch_from_buf(headers,amt,conn) < 0) { connection_fetch_from_buf(headers,amt,conn);
log_fn(LOG_DEBUG,"fetch_from_buf failed (reading headers).");
return -1;
}
headers[amt] = 0; /* null terminate it, */ headers[amt] = 0; /* null terminate it, */
free(headers); /* and then throw it away */ free(headers); /* and then throw it away */
reading_headers = 0; reading_headers = 0;
@ -243,10 +238,7 @@ int directory_handle_reading(connection_t *conn) {
log_fn(LOG_DEBUG,"Pulling %d bytes in at offset %d.", log_fn(LOG_DEBUG,"Pulling %d bytes in at offset %d.",
amt, directorylen); amt, directorylen);
if(connection_fetch_from_buf(the_directory+directorylen,amt,conn) < 0) { connection_fetch_from_buf(the_directory+directorylen,amt,conn);
log_fn(LOG_DEBUG,"fetch_from_buf failed (reading dir).");
return -1;
}
directorylen += amt; directorylen += amt;

View File

@ -14,6 +14,7 @@
#define MAX_DNSWORKERS 50 #define MAX_DNSWORKERS 50
#define MIN_DNSWORKERS 3 #define MIN_DNSWORKERS 3
#define MAX_IDLE_DNSWORKERS 10
int num_workers=0; int num_workers=0;
int num_workers_busy=0; int num_workers_busy=0;
@ -118,21 +119,14 @@ int dns_resolve(connection_t *exitconn) {
static int dns_assign_to_worker(connection_t *exitconn) { static int dns_assign_to_worker(connection_t *exitconn) {
connection_t *dnsconn; connection_t *dnsconn;
unsigned char len; unsigned char len;
struct hostent *rent;
spawn_enough_workers(); /* respawn here, to be sure there are enough */ spawn_enough_workers(); /* respawn here, to be sure there are enough */
dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE); dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
if(!dnsconn) { if(!dnsconn) {
log(LOG_INFO,"dns_assign_to_worker(): no idle dns workers. Doing it myself."); log(LOG_INFO,"dns_assign_to_worker(): no idle dns workers. Failing.");
return -1;
/* short version which does it all right here */
rent = gethostbyname(exitconn->address);
if (!rent) {
return dns_found_answer(exitconn->address, 0);
}
return dns_found_answer(exitconn->address, *(uint32_t *)rent->h_addr);
} }
dnsconn->address = strdup(exitconn->address); dnsconn->address = strdup(exitconn->address);
@ -217,11 +211,7 @@ int connection_dns_process_inbuf(connection_t *conn) {
return 0; /* not yet */ return 0; /* not yet */
assert(conn->inbuf_datalen == 4); assert(conn->inbuf_datalen == 4);
if(connection_fetch_from_buf((char*)&answer,sizeof(answer),conn) < 0) { connection_fetch_from_buf((char*)&answer,sizeof(answer),conn);
log(LOG_ERR,"connection_dnsworker_process_inbuf(): Broken inbuf. Worker dying.");
/* XXX exitconn's never going to get his answer :( */
return -1;
}
dns_found_answer(conn->address, answer); dns_found_answer(conn->address, answer);
@ -324,6 +314,14 @@ static int dns_spawn_worker(void) {
static void spawn_enough_workers(void) { static void spawn_enough_workers(void) {
int num_workers_needed; /* aim to have 1 more than needed, int num_workers_needed; /* aim to have 1 more than needed,
* but no less than min and no more than max */ * but no less than min and no more than max */
connection_t *dnsconn;
if(num_workers_busy == MAX_DNSWORKERS) {
/* We always want at least one worker idle.
* So find the oldest busy worker and kill it.
*/
}
if(num_workers_busy >= MIN_DNSWORKERS) if(num_workers_busy >= MIN_DNSWORKERS)
num_workers_needed = num_workers_busy+1; num_workers_needed = num_workers_busy+1;
@ -341,7 +339,13 @@ static void spawn_enough_workers(void) {
num_workers++; num_workers++;
} }
/* FFFF this is where we will cull extra workers */ while(num_workers > num_workers_needed+MAX_IDLE_DNSWORKERS) { /* too many idle? */
/* cull excess workers */
dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
assert(dnsconn);
dnsconn->marked_for_close = 1;
num_workers--;
}
} }
/* /*

View File

@ -120,10 +120,6 @@ test_buffers() {
test_memeq(str+10,buf,246); test_memeq(str+10,buf,246);
test_eq(buf_datalen,246); test_eq(buf_datalen,246);
test_eq(-1, fetch_from_buf(str2, 247, &buf, &buflen, &buf_datalen));
test_memeq(str+10,buf,246);
test_eq(buf_datalen, 246);
test_eq(0, fetch_from_buf(str2, 246, &buf, &buflen, &buf_datalen)); test_eq(0, fetch_from_buf(str2, 246, &buf, &buflen, &buf_datalen));
test_memeq(str2, str+10, 246); test_memeq(str2, str+10, 246);
test_eq(buflen,MAX_BUF_SIZE); test_eq(buflen,MAX_BUF_SIZE);