Skip to content

Commit

Permalink
Set client's IP address on client setup
Browse files Browse the repository at this point in the history
This is to address the issue #101.
Use that IP address without the need to obtain it again and again.
  • Loading branch information
keshonok committed May 9, 2015
1 parent 165ba9b commit 6bb7ed8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
17 changes: 11 additions & 6 deletions tempesta_fw/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,28 @@ tfw_client_put(TfwClient *clnt)
TfwClient *
tfw_client_obtain(struct sock *sk)
{
static const TfwAddr dummy_addr;
int daddr_len;
TfwAddr daddr;

/*
* TODO: currently there is one to one socket-client
* mapping, which isn't appropriate since a client can
* have more than one socket with the server.
*
* We have too lookup the client by the socket and create a new one
* only if it's really new.
* We need to look up a client by the socket and create
* a new one only if it's really new.
*/
TfwClient *cli = kmem_cache_alloc(cli_cache, GFP_ATOMIC);
if (!cli)
return NULL;
atomic_set(&cli->conn_users, 1);

/* TODO: Derive the client IP address from @sk. */
tfw_peer_init((TfwPeer *)cli, &dummy_addr);
/* Derive client's IP address from @sk. */
if (ss_getpeername(sk, &daddr.sa, &daddr_len)) {
kmem_cache_free(cli_cache, cli);
return NULL;
}
tfw_peer_init((TfwPeer *)cli, &daddr);
atomic_set(&cli->conn_users, 1);

TFW_DBG("new client: cli=%p\n", cli);

Expand Down
41 changes: 5 additions & 36 deletions tempesta_fw/http_sticky.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,29 +197,6 @@ tfw_http_sticky_get(TfwHttpMsg *hm, TfwStr *cookie)
return 1;
}

/* Probably stil needed for #101. */
#if 0
/*
* Get IP address of the peer (remote address of the connection).
* inet_getname()/inet6_getname() only use sock to access sock->sk.
* Make use of that.
*/
static int
tfw_getpeeraddr(struct sock *sk, struct sockaddr *uaddr, int *uaddr_len)
{
struct socket sock = { .sk = sk };

if (sk->sk_family == AF_INET6) {
inet6_getname(&sock, uaddr, uaddr_len, 1);
} else if (sk->sk_family == AF_INET) {
inet_getname(&sock, uaddr, uaddr_len, 1);
} else {
return -1;
}
return 0;
}
#endif

/*
* Create Tempesta sticky cookie value and set it for the client.
*
Expand All @@ -231,10 +208,9 @@ tfw_getpeeraddr(struct sock *sk, struct sockaddr *uaddr, int *uaddr_len)
static int
tfw_http_sticky_set(TfwHttpMsg *hm)
{
TfwAddr addr;
int ret, addr_len;
TfwStr ua_value = { 0 };
const TfwStr s_field_name = TfwStr_string("User-Agent:");
int ret, addr_len = sizeof(addr);
TfwClient *client = (TfwClient *)hm->conn->peer;

char desc[sizeof(struct shash_desc)
Expand All @@ -248,27 +224,20 @@ tfw_http_sticky_set(TfwHttpMsg *hm)
if ((ret = tfw_http_field_value(hm, &s_field_name, &ua_value)) <= 0) {
return ret;
}
/*
* TODO #101 get client address from client->addr.
* tfw_client_update() must be updated for this.
*/
#if 0
ret = tfw_getpeeraddr(client->sock, &addr.sa, &addr_len);
if (ret != 0) {
return ret;
}
#endif

/* Set only once per client's session */
if (!client->cookie.ts.tv_sec) {
getnstimeofday(&client->cookie.ts);
}
addr_len = (hm->conn->sk->sk_family == AF_INET)
? sizeof(client->addr.v4) : sizeof(client->addr.v6);

memset(desc, 0, sizeof(desc));
shash_desc->tfm = tfw_sticky_shash;
shash_desc->flags = 0;

crypto_shash_init(shash_desc);
crypto_shash_update(shash_desc, (u8 *)&addr.sa, addr_len);
crypto_shash_update(shash_desc, (u8 *)&client->addr.sa, addr_len);
crypto_shash_update(shash_desc, (u8 *)ua_value.ptr, ua_value.len);
crypto_shash_finup(shash_desc, (u8 *)&client->cookie.ts,
sizeof(client->cookie.ts),
Expand Down

0 comments on commit 6bb7ed8

Please sign in to comment.