├── .gitattributes ├── .gitignore ├── README.md ├── main.go ├── netapp └── sntp.go ├── netevent ├── handler.go ├── listener.go ├── listening.go ├── reactor.go └── transport.go └── sntp ├── client.go ├── doc.go └── server.go /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sntp 2 | ==== 3 | 4 | ### A implementation of NTP server with Golang 5 | ##### What this? 6 | - Base on [RFC 2030](http://tools.ietf.org/html/rfc2030) 7 | - Multiple equipments sync time from local 8 | - Design for multiple equipments which can't connect to internet and need synchronization time 9 | - Compatible with [ntpdate](http://www.eecis.udel.edu/~mills/ntp/html/ntpdate.html) service on the linux 10 | - NTP client is fork from [beevik](https://github.com/beevik/ntp/),a better client 11 | 12 | #### Usage manual 13 | ##### 1. install Golang 14 | 15 | Please reference [Go install](https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/01.1.md) chapter of open-source Golang book "build-web-application-with-golang". 16 | 17 | ##### 2. install Sntp 18 | 19 | $ go get github.com/btfak/sntp 20 | 21 | ##### More? 22 | [My Blog](http://www.btfak.com) 23 | 24 | ##### License 25 | [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). 26 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | //package main 2 | //about: btfak.com 3 | //create: 2013-9-25 4 | //update: 2016-08-22 5 | 6 | package main 7 | 8 | import ( 9 | "github.com/btfak/sntp/netapp" 10 | "github.com/btfak/sntp/netevent" 11 | ) 12 | 13 | func main() { 14 | var handler = netapp.GetHandler() 15 | netevent.Reactor.ListenUdp(123, handler) 16 | netevent.Reactor.Run() 17 | } 18 | -------------------------------------------------------------------------------- /netapp/sntp.go: -------------------------------------------------------------------------------- 1 | //package netapp 2 | //author: btfak.com 3 | //create: 2013-9-24 4 | //update: 2016-08-22 5 | 6 | package netapp 7 | 8 | import ( 9 | "github.com/btfak/sntp/netevent" 10 | "github.com/btfak/sntp/sntp" 11 | "net" 12 | "strconv" 13 | "strings" 14 | ) 15 | 16 | var handler *Handler 17 | 18 | type Handler struct { 19 | netevent.UdpHandler 20 | } 21 | 22 | func GetHandler() *Handler { 23 | if handler == nil { 24 | handler = new(Handler) 25 | } 26 | return handler 27 | } 28 | 29 | // DatagramReceived 30 | // every udp request trigger it. 31 | func (p *Handler) DatagramReceived(data []byte, addr net.Addr) { 32 | res, err := sntp.Serve(data) 33 | if err == nil { 34 | ip, port := spliteAddr(addr.String()) 35 | p.UdpWrite(string(res), ip, port) 36 | } 37 | } 38 | 39 | func spliteAddr(addr string) (string, int) { 40 | ip := strings.Split(addr, ":")[0] 41 | port := strings.Split(addr, ":")[1] 42 | p, _ := strconv.Atoi(port) 43 | return ip, p 44 | } 45 | -------------------------------------------------------------------------------- /netevent/handler.go: -------------------------------------------------------------------------------- 1 | //package netevent 2 | //about: btfak.com 3 | //create: 2013-7-20 4 | 5 | package netevent 6 | 7 | import () 8 | 9 | type UdpHandler struct { 10 | udptransport Transport 11 | } 12 | 13 | type TcpHandler struct { 14 | tcptransport Transport 15 | } 16 | 17 | func (p *UdpHandler) SetUdpTransport(transport Transport) { 18 | p.udptransport = transport 19 | } 20 | 21 | func (p *TcpHandler) SetTcpTransport(transport Transport) { 22 | p.tcptransport = transport 23 | } 24 | 25 | func (p *UdpHandler) UdpWrite(data string, addr string, port int) { 26 | p.udptransport.Write(data, addr, port) 27 | } 28 | 29 | func (p *TcpHandler) TcpWrite(data string, addr string, port int) { 30 | //p.transport.Write(data,addr,port) 31 | } 32 | -------------------------------------------------------------------------------- /netevent/listener.go: -------------------------------------------------------------------------------- 1 | //package netevent 2 | //author: btfak.com 3 | //create: 2013-7-20 4 | 5 | package netevent 6 | 7 | import ( 8 | "net" 9 | ) 10 | 11 | type UdpClient interface { 12 | DatagramReceived(data []byte, addr net.Addr) 13 | SetUdpTransport(Transport) 14 | } 15 | 16 | type UnixHandler interface { 17 | UnixReceived(data []byte, conn *net.UnixConn) 18 | } 19 | 20 | type TcpClient interface { 21 | DataReceived(data []byte, conn *net.TCPConn) 22 | SetTcpTransport(Transport) 23 | } 24 | -------------------------------------------------------------------------------- /netevent/listening.go: -------------------------------------------------------------------------------- 1 | //package netevent 2 | //author: btfak.com 3 | //create: 2013-7-20 4 | 5 | package netevent 6 | 7 | import ( 8 | "fmt" 9 | "net" 10 | "strconv" 11 | ) 12 | 13 | type _reactor struct { 14 | udp_listeners map[int]UdpClient 15 | tcp_clients map[int]TcpClient 16 | unix_listeners map[string]UnixHandler 17 | udp_conn map[int]*net.UDPConn 18 | tcp_listeners map[int]*net.TCPListener 19 | unix_conn map[string]*net.UnixListener 20 | timer []*LaterCalling 21 | } 22 | 23 | func (p *_reactor) ListenUnix(addr string, unix UnixHandler) { 24 | fmt.Printf("Add listener on %s\n", addr) 25 | if p.unix_listeners == nil { 26 | p.unix_listeners = make(map[string]UnixHandler) 27 | } 28 | if p.unix_conn == nil { 29 | p.unix_conn = make(map[string]*net.UnixListener) 30 | } 31 | p.unix_listeners[addr] = unix 32 | laddr, err := net.ResolveUnixAddr("unix", addr) 33 | if err != nil { 34 | fmt.Println("resolve addr err") 35 | return 36 | } 37 | c, erl := net.ListenUnix("unix", laddr) 38 | if erl != nil { 39 | fmt.Printf("Listen err, type: %T; value: %q\n", erl, erl) 40 | } else { 41 | p.unix_conn[addr] = c 42 | } 43 | } 44 | 45 | func (p *_reactor) ListenUdp(port int, udp UdpClient) { 46 | laddr, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(port)) 47 | if err == nil { 48 | p.listenUdp(laddr, udp) 49 | } else { 50 | fmt.Println("resolve addr err") 51 | return 52 | } 53 | } 54 | 55 | func (p *_reactor) ListenTcp(port int, tcp TcpClient) { 56 | p.initReactor() 57 | p.tcp_clients[port] = tcp 58 | fmt.Println("listening on " + strconv.Itoa(port)) 59 | laddr, err := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(port)) 60 | if err != nil { 61 | fmt.Println("resolve addr err") 62 | return 63 | } 64 | c, erl := net.ListenTCP("tcp", laddr) 65 | if erl != nil { 66 | fmt.Printf("type: %T; value: %q\n", erl, erl) 67 | } else { 68 | p.tcp_listeners[port] = c 69 | } 70 | transport := new(tcpTransport) 71 | transport.setListener(c) 72 | tcp.SetTcpTransport(transport) 73 | } 74 | 75 | //function for inner-file usage 76 | //helper function for the udp listening of ipv4 and ipv6 77 | func (p *_reactor) listenUdp(addr *net.UDPAddr, udp UdpClient) { 78 | p.initReactor() 79 | p.udp_listeners[addr.Port] = udp 80 | fmt.Println("listening on " + strconv.Itoa(addr.Port)) 81 | c, erl := net.ListenUDP("udp", addr) 82 | if erl != nil { 83 | fmt.Printf("type: %T; value: %q\n", erl, erl) 84 | } else { 85 | p.udp_conn[addr.Port] = c 86 | } 87 | transport := new(udpTransport) 88 | transport.setConn(c) 89 | udp.SetUdpTransport(transport) 90 | } 91 | 92 | //init the reactor 93 | func (p *_reactor) initReactor() { 94 | if p.udp_listeners == nil { 95 | p.udp_listeners = make(map[int]UdpClient) 96 | } 97 | if p.udp_conn == nil { 98 | p.udp_conn = make(map[int]*net.UDPConn) 99 | } 100 | if p.tcp_clients == nil { 101 | p.tcp_clients = make(map[int]TcpClient) 102 | } 103 | if p.tcp_listeners == nil { 104 | p.tcp_listeners = make(map[int]*net.TCPListener) 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /netevent/reactor.go: -------------------------------------------------------------------------------- 1 | //package netevent 2 | //author: btfak.com 3 | //create: 2013-7-20 4 | 5 | package netevent 6 | 7 | import ( 8 | "fmt" 9 | "net" 10 | "runtime" 11 | "time" 12 | ) 13 | 14 | var ( 15 | Reactor = new(_reactor) 16 | listening_chan chan int 17 | ) 18 | 19 | type LaterCalling struct { 20 | millisecond int 21 | call func() 22 | } 23 | 24 | type reactor interface { 25 | ListenUdp(port int, client UdpClient) 26 | ListenUnix(net, addr string) 27 | CallLater(microsecond int, latercaller func()) 28 | Run() 29 | } 30 | 31 | func (p *_reactor) CallLater(millisecond int, lc func()) { 32 | calling := new(LaterCalling) 33 | calling.millisecond = millisecond 34 | calling.call = lc 35 | p.timer = append(p.timer, calling) 36 | } 37 | 38 | func (p *_reactor) Run() { 39 | runtime.GOMAXPROCS(len(p.udp_conn) + len(p.unix_conn)) 40 | for port, l := range p.udp_conn { 41 | go handleUdpConnection(l, p.udp_listeners[port]) 42 | } 43 | for addr, l := range p.unix_conn { 44 | go handleUnixConnection(l, p.unix_listeners[addr]) 45 | } 46 | for addr, l := range p.tcp_listeners { 47 | go handleTcpListener(l, p.tcp_clients[addr]) 48 | } 49 | for len(p.timer) > 0 { 50 | caller := p.timer[0] 51 | p.timer = p.timer[1:] 52 | selectTimer(caller) 53 | } 54 | for { 55 | fmt.Println("============") 56 | select { 57 | case <-listening_chan: 58 | fmt.Println("--------") 59 | } 60 | } 61 | } 62 | 63 | func selectTimer(caller *LaterCalling) { 64 | select { 65 | case <-time.After(time.Duration(caller.millisecond) * time.Millisecond): 66 | caller.call() 67 | } 68 | } 69 | 70 | func handleUdpConnection(conn *net.UDPConn, client UdpClient) { 71 | for { 72 | data := make([]byte, 512) 73 | read_length, remoteAddr, err := conn.ReadFromUDP(data[0:]) 74 | if err != nil { // EOF, or worse 75 | return 76 | } else { 77 | } 78 | if read_length > 0 { 79 | go panicWrapping(func() { 80 | client.DatagramReceived(data[0:read_length], remoteAddr) 81 | }) 82 | } 83 | } 84 | } 85 | 86 | func panicWrapping(f func()) { 87 | defer func() { 88 | recover() 89 | }() 90 | f() 91 | } 92 | 93 | func handleTcpListener(listener *net.TCPListener, client TcpClient) { 94 | for { 95 | data := make([]byte, 1024) 96 | conn, err := listener.AcceptTCP() 97 | if err != nil { 98 | fmt.Println(err) 99 | continue 100 | } 101 | read_length, err := conn.Read(data[0:]) 102 | if err != nil { // EOF, or worse 103 | fmt.Println(err) 104 | continue 105 | } 106 | if read_length > 0 { 107 | go panicWrapping(func() { 108 | handleOneTcpConnect(client, data[0:read_length], conn) 109 | }) 110 | } 111 | } 112 | } 113 | 114 | func handleOneTcpConnect(client TcpClient, data []byte, conn *net.TCPConn) { 115 | defer conn.Close() 116 | client.DataReceived(data, conn) 117 | } 118 | 119 | func handleUnixConnection(listener *net.UnixListener, unix UnixHandler) { 120 | for { 121 | data := make([]byte, 512) 122 | conn, err := listener.AcceptUnix() 123 | if err != nil { 124 | fmt.Println(err) 125 | continue 126 | } 127 | read_length, err := conn.Read(data[0:]) 128 | if err != nil { // EOF, or worse 129 | fmt.Println(err) 130 | continue 131 | } 132 | if read_length > 0 { 133 | go panicWrapping(func() { 134 | unix.UnixReceived(data[0:read_length], conn) 135 | }) 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /netevent/transport.go: -------------------------------------------------------------------------------- 1 | //package netevent 2 | //author: btfak.com 3 | //create: 2013-7-20 4 | 5 | package netevent 6 | 7 | import ( 8 | "fmt" 9 | "net" 10 | "strconv" 11 | ) 12 | 13 | type Transport interface { 14 | Write(data string, addr string, port int) 15 | } 16 | 17 | type udpTransport struct { 18 | conn *net.UDPConn 19 | } 20 | 21 | func (p *udpTransport) setConn(conn *net.UDPConn) { 22 | p.conn = conn 23 | } 24 | 25 | type tcpTransport struct { 26 | listener *net.TCPListener 27 | } 28 | 29 | func (p *tcpTransport) setListener(listener *net.TCPListener) { 30 | p.listener = listener 31 | } 32 | 33 | func (p *udpTransport) Write(data string, addr string, port int) { 34 | laddr, err := net.ResolveUDPAddr("udp", addr+":"+strconv.Itoa(port)) 35 | if err != nil { 36 | fmt.Println("resolve addr err") 37 | return 38 | } 39 | _, er := p.conn.WriteTo([]byte(data), laddr) 40 | if er != nil { 41 | fmt.Println(er) 42 | return 43 | } 44 | } 45 | 46 | func (p *tcpTransport) Write(data string, addr string, port int) { 47 | _, err := net.ResolveUDPAddr("udp", addr+":"+strconv.Itoa(port)) 48 | if err != nil { 49 | fmt.Println("resolve addr err") 50 | return 51 | } 52 | } 53 | 54 | type unixTransport struct { 55 | conn *net.UnixConn 56 | } 57 | 58 | func (p *unixTransport) setConn(conn *net.UnixConn) { 59 | p.conn = conn 60 | } 61 | 62 | func (p *unixTransport) Write(data string, addr string, port int) { 63 | laddr, err := net.ResolveUDPAddr("udp", addr+":"+strconv.Itoa(port)) 64 | if err != nil { 65 | fmt.Println("resolve addr err") 66 | return 67 | } 68 | _, er := p.conn.WriteTo([]byte(data), laddr) 69 | if er != nil { 70 | fmt.Println(er) 71 | return 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /sntp/client.go: -------------------------------------------------------------------------------- 1 | // fork from https://github.com/beevik/ntp/ 2 | // Package ntp provides a simple mechanism for querying the current time 3 | // from a remote NTP server. This package only supports NTP client mode 4 | // behavior and version 4 of the NTP protocol. 5 | package sntp 6 | 7 | import ( 8 | "encoding/binary" 9 | "net" 10 | "time" 11 | ) 12 | 13 | type mode byte 14 | 15 | const ( 16 | reserved mode = 0 + iota 17 | symmetricActive 18 | symmetricPassive 19 | client 20 | server 21 | broadcast 22 | controlMessage 23 | reservedPrivate 24 | ) 25 | 26 | type ntpTime struct { 27 | Seconds uint32 28 | Fraction uint32 29 | } 30 | 31 | func (t ntpTime) UTC() time.Time { 32 | nsec := uint64(t.Seconds)*1e9 + (uint64(t.Fraction) * 1e9 >> 32) 33 | return time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nsec)) 34 | } 35 | 36 | type msg struct { 37 | LiVnMode byte // Leap Indicator (2) + Version (3) + Mode (3) 38 | Stratum byte 39 | Poll byte 40 | Precision byte 41 | RootDelay uint32 42 | RootDispersion uint32 43 | ReferenceId uint32 44 | ReferenceTime ntpTime 45 | OriginTime ntpTime 46 | ReceiveTime ntpTime 47 | TransmitTime ntpTime 48 | } 49 | 50 | // SetVersion sets the NTP protocol version on the message. 51 | func (m *msg) SetVersion(v byte) { 52 | m.LiVnMode = (m.LiVnMode & 0xc7) | v<<3 53 | } 54 | 55 | // SetMode sets the NTP protocol mode on the message. 56 | func (m *msg) SetMode(md mode) { 57 | m.LiVnMode = (m.LiVnMode & 0xf8) | byte(md) 58 | } 59 | 60 | // Client returns the "receive time" from the remote NTP server 61 | // specifed as host. NTP client mode is used. 62 | func Client(host string) (time.Time, error) { 63 | raddr, err := net.ResolveUDPAddr("udp", host+":123") 64 | if err != nil { 65 | return time.Now(), err 66 | } 67 | 68 | con, err := net.DialUDP("udp", nil, raddr) 69 | if err != nil { 70 | return time.Now(), err 71 | } 72 | defer con.Close() 73 | con.SetDeadline(time.Now().Add(5 * time.Second)) 74 | 75 | m := new(msg) 76 | m.SetMode(client) 77 | m.SetVersion(4) 78 | 79 | err = binary.Write(con, binary.BigEndian, m) 80 | if err != nil { 81 | return time.Now(), err 82 | } 83 | 84 | err = binary.Read(con, binary.BigEndian, m) 85 | if err != nil { 86 | return time.Now(), err 87 | } 88 | 89 | t := m.ReceiveTime.UTC().Local() 90 | return t, nil 91 | } -------------------------------------------------------------------------------- /sntp/doc.go: -------------------------------------------------------------------------------- 1 | //package sntp 2 | //author: btfak.com 3 | //create: 2013-9-25 4 | 5 | //NTP Message Format 6 | /* 7 | 1 2 3 8 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 9 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 10 | |LI | VN |Mode | Stratum | Poll | Precision | 11 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 12 | | Root Delay | 13 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 14 | | Root Dispersion | 15 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 16 | | Reference Identifier | 17 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 18 | | | 19 | | Reference Timestamp (64) | 20 | | | 21 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 22 | | | 23 | | Originate Timestamp (64) | 24 | | | 25 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 26 | | | 27 | | Receive Timestamp (64) | 28 | | | 29 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 30 | | | 31 | | Transmit Timestamp (64) | 32 | | | 33 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 34 | | Key Identifier (optional) (32) | 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36 | | | 37 | | | 38 | | Message Digest (optional) (128) | 39 | | | 40 | | | 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 42 | */ 43 | 44 | //NTP Timestamp Format 45 | /* 46 | 1 2 3 47 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 48 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 49 | | Seconds | 50 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 51 | | Seconds Fraction (0-padded) | 52 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 53 | */ 54 | package sntp 55 | -------------------------------------------------------------------------------- /sntp/server.go: -------------------------------------------------------------------------------- 1 | //package sntp 2 | //author: btfak.com 3 | //create: 2013-9-25 4 | 5 | package sntp 6 | 7 | import ( 8 | "errors" 9 | "time" 10 | ) 11 | 12 | const ( 13 | LI_NO_WARNING = 0 14 | LI_ALARM_CONDITION = 3 15 | VN_FIRST = 1 16 | VN_LAST = 4 17 | MODE_CLIENT = 3 18 | FROM_1900_TO_1970 = 2208988800 19 | ) 20 | 21 | // Serve 22 | // check the request format. 23 | // get time from local and respond. 24 | func Serve(req []byte) ([]byte, error) { 25 | if validFormat(req) { 26 | res := generate(req) 27 | return res, nil 28 | } 29 | return []byte{}, errors.New("invalid format.") 30 | } 31 | 32 | // validFormat 33 | // check the first byte,include: 34 | // LN:must be 0 or 3 35 | // VN:must be 1,2,3 or 4 36 | // Mode:must be 3 37 | func validFormat(req []byte) bool { 38 | var l = req[0] >> 6 39 | var v = (req[0] << 2) >> 5 40 | var m = (req[0] << 5) >> 5 41 | if (l == LI_NO_WARNING) || (l == LI_ALARM_CONDITION) { 42 | if (v >= VN_FIRST) && (v <= VN_LAST) { 43 | if m == MODE_CLIENT { 44 | return true 45 | } 46 | } 47 | } 48 | return false 49 | } 50 | 51 | // unix time: the number of seconds elapsed since January 1, 1970 UTC 52 | // npt time: the number of seconds elapsed since January 1, 1900 UTC 53 | // 1900~1970 54 | func unix2ntp(u int64) int64 { 55 | return u + FROM_1900_TO_1970 56 | } 57 | 58 | func ntp2unix(n int64) int64 { 59 | return n - FROM_1900_TO_1970 60 | } 61 | 62 | // int2bytes 63 | // format int number to four bytes. 64 | // big endian. 65 | func int2bytes(i int64) []byte { 66 | var b = make([]byte, 4) 67 | h1 := i >> 24 68 | h2 := (i >> 16) - (h1 << 8) 69 | h3 := (i >> 8) - (h1 << 16) - (h2 << 8) 70 | h4 := byte(i) 71 | b[0] = byte(h1) 72 | b[1] = byte(h2) 73 | b[2] = byte(h3) 74 | b[3] = byte(h4) 75 | return b 76 | } 77 | 78 | // generate 79 | /* 80 | Field Name Request Reply 81 | ---------------------------------------------------------- 82 | LI 0 or 3 0 83 | VN 1-4 copied from request 84 | Mode 3 4 85 | Stratum ignore 1 86 | Poll ignore copied from request 87 | Precision ignore -log2 server significant bits 88 | Root Delay ignore 0 89 | Root Dispersion ignore 0 90 | Reference Identifier ignore source ident 91 | Reference Timestamp ignore time of last radio update 92 | Originate Timestamp ignore copied from transmit timestamp 93 | Receive Timestamp ignore time of day 94 | Transmit Timestamp (see text) time of day 95 | */ 96 | func generate(req []byte) []byte { 97 | var second = unix2ntp(time.Now().Unix()) 98 | var fraction = unix2ntp(int64(time.Now().Nanosecond())) 99 | var res = make([]byte, 48) 100 | var vn = req[0] & 0x38 101 | res[0] = vn + 4 102 | res[1] = 1 103 | res[2] = req[2] 104 | res[3] = 0xEC 105 | res[12] = 0x4E 106 | res[13] = 0x49 107 | res[14] = 0x43 108 | res[15] = 0x54 109 | copy(res[16:20], int2bytes(second)[0:]) 110 | copy(res[24:32], req[40:48]) 111 | copy(res[32:36], int2bytes(second)[0:]) 112 | copy(res[36:40], int2bytes(fraction)[0:]) 113 | copy(res[40:48], res[32:40]) 114 | return res 115 | } 116 | --------------------------------------------------------------------------------