|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "flag" |
6 | 5 | "fmt" |
7 | 6 | "net" |
@@ -49,26 +48,31 @@ var configDir string |
49 | 48 | // - TAI64 epoch: 1970-01-01 00:00:10 TAI (10 seconds after Unix epoch) |
50 | 49 | // - Current offset: TAI = UTC + 37 seconds (as of 2025) |
51 | 50 |
|
| 51 | +var responseHeader = []byte("s") |
| 52 | + |
52 | 53 | // sendResponse handles TAIN protocol response |
53 | 54 | func sendResponse(conn *net.UDPConn, _ int, remoteaddr *net.UDPAddr, buf []byte) { |
54 | | - s := []byte("s") |
55 | | - copy(buf[0:], s) |
56 | | - copy(buf[4:], glibtai.TAINPack(glibtai.TAINNow())) |
57 | | - _, err := conn.WriteToUDP(buf, remoteaddr) |
58 | | - if err != nil { |
59 | | - _, _ = fmt.Printf("Couldn't send response %v", err) |
60 | | - } |
| 55 | + |
| 56 | + copy(buf[0:1], responseHeader) |
| 57 | + taiTime := glibtai.TAINNow() |
| 58 | + copy(buf[4:16], glibtai.TAINPack(taiTime)) |
| 59 | + // Send response - ignore errors for performance (UDP is best-effort anyway) |
| 60 | + _, _ = conn.WriteToUDP(buf, remoteaddr) |
61 | 61 | } |
62 | 62 |
|
63 | 63 | // validateTAINRequest validates TAIN protocol requests |
64 | 64 | func validateTAINRequest(config *gtudpd.Config) gtudpd.RequestValidator { |
65 | 65 | return func(n int, buf []byte, remoteIP net.IP) bool { |
66 | | - // Check protocol format: minimum 20 bytes, maximum config.MaxRequestSize, starts with "ctai" |
67 | | - if n < 20 || n > config.MaxRequestSize || !bytes.Equal(buf[:4], []byte("ctai")) { |
| 66 | + if n < 20 || n > config.MaxRequestSize { |
68 | 67 | return false |
69 | 68 | } |
70 | | - // Check client permissions using gtudpd |
| 69 | + if buf[0] != 'c' || buf[1] != 't' || buf[2] != 'a' || buf[3] != 'i' { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + // Check client permissions (this may involve filesystem operations) |
71 | 74 | return config.ClientOK(remoteIP) |
| 75 | + |
72 | 76 | } |
73 | 77 | } |
74 | 78 |
|
|
0 commit comments