Skip to content

Commit a72f2e9

Browse files
committed
use user-agent for searching a client
1 parent 03a0fd5 commit a72f2e9

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

tempesta_fw/client.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ CliHashBucket cli_hash[CLI_HASH_SZ];
5959

6060
static TDB *client_db;
6161

62+
unsigned long tfw_hash_str_len(const TfwStr *str, unsigned long str_len);
63+
6264
/**
6365
* Called when a client socket is closed.
6466
*/
@@ -101,16 +103,16 @@ tfw_client_addr_eq(TdbRec *rec, void *data)
101103
}
102104

103105
/**
104-
* Find a client corresponding to @addr and @cli_addr (e.g. from X-Forwarded-For).
105-
* More advanced identification is possible based on User-Agent,
106-
* Cookie and other HTTP headers.
106+
* Find a client corresponding to @addr, @cli_addr (e.g. from X-Forwarded-For)
107+
* and @user_agent.
107108
*
108109
* The returned TfwClient reference must be released via tfw_client_put()
109110
* when the @sk is closed.
110111
* TODO #515 employ eviction strategy for the table.
111112
*/
112113
TfwClient *
113-
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *))
114+
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, TfwStr *user_agent,
115+
void (*init)(TfwClient *))
114116
{
115117
TfwClientEntry *ent;
116118
TfwClient *cli;
@@ -132,6 +134,8 @@ tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *))
132134
if (cli_addr)
133135
key ^= hash_calc((const char *)&cli_addr->sin6_addr,
134136
sizeof(cli_addr->sin6_addr));
137+
if (user_agent)
138+
key ^= tfw_hash_str_len(user_agent, 256);
135139

136140
len = sizeof(*ent);
137141
rec = tdb_rec_get_alloc(client_db, key, &len, &tfw_client_addr_eq,

tempesta_fw/client.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ typedef struct {
4040
TfwClassifierPrvt class_prvt;
4141
} TfwClient;
4242

43-
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *));
43+
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr,
44+
TfwStr *user_agent, void (*init)(TfwClient *));
4445
void tfw_client_put(TfwClient *cli);
4546
int tfw_client_for_each(int (*fn)(void *));
4647
void tfw_cli_conn_release(TfwCliConn *cli_conn);

tempesta_fw/http.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,7 @@ tfw_http_get_ip_from_xff(TfwHttpReq *req)
28412841
static int
28422842
tfw_http_req_cient_link(TfwConn *conn, TfwHttpReq *req)
28432843
{
2844-
TfwStr s_ip;
2844+
TfwStr s_ip, s_user_agent, *ua;
28452845
TfwAddr addr;
28462846
TfwClient *cli, *conn_cli;
28472847

@@ -2851,7 +2851,11 @@ tfw_http_req_cient_link(TfwConn *conn, TfwHttpReq *req)
28512851
return TFW_BLOCK;
28522852

28532853
conn_cli = (TfwClient *)conn->peer;
2854-
cli = tfw_client_obtain(conn_cli->addr, &addr, NULL);
2854+
ua = &req->h_tbl->tbl[TFW_HTTP_HDR_USER_AGENT];
2855+
tfw_http_msg_clnthdr_val(ua, TFW_HTTP_HDR_USER_AGENT,
2856+
&s_user_agent);
2857+
cli = tfw_client_obtain(conn_cli->addr, &addr, &s_user_agent,
2858+
NULL);
28552859
if (cli) {
28562860
if (cli != conn_cli)
28572861
req->peer = cli;

tempesta_fw/http_limits.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ frang_conn_new(struct sock *sk)
338338
TfwAddr addr;
339339

340340
ss_getpeername(sk, &addr);
341-
cli = tfw_client_obtain(addr, NULL, __frang_init_acc);
341+
cli = tfw_client_obtain(addr, NULL, NULL, __frang_init_acc);
342342
if (unlikely(!cli)) {
343343
TFW_ERR("can't obtain a client for frang accounting\n");
344344
return TFW_BLOCK;

tempesta_fw/sock_clnt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ tfw_sock_clnt_new(struct sock *sk)
160160
tfw_connection_unlink_from_sk(sk);
161161

162162
ss_getpeername(sk, &addr);
163-
cli = tfw_client_obtain(addr, NULL, NULL);
163+
cli = tfw_client_obtain(addr, NULL, NULL, NULL);
164164
if (!cli) {
165165
TFW_ERR("can't obtain a client for the new socket\n");
166166
return -ENOENT;

0 commit comments

Comments
 (0)