-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtftp.go
67 lines (54 loc) · 1.49 KB
/
tftp.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
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"path"
"github.com/google/goterm/term"
"github.com/pin/tftp"
)
const kernelFilename = "vme50"
func startTFTP() {
bindAddr := net.JoinHostPort(localAddr.String(), "69")
s := tftp.NewServer(tftpReadHandler, nil)
go func() {
log.Println("listening on", bindAddr)
err := s.ListenAndServe(bindAddr) // blocks until s.Shutdown() is called
if err != nil {
fmt.Fprintf(os.Stdout, "server: %v\n", err)
os.Exit(1)
}
}()
}
// tftpReadHandler is called when client starts file download from server
func tftpReadHandler(filename string, rf io.ReaderFrom) error {
if filename != kernelFilename {
fmt.Fprintf(os.Stderr, "filename mismatch, requested=%s expected=%s", filename, kernelFilename)
return os.ErrNotExist
}
remoteAddr := rf.(tftp.OutgoingTransfer).RemoteAddr().IP
// Figure out remote MAC address
mac := getMACAddress(localInterface.Index, remoteAddr)
if mac == nil {
log.Fatalf("unable to get MAC address for %v", remoteAddr)
}
log.Println(term.Greenf("Download started from %v (%v)", remoteAddr, mac))
// Set the logfile path
writer.setFile(path.Join(*logFolder, fmt.Sprintf("%x.log", []byte(mac))))
// open image file
file, err := os.Open(*kernelPath)
if err != nil {
log.Println(term.Red(err.Error()))
return err
}
// read image file
n, err := rf.ReadFrom(file)
if err != nil {
log.Println(term.Red(err.Error()))
return err
}
log.Println(term.Greenf("Download finished (%d bytes sent)", n))
return nil
}