mirror of
https://github.com/amigan/aim-oscar-server.git
synced 2024-11-21 03:59:47 -05:00
fix tcp message buffer bug
This commit is contained in:
parent
0d201678de
commit
91e7b8b287
2 changed files with 17 additions and 9 deletions
1
main.go
1
main.go
|
@ -187,6 +187,7 @@ func main() {
|
||||||
if flap.Header.Channel == 1 {
|
if flap.Header.Channel == 1 {
|
||||||
// Is this a hello?
|
// Is this a hello?
|
||||||
if bytes.Equal(flap.Data.Bytes(), []byte{0, 0, 0, 1}) {
|
if bytes.Equal(flap.Data.Bytes(), []byte{0, 0, 0, 1}) {
|
||||||
|
log.Println("this is a hello")
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package oscar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"aim-oscar/util"
|
"aim-oscar/util"
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -32,7 +33,7 @@ func (h *Handler) Handle(conn net.Conn) {
|
||||||
ctx := NewContextWithSession(context.Background(), conn)
|
ctx := NewContextWithSession(context.Background(), conn)
|
||||||
session, _ := SessionFromContext(ctx)
|
session, _ := SessionFromContext(ctx)
|
||||||
|
|
||||||
buf := make([]byte, 2048)
|
var buf bytes.Buffer
|
||||||
for {
|
for {
|
||||||
if !session.GreetedClient {
|
if !session.GreetedClient {
|
||||||
// send a hello
|
// send a hello
|
||||||
|
@ -42,7 +43,8 @@ func (h *Handler) Handle(conn net.Conn) {
|
||||||
session.GreetedClient = true
|
session.GreetedClient = true
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := conn.Read(buf)
|
incoming := make([]byte, 512)
|
||||||
|
n, err := conn.Read(incoming)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||||
session.Disconnect()
|
session.Disconnect()
|
||||||
|
@ -58,22 +60,27 @@ func (h *Handler) Handle(conn net.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf.Write(incoming[:n])
|
||||||
|
|
||||||
// Try to parse all of the FLAPs in the buffer if we have enough bytes to
|
// Try to parse all of the FLAPs in the buffer if we have enough bytes to
|
||||||
// fill a FLAP header
|
// fill a FLAP header
|
||||||
for len(buf) >= 6 && buf[0] == 0x2a {
|
for buf.Len() >= 6 && buf.Bytes()[0] == 0x2a {
|
||||||
dataLength := binary.BigEndian.Uint16(buf[4:6])
|
bufBytes := buf.Bytes()
|
||||||
|
dataLength := binary.BigEndian.Uint16(bufBytes[4:6])
|
||||||
flapLength := int(dataLength) + 6
|
flapLength := int(dataLength) + 6
|
||||||
if len(buf) < flapLength {
|
if len(bufBytes) < flapLength {
|
||||||
log.Printf("not enough data, only %d bytes\n", len(buf))
|
log.Printf("not enough data, expected %d bytes but have %d bytes\n", flapLength, len(bufBytes))
|
||||||
fmt.Printf("%s\n", util.PrettyBytes(buf))
|
fmt.Printf("%s\n", util.PrettyBytes(bufBytes))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
flap := &FLAP{}
|
flap := &FLAP{}
|
||||||
if err := flap.UnmarshalBinary(buf[:flapLength]); err != nil {
|
flapBuf := make([]byte, flapLength)
|
||||||
|
buf.Read(flapBuf)
|
||||||
|
if err := flap.UnmarshalBinary(flapBuf); err != nil {
|
||||||
util.PanicIfError(errors.Wrap(err, "could not unmarshal FLAP"))
|
util.PanicIfError(errors.Wrap(err, "could not unmarshal FLAP"))
|
||||||
}
|
}
|
||||||
buf = buf[flapLength:]
|
|
||||||
ctx = h.handle(ctx, flap)
|
ctx = h.handle(ctx, flap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue