Skip to content

Commit d6ad7db

Browse files
committed
use user-agent for searching a client
1 parent 1175993 commit d6ad7db

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

tempesta_fw/client.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef struct {
4646

4747
static TDB *client_db;
4848

49+
unsigned long tfw_hash_str_len(const TfwStr *str, unsigned long str_len);
50+
4951
/**
5052
* Called when a client socket is closed.
5153
*/
@@ -137,16 +139,16 @@ tfw_client_ent_init(TdbRec *rec, void (*init)(void *), void *data)
137139
}
138140

139141
/**
140-
* Find a client corresponding to @addr and @cli_addr (e.g. from X-Forwarded-For).
141-
* More advanced identification is possible based on User-Agent,
142-
* Cookie and other HTTP headers.
142+
* Find a client corresponding to @addr, @cli_addr (e.g. from X-Forwarded-For)
143+
* and @user_agent.
143144
*
144145
* The returned TfwClient reference must be released via tfw_client_put()
145146
* when the @sk is closed.
146147
* TODO #515 employ eviction strategy for the table.
147148
*/
148149
TfwClient *
149-
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(void *))
150+
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, TfwStr *user_agent,
151+
void (*init)(void *))
150152
{
151153
TfwClientEntry *ent;
152154
TfwClient *cli;
@@ -161,6 +163,8 @@ tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(void *))
161163
if (cli_addr)
162164
key ^= hash_calc((const char *)&cli_addr->sin6_addr,
163165
sizeof(cli_addr->sin6_addr));
166+
if (user_agent)
167+
key ^= tfw_hash_str_len(user_agent, 256);
164168

165169
len = sizeof(*ent);
166170
rec = tdb_rec_get_alloc(client_db, key, &len, &tfw_client_addr_eq,

tempesta_fw/client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct {
4141
} TfwClient;
4242

4343
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr,
44-
void (*init)(void *));
44+
TfwStr *user_agent, void (*init)(void *));
4545
void tfw_client_put(TfwClient *cli);
4646
int tfw_client_for_each(int (*fn)(void *));
4747
void tfw_client_set_lifetime(unsigned int lifetime);

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
@@ -339,7 +339,7 @@ frang_conn_new(struct sock *sk)
339339
TfwAddr addr;
340340

341341
ss_getpeername(sk, &addr);
342-
cli = tfw_client_obtain(addr, NULL, __frang_init_acc);
342+
cli = tfw_client_obtain(addr, NULL, NULL, __frang_init_acc);
343343
if (unlikely(!cli)) {
344344
TFW_ERR("can't obtain a client for frang accounting\n");
345345
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)