Sfoglia il codice sorgente

toc: read fixed number of bytes for FLAPON

Mike 1 anno fa
parent
commit
2e88ac72ce
1 ha cambiato i file con 7 aggiunte e 9 eliminazioni
  1. 7 9
      server/toc/server.go

+ 7 - 9
server/toc/server.go

@@ -320,17 +320,15 @@ func (rt Server) readFromClient(ctx context.Context, msgCh chan<- wire.FLAPFrame
 // initFLAP sets up a new FLAP connection. It returns a flap client if the
 // connection successfully initialized.
 func (rt Server) initFLAP(rw io.ReadWriter) (*wire.FlapClient, error) {
-	scanner := bufio.NewScanner(rw)
+	expected := "FLAPON\r\n\r\n"
+	buf := make([]byte, len(expected))
 
-	// read FLAPON\r\n\r\n
-	if !scanner.Scan() {
-		return nil, fmt.Errorf("no line scanned: %w", scanner.Err())
-	}
-	if scanner.Text() != "FLAPON" {
-		return nil, fmt.Errorf("expected FLAPON, got %s", scanner.Text())
+	_, err := io.ReadFull(rw, buf)
+	if err != nil {
+		return nil, fmt.Errorf("io.ReadFull: %w", err)
 	}
-	if !scanner.Scan() {
-		return nil, fmt.Errorf("no line scanned: %w", scanner.Err())
+	if expected != string(buf) {
+		return nil, fmt.Errorf("expected FLAPON, got %s", buf)
 	}
 
 	clientFlap := wire.NewFlapClient(0, rw, rw)