CMD_Reply rx_message;
   char line[2048], keyfile[2048], *command, *arg, *password;
   const char *hashname;
-  unsigned long key_id = 0, key_id2 = -1;
-  int ret;
+  uint32_t key_id = 0, key_id2;
+  int key_id_valid = 1, ret;
   FILE *in;
 
   in = fopen(filename, "r");
     if (!strcasecmp(command, "keyfile")) {
       snprintf(keyfile, sizeof (keyfile), "%s", arg);
     } else if (!strcasecmp(command, "commandkey")) {
-      if (sscanf(arg, "%lu", &key_id) != 1)
-        key_id = -1;
+      key_id_valid = sscanf(arg, "%"SCNu32, &key_id) == 1;
     }
   }
   fclose(in);
 
-  if (!*keyfile || key_id == -1) {
+  if (!*keyfile || !key_id_valid) {
     fprintf(stderr, "Could not read keyfile or commandkey in file %s\n", filename);
     return 0;
   }
     return 0;
   }
 
+  key_id2 = key_id + 1;
   while (fgets(line, sizeof (line), in)) {
     CPS_NormalizeLine(line);
     if (!*line || !CPS_ParseKey(line, &key_id2, &hashname, &password))
       ret = 0;
     }
   } else {
-    fprintf(stderr, "Could not find key %lu in keyfile %s\n", key_id, keyfile);
+    fprintf(stderr, "Could not find key %"PRIu32" in keyfile %s\n", key_id, keyfile);
     ret = 0;
   }
 
 
             line += n;
           }
         } else if (!strcasecmp(cmd, "key")) {
-          if (sscanf(line, "%lu%n", &src->params.authkey, &n) != 1 ||
+          if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
               src->params.authkey == INACTIVE_AUTHKEY) {
             result = CPS_BadKey;
             done = 1;
 /* ================================================== */
 
 int
-CPS_ParseKey(char *line, unsigned long *id, const char **hash, char **key)
+CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
 {
   char *s1, *s2, *s3, *s4;
 
   if (!*s2 || *s4)
     return 0;
 
-  if (sscanf(s1, "%lu", id) != 1)
+  if (sscanf(s1, "%"SCNu32, id) != 1)
     return 0;
 
   if (*s3) {
 
 extern char *CPS_SplitWord(char *line);
 
 /* Parse a key from keyfile */
-extern int CPS_ParseKey(char *line, unsigned long *id, const char **hash, char **key);
+extern int CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key);
 
 #endif /* GOT_CMDPARSE_H */
 
 
 static int parse_string(char *line, char **result);
 static int parse_int(char *line, int *result);
-static int parse_unsignedlong(char *, unsigned long *result);
+static int parse_uint32(char *, uint32_t *result);
 static int parse_double(char *line, double *result);
 static int parse_null(char *line);
 
 static char *keys_file = NULL;
 static char *drift_file = NULL;
 static char *rtc_file = NULL;
-static unsigned long command_key_id;
+static uint32_t command_key_id;
 static double max_update_skew = 1000.0;
 static double correction_time_ratio = 3.0;
 static double max_clock_error = 1.0; /* in ppm */
   } else if (!strcasecmp(command, "combinelimit")) {
     parse_double(p, &combine_limit);
   } else if (!strcasecmp(command, "commandkey")) {
-    parse_unsignedlong(p, &command_key_id);
+    parse_uint32(p, &command_key_id);
   } else if (!strcasecmp(command, "corrtimeratio")) {
     parse_double(p, &correction_time_ratio);
   } else if (!strcasecmp(command, "deny")) {
 /* ================================================== */
 
 static int
-parse_unsignedlong(char *line, unsigned long *result)
+parse_uint32(char *line, uint32_t *result)
 {
   check_number_of_args(line, 1);
-  if (sscanf(line, "%lu", result) != 1) {
+  if (sscanf(line, "%"SCNu32, result) != 1) {
     command_parse_error();
     return 0;
   }
 
 /* ================================================== */
 
-unsigned long
+uint32_t
 CNF_GetCommandKey(void)
 {
   return command_key_id;
 
 extern int CNF_GetLogTempComp(void);
 extern char *CNF_GetKeysFile(void);
 extern char *CNF_GetRtcFile(void);
-extern unsigned long CNF_GetCommandKey(void);
+extern uint32_t CNF_GetCommandKey(void);
 extern int CNF_GetGenerateCommandKey(void);
 extern int CNF_GetDumpOnExit(void);
 extern int CNF_GetManualEnabled(void);
 
 
 
 typedef struct {
-  unsigned long id;
+  uint32_t id;
   char *val;
   int len;
   int hash_id;
 static ARR_Instance keys;
 
 static int command_key_valid;
-static int command_key_id;
+static uint32_t command_key_id;
 static int cache_valid;
-static unsigned long cache_key_id;
+static uint32_t cache_key_id;
 static int cache_key_pos;
 
 /* ================================================== */
 
 static int
-generate_key(unsigned long key_id)
+generate_key(uint32_t key_id)
 {
 #ifdef FEAT_SECHASH
   unsigned char key[20];
     return 0;
   }
 
-  fprintf(f, "\n%lu %s HEX:", key_id, hashname);
+  fprintf(f, "\n%"PRIu32" %s HEX:", key_id, hashname);
   for (i = 0; i < sizeof (key); i++)
     fprintf(f, "%02hhX", key[i]);
   fprintf(f, "\n");
   /* Erase the key from stack */
   memset(key, 0, sizeof (key));
 
-  LOG(LOGS_INFO, LOGF_Keys, "Generated key %lu", key_id);
+  LOG(LOGS_INFO, LOGF_Keys, "Generated key %"PRIu32, key_id);
 
   return 1;
 }
 /* ================================================== */
 
 static int
-determine_hash_delay(unsigned long key_id)
+determine_hash_delay(uint32_t key_id)
 {
   NTP_Packet pkt;
   struct timeval before, after;
   /* Add on a bit extra to allow for copying, conversions etc */
   min_usecs += min_usecs >> 4;
 
-  DEBUG_LOG(LOGF_Keys, "authentication delay for key %lu: %ld useconds", key_id, min_usecs);
+  DEBUG_LOG(LOGF_Keys, "authentication delay for key %"PRIu32": %ld useconds", key_id, min_usecs);
 
   return min_usecs;
 }
 {
   unsigned int i, line_number;
   FILE *in;
-  unsigned long key_id;
+  uint32_t key_id;
   char line[2048], *keyval, *key_file;
   const char *hashname;
   Key key;
 
     key.hash_id = HSH_GetHashId(hashname);
     if (key.hash_id < 0) {
-      LOG(LOGS_WARN, LOGF_Keys, "Unknown hash function in key %lu", key_id);
+      LOG(LOGS_WARN, LOGF_Keys, "Unknown hash function in key %"PRIu32, key_id);
       continue;
     }
 
     key.len = UTI_DecodePasswordFromText(keyval);
     if (!key.len) {
-      LOG(LOGS_WARN, LOGF_Keys, "Could not decode password in key %lu", key_id);
+      LOG(LOGS_WARN, LOGF_Keys, "Could not decode password in key %"PRIu32, key_id);
       continue;
     }
 
   /* Check for duplicates */
   for (i = 1; i < ARR_GetSize(keys); i++) {
     if (get_key(i - 1)->id == get_key(i)->id)
-      LOG(LOGS_WARN, LOGF_Keys, "Detected duplicate key %lu", get_key(i - 1)->id);
+      LOG(LOGS_WARN, LOGF_Keys, "Detected duplicate key %"PRIu32, get_key(i - 1)->id);
   }
 
   /* Erase any passwords from stack */
 /* ================================================== */
 
 static int
-lookup_key(unsigned long id)
+lookup_key(uint32_t id)
 {
   Key specimen, *where, *keys_ptr;
   int pos;
 /* ================================================== */
 
 static Key *
-get_key_by_id(unsigned long key_id)
+get_key_by_id(uint32_t key_id)
 {
   int position;
 
 
 /* ================================================== */
 
-unsigned long
+uint32_t
 KEY_GetCommandKey(void)
 {
   if (!command_key_valid) {
 /* ================================================== */
 
 int
-KEY_KeyKnown(unsigned long key_id)
+KEY_KeyKnown(uint32_t key_id)
 {
   return get_key_by_id(key_id) != NULL;
 }
 /* ================================================== */
 
 int
-KEY_GetAuthDelay(unsigned long key_id)
+KEY_GetAuthDelay(uint32_t key_id)
 {
   Key *key;
 
 /* ================================================== */
 
 int
-KEY_GenerateAuth(unsigned long key_id, const unsigned char *data, int data_len,
+KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
     unsigned char *auth, int auth_len)
 {
   Key *key;
 /* ================================================== */
 
 int
-KEY_CheckAuth(unsigned long key_id, const unsigned char *data, int data_len,
+KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
     const unsigned char *auth, int auth_len)
 {
   Key *key;
 
 #ifndef GOT_KEYS_H
 #define GOT_KEYS_H
 
+#include "sysincl.h"
+
 extern void KEY_Initialise(void);
 extern void KEY_Finalise(void);
 
 extern void KEY_Reload(void);
 
-extern unsigned long KEY_GetCommandKey(void);
+extern uint32_t KEY_GetCommandKey(void);
 
-extern int KEY_GetKey(unsigned long key_id, char **key, int *len);
-extern int KEY_KeyKnown(unsigned long key_id);
-extern int KEY_GetAuthDelay(unsigned long key_id);
+extern int KEY_GetKey(uint32_t key_id, char **key, int *len);
+extern int KEY_KeyKnown(uint32_t key_id);
+extern int KEY_GetAuthDelay(uint32_t key_id);
 
-extern int KEY_GenerateAuth(unsigned long key_id, const unsigned char *data,
+extern int KEY_GenerateAuth(uint32_t key_id, const unsigned char *data,
     int data_len, unsigned char *auth, int auth_len);
-extern int KEY_CheckAuth(unsigned long key_id, const unsigned char *data,
+extern int KEY_CheckAuth(uint32_t key_id, const unsigned char *data,
     int data_len, const unsigned char *auth, int auth_len);
 
 #endif /* GOT_KEYS_H */
 
                                    the association is symmetric). Note
                                    : we don't authenticate if we can't
                                    find the key in our database. */
-  unsigned long auth_key_id;    /* The ID of the authentication key to
+  uint32_t auth_key_id;          /* The ID of the authentication key to
                                    use. */
 
   /* Count of how many packets we have transmitted since last successful
 
   if (params->authkey == INACTIVE_AUTHKEY) {
     result->do_auth = 0;
-    result->auth_key_id = 0UL;
+    result->auth_key_id = 0;
   } else {
     result->do_auth = 1;
     result->auth_key_id = params->authkey;
     if (!KEY_KeyKnown(result->auth_key_id)) {
-      LOG(LOGS_WARN, LOGF_NtpCore, "Source %s added with unknown key %lu",
+      LOG(LOGS_WARN, LOGF_NtpCore, "Source %s added with unknown key %"PRIu32,
           UTI_IPToString(&result->remote_addr.ip_addr), result->auth_key_id);
     }
   }
 /* ================================================== */
 
 static int
-check_packet_auth(NTP_Packet *pkt, unsigned long keyid, int auth_len)
+check_packet_auth(NTP_Packet *pkt, uint32_t keyid, int auth_len)
 {
   return KEY_CheckAuth(keyid, (void *)pkt, offsetof(NTP_Packet, auth_keyid), 
       (void *)&(pkt->auth_data), auth_len);
                 int my_poll, /* The log2 of the local poll interval */
                 int version, /* The NTP version to be set in the packet */
                 int do_auth, /* Boolean indicating whether to authenticate the packet or not */
-                unsigned long key_id, /* The authentication key ID */
+                uint32_t key_id, /* The authentication key ID */
                 NTP_int64 *orig_ts, /* Originate timestamp (from received packet) */
                 struct timeval *local_rx, /* Local time request packet was received */
                 struct timeval *local_tx, /* RESULT : Time this reply
           sizeof (message.auth_keyid) + auth_len);
     } else {
       DEBUG_LOG(LOGF_NtpCore,
-                "Could not generate auth data with key %lu to send packet",
+                "Could not generate auth data with key %"PRIu32" to send packet",
                 key_id);
       return 0;
     }
   double pkt_root_delay;
   double pkt_root_dispersion;
 
-  unsigned long auth_key_id;
+  uint32_t auth_key_id;
 
   /* The local time to which the (offset, delay, dispersion) triple will
      be taken to relate.  For client/server operation this is practically
   NTP_Mode my_mode;
   int my_poll, version;
   int valid_auth, auth_len;
-  unsigned long key_id;
+  uint32_t key_id;
 
   /* Ignore the packet if it wasn't received by server socket */
   if (!NIO_IsServerSocket(local_addr->sock_fd)) {
 
   int iburst;
   int min_stratum;
   int poll_target;
-  unsigned long authkey;
+  uint32_t authkey;
   double max_delay;
   double max_delay_ratio;
   double max_delay_dev_ratio;
 #define SRC_DEFAULT_MAXDELAYDEVRATIO 10.0
 #define SRC_DEFAULT_MINSTRATUM 0
 #define SRC_DEFAULT_POLLTARGET 6
-#define INACTIVE_AUTHKEY 0UL
+#define INACTIVE_AUTHKEY 0
 
 #endif /* GOT_SRCPARAMS_H */