⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 144

📁 Unix/Linux 网络时间协议版本3 Network Time Protocol Version 3 (NTP) distribution for Unix systems
💻
📖 第 1 页 / 共 5 页
字号:
        up->ev.event_handler = arc_event_handler;        up->ev.next = up->ev.prev = 0;  /* Be ultra-cautious... */        ENQUEUE(up);                    /* Start ticking. */        /* Spread out resyncs so that they should remain separated. */        up->next_resync = current_time + INITIAL_RESYNC_DELAY + (67*unit)%1009;        up->resyncing = 0;              /* Not resyncing yet. */        up->saved_flags = 0;            /* Default is all flags off. */        return(1);}/* * arc_shutdown - shut down the clock */static voidarc_shutdown(unit, peer)        int unit;        struct peer *peer;{        register struct arcunit *up;        struct refclockproc *pp;        pp = peer->procptr;        up = (struct arcunit *)pp->unitptr;        up->ev.event_handler = dummy_event_handler;        TIMER_DEQUEUE(&(up->ev));       /* Stop ticking. */        io_closeclock(&pp->io);        free(up);}/*Compute space left in output buffer.*/static int space_left(up)register struct arcunit *up;{    int spaceleft;    /* Compute space left in buffer after any pending output. */    for(spaceleft = 0; spaceleft < CMDQUEUELEN; ++spaceleft)        { if(up->cmdqueue[CMDQUEUELEN - 1 - spaceleft] != '\0') { break; } }    return(spaceleft);}/*Send command by copying into command buffer as far forward as possible,after any pending output.Indicate an error by returning 0 if there is not space for the command.*/static intsend_slow(up, fd, s)register struct arcunit *up;int fd;char *s;{    int sl = strlen(s);    int spaceleft = space_left(up);    if(debug > 1) { printf("arc: spaceleft = %d.\n", spaceleft); }    if(spaceleft < sl) {        syslog(LOG_NOTICE, "ARCRON: send-buffer overrun (%d/%d)",                           sl, spaceleft);        return(0);                      /* FAILED! */    }    /* Copy in the command to be sent. */    while(*s) { up->cmdqueue[CMDQUEUELEN - spaceleft--] = *s++; }    return(1);}#ifdef ARCRON_OWN_FILTERstatic int arc_refclock_process P((struct refclockproc *, int, int));static int arc_refclock_sample P((l_fp *, struct refclockproc *, int, int));static int arc_refclock_cmpl_fp P((const void *, const void *));#endif/* Macro indicating action we will take for different quality values. */#define quality_action(q) \        (((q) == QUALITY_UNKNOWN) ?         "UNKNOWN, will use clock anyway" : \            (((q) < MIN_CLOCK_QUALITY_OK) ? "TOO POOR, will not use clock" : \                                            "OK, will use clock"))/* * arc_receive - receive data from the serial interface */static voidarc_receive(rbufp)        struct recvbuf *rbufp;{        register struct arcunit *up;        struct refclockproc *pp;        struct peer *peer;        l_fp trtmp;        char c;        int i, n, wday, month, bst, status;        int last_offset;        /*         * Initialize pointers and read the timecode and timestamp         */        peer = (struct peer *)rbufp->recv_srcclock;        pp = peer->procptr;        up = (struct arcunit *)pp->unitptr;        /*        If the command buffer is empty, and we are resyncing, insert a        g\r quality request into it to poll for signal quality again.        */        if((up->resyncing) && (space_left(up) == CMDQUEUELEN)) {            if(debug > 1) { printf("arc: inserting signal-quality poll.\n"); }            send_slow(up, pp->io.fd, "g\r");        }        /*        The `last_offset' is the offset in lastcode[] of the last byte        received, and which we assume actually received the input        timestamp.        (When we get round to using tty_clk and it is available, we        assume that we will receive the whole timecode with the        trailing \r, and that that \r will be timestamped.  But this        assumption also works if receive the characters one-by-one.)        */        last_offset = pp->lencode+rbufp->recv_length - 1;        /*        We catch a timestamp iff:          * The command code is `o' for a timestamp.          * If ARCRON_MULTIPLE_SAMPLES is undefined then we must have            exactly char in the buffer (the command code) so that we            only sample the first character of the timecode as our            `on-time' character.          * The first character in the buffer is not the echoed `\r'            from the `o` command (so if we are to timestamp an `\r' it            must not be first in the receive buffer with lencode==1.            (Even if we had other characters following it, we probably            would have a premature timestamp on the '\r'.)          * We have received at least one character (I cannot imagine            how it could be otherwise, but anyway...).        */        c = rbufp->recv_buffer[0];        if((pp->lastcode[0] == 'o') &&#ifndef ARCRON_MULTIPLE_SAMPLES           (pp->lencode == 1) &&#endif           ((pp->lencode != 1) || (c != '\r')) &&           (last_offset >= 1)) {            /* Note that the timestamp should be corrected if >1 char rcvd. */            l_fp timestamp = rbufp->recv_time;            if(debug) { /* Show \r as `R', other non-printing char as `?'. */                printf("arc: stamp -->%c<-- (%d chars rcvd)\n",                       ((c == '\r') ? 'R' : (isgraph(c) ? c : '?')),                       rbufp->recv_length);            }            /*            Now correct timestamp by offset of last byte received---we            subtract from the receive time the delay implied by the            extra characters received.            Reject the input if the resulting code is too long, but            allow for the trailing \r, normally not used but a good            handle for tty_clk or somesuch kernel timestamper.            */            if(last_offset > LENARC) {                if(debug) {                    printf("arc: input code too long (%d cf %d); rejected.\n",                           last_offset, LENARC);                }                pp->lencode = 0;                refclock_report(peer, CEVNT_BADREPLY);                return;            }            L_SUBUF(&timestamp, charoffsets[last_offset]);            if(debug > 1) {                printf("arc: %s%d char(s) rcvd, the last for lastcode[%d]; -%sms offset applied.\n",                        ((rbufp->recv_length > 1) ? "*** " : ""),                        rbufp->recv_length,                        last_offset,                        mfptoms((unsigned long)0,                                charoffsets[last_offset],                                1));            }#ifdef ARCRON_MULTIPLE_SAMPLES            /*            If taking multiple samples, capture the current adjusted            sample iff:              * No timestamp has yet been captured (it is zero), OR              * This adjusted timestamp is earlier than the one already                captured, on the grounds that this one suffered less                delay in being delivered to us and is more accurate.            */            if(L_ISZERO(&(up->lastrec)) ||               L_ISGEQ(&(up->lastrec), &timestamp))#endif                {                if(debug > 1) {                    printf("arc: system timestamp captured.\n");#ifdef ARCRON_MULTIPLE_SAMPLES                    if(!L_ISZERO(&(up->lastrec))) {                        l_fp diff = up->lastrec;                        L_SUB(&diff, &timestamp);                        printf("arc: adjusted timestamp by -%sms.\n",                               mfptoms(diff.l_i, diff.l_f, 3));                        }#endif                }                up->lastrec = timestamp;            }        }        /* Just in case we still have lots of rubbish in the buffer... */        /* ...and to avoid the same timestamp being reused by mistake, */        /* eg on receipt of the \r coming in on its own after the      */        /* timecode.                                                   */        if(pp->lencode >= LENARC) {            if(debug && (rbufp->recv_buffer[0] != '\r'))                { printf("arc: rubbish in pp->lastcode[].\n"); }            pp->lencode = 0;            return;        }        /* Append input to code buffer, avoiding overflow. */        for(i = 0; i < rbufp->recv_length; i++) {            if(pp->lencode >= LENARC) { break; } /* Avoid overflow... */            c = rbufp->recv_buffer[i];            /* Drop trailing '\r's and drop `h' command echo totally. */            if(c != '\r' && c != 'h') { pp->lastcode[pp->lencode++] = c; }            /*            If we've just put an `o' in the lastcode[0], clear the            timestamp in anticipation of a timecode arriving soon.            We would expect to get to process this before any of the            timecode arrives.            */            if((c == 'o') && (pp->lencode == 1)) {                L_CLR(&(up->lastrec));                if(debug > 1) { printf("arc: clearing timestamp.\n"); }            }        }        /* Handle a quality message. */        if(pp->lastcode[0] == 'g') {            int r, q;            if(pp->lencode < 3) { return; } /* Need more data... */            r = (pp->lastcode[1] & 0x7f); /* Strip parity. */            q = (pp->lastcode[2] & 0x7f); /* Strip parity. */            if(((q & 0x70) != 0x30) || ((q & 0xf) > MAX_CLOCK_QUALITY) ||               ((r & 0x70) != 0x30)) {                /* Badly formatted response. */                if(debug) { printf("arc: bad `g' response %2x %2x.\n", r, q); }                return;            }            if(r == '3') { /* Only use quality value whilst sync in progress. */                up->quality = (q & 0xf);                if(debug) { printf("arc: signal quality %d.\n", up->quality); }            } else if( /* (r == '2') && */ up->resyncing) {                if(debug)                    {                    printf("arc: sync finished, signal quality %d: %s\n",                       up->quality,                       quality_action(up->quality));                    }                syslog(LOG_NOTICE,                       "ARCRON: sync finished, signal quality %d: %s",                       up->quality,                       quality_action(up->quality));                up->resyncing = 0; /* Resync is over. */#ifdef ARCRON_KEEN                /* Clock quality dubious; resync earlier than usual. */                if((up->quality == QUALITY_UNKNOWN) ||                   (up->quality < MIN_CLOCK_QUALITY_OK))                    { up->next_resync = current_time + RETRY_RESYNC_TIME; }#endif            }            pp->lencode = 0;            return;        }        /* Stop now if this is not a timecode message. */        if(pp->lastcode[0] != 'o') {            pp->lencode = 0;            refclock_report(peer, CEVNT_BADREPLY);            return;        }        /* If we don't have enough data, wait for more... */        if(pp->lencode < LENARC) { return; }        /* WE HAVE NOW COLLECTED ONE TIMESTAMP (phew)... */        if(debug > 1) { printf("arc: NOW HAVE TIMESTAMP...\n"); }        /* But check that we actually captured a system timestamp on it. */        if(L_ISZERO(&(up->lastrec))) {            if(debug) { printf("arc: FAILED TO GET SYSTEM TIMESTAMP\n"); }            pp->lencode = 0;            refclock_report(peer, CEVNT_BADREPLY);            return;        }        /* Give us some leeway before deciding that the clock is ill... */        up->pollcnt = 2;        /*        Append a mark of the clock's received signal quality for the        benefit of Derek Mulcahy's Tcl/Tk utility (we map the `unknown'        quality value to `6' for his s/w) and terminate the string for        sure.  This should not go off the buffer end.        */        pp->lastcode[pp->lencode] = ((up->quality == QUALITY_UNKNOWN) ?                                                '6' : ('0' + up->quality));        pp->lastcode[pp->lencode + 1] = '\0'; /* Terminate for printf(). */        record_clock_stats(&peer->srcadr, pp->lastcode);        /* We don't use the micro-/milli- second part... */        pp->usec = 0;        pp->msec = 0;        n = sscanf(pp->lastcode, "o%2d%2d%2d%1d%2d%2d%2d%1d%1d",            &pp->hour, &pp->minute, &pp->second,            &wday, &pp->day, &month, &pp->year, &bst, &status);        /* Validate format and numbers. */        if(n != 9) {            if(debug) { printf("arc: badly formatted data.\n"); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -