]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_db.cc
gindent
[thirdparty/squid.git] / src / client_db.cc
CommitLineData
8eb58c9c 1
516350ca 2/*
985b7fb7 3 * $Id: client_db.cc,v 1.9 1996/12/20 23:21:26 wessels Exp $
516350ca 4 *
f43e2ec2 5 * DEBUG: section 0 Client Database
516350ca 6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * --------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
c41d4b6d 32#include "squid.h"
33
34typedef struct _client_info {
35 char *key;
36 struct client_info *next;
37 struct in_addr addr;
985b7fb7 38 struct {
39 int result_hist[ERR_MAX];
40 int n_requests;
41 } Http, Icp;
c41d4b6d 42} ClientInfo;
43
44int client_info_sz;
45
19054954 46static HashID client_table = 0;
c41d4b6d 47
48static ClientInfo *clientdbAdd _PARAMS((struct in_addr addr));
49
50static ClientInfo *
51clientdbAdd(struct in_addr addr)
52{
53 ClientInfo *c;
54 c = xcalloc(1, sizeof(ClientInfo));
55 c->key = xstrdup(inet_ntoa(addr));
56 c->addr = addr;
57 hash_join(client_table, (hash_link *) c);
58 meta_data.client_info++;
59 return c;
60}
61
62void
63clientdbInit(void)
64{
19054954 65 if (client_table)
66 return;
0ee4272b 67 client_table = hash_create((int (*)_PARAMS((const char *, const char *))) strcmp,
c41d4b6d 68 229,
69 hash_string);
70 client_info_sz = sizeof(ClientInfo);
71}
72
73void
74clientdbUpdate(struct in_addr addr, log_type log_type, u_short port)
75{
76 char *key = inet_ntoa(addr);
77 ClientInfo *c = (ClientInfo *) hash_lookup(client_table, key);
78 if (c == NULL)
79 c = clientdbAdd(addr);
80 if (c == NULL)
81 debug_trap("clientdbUpdate: Failed to add entry");
985b7fb7 82 if (port == Config.Port.http) {
83 c->Http.n_requests++;
84 c->Http.result_hist[log_type]++;
85 } else if (port == Config.Port.icp) {
86 c->Icp.n_requests++;
87 c->Icp.result_hist[log_type]++;
88 }
c41d4b6d 89}
90
91int
92clientdbDeniedPercent(struct in_addr addr)
93{
94 char *key = inet_ntoa(addr);
7fdce58c 95 int n = 100;
c41d4b6d 96 ClientInfo *c = (ClientInfo *) hash_lookup(client_table, key);
97 if (c == NULL)
98 return 0;
985b7fb7 99 if (c->Icp.n_requests > 100)
100 n = c->Icp.n_requests;
101 return 100 * c->Icp.result_hist[ICP_OP_DENIED] / n;
c41d4b6d 102}
103
8eb58c9c 104void
c41d4b6d 105clientdbDump(StoreEntry * sentry)
106{
107 ClientInfo *c;
108 log_type l;
109 storeAppendPrintf(sentry, "{Cache Clients:\n");
110 c = (ClientInfo *) hash_first(client_table);
111 while (c) {
112 storeAppendPrintf(sentry, "{Address: %s}\n", c->key);
113 storeAppendPrintf(sentry, "{Name: %s}\n", fqdnFromAddr(c->addr));
c41d4b6d 114 storeAppendPrintf(sentry, "{ ICP Requests %d}\n",
985b7fb7 115 c->Icp.n_requests);
116 for (l = LOG_TAG_NONE; l < ERR_MAX; l++) {
117 if (c->Icp.result_hist[l] == 0)
118 continue;
119 storeAppendPrintf(sentry,
120 "{ %-20.20s %7d %3d%%}\n",
121 log_tags[l],
122 c->Icp.result_hist[l],
123 percent(c->Icp.result_hist[l], c->Icp.n_requests));
124 }
125 storeAppendPrintf(sentry, "{ HTTP Requests %d}\n",
126 c->Http.n_requests);
c41d4b6d 127 for (l = LOG_TAG_NONE; l < ERR_MAX; l++) {
985b7fb7 128 if (c->Http.result_hist[l] == 0)
c41d4b6d 129 continue;
130 storeAppendPrintf(sentry,
131 "{ %-20.20s %7d %3d%%}\n",
132 log_tags[l],
985b7fb7 133 c->Http.result_hist[l],
134 percent(c->Http.result_hist[l], c->Http.n_requests));
c41d4b6d 135 }
7fdce58c 136 storeAppendPrintf(sentry, "{}\n");
c41d4b6d 137 c = (ClientInfo *) hash_next(client_table);
138 }
139 storeAppendPrintf(sentry, close_bracket);
140}