FreeBSD/contrib/ntp/ntpd/ntp_io.c
/*
 * extract timestamps from control message buffer
 */
extract timestamps from control message buffer
extract timestamps from control message buffer
static l_fp
fetch_timestamp(
	struct recvbuf *	rb,
	struct msghdr *		msghdr,
	l_fp			ts
	)
{
	struct cmsghdr *	cmsghdr;
	unsigned long		ticks;
	double			fuzz;
	l_fp			lfpfuzz;
	l_fp			nts;
#ifdef DEBUG_TIMING
	l_fp			dts;
#endif
	cmsghdr = CMSG_FIRSTHDR(msghdr);
	while (cmsghdr != NULL) {
		switch (cmsghdr->cmsg_type)
		{
#ifdef HAVE_BINTIME
		case SCM_BINTIME:
#endif  
#ifdef HAVE_TIMESTAMPNS
		case SCM_TIMESTAMPNS:
#endif	
#ifdef HAVE_TIMESTAMP
		case SCM_TIMESTAMP:
#endif	
#if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
			switch (cmsghdr->cmsg_type)
			{
#ifdef HAVE_BINTIME
			case SCM_BINTIME:
				{
					struct bintime	pbt;
					memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt));
					nts.l_i = pbt.sec + JAN_1970;
					nts.l_uf = (u_int32)(pbt.frac >> 32);
					if (sys_tick > measured_tick &&
					    sys_tick > 1e-9) {
						ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
						nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
					}
					DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
						    pbt.sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
				}
				break;
#endif  
#ifdef HAVE_TIMESTAMPNS
			case SCM_TIMESTAMPNS:
				{
					struct timespec	pts;
					memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts));
					if (sys_tick > measured_tick &&
					    sys_tick > 1e-9) {
						ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
									sys_tick);
						pts.tv_nsec = (long)(ticks * 1e9 *
								     sys_tick);
					}
					DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
						    pts.tv_sec, pts.tv_nsec));
					nts = tspec_stamp_to_lfp(pts);
				}
				break;
#endif	
#ifdef HAVE_TIMESTAMP
			case SCM_TIMESTAMP:
				{
					struct timeval	ptv;
					memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv));
					if (sys_tick > measured_tick &&
					    sys_tick > 1e-6) {
						ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
									sys_tick);
						ptv.tv_usec = (long)(ticks * 1e6 *
								    sys_tick);
					}
					DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
						    (intmax_t)ptv.tv_sec, (long)ptv.tv_usec));
					nts = tval_stamp_to_lfp(ptv);
				}
				break;
#endif  
			}
			fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
			DTOLFP(fuzz, &lfpfuzz);
			L_ADD(&nts, &lfpfuzz);
#ifdef DEBUG_TIMING
			dts = ts;
			L_SUB(&dts, &nts);
			collect_timing(rb, "input processing delay", 1,
				       &dts);
			DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
				    lfptoa(&dts, 9)));
#endif	
			ts = nts;  
			break;
#endif	
		default:
			DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
				    cmsghdr->cmsg_type));
		}
		cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
	}
	return ts;
}
static l_fp fetch_timestamp ( struct recvbuf * rb , struct msghdr * msghdr , l_fp ts ) { struct cmsghdr * cmsghdr ; unsigned long ticks ; double fuzz ; l_fp lfpfuzz ; l_fp nts ; #ifdef DEBUG_TIMING l_fp dts ; #endif cmsghdr = CMSG_FIRSTHDR ( msghdr ) ; while ( cmsghdr != NULL ) { switch ( cmsghdr -> cmsg_type ) { #ifdef HAVE_BINTIME case SCM_BINTIME : #endif #ifdef HAVE_TIMESTAMPNS case SCM_TIMESTAMPNS : #endif #ifdef HAVE_TIMESTAMP case SCM_TIMESTAMP : #endif #if defined ( HAVE_BINTIME ) || defined ( HAVE_TIMESTAMPNS ) || defined ( HAVE_TIMESTAMP ) switch ( cmsghdr -> cmsg_type ) { #ifdef HAVE_BINTIME case SCM_BINTIME : { struct bintime pbt ; memcpy ( & pbt , CMSG_DATA ( cmsghdr ) , sizeof ( pbt ) ) ; nts . l_i = pbt . sec + JAN_1970 ; nts . l_uf = ( u_int32 ) ( pbt . frac >> 32 ) ; if ( sys_tick > measured_tick && sys_tick > 1e-9 ) { ticks = ( unsigned long ) ( nts . l_uf / ( unsigned long ) ( sys_tick * FRAC ) ) ; nts . l_uf = ( unsigned long ) ( ticks * ( unsigned long ) ( sys_tick * FRAC ) ) ; } DPRINTF ( 4 , ( " \n " , pbt . sec , ( unsigned long ) ( ( nts . l_uf / FRAC ) * 1e9 ) ) ) ; } break ; #endif #ifdef HAVE_TIMESTAMPNS case SCM_TIMESTAMPNS : { struct timespec pts ; memcpy ( & pts , CMSG_DATA ( cmsghdr ) , sizeof ( pts ) ) ; if ( sys_tick > measured_tick && sys_tick > 1e-9 ) { ticks = ( unsigned long ) ( ( pts . tv_nsec * 1e-9 ) / sys_tick ) ; pts . tv_nsec = ( long ) ( ticks * 1e9 * sys_tick ) ; } DPRINTF ( 4 , ( " \n " , pts . tv_sec , pts . tv_nsec ) ) ; nts = tspec_stamp_to_lfp ( pts ) ; } break ; #endif #ifdef HAVE_TIMESTAMP case SCM_TIMESTAMP : { struct timeval ptv ; memcpy ( & ptv , CMSG_DATA ( cmsghdr ) , sizeof ( ptv ) ) ; if ( sys_tick > measured_tick && sys_tick > 1e-6 ) { ticks = ( unsigned long ) ( ( ptv . tv_usec * 1e-6 ) / sys_tick ) ; ptv . tv_usec = ( long ) ( ticks * 1e6 * sys_tick ) ; } DPRINTF ( 4 , ( " \n " , ( intmax_t ) ptv . tv_sec , ( long ) ptv . tv_usec ) ) ; nts = tval_stamp_to_lfp ( ptv ) ; } break ; #endif } fuzz = ntp_random ( ) * 2. / FRAC * sys_fuzz ; DTOLFP ( fuzz , & lfpfuzz ) ; L_ADD ( & nts , & lfpfuzz ) ; #ifdef DEBUG_TIMING dts = ts ; L_SUB ( & dts , & nts ) ; collect_timing ( rb , " " , 1 , & dts ) ; DPRINTF ( 4 , ( " \n " , lfptoa ( & dts , 9 ) ) ) ; #endif ts = nts ; break ; #endif default : DPRINTF ( 4 , ( " \n " , cmsghdr -> cmsg_type ) ) ; } cmsghdr = CMSG_NXTHDR ( msghdr , cmsghdr ) ; } return ts ; }
extract timestamps from control message buffer
extract timestamps from control message buffer
/* HAVE_BINTIME */ /* HAVE_TIMESTAMPNS */ /* HAVE_TIMESTAMP */ /*
					 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
					 */ /* HAVE_BINTIME */ /* HAVE_TIMESTAMPNS */ /* HAVE_TIMESTAMP */ /* DEBUG_TIMING */ /* network time stamp */ /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */