Skip to content

Commit

Permalink
config in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
cbix committed Oct 6, 2015
1 parent bb8d97a commit f436426
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
"time"
)

const (
AssignedPrefixLength = 64
OnLinkPrefixLength = 48
DefaultValidLifetime = 86400
DefaultPreferredLifetime = 14400
TickerDelay = 5 * time.Minute
)

type routerSolicitation struct {
ip net.Addr
mac net.HardwareAddr
Expand All @@ -28,6 +20,19 @@ var (
pc *ipv6.PacketConn
db *pool.Pool
rschan chan routerSolicitation
// default config, might be overwritten by redis hash key fahrrad/config
AssignedPrefixLength uint8 = 64
OnLinkPrefixLength uint8 = 48
DefaultValidLifetime uint32 = 86400
DefaultPreferredLifetime uint32 = 14400
TickerDelay time.Duration = 5 * time.Minute
defaultConfig map[string]int = map[string]int{
"AssignedPrefixLength": int(AssignedPrefixLength),
"OnLinkPrefixLength": int(OnLinkPrefixLength),
"DefaultValidLifetime": int(DefaultValidLifetime),
"DefaultPreferredLifetime": int(DefaultPreferredLifetime),
"TickerDelay": int(TickerDelay / time.Second),
}
)

func main() {
Expand All @@ -37,6 +42,41 @@ func main() {
panic(err)
}
defer db.Empty()
dbc, err := db.Get()
if err != nil {
fmt.Println(err)
}
for k, v := range defaultConfig {
dbc.PipeAppend("HSETNX", "fahrrad/config", k, v)
}
for k, _ := range defaultConfig {
dbc.PipeAppend("HGET", "fahrrad/config", k)
}
for _, _ = range defaultConfig {
dbc.PipeResp()
}
var v int
v, err = dbc.PipeResp().Int()
if err == nil {
AssignedPrefixLength = uint8(v)
}
v, err = dbc.PipeResp().Int()
if err == nil {
OnLinkPrefixLength = uint8(v)
}
v, err = dbc.PipeResp().Int()
if err == nil {
DefaultValidLifetime = uint32(v)
}
v, err = dbc.PipeResp().Int()
if err == nil {
DefaultPreferredLifetime = uint32(v)
}
v, err = dbc.PipeResp().Int()
if err == nil {
TickerDelay = time.Duration(v) * time.Second
}
defer db.Put(dbc)

// open listening connection
conn, err := net.ListenIP("ip6:ipv6-icmp", &net.IPAddr{net.IPv6unspecified, ""})
Expand Down Expand Up @@ -154,7 +194,7 @@ func handleRS(rs routerSolicitation) bool {
fmt.Printf("invalid length: %x\n", prefix)
return false
}
fmt.Printf("found %v/64\n", net.IP(prefix))
fmt.Printf("found %v/%d\n", net.IP(prefix), AssignedPrefixLength)
// ICMPv6 RA header:
msgbody := []byte{0x40, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

Expand All @@ -172,7 +212,7 @@ func handleRS(rs routerSolicitation) bool {
AutoConf: true,
ValidLifetime: DefaultValidLifetime,
PreferredLifetime: DefaultPreferredLifetime,
Prefix: net.IP(prefix).Mask(net.CIDRMask(AssignedPrefixLength, 128)),
Prefix: net.IP(prefix).Mask(net.CIDRMask(int(AssignedPrefixLength), 128)),
}
// onlink prefix option
olopt := &NDOptionPrefix{
Expand All @@ -181,7 +221,7 @@ func handleRS(rs routerSolicitation) bool {
AutoConf: false,
ValidLifetime: DefaultValidLifetime,
PreferredLifetime: DefaultPreferredLifetime,
Prefix: net.IP(prefix).Mask(net.CIDRMask(OnLinkPrefixLength, 128)),
Prefix: net.IP(prefix).Mask(net.CIDRMask(int(OnLinkPrefixLength), 128)),
}
apoptbytes, err := apopt.Marshal()
if err != nil {
Expand Down

0 comments on commit f436426

Please sign in to comment.