-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
119 lines (95 loc) · 2.69 KB
/
main.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
// +build linux
package main
import (
"fmt"
"net/http"
"os"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
func main() {
if err := run(); err != nil {
fmt.Println(err.Error())
}
}
func run() error {
// Before we can configure ethernet we need to load hardware drivers
if err := addDriverModule(); err != nil {
return errors.Wrap(err, "failed to add driver")
}
fmt.Printf("driver added\n")
if err := configureEthernet(); err != nil {
return errors.Wrap(err, "failed to configure ethernet")
}
fmt.Printf("Ethernet configured\n")
http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Scratch Machine!\n")
}))
return nil
}
var fakeString [3]byte
func addDriverModule() error {
// We need a file descriptor for our file
driverPath := "/e1000.ko"
f, err := os.Open(driverPath)
if err != nil {
return errors.Wrap(err, "open of driver file failed")
}
defer f.Close()
fd := f.Fd()
_, _, errno := unix.Syscall(unix.SYS_FINIT_MODULE, fd, uintptr(unsafe.Pointer(&fakeString)), 0)
if errno != 0 && errno != unix.EEXIST {
return errors.Wrap(errno, "init module failed")
}
return nil
}
type socketAddrRequest struct {
name [unix.IFNAMSIZ]byte
addr unix.RawSockaddrInet4
}
type socketFlagsRequest struct {
name [unix.IFNAMSIZ]byte
flags uint16
pad [22]byte
}
func configureEthernet() error {
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
if err != nil {
return errors.Wrap(err, "could not open control socket")
}
defer unix.Close(fd)
// We want to associate an IP address with eth0, then set flags to
// activate it
sa := socketAddrRequest{}
copy(sa.name[:], "eth0")
sa.addr.Family = unix.AF_INET
copy(sa.addr.Addr[:], []byte{192, 168, 59, 4})
// Set address
if err := ioctl(fd, unix.SIOCSIFADDR, uintptr(unsafe.Pointer(&sa))); err != nil {
return errors.Wrap(err, "failed setting address for eth0")
}
// Set netmask
copy(sa.addr.Addr[:], []byte{255, 255, 255, 0})
if err := ioctl(fd, unix.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&sa))); err != nil {
return errors.Wrap(err, "failed setting netmask for eth0")
}
// Get flags
sf := socketFlagsRequest{}
sf.name = sa.name
if err := ioctl(fd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&sf))); err != nil {
return errors.Wrap(err, "failed getting flags for eth0")
}
sf.flags |= unix.IFF_UP | unix.IFF_RUNNING
if err := ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&sf))); err != nil {
return errors.Wrap(err, "failed getting flags for eth0")
}
return nil
}
func ioctl(fd int, code, data uintptr) error {
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), code, data)
if errno != 0 {
return errno
}
return nil
}