Various code improvements.

Remove useless if() statements, try to factor common code, use
NULL instead of 0 where it seems to make sense, fix indentation
in switch/case for readability.
This commit is contained in:
Sam Hocevar
2016-02-06 12:53:52 +01:00
parent bf11a9a4b5
commit 6c385facd2

View File

@@ -110,7 +110,7 @@ struct _server_info
int allowRulesTotal, denyRulesTotal;
};
ServerInfo *seInfo = 0;
ServerInfo *seInfo = NULL;
int seTotal = 0;
int globalAllowRules = 0;
@@ -136,16 +136,16 @@ struct _connection_info
ConnectionInfo *coInfo = NULL;
int coTotal = 0;
char **allowRules = 0;
char **denyRules = 0;
int *denyRulesFor = 0;
char **allowRules = NULL;
char **denyRules = NULL;
int *denyRulesFor = NULL;
int allowRulesTotal = 0;
int denyRulesTotal = 0;
int maxfd = 0;
char *logFileName = 0;
char *pidLogFileName = 0;
char *logFileName = NULL;
char *pidLogFileName = NULL;
int logFormatCommon = 0;
FILE *logFile = 0;
FILE *logFile = NULL;
/*
se: (se)rver sockets
@@ -288,7 +288,6 @@ static void readConfiguration(void)
{
FILE *in;
char line[16384];
if (seInfo) {
/* Close existing server sockets. */
for (int i = 0; i < seTotal; ++i) {
ServerInfo *srv = &seInfo[i];
@@ -301,9 +300,7 @@ static void readConfiguration(void)
/* Free memory associated with previous set. */
free(seInfo);
seInfo = NULL;
}
seTotal = 0;
if (allowRules) {
/* Forget existing allow rules. */
for (int i = 0; i < allowRulesTotal; ++i) {
free(allowRules[i]);
@@ -311,10 +308,7 @@ static void readConfiguration(void)
/* Free memory associated with previous set. */
free(allowRules);
allowRules = NULL;
globalAllowRules = 0;
}
allowRulesTotal = 0;
if (denyRules) {
globalAllowRules = allowRulesTotal = 0;
/* Forget existing deny rules. */
for (int i = 0; i < denyRulesTotal; ++i) {
free(denyRules[i]);
@@ -322,17 +316,12 @@ static void readConfiguration(void)
/* Free memory associated with previous set. */
free(denyRules);
denyRules = NULL;
globalDenyRules = 0;
}
denyRulesTotal = 0;
if (logFileName) {
globalDenyRules = denyRulesTotal = 0;
/* Free file names */
free(logFileName);
logFileName = NULL;
}
if (pidLogFileName) {
free(pidLogFileName);
pidLogFileName = NULL;
}
/* Parse the configuration file. */
in = fopen(options.conf_file, "r");
if (!in) {
@@ -548,7 +537,7 @@ static void readConfiguration(void)
/* Open the log file */
if (logFile) {
fclose(logFile);
logFile = 0;
logFile = NULL;
}
if (logFileName) {
logFile = fopen(logFileName, "a");
@@ -719,17 +708,11 @@ static void selectPass(void) {
void handleRemoteRead(ConnectionInfo *cnx)
{
int got;
if (bufferSpace == cnx->inputRPos) {
return;
}
got = recv(cnx->reFd, cnx->input + cnx->inputRPos,
int got = recv(cnx->reFd, cnx->input + cnx->inputRPos,
bufferSpace - cnx->inputRPos, 0);
if (got == 0) {
/* Prepare for closing */
handleCloseFromRemote(cnx);
return;
}
if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) {
return;
@@ -737,6 +720,9 @@ void handleRemoteRead(ConnectionInfo *cnx)
if (GetLastError() == WSAEINPROGRESS) {
return;
}
}
if (got <= 0) {
/* Prepare for closing */
handleCloseFromRemote(cnx);
return;
}
@@ -746,7 +732,6 @@ void handleRemoteRead(ConnectionInfo *cnx)
void handleRemoteWrite(ConnectionInfo *cnx)
{
int got;
if (cnx->coClosing && (cnx->outputWPos == cnx->outputRPos)) {
cnx->reClosed = 1;
cnx->coClosed = 1;
@@ -755,7 +740,7 @@ void handleRemoteWrite(ConnectionInfo *cnx)
closesocket(cnx->reFd);
return;
}
got = send(cnx->reFd, cnx->output + cnx->outputWPos,
int got = send(cnx->reFd, cnx->output + cnx->outputWPos,
cnx->outputRPos - cnx->outputWPos, 0);
if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) {
@@ -777,16 +762,11 @@ void handleRemoteWrite(ConnectionInfo *cnx)
void handleLocalRead(ConnectionInfo *cnx)
{
int got;
if (bufferSpace == cnx->outputRPos) {
return;
}
got = recv(cnx->loFd, cnx->output + cnx->outputRPos,
int got = recv(cnx->loFd, cnx->output + cnx->outputRPos,
bufferSpace - cnx->outputRPos, 0);
if (got == 0) {
handleCloseFromLocal(cnx);
return;
}
if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) {
return;
@@ -794,6 +774,8 @@ void handleLocalRead(ConnectionInfo *cnx)
if (GetLastError() == WSAEINPROGRESS) {
return;
}
}
if (got <= 0) {
handleCloseFromLocal(cnx);
return;
}
@@ -802,7 +784,6 @@ void handleLocalRead(ConnectionInfo *cnx)
void handleLocalWrite(ConnectionInfo *cnx)
{
int got;
if (cnx->coClosing && (cnx->inputWPos == cnx->inputRPos)) {
cnx->loClosed = 1;
cnx->coClosed = 1;
@@ -811,7 +792,7 @@ void handleLocalWrite(ConnectionInfo *cnx)
closesocket(cnx->loFd);
return;
}
got = send(cnx->loFd, cnx->input + cnx->inputWPos,
int got = send(cnx->loFd, cnx->input + cnx->inputWPos,
cnx->inputRPos - cnx->inputWPos, 0);
if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) {
@@ -956,9 +937,9 @@ void handleAccept(int i)
global allow rules, it's presumed OK at
this step. If there are any, and it doesn't
match at least one, kick it out. */
if (globalAllowRules) {
int good = 0;
int good = 1;
for (int j = 0; j < globalAllowRules; ++j) {
good = 0;
if (match(addressText, allowRules[j])) {
good = 1;
break;
@@ -968,22 +949,19 @@ void handleAccept(int i)
refuse(cnx, logNotAllowed);
return;
}
}
/* 2. Check global deny rules. If it matches
any of the global deny rules, kick it out. */
if (globalDenyRules) {
for (int j = 0; j < globalDenyRules; ++j) {
if (match(addressText, denyRules[j])) {
refuse(cnx, logDenied);
}
}
}
/* 3. Check allow rules specific to this forwarding rule.
If there are none, it's OK. If there are any,
it must match at least one. */
if (srv->allowRulesTotal) {
int good = 0;
good = 1;
for (int j = 0; j < srv->allowRulesTotal; ++j) {
good = 0;
if (match(addressText,
allowRules[srv->allowRules + j])) {
good = 1;
@@ -994,17 +972,14 @@ void handleAccept(int i)
refuse(cnx, logNotAllowed);
return;
}
}
/* 2. Check deny rules specific to this forwarding rule. If
/* 4. Check deny rules specific to this forwarding rule. If
it matches any of the deny rules, kick it out. */
if (srv->denyRulesTotal) {
for (int j = 0; j < srv->denyRulesTotal; ++j) {
if (match(addressText,
denyRules[srv->denyRules + j])) {
refuse(cnx, logDenied);
}
}
}
/* Now open a connection to the local server.
This, too, is nonblocking. Why wait
for anything when you don't have to? */