分析ntp调用过程

Posted liushuhe1990

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分析ntp调用过程相关的知识,希望对你有一定的参考价值。

//分析ntp调用过程
//刘术河 2016.08.17

Ntp-keygen.c (util):main(
Ntpd.c (ntpd):main(
Ntpd.c (ntpd):main(
Ntpdate.c (ntpdate):main(
Ntpdc.c (ntpdc):main(
Ntpq.c (ntpq):main(
Ntptime.c (util):main(
Sntp.c (sntp):main (
Tickadj.c (util):main(
Tickadj.c (util):main(

//开发板上电只调用ntpd
tp-4.2.6p5 tpdNtpd.c
main(
ntpdmain(argc, argv);
get_systime(&now);
ntp_srandom((int)(now.l_i * now.l_uf));
init_auth();
init_util();
init_restrict();
init_mon();
init_timer();
init_lib();
init_request();
init_control();
init_peer();
#ifdef REFCLOCK
init_refclock();
{
for (i = 0; i < (int)num_refclock_conf; i++)
if (refclock_conf[i]->clock_init != noentry)
(refclock_conf[i]->clock_init)();
//这个refclock_conf----是一个结构体,并调用结构体的clock_init,但是refclock_nmea没有init所以跳过
struct refclock * const refclock_conf[] = {
................
&refclock_nmea, /* 20 REFCLK_GPS_NMEA */
................
}


}
#endif
set_process_priority();
init_proto(); /* Call at high priority */
init_io();
init_loopfilter();
mon_start(MON_ON); /* monitor on by default now */
/* turn off in config if unwanted */

/*
* Get the configuration. This is done in a separate module
* since this will definitely be different for the gizmo board.
*/
getconfig(argc, argv);
report_event(EVNT_SYSRESTART, NULL, NULL);
loop_config(LOOP_DRIFTCOMP, old_drift);
////////////////////////////////////////////////////////////////////////////////
//ntpdNtp_peer.c
1.peer_config
return(newpeer(srcadr, dstadr, hmode, version, minpoll, maxpoll,
newpeer(
if (!refclock_newpeer(peer)) {
refclock_newpeer
refclock_conf[clktype]->clock_start == noentry) {
// tp-4.2.6p5 tpdNtp_proto.c
2.receive
if ((peer = newpeer(&rbufp->recv_srcadr, rbufp->dstadr,
newpeer(
if (!refclock_newpeer(peer)) {
refclock_newpeer
refclock_conf[clktype]->clock_start == noentry) {
//先分析peer_config
//ntpdNtp_request.c
proc = ntp_codes;
inpkt->request == REQ_CONFIG &&
req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
static struct req_proc ntp_codes[] = {
sizeof(struct conf_peer), do_conf },
}

do_conf
if (peer_config(&peeraddr, (struct interface *)0,
//ntpdNtp_config.c
#ifndef SIM //如果sim没有定义,才执行,所以Ntp_config这条分之不执行
config_ntpd(ptree);
#else
config_ntpd
config_peers(ptree);
config_peers
peer_config(&peeraddr,

//再分析receive
Ntpsim.c (ntpd): rbuf.receiver = receive; /* Function to call to process the packet */
Ntptimeset.c (ntpdate): receive(rbuf);
Ntp_io.c (ntpd): rb->receiver = receive;
Ntp_iocompletionport.c (portswinnt tpd): buff->receiver = receive;

0.先看Ntpsim和Ntp_io,最终调用到这里
#ifdef SIM
cc = simulate_server(dest, src, pkt);
#else

1.先看Ntptimeset.c
main
ntptimesetmain
receive(rbuf);
但是这个程序,我们不调用它,所以现在只剩下Ntp_iocompletionport.c
2.分析Ntp_iocompletionport.c
//这个SYS_WINNT没有定义,所以都不执行
#ifdef SYS_WINNT
init_io_completion_port();
#endif /* SYS_WINNT */

thread = (HANDLE)_beginthreadex(
NULL,
0,
iocompletionthread,
NULL,
CREATE_SUSPENDED,
&tid);
ResumeThread(thread);
CloseHandle(thread);

iocompletionthread
case SOCK_RECV:
OnSocketRecv(Key, lpo, BytesTransferred, errstatus);
buff->receiver = receive;

//到这里主线清楚
//ntp-4.2.6p5 tpdNtp_request.c
req_ack
//ntp-4.2.6p5 tpdNtp_io.c
sendpkt(srcadr, inter, -1, (struct pkt *)&rpkt, RESP_HEADER_SIZE);
#ifdef SIM
cc = simulate_server(dest, src, pkt);
#else

 























































































































以上是关于分析ntp调用过程的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 调用过程源码分析

Socket与系统调用深度分析

调用过程分析

进程装载过程分析(execve系统调用分析)

Dubbo 服务调用过程源码分析

Java中的方法调用分析!详细解析静态分派和动态分派的执行过程