| 1 |
package birdsocket |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
var birdReturnCodeRegex *regexp.Regexp |
| 10 |
|
| 11 |
func init() { |
| 12 |
// Requests are commands encoded as a single line of text, |
| 13 |
// replies are sequences of lines starting with a four-digit code |
| 14 |
// followed by either a space (if it's the last line of the reply) |
| 15 |
// or a minus sign (when the reply is going to continue with the next line), |
| 16 |
// the rest of the line contains a textual message semantics of which depends |
| 17 |
// on the numeric code. |
| 18 |
|
| 19 |
// https://gitlab.nic.cz/labs/bird/-/blob/master/doc/reply_codes |
| 20 |
birdReturnCodeRegex = regexp.MustCompile(`(?m)^([089]\d{3})`) |
| 21 |
} |
| 22 |
|
| 23 |
// BirdSocket encapsulates communication with Bird routing daemon |
| 24 |
type BirdSocket struct { |
| 25 |
socketPath string |
| 26 |
bufferSize int |
| 27 |
conn net.Conn |
| 28 |
} |
| 29 |
|
| 30 |
// BirdSocketOption applies options to BirdSocket |
| 31 |
type Option func(*BirdSocket) |
| 32 |
|
| 33 |
// WithBufferSize sets the buffer size for BirdSocket |
| 34 |
func WithBufferSize(bufferSize int) Option { |
| 35 |
return func(s *BirdSocket) { |
| 36 |
s.bufferSize = bufferSize |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// NewSocket creates a new socket |
| 41 |
func NewSocket(socketPath string, opts ...Option) *BirdSocket { |
| 42 |
socket := &BirdSocket{socketPath: socketPath, bufferSize: 4096} |
| 43 |
|
| 44 |
for _, o := range opts { |
| 45 |
o(socket) |
| 46 |
} |
| 47 |
|
| 48 |
return socket |
| 49 |
} |
| 50 |
|
| 51 |
// Query sends an ad hoc query to Bird and waits for the reply |
| 52 |
func Query(socketPath, qry string) ([]byte, error) { |
| 53 |
s := NewSocket(socketPath) |
| 54 |
_, err := s.Connect() |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
defer s.Close() |
| 59 |
|
| 60 |
return s.Query(qry) |
| 61 |
} |
| 62 |
|
| 63 |
// Connect connects to the Bird unix socket |
| 64 |
func (s *BirdSocket) Connect() ([]byte, error) { |
| 65 |
var err error |
| 66 |
s.conn, err = net.Dial("unix", s.socketPath) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
buf := make([]byte, s.bufferSize) |
| 72 |
n, err := s.conn.Read(buf[:]) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
|
| 77 |
return buf[:n], err |
| 78 |
} |
| 79 |
|
| 80 |
// Close closes the connection to the socket |
| 81 |
func (s *BirdSocket) Close() { |
| 82 |
if s.conn != nil { |
| 83 |
s.conn.Close() |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Query sends an query to Bird and waits for the reply |
| 88 |
func (s *BirdSocket) Query(qry string) ([]byte, error) { |
| 89 |
_, err := s.conn.Write([]byte(strings.Trim(qry, "\n") + "\n")) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
|
| 94 |
output, err := s.readFromSocket(s.conn) |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
return output, nil |
| 100 |
} |
| 101 |
|
| 102 |
func (s *BirdSocket) readFromSocket(conn net.Conn) ([]byte, error) { |
| 103 |
b := make([]byte, 0) |
| 104 |
buf := make([]byte, s.bufferSize) |
| 105 |
|
| 106 |
done := false |
| 107 |
for !done { |
| 108 |
n, err := conn.Read(buf[:]) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
|
| 113 |
b = append(b, buf[:n]...) |
| 114 |
done = containsActionCompletedCode(buf[:n]) |
| 115 |
} |
| 116 |
|
| 117 |
return b, nil |
| 118 |
} |
| 119 |
|
| 120 |
func containsActionCompletedCode(b []byte) bool { |
| 121 |
return birdReturnCodeRegex.Match(b) |
| 122 |
} |