Skip to content

Commit

Permalink
replace server states with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Mauchle committed Aug 6, 2017
1 parent 14cac40 commit 05ed43d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
8 changes: 4 additions & 4 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *
return 0;
}

if (server->connectionok) {
server->connectionok = 0;
if (server->state == RSP_SERVER_STATE_CONNECTED) {
server->state = RSP_SERVER_STATE_RECONNECTING;
sleep(2);
} else if (elapsed < 1)
sleep(2);
Expand Down Expand Up @@ -591,7 +591,7 @@ int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *
}
X509_free(cert);
debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", hp->host, hp->port);
server->connectionok = 1;
server->state = RSP_SERVER_STATE_CONNECTED;
gettimeofday(&server->lastconnecttry, NULL);
pthread_mutex_unlock(&server->lock);
return 1;
Expand All @@ -603,7 +603,7 @@ int clientradputdtls(struct server *server, unsigned char *rad) {
unsigned long error;
struct clsrvconf *conf = server->conf;

if (!server->connectionok)
if (server->state != RSP_SERVER_STATE_CONNECTED)
return 0;
len = RADLEN(rad);
if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
Expand Down
28 changes: 12 additions & 16 deletions radsecproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1260,11 +1260,11 @@ struct clsrvconf *choosesrvconf(struct list *srvconfs) {
server = (struct clsrvconf *)entry->data;
if (!server->servers)
return server;
if (server->servers->dynfailing)
if (server->servers->state == RSP_SERVER_STATE_FAILING)
continue;
if (!first)
first = server;
if (!server->servers->connectionok && !server->servers->dynstartup)
if (server->servers->state == RSP_SERVER_STATE_STARTUP || server->servers->state == RSP_SERVER_STATE_RECONNECTING)
continue;
if (!server->servers->lostrqs)
return server;
Expand Down Expand Up @@ -1560,7 +1560,6 @@ void replyh(struct server *server, unsigned char *buf) {
struct tlv *attr;
struct list_node *node;

server->connectionok = 1;
server->lostrqs = 0;

rqout = server->requests + buf[1];
Expand Down Expand Up @@ -1753,10 +1752,10 @@ void *clientwr(void *arg) {

#define ZZZ 900

server->state = RSP_SERVER_STATE_STARTUP;
if (server->dynamiclookuparg && !dynamicconfig(server)) {
dynconffail = 1;
server->dynstartup = 0;
server->dynfailing = 1;
server->state = RSP_SERVER_STATE_FAILING;
debug(DBG_WARN, "%s: dynamicconfig(%s: %s) failed, sleeping %ds",
__func__, server->conf->name, server->dynamiclookuparg, ZZZ);
sleep(ZZZ);
Expand All @@ -1767,7 +1766,7 @@ void *clientwr(void *arg) {
* dynamicconfig() above? */
if (!resolvehostports(conf->hostports, conf->hostaf, conf->pdef->socktype)) {
debug(DBG_WARN, "%s: resolve failed, sleeping %ds", __func__, ZZZ);
server->dynfailing=1;
server->state = RSP_SERVER_STATE_FAILING;
sleep(ZZZ);
goto errexit;
}
Expand All @@ -1781,24 +1780,21 @@ void *clientwr(void *arg) {

if (conf->pdef->connecter) {
if (!conf->pdef->connecter(server, NULL, server->dynamiclookuparg ? 5 : 0, "clientwr")) {
server->dynfailing = 1;
server->state = RSP_SERVER_STATE_FAILING;
if (server->dynamiclookuparg) {
server->dynstartup = 0;
debug(DBG_WARN, "%s: connect failed, sleeping %ds",
__func__, ZZZ);
sleep(ZZZ);
}
goto errexit;
}
server->connectionok = 1;
if (pthread_create(&clientrdth, &pthread_attr, conf->pdef->clientconnreader, (void *)server)) {
debugerrno(errno, DBG_ERR, "clientwr: pthread_create failed");
server->dynfailing=1;
server->state = RSP_SERVER_STATE_FAILING;
goto errexit;
}
} else
server->connectionok = 1;
server->dynstartup = 0;
}
server->state = RSP_SERVER_STATE_CONNECTED;

for (;;) {
pthread_mutex_lock(&server->newrq_mutex);
Expand Down Expand Up @@ -1836,7 +1832,7 @@ void *clientwr(void *arg) {

for (i = 0; i < MAX_REQUESTS; i++) {
if (server->clientrdgone) {
server->dynfailing=1;
server->state = RSP_SERVER_STATE_FAILING;
if (conf->pdef->connecter)
pthread_join(clientrdth, NULL);
goto errexit;
Expand Down Expand Up @@ -1888,7 +1884,7 @@ void *clientwr(void *arg) {
conf->pdef->clientradput(server, rqout->rq->buf);
pthread_mutex_unlock(rqout->lock);
}
if (conf->statusserver && server->connectionok) {
if (conf->statusserver && server->state == RSP_SERVER_STATE_CONNECTED) {
secs = server->lastrcv.tv_sec > laststatsrv.tv_sec ? server->lastrcv.tv_sec : laststatsrv.tv_sec;
gettimeofday(&now, NULL);
if (now.tv_sec - secs > STATUS_SERVER_PERIOD) {
Expand Down Expand Up @@ -2189,7 +2185,7 @@ struct list *createsubrealmservers(struct realm *realm, struct list *srvconfs) {
* the srvconfs list. */
if (addserver(srvconf)) {
srvconf->servers->dynamiclookuparg = stringcopy(realm->name, 0);
srvconf->servers->dynstartup = 1;
srvconf->servers->state = RSP_SERVER_STATE_STARTUP;
debug(DBG_DBG, "%s: new client writer for %s",
__func__, srvconf->servers->conf->name);
if (pthread_create(&clientth, &pthread_attr, clientwr, (void *)(srvconf->servers))) {
Expand Down
11 changes: 8 additions & 3 deletions radsecproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ enum rsp_fticks_mac_type {
RSP_FTICKS_MAC_FULLY_KEY_HASHED
};

enum rsp_server_state {
RSP_SERVER_STATE_STARTUP = 0, /* default */
RSP_SERVER_STATE_CONNECTED,
RSP_SERVER_STATE_RECONNECTING,
RSP_SERVER_STATE_FAILING
};

struct options {
char *pidfile;
char *logdestination;
Expand Down Expand Up @@ -165,10 +172,8 @@ struct server {
uint8_t clientrdgone;
struct timeval lastconnecttry;
struct timeval lastreply;
uint8_t connectionok;
enum rsp_server_state state;
uint8_t lostrqs;
uint8_t dynstartup;
uint8_t dynfailing;
char *dynamiclookuparg;
int nextid;
struct timeval lastrcv;
Expand Down
8 changes: 4 additions & 4 deletions tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t
pthread_mutex_unlock(&server->lock);
return 0;
}
if (server->connectionok) {
server->connectionok = 0;
if (server->state == RSP_SERVER_STATE_CONNECTED) {
server->state = RSP_SERVER_STATE_RECONNECTING;
sleep(2);
} else if (elapsed < 1)
sleep(2);
Expand All @@ -121,7 +121,7 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t
if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
break;
}
server->connectionok = 1;
server->state = RSP_SERVER_STATE_CONNECTED;
gettimeofday(&server->lastconnecttry, NULL);
pthread_mutex_unlock(&server->lock);
return 1;
Expand Down Expand Up @@ -202,7 +202,7 @@ int clientradputtcp(struct server *server, unsigned char *rad) {
size_t len;
struct clsrvconf *conf = server->conf;

if (!server->connectionok)
if (server->state != RSP_SERVER_STATE_CONNECTED)
return 0;
len = RADLEN(rad);
if ((cnt = write(server->sock, rad, len)) <= 0) {
Expand Down
8 changes: 4 additions & 4 deletions tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ int tlsconnect(struct server *server, struct timeval *when, int timeout, char *t
pthread_mutex_unlock(&server->lock);
return 0;
}
if (server->connectionok) {
server->connectionok = 0;
if (server->state == RSP_SERVER_STATE_CONNECTED) {
server->state = RSP_SERVER_STATE_RECONNECTING;
sleep(2);
} else if (elapsed < 1)
sleep(2);
Expand Down Expand Up @@ -155,7 +155,7 @@ int tlsconnect(struct server *server, struct timeval *when, int timeout, char *t
X509_free(cert);
}
debug(DBG_WARN, "tlsconnect: TLS connection to %s up", server->conf->name);
server->connectionok = 1;
server->state = RSP_SERVER_STATE_CONNECTED;
gettimeofday(&server->lastconnecttry, NULL);
pthread_mutex_unlock(&server->lock);
return 1;
Expand Down Expand Up @@ -251,7 +251,7 @@ int clientradputtls(struct server *server, unsigned char *rad) {
unsigned long error;
struct clsrvconf *conf = server->conf;

if (!server->connectionok)
if (server->state != RSP_SERVER_STATE_CONNECTED)
return 0;
len = RADLEN(rad);
if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
Expand Down

0 comments on commit 05ed43d

Please sign in to comment.