File: cmdparse.c

package info (click to toggle)
chrony 1.14-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,480 kB
  • ctags: 2,091
  • sloc: ansic: 15,266; sh: 194; makefile: 113
file content (142 lines) | stat: -rw-r--r-- 3,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
  $Header: /cvs/src/chrony/cmdparse.c,v 1.7 1999/04/19 20:27:29 richard Exp $

  =======================================================================

  chronyd/chronyc - Programs for keeping computer clocks accurate.

  Copyright (C) 1997-1999 Richard P. Curnow
  All rights reserved.

  For conditions of use, refer to the file LICENCE.

  =======================================================================
  
  Module for parsing various forms of directive and command lines that
  are common to the configuration file and to the command client.

  */

#include "sysincl.h"

#include "cmdparse.h"
#include "nameserv.h"

/* ================================================== */

CPS_Status
CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
{
  int ok, n, done;
  char cmd[2048], hostname[2048];
  CPS_Status result;
  
  src->port = 123;
  src->params.minpoll = 6;
  src->params.maxpoll = 10;
  src->params.presend_minpoll = 0;
  src->params.authkey = INACTIVE_AUTHKEY;
  src->params.max_delay = 16.0;
  src->params.max_delay_ratio = 16384.0;
  src->params.online = 1;

  result = CPS_Success;
  
  ok = 0;
  if (sscanf(line, "%s%n", hostname, &n) == 1) {
    src->ip_addr = DNS_Name2IPAddress(hostname);
    if (src->ip_addr != DNS_Failed_Address) {
      ok = 1;
    }
  }

  if (!ok) {
    result = CPS_BadHost;
  } else {

    line += n;
    
    /* Parse subfields */
    ok = 1;
    done = 0;
    do {
      if (sscanf(line, "%s%n", cmd, &n) == 1) {
        
        line += n;
        
        if (!strncasecmp(cmd, "port", 4)) {
          if (sscanf(line, "%hu%n", &src->port, &n) != 1) {
            result = CPS_BadPort;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "minpoll", 7)) {
          if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1) {
            result = CPS_BadMinpoll;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "maxpoll", 7)) {
          if (sscanf(line, "%d%n", &src->params.maxpoll, &n) != 1) {
            result = CPS_BadMaxpoll;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "presend", 7)) {
          if (sscanf(line, "%d%n", &src->params.presend_minpoll, &n) != 1) {
            result = CPS_BadPresend;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
          /* This MUST come before the following one ! */
        } else if (!strncasecmp(cmd, "maxdelayratio", 13)) {
          if (sscanf(line, "%lf%n", &src->params.max_delay_ratio, &n) != 1) {
            result = CPS_BadMaxdelayratio;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "maxdelay", 8)) {
          if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1) {
            result = CPS_BadMaxdelay;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "key", 3)) {
          if (sscanf(line, "%lu%n", &src->params.authkey, &n) != 1) {
            result = CPS_BadKey;
            ok = 0;
            done = 1;
          } else {
            line += n;
          }
        } else if (!strncasecmp(cmd, "offline", 7)) {
          src->params.online = 0;
        } else {
          result = CPS_BadOption;
          ok = 0;
          done = 1;
        }
      } else {
        done = 1;
      }
    } while (!done);
  }

  return result;
    
}

/* ================================================== */