Created
June 6, 2018 16:12
-
-
Save Boot-Error/d69f504a25613b49fded4e303fea6ee9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/binary" | |
| "fmt" | |
| "net" | |
| "os" | |
| "time" | |
| ) | |
| const NTP_DELTA uint32 = 2208988800 | |
| type NtpPacket struct { | |
| li_vn_mode uint8 | |
| stratum uint8 | |
| poll uint8 | |
| precision uint8 | |
| rootDelay uint32 | |
| rootDispertion uint32 | |
| refId uint32 | |
| refTm_s uint32 | |
| refTm_f uint32 | |
| origTm_s uint32 | |
| origTm_f uint32 | |
| rxTm_s uint32 | |
| rxTm_f uint32 | |
| txTm_s uint32 | |
| txTm_f uint32 | |
| } | |
| func checkErr(err error) { | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "Error: %s", err) | |
| os.Exit(1) | |
| } | |
| } | |
| func (ntp_p *NtpPacket) fromBytes(b []byte) { | |
| ntp_p.li_vn_mode = b[0] | |
| ntp_p.stratum = b[1] | |
| ntp_p.poll = b[2] | |
| ntp_p.precision = b[3] | |
| offset := 4 | |
| ntp_p.rootDelay = binary.BigEndian.Uint32(b[offset : offset+4]) | |
| offset += 4 | |
| ntp_p.rootDispertion = binary.BigEndian.Uint32(b[offset : offset+4]) | |
| offset += 4 | |
| ntp_p.refId = binary.BigEndian.Uint32(b[offset : offset+4]) | |
| offset += 4 | |
| ntp_p.refTm_s = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| offset += 4 | |
| ntp_p.refTm_f = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| offset += 4 | |
| ntp_p.origTm_s = binary.BigEndian.Uint32(b[offset : offset+4]) | |
| offset += 4 | |
| ntp_p.origTm_f = binary.BigEndian.Uint32(b[offset : offset+4]) | |
| offset += 4 | |
| ntp_p.rxTm_s = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| offset += 4 | |
| ntp_p.rxTm_f = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| offset += 4 | |
| ntp_p.txTm_s = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| offset += 4 | |
| ntp_p.txTm_f = binary.BigEndian.Uint32(b[offset:offset+4]) - NTP_DELTA | |
| } | |
| func (ntp_p NtpPacket) String() string { | |
| return fmt.Sprintf("Reference timestamp: %s\nOrigin timestamp: %s\nReceive timestamp: %s\nTransmit timestamp: %s\n", | |
| time.Unix(int64(ntp_p.refTm_s), int64(ntp_p.refTm_f)), | |
| time.Unix(int64(ntp_p.origTm_s), int64(ntp_p.origTm_f)), | |
| time.Unix(int64(ntp_p.rxTm_s), int64(ntp_p.rxTm_f)), | |
| time.Unix(int64(ntp_p.txTm_s), int64(ntp_p.txTm_f)), | |
| ) | |
| } | |
| func main() { | |
| udpAddr, err := net.ResolveUDPAddr("udp", "in.pool.ntp.org:123") | |
| checkErr(err) | |
| conn, err := net.DialUDP("udp", nil, udpAddr) | |
| checkErr(err) | |
| buf := make([]byte, 48) | |
| buf[0] = 0x1b | |
| for i := 1; i < 48; i += 1 { | |
| buf[i] = 0x0 | |
| } | |
| _, err = conn.Write(buf) | |
| checkErr(err) | |
| var data [48]byte | |
| _, err = conn.Read(data[0:]) | |
| checkErr(err) | |
| var received NtpPacket | |
| received.fromBytes(data[0:48]) | |
| fmt.Println(received) | |
| os.Exit(0) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment