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