Skip to content

Commit 18cc11e

Browse files
committed
use user-agent for searching a client
1 parent 0ae4c3e commit 18cc11e

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

tempesta_fw/client.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ addr_eq(TdbRec *rec, void *data)
7878
}
7979

8080
/**
81-
* Find a client corresponding to @addr.
82-
* More advanced identification is possible based on User-Agent,
83-
* Cookie and other HTTP headers.
81+
* Find a client corresponding to @addr and @user_agent.
82+
* More advanced identification is possible based on Cookie and
83+
* other HTTP headers.
8484
* TODO (#488,#598,#1054?) actually clients can be relatively reliably
8585
* identified with TLS sessions (#1054) or HTTP sticky cookies.
8686
*
8787
* The returned TfwClient reference must be released via tfw_client_put()
8888
* when the @sk is closed.
8989
*/
9090
TfwClient *
91-
tfw_client_obtain(TfwAddr addr, void (*init)(TfwClient *))
91+
tfw_client_obtain(TfwAddr addr, TfwStr *user_agent, void (*init)(TfwClient *))
9292
{
9393
TfwClientEntry *ent;
9494
TfwClient *cli;
@@ -102,6 +102,10 @@ tfw_client_obtain(TfwAddr addr, void (*init)(TfwClient *))
102102
key = hash_calc((const char *)&addr.sin6_addr,
103103
sizeof(addr.sin6_addr));
104104

105+
if (user_agent)
106+
key += hash_calc((const char *)user_agent->data,
107+
min(user_agent->len, 256UL));
108+
105109
len = sizeof(*cli);
106110
rec = tdb_rec_get_alloc(client_db, key, &len, &addr_eq, &is_new, &addr);
107111
if (!rec)

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, void (*init)(TfwClient *));
43+
TfwClient *tfw_client_obtain(TfwAddr addr, TfwStr *user_agent,
44+
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,7 @@ tfw_http_req_process(TfwConn *conn, const TfwFsmData *data)
28292829
TfwHttpParser *parser;
28302830
TfwFsmData data_up;
28312831
TfwAddr addr;
2832-
TfwStr s_xff, s_ip, *c, *end;
2832+
TfwStr s_xff, s_ip, s_user_agent, *c, *end;
28332833
unsigned int nchunks;
28342834

28352835
BUG_ON(!conn->msg);
@@ -2948,8 +2948,15 @@ tfw_http_req_process(TfwConn *conn, const TfwFsmData *data)
29482948
}
29492949
s_ip.nchunks = nchunks;
29502950

2951-
if (!TFW_STR_EMPTY(&s_ip) && tfw_addr_pton(&s_ip, &addr) == 0) {
2952-
TfwClient *cli = tfw_client_obtain(addr, NULL);
2951+
if (TFW_STR_EMPTY(&s_ip) || tfw_addr_pton(&s_ip, &addr) != 0) {
2952+
ss_getpeername(conn->sk, &addr);
2953+
}
2954+
2955+
tfw_http_msg_clnthdr_val(&req->h_tbl->tbl[TFW_HTTP_HDR_USER_AGENT],
2956+
TFW_HTTP_HDR_USER_AGENT, &s_user_agent);
2957+
2958+
if (!TFW_STR_EMPTY(&s_ip) || !TFW_STR_EMPTY(&s_user_agent)) {
2959+
TfwClient *cli = tfw_client_obtain(addr, &s_user_agent, NULL);
29532960
TfwClient *conn_cli = (TfwClient *)conn->peer;
29542961
if (cli && conn_cli != cli) {
29552962
tfw_connection_unlink_from_peer(conn);

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, __frang_init_acc);
341+
cli = tfw_client_obtain(addr, 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);
163+
cli = tfw_client_obtain(addr, 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)