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

📄 ntpd.c

📁 ftam等标准协议服务器和客户端的源代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
	static char *knames[] = { "_tickadj", "_hz", "_tick" };	static int *kern_vars[] = {&kern_tickadj, &kern_hz, &kern_tick};	int i;	kmem = open(memory, O_RDONLY);	if (kmem < 0) {		advise (LLOG_EXCEPTIONS, memory, "Can't open");		return;	}	for (i = 0; i < 3; i++) {#ifdef SYS5				(void) strcpy (nl[i].n_name, knames[i]);#else		nl[i].n_name = knames[i];#endif	}#ifdef SYS5	(void) nlist("/unix", nl);#else	(void) nlist("/vmunix", nl);#endif	for (i = 0; i < (sizeof(kern_vars)/sizeof(kern_vars[0])); i++) {		long where;		if ((where = nl[i].n_value) == 0) {			advise (LLOG_EXCEPTIONS, NULLCP,				"Unknown kernal var %s", nl[i].n_name);			continue;		}		if (lseek(kmem, where, L_SET) == -1) {			advise (LLOG_EXCEPTIONS, "", "lseek for %s fails",			       nl[i].n_name);			continue;		}		if (read(kmem, (char *)kern_vars[i], sizeof(int)) != sizeof(int)) {			advise (LLOG_EXCEPTIONS, "", "read for %s fails",			       nl[i].n_name);			*kern_vars[i] = 0;		}	}#ifdef	SETTICKADJ	/*	 *  If desired value of tickadj is not specified in the configuration	 *  file, compute a "reasonable" value here, based on the assumption 	 *  that we don't have to slew more than 2ms every 4 seconds.	 *	 *  TODO: the 500 needs to be parameterized.	 */	if (tickadj == 0 && kern_hz)		tickadj = 500/kern_hz;	TRACE (1, ("kernel vars: tickadj = %d, hz = %d, tick = %d",		   kern_tickadj, kern_hz, kern_tick));	TRACE (1, ("desired tickadj = %d, dotickadj = %d", tickadj,		   dotickadj));	if (dotickadj && tickadj && (tickadj != kern_tickadj)) {		(void) close(kmem);		if ((kmem = open(memory, O_RDWR)) >= 0) {			if (lseek(kmem, (long)nl[0].n_value, L_SET) == -1) {				advise (LLOG_EXCEPTIONS, memory, "lseek fails");				(void) close(kmem);				tickadj = 0;			}			if (tickadj && write(kmem, (char *)&tickadj,					     sizeof(tickadj)) !=			    sizeof(tickadj)) {				advise (LLOG_EXCEPTIONS, memory, "tickadj set fails");				tickadj = 0;			} 			if (tickadj && tickadj != kern_tickadj)				advise (LLOG_NOTICE, NULLCP,				       "System tickadj SET to %d",				       tickadj);		} else {			advise (LLOG_EXCEPTIONS, memory, "Can't open");		}	}#endif	/* SETTICKADJ */	(void) close(kmem);	/*	 *  If we have successfully discovered `hz' from the kernel, then we	 *  can set sys.precision, if it has not already been specified.  If	 *  no value of `hz' is available, then use default (-6)	 */	if (sys.precision == 0) {		if (kern_hz <= 64)			sys.precision = -6;		else if (kern_hz <= 128)			sys.precision = -7;		else if (kern_hz <= 256)			sys.precision = -8;		else if (kern_hz <= 512)			sys.precision = -9;		else if (kern_hz <= 1024)			sys.precision = -10;		else sys.precision = -11;		advise (LLOG_NOTICE, NULLCP,			"sys.precision set to %d from sys clock of %d HZ",		       sys.precision, kern_hz);	}}/* * Given host or net name or internet address in dot notation assign the * internet address in byte format. source is ../routed/startup.c with minor * changes to detect syntax errors.  * * We now try to interpret the name as in address before we go off and bother * the domain name servers. * * Unfortunately the library routine inet_addr() does not detect mal formed * addresses that have characters or byte values > 255.  */GetHostName(name, addr)	char *name;	struct Naddr *addr;{	long HostAddr;	struct hostent *hp;	char	*cp;	if (cp = index (name, ':')) {		*cp ++ = '\0';		if (strcmp (name, "INET") == 0)			addr->type = AF_INET;		else if (strcmp (name, "OSI") == 0)			addr->type = AF_OSI;		else {			advise  (LLOG_EXCEPTIONS, NULLCP, "Unknown prefix %s", name);			return NOTOK;		}		name = cp;				}	else 		addr->type = AF_INET;	if (addr->type == AF_INET && (HostAddr = inet_addr(name)) != -1) {		addr->inet_ad.sin_addr.s_addr = (u_long) HostAddr;		addr->inet_ad.sin_family = AF_INET;		addr->inet_ad.sin_port = servport;		return OK;	}	if (addr->type == AF_INET && (hp = gethostbyname(name)) != NULL) {		if (hp->h_addrtype != AF_INET)			return NOTOK;		bcopy((char *) hp->h_addr, (char *) &addr->inet_ad.sin_addr,		      hp->h_length);		addr->inet_ad.sin_family = hp->h_addrtype;		addr->inet_ad.sin_port = servport;		return OK;	}	if (addr->type == AF_OSI) {		struct PSAPaddr *pa;		if ((pa = str2paddr (name)) == NULLPA)			return NOTOK;		addr->psap_ad = *pa;		return OK;	}	return (NOTOK);}/* every hour, dump some useful information to the log */static voidhourly() {	char buf[200];	register int p = 0;	static double drifts[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };	static int drift_count = 0;	extern double drift_comp, compliance;	extern int peer_switches, peer_sw_inhibited;	(void) sprintf(buf, "stats: dc %f comp %f peersw %d inh %d",		       drift_comp, compliance, peer_switches,		       peer_sw_inhibited);	if (sys.peer == NULL) {		(void) strcat(buf, " UNSYNC");#ifdef	REFCLOCK	} else if (sys.peer->flags & PEER_FL_REFCLOCK) {		p = strlen(buf);		(void) sprintf(buf + p, " off %f SYNC %.4s %d",			       sys.peer->estoffset,			       sys.peer->refid.rid_string,			       sys.peer->stratum);#endif	} else {		p = strlen(buf);		(void) sprintf(buf + p, " off %f SYNC %s %d",			       sys.peer->estoffset,			       paddr (&sys.peer->src),			       sys.peer->stratum);	}	advise (LLOG_NOTICE, NULLCP, buf);	/*	 *  If the drift compensation snapshot file is open, then write	 *  the current value to it.  Since there's only one block in the	 *  file, and no one else is reading it, we'll just keep the file	 *  open and write to it.	 */	if (drift_fd >= 0) {		drifts[drift_count % 5] = drift_comp;		/* works out to be 70 bytes */		(void) sprintf(buf,		     "%+12.10f %+12.10f %+12.10f %+12.10f %+12.10f %4d\n",			       drifts[drift_count % 5],			       drifts[(drift_count+4) % 5],			       drifts[(drift_count+3) % 5],			       drifts[(drift_count+2) % 5],			       drifts[(drift_count+1) % 5],			       drift_count + 1);		(void) lseek(drift_fd, 0L, L_SET);		if (write(drift_fd, buf, strlen(buf)) < 0) {			advise (LLOG_EXCEPTIONS, "write error", "drift comp file");		}		drift_count++;	}}/* Debugging stuff *//* ARGSUSED */#if	defined(DEBUG) && defined(SIGUSR1) && defined(SIGUSR2)static SFD incdebug(sig)int sig;{	if (debug == 255)		return;	debug++;	advise (LLOG_DEBUG, NULLCP, "DEBUG LEVEL %d", debug);}/* ARGSUSED */static SFDdecdebug(sig)int sig;{	if (debug == 0)		return;	debug--;	advise (LLOG_DEBUG, NULLCP, "DEBUG LEVEL %d", debug);}#endif/* ARGSUSED */static SFDfinish(sig)	int sig;{	exit(1);}#ifdef	REFCLOCKstruct refclock {	int fd;	int (*reader)();	struct refclock *next;} *refclocks = NULL;int init_clock_local(), read_clock_local();#ifdef PSTIint init_clock_psti(), read_clock_psti();#endif PSTIinit_clock(name, type)char *name, *type;{	struct refclock *r;	int (*reader)();	int cfd;	if (strcmp(type, "local") == 0) {		reader = read_clock_local;		cfd = init_clock_local(name);	}#ifdef PSTI	else if (strcmp(type, "psti") == 0) {		reader = read_clock_psti;		cfd = init_clock_psti(name);	}#endif PSTI	else {		advise (LLOG_EXCEPTIONS, NULLCP,			"Unknown reference clock type (%s)", type);		return(-1);	}	if (cfd >= 0) {		r = (struct refclock *)malloc(sizeof(struct refclock));		r->fd = cfd;		r->reader = reader;		r->next = refclocks;		refclocks = r;	}	return(cfd);}read_clock(cfd, tvpp, otvpp)int cfd;struct timeval **tvpp, **otvpp;{	struct refclock *r;	for (r = refclocks; r; r = r->next)		if(r->fd == cfd)			return((r->reader)(cfd, tvpp, otvpp));	return(1); /* Can't happen */}#endifcreate_listeners (){	(void) create_sockets(servport);	create_osilisten (osiaddress);}char	*paddr (addr)struct Naddr *addr;{	static char buf[128];	switch (addr -> type) {	    case AF_UNSPEC:		return "None";	    case AF_INET:		return ntoa (&addr -> inet_ad);			    case AF_OSI:		return paddr2str (&addr -> psap_ad, NULLNA);	    default:		(void) sprintf (buf, "Unknown address type %d", addr -> type);		return buf;	}}envinit (){	int s;	if (!debug) {		if (fork())			exit(0);		for (s = getdtablesize(); s >= 0; s--)			(void) close(s);		(void) open("/", 0);		(void) dup2(0, 1);		(void) dup2(0, 2);		(void) setpgrp(0, getpid());#ifdef TIOCNOTTY		s = open("/dev/tty", O_RDWR);		if (s >= 0) {			(void) ioctl(s, TIOCNOTTY, (char *) 0);			(void) close(s);		}#endif		ll_hdinit (pgm_log, myname);	}	else	{		pgm_log -> ll_events = LLOG_ALL;		ll_dbinit (pgm_log, myname);	}	advise (LLOG_NOTICE, NULLCP,		"%s starting: version $Revision: 9.0 $ patchlevel %d",		myname, PATCHLEVEL);#ifdef SYS5	(void) nice (priority);#else	(void) setpriority(PRIO_PROCESS, 0, priority);#endif#ifdef	NOSWAP	if (noswap)		if (plock(PROCLOCK) != 0)			advise (LLOG_EXCEPTIONS, "failed", "plock()");#endif}#include <varargs.h>#ifndef	lintvoid	adios (va_alist)va_dcl{    va_list ap;    extern LLog *pgm_log;    va_start (ap);        _ll_log (pgm_log, LLOG_FATAL, ap);    va_end (ap);    _exit (1);}#else/* VARARGS2 */void	adios (what, fmt)char   *what,       *fmt;{    adios (what, fmt);}#endif#ifndef	lintvoid	advise (va_alist)va_dcl{    int	    code;    extern LLog    *pgm_log;    va_list ap;    va_start (ap);        code = va_arg (ap, int);    _ll_log (pgm_log, code, ap);    va_end (ap);}#else/* VARARGS3 */void	advise (code, what, fmt)char   *what,       *fmt;int	code;{    advise (code, what, fmt);}#endifaddr_compare (pa1, pa2)struct Naddr *pa1, *pa2;{	if (pa1 -> type != pa2 -> type)		return 0;	switch (pa1 -> type) {	    case AF_INET:		if (pa1 -> inet_ad.sin_addr.s_addr !=		    pa2 -> inet_ad.sin_addr.s_addr)			return 0;		if (pa1 -> inet_ad.sin_port != pa2 -> inet_ad.sin_port)			return 0;		return 1;	    case AF_OSI:		return psapaddr_cmp (&pa1 -> psap_ad, &pa2 -> psap_ad);	    default:		return 0;	}}psapaddr_cmp (pa1, pa2)struct PSAPaddr *pa1, *pa2;{	char	*ps1, *ps2;	if ((ps1 = _paddr2str (pa1, NULLNA, -1)) == NULLCP)		return 0;	if ((ps2 = _paddr2str (pa2, NULLNA, -1)) == NULLCP)		return 0;	if (strcmp (ps1, ps2) == 0)		return 1;	return 0;}struct ntp_peer *find_peer (n){	struct ntp_peer *peer;	for (peer = peer_list.head; peer; peer = peer->next)		if (peer -> sock == n)			return peer;	TRACE (1, ("Can't find peer with sock %d", n));	return NULL;}struct intf *getintf (n)int	*n;{	int acount;	struct intf *ap;	for (acount = 0, ap = addrs; acount < nintf; ap++, acount++)		if (ap -> flags == 0) {			*n = acount;			return ap;		}	if (nintf == 0)		addrs = (struct intf *) malloc (sizeof *ap);	else		addrs = (struct intf *)realloc ((char *)addrs,						(unsigned)(nintf+1) * sizeof *ap);	if (addrs == NULL)		adios ("memory", "out of");	ap = &addrs[nintf];	bzero ((char *)ap, sizeof *ap);	*n = nintf++;	return ap;}

⌨️ 快捷键说明

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