summaryrefslogtreecommitdiffhomepage
path: root/src/conns.c
blob: 433f59afdc2454d9833b1d31508e45dd117c4001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* tinyproxy - A fast light-weight HTTP proxy
 * Copyright (C) 2001 Robert James Kaes <rjkaes@users.sourceforge.net>
 *
 * 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 of the License, 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Create and free the connection structure. One day there could be
 * other connection 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.
 */

#include "tinyproxy.h"

#include "buffer.h"
#include "conns.h"
#include "heap.h"
#include "log.h"
#include "stats.h"

struct conn_s *
initialize_conn (int client_fd, const char *ipaddr, const char *string_addr,
                 const char *sock_ipaddr)
{
  struct conn_s *connptr;
  struct buffer_s *cbuffer, *sbuffer;

  assert (client_fd >= 0);

  /*
   * Allocate the memory for all the internal components
   */
  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;

  connptr->request_line = NULL;

  /* These store any error strings */
  connptr->error_variables = NULL;
  connptr->error_string = NULL;
  connptr->error_number = -1;

  connptr->connect_method = FALSE;
  connptr->show_stats = FALSE;

  connptr->protocol.major = connptr->protocol.minor = 0;

  /* There is _no_ content length initially */
  connptr->content_length.server = connptr->content_length.client = -1;

  connptr->server_ip_addr = sock_ipaddr ? safestrdup (sock_ipaddr) : 0;
  connptr->client_ip_addr = safestrdup (ipaddr);
  connptr->client_string_addr = safestrdup (string_addr);

  connptr->upstream_proxy = NULL;

  update_stats (STAT_OPEN);

#ifdef REVERSE_SUPPORT
  connptr->reversepath = NULL;
#endif

  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;
}

void
destroy_conn (struct conn_s *connptr)
{
  assert (connptr != NULL);

  if (connptr->client_fd != -1)
    if (close (connptr->client_fd) < 0)
      log_message (LOG_INFO, "Client (%d) close message: %s",
                   connptr->client_fd, strerror (errno));
  if (connptr->server_fd != -1)
    if (close (connptr->server_fd) < 0)
      log_message (LOG_INFO, "Server (%d) close message: %s",
                   connptr->server_fd, strerror (errno));

  if (connptr->cbuffer)
    delete_buffer (connptr->cbuffer);
  if (connptr->sbuffer)
    delete_buffer (connptr->sbuffer);

  if (connptr->request_line)
    safefree (connptr->request_line);

  if (connptr->error_variables)
    hashmap_delete (connptr->error_variables);

  if (connptr->error_string)
    safefree (connptr->error_string);

  if (connptr->server_ip_addr)
    safefree (connptr->server_ip_addr);
  if (connptr->client_ip_addr)
    safefree (connptr->client_ip_addr);
  if (connptr->client_string_addr)
    safefree (connptr->client_string_addr);

#ifdef REVERSE_SUPPORT
  if (connptr->reversepath)
    safefree (connptr->reversepath);
#endif

  safefree (connptr);

  update_stats (STAT_CLOSE);
}