Moved session packet handlers to functions for reasability

live
noah metz 2024-04-09 17:20:37 -06:00
parent 17990e35d9
commit f32087f729
1 changed files with 205 additions and 182 deletions

@ -141,26 +141,15 @@ func(server *Server) Stop() error {
const SESSION_BUFFER_SIZE = 256 const SESSION_BUFFER_SIZE = 256
func(server *Server) listen_udp() { func(server *Server) handle_session_open(client_session_open []byte, from *net.UDPAddr) error {
server.Log("Started server on %s", server.connection.LocalAddr())
var buf [SERVER_UDP_BUFFER_SIZE]byte
for true {
read, from, err := server.connection.ReadFromUDP(buf[:])
if err == nil {
var packet_type SessionPacketType = SessionPacketType(buf[0])
switch packet_type {
case SESSION_OPEN:
session_open, ecdh_private, err := NewSessionOpen(server.key) session_open, ecdh_private, err := NewSessionOpen(server.key)
if err != nil { if err != nil {
server.Log("NewSessionOpen error - %s: %x", err, buf[COMMAND_LENGTH:read]) return err
continue
} }
session, err := ParseSessionOpen(ecdh_private, buf[COMMAND_LENGTH:read]) session, err := ParseSessionOpen(ecdh_private, client_session_open)
if err != nil { if err != nil {
server.Log("ParseSessionOpen error - %s: %x", err, buf[COMMAND_LENGTH:read]) return err
continue
} }
server.sessions_lock.Lock() server.sessions_lock.Lock()
@ -272,23 +261,22 @@ func(server *Server) listen_udp() {
_, err = server.connection.WriteToUDP(session_open, from) _, err = server.connection.WriteToUDP(session_open, from)
if err != nil { if err != nil {
server.Log("WriteToUDP error %s", err) return err
continue
} }
server.Log("Started session %s with %s", session.ID, session.Peer) server.Log("Started session %s with %s", session.ID, session.Peer)
return nil
}
case SESSION_CONNECT: func(server *Server) handle_session_connect(client_session_connect []byte, from *net.UDPAddr) error {
session_id := SessionID(buf[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH]) session_id := SessionID(client_session_connect[:ID_LENGTH])
session, exists := server.sessions[session_id] session, exists := server.sessions[session_id]
if exists == false { if exists == false {
server.Log("Session %s does not exist, can't connect", session_id) return fmt.Errorf("Session %s does not exist, can't connect", session_id)
continue
} }
_, err := ParseSessionConnect(buf[COMMAND_LENGTH+ID_LENGTH:read], session.secret) _, err := ParseSessionConnect(client_session_connect[ID_LENGTH:], session.secret)
if err != nil { if err != nil {
server.Log("Error parsing session connect: %s", err) return err
continue
} }
// TODO: fix, was client_addr but the client doesnt know it's nat assignment // TODO: fix, was client_addr but the client doesnt know it's nat assignment
@ -298,29 +286,27 @@ func(server *Server) listen_udp() {
// TODO: Make a better server hello // TODO: Make a better server hello
server_hello, err := NewSessionData(&session.Session, []byte("hello")) server_hello, err := NewSessionData(&session.Session, []byte("hello"))
if err != nil { if err != nil {
server.Log("Error generating server hello: %s", err) return err
continue
} }
_, err = server.connection.WriteToUDP(server_hello, session.remote) _, err = server.connection.WriteToUDP(server_hello, session.remote)
if err != nil { if err != nil {
server.Log("Error sending server hello: %s", err) return err
continue
} }
server.Log("Sent server_hello for %s to %s(from %s)", session.ID, session.remote, from) server.Log("Sent server_hello for %s to %s(from %s)", session.ID, session.remote, from)
return nil
}
case SESSION_CLOSE: func(server *Server) handle_session_close(client_session_close []byte, from *net.UDPAddr) error {
session_id := SessionID(buf[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH]) session_id := SessionID(client_session_close[:ID_LENGTH])
session, exists := server.sessions[session_id] session, exists := server.sessions[session_id]
if exists == false { if exists == false {
server.Log("Session %s does not exist, can't close", session_id) return fmt.Errorf("Session %s does not exist, can't close", session_id)
continue
} }
err := ParseSessionClose(&session.Session, buf[COMMAND_LENGTH+ID_LENGTH:read]) err := ParseSessionClose(&session.Session, client_session_close[ID_LENGTH:])
if err != nil { if err != nil {
server.Log("Session close error for %s - %s", session_id, err) return err
continue
} }
server.sessions_lock.Lock() server.sessions_lock.Lock()
@ -328,25 +314,62 @@ func(server *Server) listen_udp() {
server.sessions_lock.Unlock() server.sessions_lock.Unlock()
server.Log("Session %s closed", session_id) server.Log("Session %s closed", session_id)
return nil
}
case SESSION_DATA: func(server *Server) handle_session_data(data []byte, from *net.UDPAddr) error {
session_id := SessionID(buf[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH]) session_id := SessionID(data[:ID_LENGTH])
session, exists := server.sessions[session_id] session, exists := server.sessions[session_id]
if exists == false { if exists == false {
server.Log("Session %s does not exist, can't receive data", session_id) return fmt.Errorf("Session %s does not exist, can't receive data", session_id)
continue
} }
session.LastSeen = time.Now() session.LastSeen = time.Now()
buf_copy := make([]byte, read - COMMAND_LENGTH - ID_LENGTH) buf_copy := make([]byte, len(data) - ID_LENGTH)
copy(buf_copy, buf[COMMAND_LENGTH+ID_LENGTH:read]) copy(buf_copy, data[ID_LENGTH:])
select { select {
case session.IncomingPackets<-buf_copy: case session.IncomingPackets<-buf_copy:
return nil
default: default:
server.Log("Dropped packet to session %s", session_id) return fmt.Errorf("Dropped packet to session %s", session_id)
}
}
func(server *Server) listen_udp() {
server.Log("Started server on %s", server.connection.LocalAddr())
var buf [SERVER_UDP_BUFFER_SIZE]byte
for true {
read, from, err := server.connection.ReadFromUDP(buf[:])
if err == nil {
var packet_type SessionPacketType = SessionPacketType(buf[0])
switch packet_type {
case SESSION_OPEN:
err := server.handle_session_open(buf[COMMAND_LENGTH:read], from)
if err != nil {
server.Log("handle_session_open erorr - %s", err)
}
case SESSION_CONNECT:
err := server.handle_session_connect(buf[COMMAND_LENGTH:read], from)
if err != nil {
server.Log("handle_session_connect error - %s", err)
}
case SESSION_CLOSE:
err := server.handle_session_close(buf[COMMAND_LENGTH:read], from)
if err != nil {
server.Log("handle_session_close error - %s", err)
}
case SESSION_DATA:
err := server.handle_session_data(buf[COMMAND_LENGTH:read], from)
if err != nil {
server.Log("handle_session_data error - %s", err)
} }
default: default:
server.Log("Unhandled packet type 0x%04x from %s: %+v", packet_type, from, buf[COMMAND_LENGTH:read]) server.Log("Unhandled packet type 0x%04x from %s: %+v", packet_type, from, buf[COMMAND_LENGTH:read])
} }