Skip to content

Commit ddf7998

Browse files
committed
use user-agent for searching a client
1 parent 9e549dc commit ddf7998

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
@@ -58,6 +58,8 @@ typedef struct {
5858

5959
static TDB *client_db;
6060

61+
unsigned long tfw_hash_str_len(const TfwStr *str, unsigned long str_len);
62+
6163
/**
6264
* Called when a client socket is closed.
6365
*/
@@ -147,16 +149,16 @@ tfw_client_ent_init(TdbRec *rec, void (*init)(void *), void *data)
147149
}
148150

149151
/**
150-
* Find a client corresponding to @addr and @cli_addr (e.g. from X-Forwarded-For).
151-
* More advanced identification is possible based on User-Agent,
152-
* Cookie and other HTTP headers.
152+
* Find a client corresponding to @addr, @cli_addr (e.g. from X-Forwarded-For)
153+
* and @user_agent.
153154
*
154155
* The returned TfwClient reference must be released via tfw_client_put()
155156
* when the @sk is closed.
156157
* TODO #515 employ eviction strategy for the table.
157158
*/
158159
TfwClient *
159-
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(void *))
160+
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, TfwStr *user_agent,
161+
void (*init)(void *))
160162
{
161163
TfwClientEntry *ent;
162164
TfwClient *cli;
@@ -171,6 +173,8 @@ tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(void *))
171173
if (cli_addr)
172174
key ^= hash_calc((const char *)&cli_addr->sin6_addr,
173175
sizeof(cli_addr->sin6_addr));
176+
if (user_agent)
177+
key ^= tfw_hash_str_len(user_agent, 256);
174178

175179
len = sizeof(*ent);
176180
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
@@ -37,7 +37,7 @@ typedef struct {
3737
} TfwClient;
3838

3939
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr,
40-
void (*init)(void *));
40+
TfwStr *user_agent, void (*init)(void *));
4141
void tfw_client_put(TfwClient *cli);
4242
int tfw_client_for_each(int (*fn)(void *));
4343
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_client_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_client_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)