2002-05-24 02:23:29 +08:00
|
|
|
/* $Id: conns.c,v 1.11 2002-05-23 18:23:29 rjkaes Exp $
|
2001-10-26 00:58:09 +08:00
|
|
|
*
|
|
|
|
* Create and free the connection structure. One day there could be
|
|
|
|
* other connnection related tasks put here, but for now the header
|
|
|
|
* file and this file are only used for create/free functions and the
|
|
|
|
* connection structure definition.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinyproxy.h"
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "conns.h"
|
2002-05-24 02:23:29 +08:00
|
|
|
#include "heap.h"
|
2001-10-26 00:58:09 +08:00
|
|
|
#include "stats.h"
|
|
|
|
|
2002-04-08 05:32:01 +08:00
|
|
|
struct conn_s *
|
2002-05-24 02:23:29 +08:00
|
|
|
initialize_conn(int client_fd, const char* ipaddr, const char* string_addr)
|
2001-10-26 00:58:09 +08:00
|
|
|
{
|
2002-04-08 05:32:01 +08:00
|
|
|
struct conn_s *connptr;
|
|
|
|
struct buffer_s *cbuffer, *sbuffer;
|
|
|
|
|
|
|
|
assert(client_fd >= 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the memory for all the internal componets
|
|
|
|
*/
|
|
|
|
cbuffer = new_buffer();
|
|
|
|
sbuffer = new_buffer();
|
|
|
|
|
|
|
|
if (!cbuffer || !sbuffer)
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the space for the conn_s structure itself.
|
|
|
|
*/
|
|
|
|
connptr = safemalloc(sizeof(struct conn_s));
|
|
|
|
if (!connptr)
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
connptr->client_fd = client_fd;
|
|
|
|
connptr->server_fd = -1;
|
|
|
|
|
|
|
|
connptr->cbuffer = cbuffer;
|
|
|
|
connptr->sbuffer = sbuffer;
|
2001-10-26 00:58:09 +08:00
|
|
|
|
2002-04-08 05:32:01 +08:00
|
|
|
connptr->request_line = NULL;
|
|
|
|
|
2002-04-15 10:07:27 +08:00
|
|
|
/* These store any error strings */
|
|
|
|
connptr->error_string = NULL;
|
|
|
|
connptr->error_number = -1;
|
|
|
|
|
2001-11-26 06:06:20 +08:00
|
|
|
connptr->connect_method = FALSE;
|
2002-04-19 05:43:53 +08:00
|
|
|
connptr->show_stats = FALSE;
|
2001-10-26 00:58:09 +08:00
|
|
|
|
2001-11-21 09:00:09 +08:00
|
|
|
connptr->protocol.major = connptr->protocol.minor = 0;
|
|
|
|
|
2002-04-12 04:27:51 +08:00
|
|
|
connptr->remote_content_length = -1;
|
|
|
|
|
2002-05-24 02:23:29 +08:00
|
|
|
connptr->client_ip_addr = safestrdup(ipaddr);
|
|
|
|
connptr->client_string_addr = safestrdup(string_addr);
|
|
|
|
|
2001-10-26 00:58:09 +08:00
|
|
|
update_stats(STAT_OPEN);
|
2002-04-08 05:32:01 +08:00
|
|
|
|
|
|
|
return connptr;
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
/*
|
|
|
|
* If we got here, there was a problem allocating memory
|
|
|
|
*/
|
|
|
|
if (cbuffer)
|
|
|
|
delete_buffer(cbuffer);
|
|
|
|
if (sbuffer)
|
|
|
|
delete_buffer(sbuffer);
|
|
|
|
|
|
|
|
return NULL;
|
2001-10-26 00:58:09 +08:00
|
|
|
}
|
|
|
|
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
|
|
|
destroy_conn(struct conn_s *connptr)
|
2001-10-26 00:58:09 +08:00
|
|
|
{
|
2002-04-08 05:32:01 +08:00
|
|
|
assert(connptr != NULL);
|
|
|
|
|
2001-10-26 00:58:09 +08:00
|
|
|
if (connptr->client_fd != -1)
|
|
|
|
close(connptr->client_fd);
|
|
|
|
if (connptr->server_fd != -1)
|
|
|
|
close(connptr->server_fd);
|
|
|
|
|
|
|
|
if (connptr->cbuffer)
|
|
|
|
delete_buffer(connptr->cbuffer);
|
|
|
|
if (connptr->sbuffer)
|
|
|
|
delete_buffer(connptr->sbuffer);
|
|
|
|
|
2002-04-08 05:32:01 +08:00
|
|
|
if (connptr->request_line)
|
|
|
|
safefree(connptr->request_line);
|
|
|
|
|
2002-04-15 10:07:27 +08:00
|
|
|
if (connptr->error_string)
|
|
|
|
safefree(connptr->error_string);
|
|
|
|
|
2002-05-24 02:23:29 +08:00
|
|
|
if (connptr->client_ip_addr)
|
|
|
|
safefree(connptr->client_ip_addr);
|
|
|
|
if (connptr->client_string_addr)
|
|
|
|
safefree(connptr->client_string_addr);
|
|
|
|
|
2001-10-26 00:58:09 +08:00
|
|
|
safefree(connptr);
|
|
|
|
|
|
|
|
update_stats(STAT_CLOSE);
|
|
|
|
}
|