Blame pktlength.c

Packit Service 623930
/*
Packit Service 623930
  chronyd/chronyc - Programs for keeping computer clocks accurate.
Packit Service 623930
Packit Service 623930
 **********************************************************************
Packit Service 623930
 * Copyright (C) Richard P. Curnow  1997-2002
Packit Service 623930
 * Copyright (C) Miroslav Lichvar  2014-2016
Packit Service 623930
 * 
Packit Service 623930
 * This program is free software; you can redistribute it and/or modify
Packit Service 623930
 * it under the terms of version 2 of the GNU General Public License as
Packit Service 623930
 * published by the Free Software Foundation.
Packit Service 623930
 * 
Packit Service 623930
 * This program is distributed in the hope that it will be useful, but
Packit Service 623930
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 623930
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 623930
 * General Public License for more details.
Packit Service 623930
 * 
Packit Service 623930
 * You should have received a copy of the GNU General Public License along
Packit Service 623930
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit Service 623930
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit Service 623930
 * 
Packit Service 623930
 **********************************************************************
Packit Service 623930
Packit Service 623930
  =======================================================================
Packit Service 623930
Packit Service 623930
  Routines to compute the expected length of a command or reply packet.
Packit Service 623930
  These operate on the RAW NETWORK packets, from the point of view of
Packit Service 623930
  integer endianness within the structures.
Packit Service 623930
Packit Service 623930
  */
Packit Service 623930
#include "config.h"
Packit Service 623930
Packit Service 623930
#include "sysincl.h"
Packit Service 623930
Packit Service 623930
#include "util.h"
Packit Service 623930
#include "pktlength.h"
Packit Service 623930
Packit Service 623930
#define PADDING_LENGTH_(request_length, reply_length) \
Packit Service 623930
  (uint16_t)((request_length) < (reply_length) ? (reply_length) - (request_length) : 0)
Packit Service 623930
Packit Service 623930
#define PADDING_LENGTH(request_data, reply_data) \
Packit Service 623930
  PADDING_LENGTH_(offsetof(CMD_Request, request_data), offsetof(CMD_Reply, reply_data))
Packit Service 623930
Packit Service 623930
#define REQ_LENGTH_ENTRY(request_data_field, reply_data_field) \
Packit Service 623930
  { offsetof(CMD_Request, data.request_data_field.EOR), \
Packit Service 623930
    PADDING_LENGTH(data.request_data_field.EOR, data.reply_data_field.EOR) }
Packit Service 623930
Packit Service 623930
#define RPY_LENGTH_ENTRY(reply_data_field) \
Packit Service 623930
  offsetof(CMD_Reply, data.reply_data_field.EOR)
Packit Service 623930
Packit Service 623930
/* ================================================== */
Packit Service 623930
Packit Service 623930
struct request_length {
Packit Service 623930
  uint16_t command;
Packit Service 623930
  uint16_t padding;
Packit Service 623930
};
Packit Service 623930
Packit Service 623930
static const struct request_length request_lengths[] = {
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* NULL */
Packit Service 623930
  REQ_LENGTH_ENTRY(online, null),               /* ONLINE */
Packit Service 623930
  REQ_LENGTH_ENTRY(offline, null),              /* OFFLINE */
Packit Service 623930
  REQ_LENGTH_ENTRY(burst, null),                /* BURST */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_minpoll, null),       /* MODIFY_MINPOLL */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_maxpoll, null),       /* MODIFY_MAXPOLL */
Packit Service 623930
  REQ_LENGTH_ENTRY(dump, null),                 /* DUMP */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_maxdelay, null),      /* MODIFY_MAXDELAY */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_maxdelayratio, null), /* MODIFY_MAXDELAYRATIO */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_maxupdateskew, null), /* MODIFY_MAXUPDATESKEW */
Packit Service 623930
  REQ_LENGTH_ENTRY(logon, null),                /* LOGON */
Packit Service 623930
  REQ_LENGTH_ENTRY(settime, manual_timestamp),  /* SETTIME */
Packit Service 623930
  { 0, 0 },                                     /* LOCAL */
Packit Service 623930
  REQ_LENGTH_ENTRY(manual, null),               /* MANUAL */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, n_sources),            /* N_SOURCES */
Packit Service 623930
  REQ_LENGTH_ENTRY(source_data, source_data),   /* SOURCE_DATA */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* REKEY */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* ALLOW */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* ALLOWALL */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* DENY */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* DENYALL */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* CMDALLOW */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* CMDALLOWALL */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* CMDDENY */
Packit Service 623930
  REQ_LENGTH_ENTRY(allow_deny, null),           /* CMDDENYALL */
Packit Service 623930
  REQ_LENGTH_ENTRY(ac_check, null),             /* ACCHECK */
Packit Service 623930
  REQ_LENGTH_ENTRY(ac_check, null),             /* CMDACCHECK */
Packit Service 623930
  { 0, 0 },                                     /* ADD_SERVER */
Packit Service 623930
  { 0, 0 },                                     /* ADD_PEER */
Packit Service 623930
  REQ_LENGTH_ENTRY(del_source, null),           /* DEL_SOURCE */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* WRITERTC */
Packit Service 623930
  REQ_LENGTH_ENTRY(dfreq, null),                /* DFREQ */
Packit Service 623930
  REQ_LENGTH_ENTRY(doffset, null),              /* DOFFSET */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, tracking),             /* TRACKING */
Packit Service 623930
  REQ_LENGTH_ENTRY(sourcestats, sourcestats),   /* SOURCESTATS */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, rtc),                  /* RTCREPORT */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* TRIMRTC */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* CYCLELOGS */
Packit Service 623930
  { 0, 0 },                                     /* SUBNETS_ACCESSED - not supported */
Packit Service 623930
  { 0, 0 },                                     /* CLIENT_ACCESSES - not supported */
Packit Service 623930
  { 0, 0 },                                     /* CLIENT_ACCESSES_BY_INDEX - not supported */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, manual_list),          /* MANUAL_LIST */
Packit Service 623930
  REQ_LENGTH_ENTRY(manual_delete, null),        /* MANUAL_DELETE */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* MAKESTEP */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, activity),             /* ACTIVITY */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_minstratum, null),    /* MODIFY_MINSTRATUM */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_polltarget, null),    /* MODIFY_POLLTARGET */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_maxdelaydevratio, null), /* MODIFY_MAXDELAYDEVRATIO */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* RESELECT */
Packit Service 623930
  REQ_LENGTH_ENTRY(reselect_distance, null),    /* RESELECTDISTANCE */
Packit Service 623930
  REQ_LENGTH_ENTRY(modify_makestep, null),      /* MODIFY_MAKESTEP */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, smoothing),            /* SMOOTHING */
Packit Service 623930
  REQ_LENGTH_ENTRY(smoothtime, null),           /* SMOOTHTIME */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* REFRESH */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, server_stats),         /* SERVER_STATS */
Packit Service 623930
  REQ_LENGTH_ENTRY(client_accesses_by_index,
Packit Service 623930
                   client_accesses_by_index),   /* CLIENT_ACCESSES_BY_INDEX2 */
Packit Service 623930
  REQ_LENGTH_ENTRY(local, null),                /* LOCAL2 */
Packit Service 623930
  REQ_LENGTH_ENTRY(ntp_data, ntp_data),         /* NTP_DATA */
Packit Service 623930
  { 0, 0 },                                     /* ADD_SERVER2 */
Packit Service 623930
  { 0, 0 },                                     /* ADD_PEER2 */
Packit Service 623930
  REQ_LENGTH_ENTRY(ntp_source, null),           /* ADD_SERVER3 */
Packit Service 623930
  REQ_LENGTH_ENTRY(ntp_source, null),           /* ADD_PEER3 */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* SHUTDOWN */
Packit Service 623930
  REQ_LENGTH_ENTRY(null, null),                 /* ONOFFLINE */
Packit Service 623930
};
Packit Service 623930
Packit Service 623930
static const uint16_t reply_lengths[] = {
Packit Service 623930
  0,                                            /* empty slot */
Packit Service 623930
  RPY_LENGTH_ENTRY(null),                       /* NULL */
Packit Service 623930
  RPY_LENGTH_ENTRY(n_sources),                  /* N_SOURCES */
Packit Service 623930
  RPY_LENGTH_ENTRY(source_data),                /* SOURCE_DATA */
Packit Service 623930
  0,                                            /* MANUAL_TIMESTAMP */
Packit Service 623930
  RPY_LENGTH_ENTRY(tracking),                   /* TRACKING */
Packit Service 623930
  RPY_LENGTH_ENTRY(sourcestats),                /* SOURCESTATS */
Packit Service 623930
  RPY_LENGTH_ENTRY(rtc),                        /* RTC */
Packit Service 623930
  0,                                            /* SUBNETS_ACCESSED - not supported */
Packit Service 623930
  0,                                            /* CLIENT_ACCESSES - not supported */
Packit Service 623930
  0,                                            /* CLIENT_ACCESSES_BY_INDEX - not supported */
Packit Service 623930
  0,                                            /* MANUAL_LIST - not supported */
Packit Service 623930
  RPY_LENGTH_ENTRY(activity),                   /* ACTIVITY */
Packit Service 623930
  RPY_LENGTH_ENTRY(smoothing),                  /* SMOOTHING */
Packit Service 623930
  RPY_LENGTH_ENTRY(server_stats),               /* SERVER_STATS */
Packit Service 623930
  RPY_LENGTH_ENTRY(client_accesses_by_index),   /* CLIENT_ACCESSES_BY_INDEX2 */
Packit Service 623930
  RPY_LENGTH_ENTRY(ntp_data),                   /* NTP_DATA */
Packit Service 623930
  RPY_LENGTH_ENTRY(manual_timestamp),           /* MANUAL_TIMESTAMP2 */
Packit Service 623930
  RPY_LENGTH_ENTRY(manual_list),                /* MANUAL_LIST2 */
Packit Service 623930
};
Packit Service 623930
Packit Service 623930
/* ================================================== */
Packit Service 623930
Packit Service 623930
int
Packit Service 623930
PKL_CommandLength(CMD_Request *r)
Packit Service 623930
{
Packit Service 623930
  uint32_t type;
Packit Service 623930
  int command_length;
Packit Service 623930
Packit Service 623930
  assert(sizeof (request_lengths) / sizeof (request_lengths[0]) == N_REQUEST_TYPES);
Packit Service 623930
Packit Service 623930
  type = ntohs(r->command);
Packit Service 623930
  if (type >= N_REQUEST_TYPES)
Packit Service 623930
    return 0;
Packit Service 623930
Packit Service 623930
  command_length = request_lengths[type].command;
Packit Service 623930
  if (!command_length)
Packit Service 623930
    return 0;
Packit Service 623930
Packit Service 623930
  return command_length + PKL_CommandPaddingLength(r);
Packit Service 623930
}
Packit Service 623930
Packit Service 623930
/* ================================================== */
Packit Service 623930
Packit Service 623930
int
Packit Service 623930
PKL_CommandPaddingLength(CMD_Request *r)
Packit Service 623930
{
Packit Service 623930
  uint32_t type;
Packit Service 623930
Packit Service 623930
  if (r->version < PROTO_VERSION_PADDING)
Packit Service 623930
    return 0;
Packit Service 623930
Packit Service 623930
  type = ntohs(r->command);
Packit Service 623930
Packit Service 623930
  if (type >= N_REQUEST_TYPES)
Packit Service 623930
    return 0;
Packit Service 623930
Packit Service 623930
  return request_lengths[ntohs(r->command)].padding;
Packit Service 623930
}
Packit Service 623930
Packit Service 623930
/* ================================================== */
Packit Service 623930
Packit Service 623930
int
Packit Service 623930
PKL_ReplyLength(CMD_Reply *r)
Packit Service 623930
{
Packit Service 623930
  uint32_t type;
Packit Service 623930
Packit Service 623930
  assert(sizeof (reply_lengths) / sizeof (reply_lengths[0]) == N_REPLY_TYPES);
Packit Service 623930
Packit Service 623930
  type = ntohs(r->reply);
Packit Service 623930
Packit Service 623930
  /* Note that reply type codes start from 1, not 0 */
Packit Service 623930
  if (type < 1 || type >= N_REPLY_TYPES)
Packit Service 623930
    return 0;
Packit Service 623930
Packit Service 623930
  return reply_lengths[type];
Packit Service 623930
}
Packit Service 623930
Packit Service 623930
/* ================================================== */
Packit Service 623930