Skip to content

Commit

Permalink
Merge pull request #18 from mvrogowski/master
Browse files Browse the repository at this point in the history
Hash initial server selection without save last server
  • Loading branch information
UlricE committed Oct 3, 2015
2 parents 27b64d0 + 76a2463 commit 5e33606
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
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

0 comments on commit 5e33606

Please sign in to comment.