~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ntpq/ntpq_ops.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * ntpdc_ops.c - subroutines which are called to perform operations by ntpdc
 
2
 * ntpq_ops.c - subroutines which are called to perform operations by ntpq
3
3
 */
4
4
 
5
5
#include <stdio.h>
 
6
#include <ctype.h>
 
7
#include <sys/types.h>
 
8
#include <sys/time.h>
 
9
#include <netdb.h>
6
10
 
7
11
#include "ntpq.h"
8
12
#include "ntp_stdlib.h"
9
13
 
10
 
#include <ctype.h>
11
 
#include <netdb.h>
12
 
 
13
14
extern char *   chosts[];
14
15
extern char currenthost[];
15
16
extern int      numhosts;
55
56
static  void    pstatus         P((struct parse *, FILE *));
56
57
static  long    when            P((l_fp *, l_fp *, l_fp *));
57
58
static  char *  prettyinterval  P((char *, long));
58
 
static  int doprintpeers        P((struct varlist *, int, int, int, char *, FILE *));
59
 
static  int dogetpeers  P((struct varlist *, int, FILE *));
60
 
static  void    dopeers         P((int, FILE *));
 
59
static  int doprintpeers        P((struct varlist *, int, int, int, char *, FILE *, int));
 
60
static  int dogetpeers  P((struct varlist *, int, FILE *, int));
 
61
static  void    dopeers         P((int, FILE *, int));
61
62
static  void    peers           P((struct parse *, FILE *));
62
63
static  void    lpeers          P((struct parse *, FILE *));
63
 
static  void    doopeers        P((int, FILE *));
 
64
static  void    doopeers        P((int, FILE *, int));
64
65
static  void    opeers          P((struct parse *, FILE *));
65
66
static  void    lopeers         P((struct parse *, FILE *));
66
67
 
138
139
        { "pstatus",    pstatus,    { UINT, NO, NO, NO },
139
140
          { "assocID", "", "", "" },
140
141
          "print status information returned for a peer" },
141
 
        { "peers",  peers,      { NO, NO, NO, NO },
142
 
          { "", "", "", "" },
143
 
          "obtain and print a list of the server's peers" },
144
 
        { "lpeers", lpeers,     { NO, NO, NO, NO },
145
 
          { "", "", "", "" },
146
 
          "obtain and print a list of all peers and clients" },
147
 
        { "opeers", opeers,     { NO, NO, NO, NO },
148
 
          { "", "", "", "" },
149
 
          "print peer list the old way, with dstadr shown rather than refid" },
150
 
        { "lopeers",    lopeers,    { NO, NO, NO, NO },
151
 
          { "", "", "", "" },
152
 
          "obtain and print a list of all peers and clients showing dstadr" },
 
142
        { "peers",  peers,      { OPT|IP_VERSION, NO, NO, NO },
 
143
          { "-4|-6", "", "", "" },
 
144
          "obtain and print a list of the server's peers [IP version]" },
 
145
        { "lpeers", lpeers,     { OPT|IP_VERSION, NO, NO, NO },
 
146
          { "-4|-6", "", "", "" },
 
147
          "obtain and print a list of all peers and clients [IP version]" },
 
148
        { "opeers", opeers,     { OPT|IP_VERSION, NO, NO, NO },
 
149
          { "-4|-6", "", "", "" },
 
150
          "print peer list the old way, with dstadr shown rather than refid [IP version]" },
 
151
        { "lopeers", lopeers,   { OPT|IP_VERSION, NO, NO, NO },
 
152
          { "-4|-6", "", "", "" },
 
153
          "obtain and print a list of all peers and clients showing dstadr [IP version]" },
153
154
        { 0,            0,              { NO, NO, NO, NO },
154
 
          { "", "", "", "" }, "" }
 
155
          { "-4|-6", "", "", "" }, "" }
155
156
};
156
157
 
157
158
 
521
522
        if (res != 0)
522
523
                return 0;
523
524
 
 
525
        if (numhosts > 1)
 
526
                (void) fprintf(fp, "server=%s ", currenthost);
524
527
        if (dsize == 0) {
525
528
                if (associd == 0)
526
529
                        (void) fprintf(fp, "No system%s variables returned\n",
532
535
                return 1;
533
536
        }
534
537
 
 
538
        (void) fprintf(fp,"assID=%d ",associd);
535
539
        printvars(dsize, datap, (int)rstatus, type, fp);
536
540
        return 1;
537
541
}
594
598
        if (res != 0)
595
599
                return;
596
600
 
 
601
        if (numhosts > 1)
 
602
                (void) fprintf(fp, "server=%s ", currenthost);
597
603
        if (dsize == 0)
598
604
                (void) fprintf(fp, "done! (no data returned)\n");
599
 
        else
 
605
        else {
 
606
                (void) fprintf(fp,"assID=%d ",associd);
600
607
                printvars(dsize, datap, (int)rstatus,
601
608
                          (associd != 0) ? TYPE_PEER : TYPE_SYS, fp);
 
609
        }
602
610
        return;
603
611
}
604
612
 
665
673
        if (res != 0)
666
674
                return;
667
675
 
 
676
        if (numhosts > 1)
 
677
                (void) fprintf(fp, "server=%s ", currenthost);
668
678
        if (dsize == 0)
669
679
                (void) fprintf(fp, "done! (no data returned)\n");
670
 
        else
 
680
        else {
 
681
                (void) fprintf(fp,"assID=%d ",associd);
671
682
                printvars(dsize, datap, (int)rstatus,
672
683
                          (associd != 0) ? TYPE_PEER : TYPE_SYS, fp);
 
684
        }
673
685
        return;
674
686
}
675
687
 
864
876
        u_short rstatus;
865
877
 
866
878
        res = doquery(CTL_OP_READSTAT, 0, 0, 0, (char *)0, &rstatus,
867
 
                          &dsize, (char **)&datap);
 
879
                          &dsize, (void *)&datap);
868
880
 
869
881
        if (res != 0)
870
882
                return 0;
871
883
 
872
884
        if (dsize == 0) {
 
885
                if (numhosts > 1)
 
886
                        (void) fprintf(fp, "server=%s ", currenthost);
873
887
                (void) fprintf(fp, "No association ID's returned\n");
874
888
                return 0;
875
889
        }
876
890
 
877
891
        if (dsize & 0x3) {
 
892
                if (numhosts > 1)
 
893
                        (void) fprintf(stderr, "server=%s ", currenthost);
878
894
                (void) fprintf(stderr,
879
895
                                   "***Server returned %d octets, should be multiple of 4\n",
880
896
                                   dsize);
927
943
         * Output a header
928
944
         */
929
945
        (void) fprintf(fp,
930
 
                           "ind assID status  conf reach auth condition  last_event cnt\n");
 
946
                           "\nind assID status  conf reach auth condition  last_event cnt\n");
931
947
        (void) fprintf(fp,
932
948
                           "===========================================================\n");
933
949
        for (i = 0; i < numassoc; i++) {
934
 
                statval = CTL_PEER_STATVAL(assoc_cache[i].status);
 
950
                statval = (u_char) CTL_PEER_STATVAL(assoc_cache[i].status);
935
951
                if (!showall && !(statval & (CTL_PST_CONFIG|CTL_PST_REACH)))
936
952
                        continue;
937
953
                event = CTL_PEER_EVENT(assoc_cache[i].status);
1126
1142
        if (res != 0)
1127
1143
                return;
1128
1144
 
 
1145
        if (numhosts > 1)
 
1146
                (void) fprintf(fp, "server=%s ", currenthost);
1129
1147
        if (dsize == 0) {
1130
1148
                (void) fprintf(fp, "No radio status string returned\n");
1131
1149
                return;
1160
1178
        if (res != 0)
1161
1179
                return;
1162
1180
 
 
1181
        if (numhosts > 1)
 
1182
                (void) fprintf(fp, "server=%s ", currenthost);
1163
1183
        if (dsize == 0) {
1164
1184
                (void) fprintf(fp,
1165
1185
                                   "No information returned for association %u\n",
1167
1187
                return;
1168
1188
        }
1169
1189
 
 
1190
        (void) fprintf(fp,"assID=%d ",associd);
1170
1191
        printvars(dsize, datap, (int)rstatus, TYPE_PEER, fp);
1171
1192
}
1172
1193
 
1231
1252
        return buf;
1232
1253
}
1233
1254
 
 
1255
static char
 
1256
decodeaddrtype(
 
1257
        struct sockaddr_storage *sock
 
1258
        )
 
1259
{
 
1260
        char ch = '-';
 
1261
        u_int32 dummy;
 
1262
        struct sockaddr_in6 *sin6;
 
1263
 
 
1264
        switch(sock->ss_family) {
 
1265
        case AF_INET:
 
1266
                dummy = ((struct sockaddr_in *)sock)->sin_addr.s_addr;
 
1267
                dummy = ntohl(dummy);
 
1268
                ch = (char)(((dummy&0xf0000000)==0xe0000000) ? 'm' :
 
1269
                        ((dummy&0x000000ff)==0x000000ff) ? 'b' :
 
1270
                        ((dummy&0xffffffff)==0x7f000001) ? 'l' :
 
1271
                        ((dummy&0xffffffe0)==0x00000000) ? '-' :
 
1272
                        'u');
 
1273
                break;
 
1274
        case AF_INET6:
 
1275
                sin6 = (struct sockaddr_in6 *)sock;
 
1276
                if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
 
1277
                        ch = 'm';
 
1278
                else
 
1279
                        ch = 'u';
 
1280
                break;
 
1281
        default:
 
1282
                ch = '-';
 
1283
                break;
 
1284
        }
 
1285
        return ch;
 
1286
}
1234
1287
 
1235
1288
/*
1236
1289
 * A list of variables required by the peers command
1295
1348
        int rstatus,
1296
1349
        int datalen,
1297
1350
        char *data,
1298
 
        FILE *fp
 
1351
        FILE *fp,
 
1352
        int af
1299
1353
        )
1300
1354
{
1301
1355
        char *name;
1302
 
        char *value;
 
1356
        char *value = NULL;
1303
1357
        int i;
1304
1358
        int c;
1305
1359
 
1306
 
        u_int32 srcadr;
1307
 
        u_int32 dstadr;
1308
 
        u_long srcport;
1309
 
        const char *dstadr_refid = "0.0.0.0";
1310
 
        u_long stratum;
1311
 
        long ppoll;
1312
 
        long hpoll;
1313
 
        u_long reach;
 
1360
        struct sockaddr_storage srcadr;
 
1361
        struct sockaddr_storage dstadr;
 
1362
        u_long srcport = 0;
 
1363
        char *dstadr_refid = "0.0.0.0";
 
1364
        u_long stratum = 0;
 
1365
        long ppoll = 0;
 
1366
        long hpoll = 0;
 
1367
        u_long reach = 0;
1314
1368
        l_fp estoffset;
1315
1369
        l_fp estdelay;
1316
1370
        l_fp estjitter;
1323
1377
        char type = '?';
1324
1378
        char refid_string[10];
1325
1379
        char whenbuf[8], pollbuf[8];
 
1380
        char clock_name[LENHOSTNAME];
1326
1381
 
1327
1382
        memset((char *)havevar, 0, sizeof(havevar));
1328
1383
        get_systime(&ts);
 
1384
        
 
1385
        memset((char *)&srcadr, 0, sizeof(struct sockaddr_storage));
 
1386
        memset((char *)&dstadr, 0, sizeof(struct sockaddr_storage));
 
1387
 
 
1388
        /* Initialize by zeroing out estimate variables */
 
1389
        memset((char *)&estoffset, 0, sizeof(l_fp));
 
1390
        memset((char *)&estdelay, 0, sizeof(l_fp));
 
1391
        memset((char *)&estjitter, 0, sizeof(l_fp));
 
1392
        memset((char *)&estdisp, 0, sizeof(l_fp));
1329
1393
 
1330
1394
        while (nextvar(&datalen, &data, &name, &value)) {
1331
 
                u_int32 dummy;
 
1395
                struct sockaddr_storage dum_store;
1332
1396
 
1333
1397
                i = findvar(name, peer_var);
1334
1398
                if (i == 0)
1339
1403
                                havevar[HAVE_SRCADR] = 1;
1340
1404
                        break;
1341
1405
                        case CP_DSTADR:
1342
 
                        if (decodenetnum(value, &dummy)) {
1343
 
                                dummy = ntohl(dummy);
1344
 
                                type = ((dummy&0xf0000000)==0xe0000000) ? 'm' :
1345
 
                                        ((dummy&0x000000ff)==0x000000ff) ? 'b' :
1346
 
                                        ((dummy&0xffffffff)==0x7f000001) ? 'l' :
1347
 
                                        ((dummy&0xffffffe0)==0x00000000) ? '-' :
1348
 
                                        'u';
1349
 
                        }
 
1406
                        if (decodenetnum(value, &dum_store))
 
1407
                                type = decodeaddrtype(&dum_store);
1350
1408
                        if (pvl == opeervarlist) {
1351
1409
                                if (decodenetnum(value, &dstadr)) {
1352
1410
                                        havevar[HAVE_DSTADR] = 1;
1353
 
                                        dstadr_refid = numtoa(dstadr);
 
1411
                                        dstadr_refid = stoa(&dstadr);
1354
1412
                                }
1355
1413
                        }
1356
1414
                        break;
1360
1418
                                if (*value == '\0') {
1361
1419
                                        dstadr_refid = "0.0.0.0";
1362
1420
                                } else if (decodenetnum(value, &dstadr)) {
1363
 
                                        if (dstadr == 0)
 
1421
                                        if (SOCKNUL(&dstadr))
1364
1422
                                                dstadr_refid = "0.0.0.0";
 
1423
                                        else if ((dstadr.ss_family == AF_INET)
 
1424
                                            && ISREFCLOCKADR(&dstadr))
 
1425
                                                dstadr_refid =
 
1426
                                                    refnumtoa(&dstadr);
1365
1427
                                        else
1366
 
                                                dstadr_refid = nntohost(dstadr);
 
1428
                                                dstadr_refid =
 
1429
                                                    stoa(&dstadr);
1367
1430
                                } else if ((int)strlen(value) <= 4) {
1368
1431
                                        refid_string[0] = '.';
1369
1432
                                        (void) strcpy(&refid_string[1], value);
1449
1512
                c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3];
1450
1513
        if (numhosts > 1)
1451
1514
                (void) fprintf(fp, "%-*s ", maxhostlen, currenthost);
1452
 
        (void) fprintf(fp,
1453
 
                "%c%-15.15s %-15.15s %2ld %c %4.4s %4.4s  %3lo  %7.7s %8.7s %7.7s\n",
1454
 
                c, nntohost(srcadr), dstadr_refid, stratum, type,
1455
 
                prettyinterval(whenbuf, when(&ts, &rec, &reftime)),
1456
 
                prettyinterval(pollbuf, (int)poll_sec), reach,
1457
 
                lfptoms(&estdelay, 3), lfptoms(&estoffset, 3),
1458
 
                havevar[HAVE_JITTER] ? lfptoms(&estjitter, 3) :
1459
 
                lfptoms(&estdisp, 3));
1460
 
        return (1);
 
1515
        if (af == 0 || srcadr.ss_family == af){
 
1516
                strcpy(clock_name, nntohost(&srcadr));
 
1517
                
 
1518
                (void) fprintf(fp,
 
1519
                        "%c%-15.15s %-15.15s %2ld %c %4.4s %4.4s  %3lo  %7.7s %8.7s %7.7s\n",
 
1520
                        c, clock_name, dstadr_refid, stratum, type,
 
1521
                        prettyinterval(whenbuf, when(&ts, &rec, &reftime)),
 
1522
                        prettyinterval(pollbuf, (int)poll_sec), reach,
 
1523
                        lfptoms(&estdelay, 3), lfptoms(&estoffset, 3),
 
1524
                        havevar[HAVE_JITTER] ? lfptoms(&estjitter, 3) :
 
1525
                        lfptoms(&estdisp, 3));
 
1526
                return (1);
 
1527
        }
 
1528
        else
 
1529
                return(1);
1461
1530
}
1462
1531
 
1463
1532
#undef  HAVE_SRCADR
1485
1554
dogetpeers(
1486
1555
        struct varlist *pvl,
1487
1556
        int associd,
1488
 
        FILE *fp
 
1557
        FILE *fp,
 
1558
        int af
1489
1559
        )
1490
1560
{
1491
1561
        char *datap;
1508
1578
                return 0;
1509
1579
 
1510
1580
        if (dsize == 0) {
 
1581
                if (numhosts > 1)
 
1582
                        (void) fprintf(stderr, "server=%s ", currenthost);
1511
1583
                (void) fprintf(stderr,
1512
1584
                                   "***No information returned for association %d\n",
1513
1585
                                   associd);
1514
1586
                return 0;
1515
1587
        }
1516
1588
 
1517
 
 
1518
 
        return doprintpeers(pvl, associd, (int)rstatus, dsize, datap, fp);
 
1589
        return doprintpeers(pvl, associd, (int)rstatus, dsize, datap, fp, af);
1519
1590
}
1520
1591
 
1521
1592
 
1525
1596
static void
1526
1597
dopeers(
1527
1598
        int showall,
1528
 
        FILE *fp
 
1599
        FILE *fp,
 
1600
        int af
1529
1601
        )
1530
1602
{
1531
1603
        register int i;
1532
1604
        char fullname[LENHOSTNAME];
1533
 
        u_int32 netnum;
 
1605
        struct sockaddr_storage netnum;
1534
1606
 
1535
1607
        if (!dogetassoc(fp))
1536
1608
                return;
1537
1609
 
1538
 
        maxhostlen = 0;
1539
 
        if (numhosts > 1) {
1540
 
                for (i = 0; i < numhosts; ++i)
1541
 
                { if(getnetnum(chosts[i],&netnum,fullname))
 
1610
        for (i = 0; i < numhosts; ++i) {
 
1611
                if (getnetnum(chosts[i], &netnum, fullname, af))
1542
1612
                        if ((int)strlen(fullname) > maxhostlen)
1543
 
                        maxhostlen = strlen(fullname);
1544
 
                }
1545
 
                (void) fprintf(fp, "%-*.*s ", maxhostlen, maxhostlen, "host");
 
1613
                                maxhostlen = strlen(fullname);
1546
1614
        }
1547
 
        fprintf(fp,
1548
 
           "     remote           refid      st t when poll reach   delay   offset  jitter\n");
 
1615
        if (numhosts > 1)
 
1616
                (void) fprintf(fp, "%-*.*s ", maxhostlen, maxhostlen, "server");
 
1617
        (void) fprintf(fp,
 
1618
                           "     remote           refid      st t when poll reach   delay   offset  jitter\n");
1549
1619
        if (numhosts > 1)
1550
1620
                for (i = 0; i <= maxhostlen; ++i)
1551
1621
                (void) fprintf(fp, "=");
1557
1627
                        !(CTL_PEER_STATVAL(assoc_cache[i].status)
1558
1628
                          & (CTL_PST_CONFIG|CTL_PST_REACH)))
1559
1629
                        continue;
1560
 
                if (!dogetpeers(peervarlist, (int)assoc_cache[i].assid, fp)) {
 
1630
                if (!dogetpeers(peervarlist, (int)assoc_cache[i].assid, fp, af)) {
1561
1631
                        return;
1562
1632
                }
1563
1633
        }
1575
1645
        FILE *fp
1576
1646
        )
1577
1647
{
1578
 
        dopeers(0, fp);
 
1648
        int af = 0;
 
1649
 
 
1650
        if (pcmd->nargs == 1) {
 
1651
                if (pcmd->argval->ival == 6)
 
1652
                        af = AF_INET6;
 
1653
                else
 
1654
                        af = AF_INET;
 
1655
        }
 
1656
        dopeers(0, fp, af);
1579
1657
}
1580
1658
 
1581
1659
 
1589
1667
        FILE *fp
1590
1668
        )
1591
1669
{
1592
 
        dopeers(1, fp);
 
1670
        int af = 0;
 
1671
 
 
1672
        if (pcmd->nargs == 1) {
 
1673
                if (pcmd->argval->ival == 6)
 
1674
                        af = AF_INET6;
 
1675
                else
 
1676
                        af = AF_INET;
 
1677
        }
 
1678
        dopeers(1, fp, af);
1593
1679
}
1594
1680
 
1595
1681
 
1599
1685
static void
1600
1686
doopeers(
1601
1687
        int showall,
1602
 
        FILE *fp
 
1688
        FILE *fp,
 
1689
        int af
1603
1690
        )
1604
1691
{
1605
1692
        register int i;
 
1693
        char fullname[LENHOSTNAME];
 
1694
        struct sockaddr_storage netnum;
1606
1695
 
1607
1696
        if (!dogetassoc(fp))
1608
1697
                return;
1609
1698
 
 
1699
        for (i = 0; i < numhosts; ++i) {
 
1700
                if (getnetnum(chosts[i], &netnum, fullname, af))
 
1701
                        if ((int)strlen(fullname) > maxhostlen)
 
1702
                                maxhostlen = strlen(fullname);
 
1703
        }
 
1704
        if (numhosts > 1)
 
1705
                (void) fprintf(fp, "%-*.*s ", maxhostlen, maxhostlen, "server");
1610
1706
        (void) fprintf(fp,
1611
1707
                           "     remote           local      st t when poll reach   delay   offset    disp\n");
1612
 
        (void) fprintf(fp,
1613
 
                           "                                      (s)  (s)          (ms)     (ms)     (ms)\n");
 
1708
        if (numhosts > 1)
 
1709
                for (i = 0; i <= maxhostlen; ++i)
 
1710
                (void) fprintf(fp, "=");
1614
1711
        (void) fprintf(fp,
1615
1712
                           "==============================================================================\n");
1616
1713
 
1619
1716
                        !(CTL_PEER_STATVAL(assoc_cache[i].status)
1620
1717
                          & (CTL_PST_CONFIG|CTL_PST_REACH)))
1621
1718
                        continue;
1622
 
                if (!dogetpeers(opeervarlist, (int)assoc_cache[i].assid, fp)) {
 
1719
                if (!dogetpeers(opeervarlist, (int)assoc_cache[i].assid, fp, af)) {
1623
1720
                        return;
1624
1721
                }
1625
1722
        }
1637
1734
        FILE *fp
1638
1735
        )
1639
1736
{
1640
 
        doopeers(0, fp);
 
1737
        int af = 0;
 
1738
 
 
1739
        if (pcmd->nargs == 1) {
 
1740
                if (pcmd->argval->ival == 6)
 
1741
                        af = AF_INET6;
 
1742
                else
 
1743
                        af = AF_INET;
 
1744
        }
 
1745
        doopeers(0, fp, af);
1641
1746
}
1642
1747
 
1643
1748
 
1651
1756
        FILE *fp
1652
1757
        )
1653
1758
{
1654
 
        doopeers(1, fp);
 
1759
        int af = 0;
 
1760
 
 
1761
        if (pcmd->nargs == 1) {
 
1762
                if (pcmd->argval->ival == 6)
 
1763
                        af = AF_INET6;
 
1764
                else
 
1765
                        af = AF_INET;
 
1766
        }
 
1767
        doopeers(1, fp, af);
1655
1768
}