stratux/network.go

166 wiersze
4.0 KiB
Go
Czysty Zwykły widok Historia

package main
import (
2015-08-21 08:24:51 +00:00
"golang.org/x/exp/inotify"
"io/ioutil"
2015-08-20 16:54:46 +00:00
"log"
"net"
2015-08-20 16:54:46 +00:00
"strconv"
"strings"
2015-08-20 17:06:40 +00:00
"sync"
2015-08-20 16:54:46 +00:00
"time"
)
type networkMessage struct {
msg []byte
msgType uint8
}
type networkConnection struct {
conn *net.UDPConn
ip string
port uint32
capability uint8
}
var messageQueue chan networkMessage
var outSockets map[string]networkConnection
var dhcpLeases map[string]string
2015-08-20 17:06:40 +00:00
var netMutex *sync.Mutex
const (
NETWORK_GDL90 = 1
NETWORK_AHRS = 2
2015-08-21 08:24:51 +00:00
dhcp_lease_file = "/var/lib/dhcp/dhcpd.leases"
)
// Read the "dhcpd.leases" file and parse out IP/hostname.
func getDHCPLeases() (map[string]string, error) {
2015-08-21 08:24:51 +00:00
dat, err := ioutil.ReadFile(dhcp_lease_file)
ret := make(map[string]string)
if err != nil {
return ret, err
}
lines := strings.Split(string(dat), "\n")
open_block := false
block_ip := ""
for _, line := range lines {
spaced := strings.Split(line, " ")
if len(spaced) > 2 && spaced[0] == "lease" {
open_block = true
block_ip = spaced[1]
} else if open_block && len(spaced) >= 4 && spaced[2] == "client-hostname" {
hostname := strings.TrimRight(strings.TrimLeft(strings.Join(spaced[3:], " "), "\""), "\";")
ret[block_ip] = hostname
open_block = false
}
}
return ret, nil
}
func sendToAllConnectedClients(msg networkMessage) {
2015-08-20 17:06:40 +00:00
netMutex.Lock()
defer netMutex.Unlock()
for _, netconn := range outSockets {
if (netconn.capability & msg.msgType) != 0 { // Check if this port is able to accept the type of message we're sending.
netconn.conn.Write(msg.msg)
}
}
}
// Just returns the number of DHCP leases for now.
func getNetworkStats() int {
return len(dhcpLeases)
}
// See who has a DHCP lease and make a UDP connection to each of them.
func refreshConnectedClients() {
2015-08-20 17:06:40 +00:00
netMutex.Lock()
defer netMutex.Unlock()
validConnections := make(map[string]bool)
t, err := getDHCPLeases()
if err != nil {
log.Printf("getDHCPLeases(): %s\n", err.Error())
return
}
dhcpLeases = t
// Client connected that wasn't before.
for ip, hostname := range dhcpLeases {
for _, networkOutput := range globalSettings.NetworkOutputs {
ipAndPort := ip + ":" + strconv.Itoa(int(networkOutput.port))
if _, ok := outSockets[ipAndPort]; !ok {
log.Printf("client connected: %s:%d (%s).\n", ip, networkOutput.port, hostname)
addr, err := net.ResolveUDPAddr("udp", ipAndPort)
if err != nil {
log.Printf("ResolveUDPAddr(%s): %s\n", ipAndPort, err.Error())
continue
}
outConn, err := net.DialUDP("udp", nil, addr)
if err != nil {
log.Printf("DialUDP(%s): %s\n", ipAndPort, err.Error())
continue
}
outSockets[ipAndPort] = networkConnection{outConn, ip, networkOutput.port, networkOutput.capability}
}
validConnections[ipAndPort] = true
}
}
// Client that was connected before that isn't.
for ipAndPort, conn := range outSockets {
if _, ok := validConnections[ipAndPort]; !ok {
log.Printf("removed connection %s.\n", ipAndPort)
conn.conn.Close()
delete(outSockets, ipAndPort)
}
}
}
func messageQueueSender() {
secondTimer := time.NewTicker(1 * time.Second)
for {
select {
2015-08-20 16:54:46 +00:00
case msg := <-messageQueue:
sendToAllConnectedClients(msg)
case <-secondTimer.C:
getNetworkStats()
}
}
}
func sendMsg(msg []byte, msgType uint8) {
messageQueue <- networkMessage{msg, msgType}
}
func sendGDL90(msg []byte) {
sendMsg(msg, NETWORK_GDL90)
}
2015-08-21 08:24:51 +00:00
func monitorDHCPLeases() {
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.AddWatch(dhcp_lease_file, inotify.IN_CLOSE_WRITE)
if err != nil {
log.Fatal(err)
}
for {
select {
case <-watcher.Event:
log.Println("file modified, attempting to refresh DHCP")
refreshConnectedClients()
case err := <-watcher.Error:
log.Println("error with DHCP file system watcher:", err)
}
}
}
func initNetwork() {
messageQueue = make(chan networkMessage, 1024) // Buffered channel, 1024 messages.
outSockets = make(map[string]networkConnection)
2015-08-20 17:06:40 +00:00
netMutex = &sync.Mutex{}
refreshConnectedClients()
2015-08-21 08:24:51 +00:00
go monitorDHCPLeases()
go messageQueueSender()
2015-08-20 16:54:46 +00:00
}