Skip to content

Commit

Permalink
Initial version for the address server
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Fonseca committed Jan 13, 2018
1 parent b22801f commit 72ee232
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions address-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"net"
"fmt"

log "github.com/sirupsen/logrus"
)

const (
// This is the default port for client and server; if running locally, change the game to use `hostPort=2301`
AddressServerPort = 2300
UdpBufferSize = 1200
)

func main() {
log.WithField("port", AddressServerPort).Infoln("Starting address server")

udpAddress, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", "0.0.0.0", AddressServerPort))
if err != nil {
log.WithError(err).Panic("Failed to resolve udp address")
}

udpConn, err := net.ListenUDP("udp", udpAddress)
if err != nil {
log.WithError(err).Panic("Failed to listen for UDP packets.")
}

defer udpConn.Close()

udpBuffer := make([]byte, UdpBufferSize)

for {
bytes, udpClientAddress, err := udpConn.ReadFromUDP(udpBuffer)
if err != nil {
log.WithError(err).Warn("Failed to read UDP packet.")
continue
}

if bytes == 0 {
log.Warn("Failed to read any data from the UDP packet.")
continue
}

switch udpBuffer[0] {
case 9:
reply := make([]byte, 16)
reply[0] = 9 // reply with the same first byte
copy(reply[1:], []byte {0x02, 0x00, 0xd9, 0x49}) // these 4 are unknown, but it seems to work
copy(reply[5:], udpClientAddress.IP)
// from [9:12], all 0x00s
copy(reply[13:], udpBuffer[1:4])

log.WithField("reply", reply).WithField("ip", udpClientAddress.IP).Info("Replying to request.")
udpConn.WriteToUDP(reply, udpClientAddress)
case 10:
// TODO: reply back with the same message
}

log.WithField("fist_byte", udpBuffer[0]).WithField("ip", udpClientAddress.IP).Info("Got first byte")
}
}

0 comments on commit 72ee232

Please sign in to comment.