Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash initial server selection without save last server #18

Merged
merged 1 commit into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion client.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ int store_client(struct sockaddr_storage *cli)
clients[i].last = now;
clients[i].addr = *cli;
clients[i].connects++;
// clients[i].server = NO_SERVER;

/* don't remember server */
if(server_alg & ALG_HASH_NO_SERVER) {
clients[i].server = NO_SERVER;
}

DEBUG(2, "Client %s has index %d", pen_ntoa(cli), i);

Expand Down
8 changes: 6 additions & 2 deletions pen.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ static void usage(void)
" -T sec tracking time in seconds (0 = forever) [%d]\n"
" -H add X-Forwarded-For header in http requests\n"
" -U use udp protocol support\n"
" -N use hash for initial server selection without save server\n"
" -O option use option in penctl format\n"
" -P use poll() rather than select()\n"
" -Q use kqueue to manage events (BSD)\n"
Expand Down Expand Up @@ -2239,9 +2240,9 @@ static int options(int argc, char **argv)
char b[1024];

#ifdef HAVE_LIBSSL
char *opt = "B:C:F:O:S:T:b:c:e:i:j:l:m:o:p:q:t:u:w:x:DHPQWXUadfhnrsE:K:G:A:ZRL:";
char *opt = "B:C:F:O:S:T:b:c:e:i:j:l:m:o:p:q:t:u:w:x:DHNPQWXUadfhnrsE:K:G:A:ZRL:";
#else
char *opt = "B:C:F:O:S:T:b:c:e:i:j:l:m:o:p:q:t:u:w:x:DHPQWXUadfhnrs";
char *opt = "B:C:F:O:S:T:b:c:e:i:j:l:m:o:p:q:t:u:w:x:DHNPQWXUadfhnrs";
#endif

while ((c = getopt(argc, argv, opt)) != -1) {
Expand Down Expand Up @@ -2414,6 +2415,9 @@ static int options(int argc, char **argv)
}
break;
#endif /* HAVE_LIBSSL */
case 'N':
server_alg |= ALG_HASH_NO_SERVER;
break;
case '?':
default:
usage();
Expand Down
10 changes: 8 additions & 2 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ static int pen_hash(struct sockaddr_storage *a)
struct sockaddr_in *si;
struct sockaddr_in6 *si6;
unsigned char *u;
int hash;

switch (a->ss_family) {
case AF_INET:
si = (struct sockaddr_in *)a;
return si->sin_addr.s_addr % nservers;
hash = (si->sin_addr.s_addr ^ si->sin_port) % nservers;

DEBUG(2, "Hash: %d", hash);

return hash;

case AF_INET6:
si6 = (struct sockaddr_in6 *)a;
u = (unsigned char *)(&si6->sin6_addr);
Expand Down Expand Up @@ -200,7 +206,7 @@ int initial_server(int conn)
}
if (server_alg & ALG_PRIO) return server_by_prio();
if (server_alg & ALG_WEIGHT) return server_by_weight();
if (server_alg & ALG_HASH) return pen_hash(&clients[conns[conn].client].addr);
if (server_alg & ALG_HASH || server_alg & ALG_HASH_NO_SERVER) return pen_hash(&clients[conns[conn].client].addr);
return server_by_roundrobin();
}

Expand Down
1 change: 1 addition & 0 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define ALG_PRIO 8
#define ALG_HASH 16
#define ALG_STUBBORN 32
#define ALG_HASH_NO_SERVER 64

#define EMERGENCY_SERVER (-1)
#define ABUSE_SERVER (-2)
Expand Down