cmdparse: remove CPS_Status
Remove command and option specific error codes. Return zero on any failure.
This commit is contained in:
parent
d78e8f096c
commit
f8bd9ab378
4 changed files with 26 additions and 121 deletions
13
client.c
13
client.c
|
@ -1058,14 +1058,15 @@ static int
|
|||
process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
|
||||
{
|
||||
CPS_NTP_Source data;
|
||||
CPS_Status status;
|
||||
IPAddr ip_addr;
|
||||
char str[64];
|
||||
int result = 0;
|
||||
int result = 0, status;
|
||||
|
||||
status = CPS_ParseNTPSourceAdd(line, &data);
|
||||
switch (status) {
|
||||
case CPS_Success:
|
||||
case 0:
|
||||
LOG(LOGS_ERR, LOGF_Client, "Invalid syntax for add command");
|
||||
break;
|
||||
default:
|
||||
if (DNS_Name2IPAddress(data.name, &ip_addr, 1) != DNS_Success) {
|
||||
LOG(LOGS_ERR, LOGF_Client, "Invalid host/IP address");
|
||||
break;
|
||||
|
@ -1124,10 +1125,6 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
|
|||
(data.params.sel_options & SRC_SELECT_REQUIRE ? REQ_ADDSRC_REQUIRE : 0));
|
||||
result = 1;
|
||||
|
||||
break;
|
||||
default:
|
||||
CPS_StatusToString(status, str, sizeof (str));
|
||||
LOG(LOGS_ERR, LOGF_Client, "%s", str);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
101
cmdparse.c
101
cmdparse.c
|
@ -39,7 +39,7 @@
|
|||
|
||||
/* ================================================== */
|
||||
|
||||
CPS_Status
|
||||
int
|
||||
CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
{
|
||||
char *hostname, *cmd;
|
||||
|
@ -68,7 +68,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||
line = CPS_SplitWord(line);
|
||||
|
||||
if (!*hostname)
|
||||
return CPS_BadHost;
|
||||
return 0;
|
||||
|
||||
src->name = hostname;
|
||||
|
||||
|
@ -95,52 +95,52 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||
} else if (!strcasecmp(cmd, "key")) {
|
||||
if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
|
||||
src->params.authkey == INACTIVE_AUTHKEY)
|
||||
return CPS_BadKey;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxdelay")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
|
||||
return CPS_BadMaxdelay;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxdelayratio")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay_ratio, &n) != 1)
|
||||
return CPS_BadMaxdelayratio;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxdelaydevratio")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay_dev_ratio, &n) != 1)
|
||||
return CPS_BadMaxdelaydevratio;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxpoll")) {
|
||||
if (sscanf(line, "%d%n", &src->params.maxpoll, &n) != 1)
|
||||
return CPS_BadMaxpoll;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxsamples")) {
|
||||
if (sscanf(line, "%d%n", &src->params.max_samples, &n) != 1)
|
||||
return CPS_BadMaxsamples;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxsources")) {
|
||||
if (sscanf(line, "%d%n", &src->params.max_sources, &n) != 1)
|
||||
return CPS_BadMaxsources;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "minpoll")) {
|
||||
if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1)
|
||||
return CPS_BadMinpoll;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "minsamples")) {
|
||||
if (sscanf(line, "%d%n", &src->params.min_samples, &n) != 1)
|
||||
return CPS_BadMinsamples;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "minstratum")) {
|
||||
if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
|
||||
return CPS_BadMinstratum;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "port")) {
|
||||
if (sscanf(line, "%hu%n", &src->port, &n) != 1)
|
||||
return CPS_BadPort;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "polltarget")) {
|
||||
if (sscanf(line, "%d%n", &src->params.poll_target, &n) != 1)
|
||||
return CPS_BadPolltarget;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "presend")) {
|
||||
if (sscanf(line, "%d%n", &src->params.presend_minpoll, &n) != 1)
|
||||
return CPS_BadPresend;
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "version")) {
|
||||
if (sscanf(line, "%d%n", &src->params.version, &n) != 1)
|
||||
return CPS_BadVersion;
|
||||
return 0;
|
||||
} else {
|
||||
return CPS_BadOption;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return CPS_Success;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
@ -178,71 +178,6 @@ CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance)
|
|||
|
||||
return 1;
|
||||
}
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CPS_StatusToString(CPS_Status status, char *dest, int len)
|
||||
{
|
||||
const char *s = NULL;
|
||||
|
||||
if (len > 0)
|
||||
dest[0] = '\0';
|
||||
|
||||
switch (status) {
|
||||
case CPS_Success:
|
||||
return;
|
||||
case CPS_BadOption:
|
||||
s = "server/peer/pool option";
|
||||
break;
|
||||
case CPS_BadHost:
|
||||
s = "address";
|
||||
break;
|
||||
case CPS_BadPort:
|
||||
s = "port";
|
||||
break;
|
||||
case CPS_BadMinpoll:
|
||||
s = "minpoll";
|
||||
break;
|
||||
case CPS_BadMaxpoll:
|
||||
s = "maxpoll";
|
||||
break;
|
||||
case CPS_BadPresend:
|
||||
s = "presend";
|
||||
break;
|
||||
case CPS_BadMaxdelaydevratio:
|
||||
s = "maxdelaydevratio";
|
||||
break;
|
||||
case CPS_BadMaxdelayratio:
|
||||
s = "maxdelayratio";
|
||||
break;
|
||||
case CPS_BadMaxdelay:
|
||||
s = "maxdelay";
|
||||
break;
|
||||
case CPS_BadKey:
|
||||
s = "key";
|
||||
break;
|
||||
case CPS_BadMinstratum:
|
||||
s = "minstratum";
|
||||
break;
|
||||
case CPS_BadPolltarget:
|
||||
s = "polltarget";
|
||||
break;
|
||||
case CPS_BadVersion:
|
||||
s = "version";
|
||||
break;
|
||||
case CPS_BadMaxsources:
|
||||
s = "maxsources";
|
||||
break;
|
||||
case CPS_BadMinsamples:
|
||||
s = "minsamples";
|
||||
break;
|
||||
case CPS_BadMaxsamples:
|
||||
s = "maxsamples";
|
||||
break;
|
||||
}
|
||||
|
||||
snprintf(dest, len, "Invalid %s", s);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
|
|
25
cmdparse.h
25
cmdparse.h
|
@ -30,26 +30,6 @@
|
|||
#include "srcparams.h"
|
||||
#include "addressing.h"
|
||||
|
||||
typedef enum {
|
||||
CPS_Success,
|
||||
CPS_BadOption,
|
||||
CPS_BadHost,
|
||||
CPS_BadPort,
|
||||
CPS_BadMinpoll,
|
||||
CPS_BadMaxpoll,
|
||||
CPS_BadPresend,
|
||||
CPS_BadMaxdelaydevratio,
|
||||
CPS_BadMaxdelayratio,
|
||||
CPS_BadMaxdelay,
|
||||
CPS_BadKey,
|
||||
CPS_BadMinstratum,
|
||||
CPS_BadPolltarget,
|
||||
CPS_BadVersion,
|
||||
CPS_BadMaxsources,
|
||||
CPS_BadMinsamples,
|
||||
CPS_BadMaxsamples,
|
||||
} CPS_Status;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
unsigned short port;
|
||||
|
@ -57,14 +37,11 @@ typedef struct {
|
|||
} CPS_NTP_Source;
|
||||
|
||||
/* Parse a command to add an NTP server or peer */
|
||||
extern CPS_Status CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src);
|
||||
extern int CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src);
|
||||
|
||||
/* Parse a command to enable local reference */
|
||||
extern int CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance);
|
||||
|
||||
/* Get a string describing error status */
|
||||
extern void CPS_StatusToString(CPS_Status status, char *dest, int len);
|
||||
|
||||
/* Remove extra white-space and comments */
|
||||
extern void CPS_NormalizeLine(char *line);
|
||||
|
||||
|
|
8
conf.c
8
conf.c
|
@ -604,17 +604,13 @@ parse_null(char *line)
|
|||
static void
|
||||
parse_source(char *line, NTP_Source_Type type, int pool)
|
||||
{
|
||||
CPS_Status status;
|
||||
NTP_Source source;
|
||||
char str[64];
|
||||
|
||||
source.type = type;
|
||||
source.pool = pool;
|
||||
status = CPS_ParseNTPSourceAdd(line, &source.params);
|
||||
|
||||
if (status != CPS_Success) {
|
||||
CPS_StatusToString(status, str, sizeof (str));
|
||||
other_parse_error(str);
|
||||
if (!CPS_ParseNTPSourceAdd(line, &source.params)) {
|
||||
command_parse_error();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue