/
time-server.go
155 lines (129 loc) · 3.17 KB
/
time-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Flexible time server (RFC 868)
// http://www.faqs.org/rfcs/rfc868.html
//
//
package main
import (
"os"
"fmt"
"strings"
"time"
"net"
"io/ioutil"
"encoding/binary" )
const (
Add = "+"
Subtract = "-"
Fix = "fix"
TIME_OFFSET int64 = 2208988800 // time at 00:00:00 1 Jan 1970 GMT
)
// Op can be "+", "-" or "fix" (case sensitive)
type Offset struct {
Op string
Value int
}
// per-IP offsets, read from file
type HostOffset struct {
Year, Month, Day, Hour, Minute, Second Offset
}
// Read in the offsets for an IP from a file. File name == IP address.
func GetOffset(addr string) (HostOffset, os.Error) {
data, err := ioutil.ReadFile(addr)
o := HostOffset{}
if err != nil {
return o, err
}
str := string(data)
_, err = fmt.Sscanln(str,
&o.Year.Op, &o.Year.Value,
&o.Month.Op, &o.Month.Value,
&o.Day.Op, &o.Day.Value,
&o.Hour.Op, &o.Hour.Value,
&o.Minute.Op, &o.Minute.Value,
&o.Second.Op, &o.Second.Value)
return o, err
}
// Apply offset to local time.
// Returns a Time object: http://golang.org/pkg/time/#Time
func GetFixedUpTime(o HostOffset) (time.Time) {
tm := *time.LocalTime()
switch o.Year.Op {
case Add: tm.Year += int64(o.Year.Value)
case Subtract: tm.Year -= int64(o.Year.Value)
case Fix: tm.Year = int64(o.Year.Value)
}
switch o.Month.Op {
case Add: tm.Month += o.Month.Value
case Subtract: tm.Month -= o.Month.Value
case Fix: tm.Month = o.Month.Value
}
switch o.Day.Op {
case Add: tm.Day += o.Day.Value
case Subtract: tm.Day -= o.Day.Value
case Fix: tm.Day = o.Day.Value
}
switch o.Hour.Op {
case Add: tm.Hour += o.Hour.Value
case Subtract: tm.Hour -= o.Hour.Value
case Fix: tm.Hour = o.Hour.Value
}
switch o.Minute.Op {
case Add: tm.Minute += o.Minute.Value
case Subtract: tm.Minute -= o.Minute.Value
case Fix: tm.Minute = o.Minute.Value
}
switch o.Second.Op {
case Add: tm.Second += o.Second.Value
case Subtract: tm.Second -= o.Second.Value
case Fix: tm.Second = o.Second.Value
}
// fmt.Println(tm.String())
return tm
}
//
// See also FreeBSD inetd implementation:
// http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/inetd/builtins.c?rev=1.45.14.1;content-type=text%2Fplain
// machtime()
//
func RFC868Time(tm time.Time) uint32 {
tm64 := tm.Seconds()
// fmt.Println(tm64)
return uint32((tm64 + TIME_OFFSET) & 0xFFFFFFFF)
}
func ServeTime(conn net.Conn) {
defer conn.Close()
remaddr := conn.RemoteAddr().String()
remIP := strings.Split(remaddr, ":")[0]
fmt.Println("Accepted connection from " + remIP)
hostOff, err := GetOffset(remIP)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get offset (" + err.String() + "), closing connection")
return
}
// tm32 := uint32((time.Seconds() + TIME_OFFSET) & 0xFFFFFFFF)
tm32 := RFC868Time(GetFixedUpTime(hostOff))
// fmt.Println(time.LocalTime().Seconds())
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, tm32)
conn.Write(buf)
}
func RDateListen() {
listener, err := net.Listen("tcp4", "0.0.0.0:37")
if err != nil {
panic(err)
}
defer listener.Close()
for true {
conn, aerr := listener.Accept()
if aerr != nil {
fmt.Fprintln(os.Stderr, aerr)
continue
}
go ServeTime(conn)
}
}
func main() {
fmt.Println("Time server starting")
RDateListen()
}