cmdmon: merge add server/peer requests

Instead of having two separate requests in the protocol for adding a
server and peer, specify the type of the new source in the request data.
This commit is contained in:
Miroslav Lichvar 2019-12-04 11:16:26 +01:00
parent 3763befd62
commit 00fff161cf
4 changed files with 47 additions and 35 deletions

View file

@ -101,7 +101,8 @@
#define REQ_ADD_PEER3 61
#define REQ_SHUTDOWN 62
#define REQ_ONOFFLINE 63
#define N_REQUEST_TYPES 64
#define REQ_ADD_SOURCE 64
#define N_REQUEST_TYPES 65
/* Structure used to exchange timespecs independent of time_t size */
typedef struct {
@ -245,6 +246,10 @@ typedef struct {
int32_t EOR;
} REQ_Ac_Check;
/* Source types in NTP source requests */
#define REQ_ADDSRC_SERVER 1
#define REQ_ADDSRC_PEER 2
/* Flags used in NTP source requests */
#define REQ_ADDSRC_ONLINE 0x1
#define REQ_ADDSRC_AUTOOFFLINE 0x2
@ -257,6 +262,7 @@ typedef struct {
#define REQ_ADDSRC_BURST 0x100
typedef struct {
uint32_t type;
IPAddr ip_addr;
uint32_t port;
int32_t minpoll;

View file

@ -1053,13 +1053,27 @@ process_cmd_doffset(CMD_Request *msg, char *line)
/* ================================================== */
static int
process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
process_cmd_add_source(CMD_Request *msg, char *line)
{
CPS_NTP_Source data;
IPAddr ip_addr;
int result = 0, status;
const char *opt_name;
int result = 0, status, type;
const char *opt_name, *word;
msg->command = htons(REQ_ADD_SOURCE);
word = line;
line = CPS_SplitWord(line);
if (!strcmp(word, "server")) {
type = REQ_ADDSRC_SERVER;
} else if (!strcmp(word, "peer")) {
type = REQ_ADDSRC_PEER;
} else {
LOG(LOGS_ERR, "Invalid syntax for add command");
return 0;
}
status = CPS_ParseNTPSourceAdd(line, &data);
switch (status) {
case 0:
@ -1077,6 +1091,7 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
break;
}
msg->data.ntp_source.type = htonl(type);
msg->data.ntp_source.port = htonl((unsigned long) data.port);
UTI_IPHostToNetwork(&ip_addr, &msg->data.ntp_source.ip_addr);
msg->data.ntp_source.minpoll = htonl(data.params.minpoll);
@ -1119,24 +1134,6 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
/* ================================================== */
static int
process_cmd_add_server(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_ADD_SERVER3);
return process_cmd_add_server_or_peer(msg, line);
}
/* ================================================== */
static int
process_cmd_add_peer(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_ADD_PEER3);
return process_cmd_add_server_or_peer(msg, line);
}
/* ================================================== */
static int
process_cmd_delete(CMD_Request *msg, char *line)
{
@ -2927,10 +2924,8 @@ process_line(char *line)
} else if (!strcmp(command, "activity")) {
do_normal_submit = 0;
ret = process_cmd_activity(line);
} else if (!strcmp(command, "add") && !strncmp(line, "peer", 4)) {
do_normal_submit = process_cmd_add_peer(&tx_message, CPS_SplitWord(line));
} else if (!strcmp(command, "add") && !strncmp(line, "server", 6)) {
do_normal_submit = process_cmd_add_server(&tx_message, CPS_SplitWord(line));
} else if (!strcmp(command, "add")) {
do_normal_submit = process_cmd_add_source(&tx_message, line);
} else if (!strcmp(command, "allow")) {
if (!strncmp(line, "all", 3)) {
do_normal_submit = process_cmd_allowall(&tx_message, CPS_SplitWord(line));

View file

@ -132,6 +132,7 @@ static const char permissions[] = {
PERMIT_AUTH, /* ADD_PEER3 */
PERMIT_AUTH, /* SHUTDOWN */
PERMIT_AUTH, /* ONOFFLINE */
PERMIT_AUTH, /* ADD_SOURCE */
};
/* ================================================== */
@ -675,12 +676,25 @@ handle_cmdaccheck(CMD_Request *rx_message, CMD_Reply *tx_message)
/* ================================================== */
static void
handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_message)
handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
{
NTP_Remote_Address rem_addr;
NTP_Source_Type type;
SourceParameters params;
NSR_Status status;
switch (ntohl(rx_message->data.ntp_source.type)) {
case REQ_ADDSRC_SERVER:
type = NTP_SERVER;
break;
case REQ_ADDSRC_PEER:
type = NTP_PEER;
break;
default:
tx_message->status = htons(STT_INVALID);
return;
}
UTI_IPNetworkToHost(&rx_message->data.ntp_source.ip_addr, &rem_addr.ip_addr);
rem_addr.port = (unsigned short)(ntohl(rx_message->data.ntp_source.port));
params.minpoll = ntohl(rx_message->data.ntp_source.minpoll);
@ -1426,12 +1440,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
handle_cmdaccheck(&rx_message, &tx_message);
break;
case REQ_ADD_SERVER3:
handle_add_source(NTP_SERVER, &rx_message, &tx_message);
break;
case REQ_ADD_PEER3:
handle_add_source(NTP_PEER, &rx_message, &tx_message);
case REQ_ADD_SOURCE:
handle_add_source(&rx_message, &tx_message);
break;
case REQ_DEL_SOURCE:

View file

@ -116,10 +116,11 @@ static const struct request_length request_lengths[] = {
REQ_LENGTH_ENTRY(ntp_data, ntp_data), /* NTP_DATA */
{ 0, 0 }, /* ADD_SERVER2 */
{ 0, 0 }, /* ADD_PEER2 */
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_SERVER3 */
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_PEER3 */
{ 0, 0 }, /* ADD_SERVER3 */
{ 0, 0 }, /* ADD_PEER3 */
REQ_LENGTH_ENTRY(null, null), /* SHUTDOWN */
REQ_LENGTH_ENTRY(null, null), /* ONOFFLINE */
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_SOURCE */
};
static const uint16_t reply_lengths[] = {