Esempio n. 1
0
/* Find a peer that do not exist by addr */
TEST_F(ntpPeerTest, FindNoExistPeerByAddr) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="192.168.1.1";
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);

    peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);

    EXPECT_EQ((struct peer*)NULL, peer);    
}
Esempio n. 2
0
/*
 * refclock_buginfo - return debugging info
 *
 * This routine is used mainly for debugging. It returns designated
 * values from the interface structure that can be displayed using
 * ntpdc and the clkbug command.
 */
void
refclock_buginfo(
	sockaddr_u *srcadr,	/* clock address */
	struct refclockbug *bug /* output structure */
	)
{
	struct peer *peer;
	struct refclockproc *pp;
	int clktype;
	int unit;
	unsigned u;

	/*
	 * Check for valid address and peer structure
	 */
	if (!ISREFCLOCKADR(srcadr))
		return;

	clktype = (u_char) REFCLOCKTYPE(srcadr);
	unit = REFCLOCKUNIT(srcadr);

	peer = findexistingpeer(srcadr, NULL, -1);

	if (NULL == peer || NULL == peer->procptr)
		return;

	pp = peer->procptr;

	/*
	 * Copy structure values
	 */
	bug->nvalues = 8;
	bug->svalues = 0x0000003f;
	bug->values[0] = pp->year;
	bug->values[1] = pp->day;
	bug->values[2] = pp->hour;
	bug->values[3] = pp->minute;
	bug->values[4] = pp->second;
	bug->values[5] = pp->nsec;
	bug->values[6] = pp->yearstart;
	bug->values[7] = pp->coderecv;
	bug->stimes = 0xfffffffc;
	bug->times[0] = pp->lastref;
	bug->times[1] = pp->lastrec;
	for (u = 2; u < bug->ntimes; u++)
		DTOLFP(pp->filter[u - 2], &bug->times[u]);

	/*
	 * Give the stuff to the clock
	 */
	if (refclock_conf[clktype]->clock_buginfo != noentry)
		(refclock_conf[clktype]->clock_buginfo)(unit, bug, peer);
}
Esempio n. 3
0
/* Find a peer that do not exist by hostname */
TEST_F(ntpPeerTest, FindNoExistPeerByName) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="192.168.1.1";
    char            peerhostname[]="localhost";
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);

    peer = findexistingpeer(&peeraddr, peerhostname, NULL, -1, 0);

    EXPECT_EQ((struct peer*)NULL, peer);    
}
Esempio n. 4
0
/* Add a new peer, then clear all peers */
TEST_F(ntpPeerTest, ClearAllPeer) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="127.0.0.1";
    char            peerhostname[]="localhost";
    
    char            pre_addrstr[]="0.0.0.0";
    char            pre_peerhostname[]="testhost";
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);

    /* add a new peer with MDF_TXONLY_MASK */
    peer = peer_config(&peeraddr,
				NULL,
				loopback_interface,
				MODE_CLIENT,
				NTP_VERSION,
				NTP_MINDPOLL,
				NTP_MAXDPOLL,
				0, /* peerflags */
				0, /* ttl */
				0, /* peerkey */
				NULL /* group ident */);

    peer->cast_flags &= ~(MDF_TXONLY_MASK);

    clear_all();

    /* check the peer with the MDF_TXONLY_MASK */
    EXPECT_NE(peer->ppoll, peer->maxpoll);    
    EXPECT_EQ(peer->hpoll, peer->minpoll);
    EXPECT_NE(peer->disp, MAXDISPERSE);

    /* check a peer without the MDF_TXONLY_MASK */ 
    SetSockaddr4(&peeraddr, pre_addrstr);
    peer = findexistingpeer(&peeraddr, pre_peerhostname, NULL, -1, 0);
    EXPECT_NE((struct peer*)NULL, peer); 
    EXPECT_NE(peer->disp, MAXDISPERSE);
    EXPECT_NE(peer->flip, 1);
    EXPECT_NE(peer->filter_disp[0], MAXDISPERSE);
    EXPECT_NE(peer->leap, LEAP_NOTINSYNC);

    /* refresh the peers, in order to avoid faults */
    refresh_all_peerinterfaces();
}
Esempio n. 5
0
/* set peer dist address success,
   rely on previous test case, peer has no dstadr before. */
TEST_F(ntpPeerTest, SetPeerNewDistAddr) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="192.168.1.1";
    endpt *	prevdstadr;
    u_int   prevpeercnt;
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);
    peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
    EXPECT_NE((struct peer*)NULL, peer);  

    prevdstadr = peer->dstadr;
    prevpeercnt = loopback_interface->peercnt;

    // set_peerdstadr(peer, loopback_interface);
	

    // EXPECT_EQ(1, loopback_interface->peercnt - prevpeercnt);
    // EXPECT_NE(prevdstadr, peer->dstadr);
}
Esempio n. 6
0
/* Find a peer successfully by name */
TEST_F(ntpPeerTest, FindExistPeerByName) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="0.0.0.0";
    char            peerhostname[]="testhost";
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);

    peer_config(&peeraddr,
				peerhostname,
				loopback_interface,
				MODE_CLIENT,
				NTP_VERSION,
				NTP_MINDPOLL,
				NTP_MAXDPOLL,
				0, /* peerflags */
				0, /* ttl */
				0, /* peerkey */
				NULL /* group ident */);

    peer = findexistingpeer(&peeraddr, peerhostname, NULL, -1, 0);

    EXPECT_NE((struct peer*)NULL, peer);    

    /* for next test cases use, check clear_all function */
    
    peer->cast_flags |= (MDF_TXONLY_MASK);
    peer->disp = 1;
    peer->flags |= FLAG_XLEAVE;
    peer->flip = 0;
    peer->filter_disp[0] = 1;
    peer->flags &= ~(FLAG_REFCLOCK);    
    peer->leap = LEAP_NOWARNING;
	
	/* for next ut case FindPeerByAssoc */
	UT_associd = peer->associd;
    
}
Esempio n. 7
0
/* Add a peer that do not exist then we can find it */
TEST_F(ntpPeerTest, FindExistPeerByAddr) {  
    sockaddr_u		peeraddr;
    char            addrstr[]="192.168.1.1";
    struct peer*    peer;
    
    SetSockaddr4(&peeraddr, addrstr);

    peer_config(&peeraddr,
				NULL,  /* NULL if not set MDF_POOL */
				NULL,
				MODE_CLIENT,
				NTP_VERSION,
				NTP_MINDPOLL,
				NTP_MAXDPOLL,
				0, /* peerflags, or 0 */
				0, /* ttl */
				0, /* peerkey */
				NULL /* group ident */);

    peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);

    EXPECT_NE((struct peer*)NULL, peer);    
}
Esempio n. 8
0
/*
 * refclock_control - set and/or return clock values
 *
 * This routine is used mainly for debugging. It returns designated
 * values from the interface structure that can be displayed using
 * ntpdc and the clockstat command. It can also be used to initialize
 * configuration variables, such as fudgetimes, fudgevalues, reference
 * ID and stratum.
 */
void
refclock_control(
	sockaddr_u *srcadr,
	struct refclockstat *in,
	struct refclockstat *out
	)
{
	struct peer *peer;
	struct refclockproc *pp;
	u_char clktype;
	int unit;

	/*
	 * Check for valid address and running peer
	 */
	if (!ISREFCLOCKADR(srcadr))
		return;

	clktype = (u_char)REFCLOCKTYPE(srcadr);
	unit = REFCLOCKUNIT(srcadr);

	peer = findexistingpeer(srcadr, NULL, -1);

	if (NULL == peer || NULL == peer->procptr)
		return;

	pp = peer->procptr;

	/*
	 * Initialize requested data
	 */
	if (in != 0) {
		if (in->haveflags & CLK_HAVETIME1)
			pp->fudgetime1 = in->fudgetime1;
		if (in->haveflags & CLK_HAVETIME2)
			pp->fudgetime2 = in->fudgetime2;
		if (in->haveflags & CLK_HAVEVAL1)
			peer->stratum = pp->stratum = (u_char)in->fudgeval1;
		if (in->haveflags & CLK_HAVEVAL2)
			peer->refid = pp->refid = in->fudgeval2;
		if (in->haveflags & CLK_HAVEFLAG1) {
			pp->sloppyclockflag &= ~CLK_FLAG1;
			pp->sloppyclockflag |= in->flags & CLK_FLAG1;
		}
		if (in->haveflags & CLK_HAVEFLAG2) {
			pp->sloppyclockflag &= ~CLK_FLAG2;
			pp->sloppyclockflag |= in->flags & CLK_FLAG2;
		}
		if (in->haveflags & CLK_HAVEFLAG3) {
			pp->sloppyclockflag &= ~CLK_FLAG3;
			pp->sloppyclockflag |= in->flags & CLK_FLAG3;
		}
		if (in->haveflags & CLK_HAVEFLAG4) {
			pp->sloppyclockflag &= ~CLK_FLAG4;
			pp->sloppyclockflag |= in->flags & CLK_FLAG4;
		}
	}

	/*
	 * Readback requested data
	 */
	if (out != 0) {
		out->haveflags = CLK_HAVETIME1 | CLK_HAVEVAL1 |
			CLK_HAVEVAL2 | CLK_HAVEFLAG4;
		out->fudgetime1 = pp->fudgetime1;
		out->fudgetime2 = pp->fudgetime2;
		out->fudgeval1 = pp->stratum;
		out->fudgeval2 = pp->refid;
		out->flags = (u_char) pp->sloppyclockflag;

		out->timereset = current_time - pp->timestarted;
		out->polls = pp->polls;
		out->noresponse = pp->noreply;
		out->badformat = pp->badformat;
		out->baddata = pp->baddata;

		out->lastevent = pp->lastevent;
		out->currentstatus = pp->currentstatus;
		out->type = pp->type;
		out->clockdesc = pp->clockdesc;
		out->lencode = (u_short)pp->lencode;
		out->p_lastcode = pp->a_lastcode;
	}

	/*
	 * Give the stuff to the clock
	 */
	if (refclock_conf[clktype]->clock_control != noentry)
		(refclock_conf[clktype]->clock_control)(unit, in, out, peer);
}
Esempio n. 9
0
/*
 * peer_config - configure a new association
 */
struct peer *
peer_config(
	struct sockaddr_storage *srcadr,
	struct interface *dstadr,
	int hmode,
	int version,
	int minpoll,
	int maxpoll,
	u_int flags,
	int ttl,
	keyid_t key,
	u_char *keystr
	)
{
	register struct peer *peer;
	u_char cast_flags;

	/*
	 * First search from the beginning for an association with given
	 * remote address and mode. If an interface is given, search
	 * from there to find the association which matches that
	 * destination.  If the given interface is "any", track down
	 * the actual interface, because that's what gets put into the
	 * peer structure.
	 */
	peer = findexistingpeer(srcadr, (struct peer *)0, hmode);
	if (dstadr != 0) {
		while (peer != 0) {
			if (peer->dstadr == dstadr)
				break;
			if (dstadr == ANY_INTERFACE_CHOOSE(srcadr) &&
			    peer->dstadr == findinterface(srcadr))
			     break;
			peer = findexistingpeer(srcadr, peer, hmode);
		}
	}

	/*
	 * We do a dirty little jig to figure the cast flags. This is
	 * probably not the best place to do this, at least until the
	 * configure code is rebuilt. Note only one flag can be set.
	 */
	switch (hmode) {

	case MODE_BROADCAST:
		if(srcadr->ss_family == AF_INET) {
			if (IN_CLASSD(ntohl(((struct sockaddr_in*)srcadr)->sin_addr.s_addr)))
				cast_flags = MDF_MCAST;
			else
				cast_flags = MDF_BCAST;
			break;
		}
		else {
                        if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)srcadr)->sin6_addr))
        	                cast_flags = MDF_MCAST;
	        	else
                        	cast_flags = MDF_BCAST;
                	break;
                }

	case MODE_CLIENT:
		if(srcadr->ss_family == AF_INET) {
			if (IN_CLASSD(ntohl(((struct sockaddr_in*)srcadr)->sin_addr.s_addr)))
				cast_flags = MDF_ACAST;
			else
				cast_flags = MDF_UCAST;
			break;
		}
		else {
			if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)srcadr)->sin6_addr))
				cast_flags = MDF_ACAST;
			else
				cast_flags = MDF_UCAST;
			break;
		}

	default:
		cast_flags = MDF_UCAST;
	}

	/*
	 * If the peer is already configured, some dope has a duplicate
	 * configureation entry or another dope is wiggling from afar.
	 */
	if (peer != 0) {
		peer->hmode = (u_char)hmode;
		peer->version = (u_char) version;
		peer->minpoll = (u_char) minpoll;
		peer->maxpoll = (u_char) maxpoll;
		peer->flags = flags | FLAG_CONFIG |
			(peer->flags & FLAG_REFCLOCK);
		peer->cast_flags = cast_flags;
		peer->ttl = (u_char) ttl;
		peer->keyid = key;
		peer->precision = sys_precision;
		peer_clear(peer, "RMOT");
		return (peer);
	}

	/*
	 * Here no match has been found, so presumably this is a new
	 * persistent association. Mobilize the thing and initialize its
	 * variables. If emulating ntpdate, force iburst.
	 */
	if (mode_ntpdate)
		flags |= FLAG_IBURST;
	peer = newpeer(srcadr, dstadr, hmode, version, minpoll, maxpoll,
	    flags | FLAG_CONFIG, cast_flags, ttl, key);
	return (peer);
}