File: ntp.c

package info (click to toggle)
lbcd 3.5.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: ansic: 11,073; sh: 1,816; perl: 503; makefile: 165
file content (74 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download | duplicates (4)
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
/*
 * lbcd load module to check NTP server.
 *
 * Written by Larry Schwimmer
 * Copyright 1997, 1998, 2008, 2012
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * See LICENSE for licensing terms.
 */

#include <config.h>
#include <portable/system.h>

#include <server/internal.h>
#include <modules/modules.h>
#include <modules/monlist.h>
#include <util/macros.h>


/*
 * Probe an NTP server and determine how many peers it has, returning that
 * number or -1 on an error.  Takes the host and a timeout and defaults to
 * probing localhost.
 */
static int
probe_ntp(const char *host, int timeout)
{
    int sd;
    int retval = 0;

    sd = udp_connect(host ? host : "localhost", "ntp", 123);
    if (sd == -1)
        return -1;
    else {
        retval = monlist(sd, timeout);
        close(sd);
    }
    return retval;
}


/*
 * The module interface with the rest of lbcd.
 */
int
lbcd_ntp_weight(uint32_t *weight_val, uint32_t *incr_val UNUSED, int timeout,
                const char *portarg UNUSED, struct lbcd_reply *lb UNUSED)
{
    *weight_val = (uint32_t) probe_ntp("localhost", timeout);
    return (*weight_val == (uint32_t) -1) ? -1 : 0;
}


/*
 * Test routine.
 */
#ifdef MAIN
int
main(int argc, char *argv[])
{
    int status;

    status = probe_ntp(argv[1]);
    if (status <= 0) {
        printf("ntp service not available\n");
        return -1;
    } else {
        printf("%s ntp service has %d peers\n",
               argv[1] ? argv[1] : "localhost",
               status);
    }
    return 0;
}
#endif