1
2
3
4
5
6
7
8
9
10
11
12 package lif
13
14 import "syscall"
15
16 type endpoint struct {
17 af int
18 s uintptr
19 }
20
21 func (ep *endpoint) close() error {
22 return syscall.Close(int(ep.s))
23 }
24
25 func newEndpoints(af int) ([]endpoint, error) {
26 var lastErr error
27 var eps []endpoint
28 afs := []int{sysAF_INET, sysAF_INET6}
29 if af != sysAF_UNSPEC {
30 afs = []int{af}
31 }
32 for _, af := range afs {
33 s, err := syscall.Socket(af, sysSOCK_DGRAM, 0)
34 if err != nil {
35 lastErr = err
36 continue
37 }
38 eps = append(eps, endpoint{af: af, s: uintptr(s)})
39 }
40 if len(eps) == 0 {
41 return nil, lastErr
42 }
43 return eps, nil
44 }
45
View as plain text