(conn net.Conn)
| 273 | } |
| 274 | |
| 275 | func (t *TCPTransport) handleConnection(conn net.Conn) { |
| 276 | t.debugLog().Log("msg", "New connection", "addr", conn.RemoteAddr()) |
| 277 | |
| 278 | // Wrap the connection to track sent/received bytes. |
| 279 | conn = newMeteredConn(conn, t.sentBytes, t.receivedBytes) |
| 280 | |
| 281 | closeConn := true |
| 282 | defer func() { |
| 283 | if closeConn { |
| 284 | _ = conn.Close() |
| 285 | } |
| 286 | }() |
| 287 | |
| 288 | // let's read first byte, and determine what to do about this connection |
| 289 | msgType := []byte{0} |
| 290 | _, err := io.ReadFull(conn, msgType) |
| 291 | if err != nil { |
| 292 | level.Warn(t.logger).Log("msg", "failed to read message type", "err", err, "remote", conn.RemoteAddr()) |
| 293 | return |
| 294 | } |
| 295 | |
| 296 | if messageType(msgType[0]) == stream { |
| 297 | t.incomingStreams.Inc() |
| 298 | |
| 299 | // hand over this connection to memberlist |
| 300 | closeConn = false |
| 301 | t.connCh <- conn |
| 302 | } else if messageType(msgType[0]) == packet { |
| 303 | // it's a memberlist "packet", which contains an address and data. |
| 304 | t.receivedPackets.Inc() |
| 305 | |
| 306 | // before reading packet, read the address |
| 307 | addrLengthBuf := []byte{0} |
| 308 | _, err := io.ReadFull(conn, addrLengthBuf) |
| 309 | if err != nil { |
| 310 | t.receivedPacketsErrors.Inc() |
| 311 | level.Warn(t.logger).Log("msg", "error while reading node address length from packet", "err", err, "remote", conn.RemoteAddr()) |
| 312 | return |
| 313 | } |
| 314 | |
| 315 | addrBuf := make([]byte, addrLengthBuf[0]) |
| 316 | _, err = io.ReadFull(conn, addrBuf) |
| 317 | if err != nil { |
| 318 | t.receivedPacketsErrors.Inc() |
| 319 | level.Warn(t.logger).Log("msg", "error while reading node address from packet", "err", err, "remote", conn.RemoteAddr()) |
| 320 | return |
| 321 | } |
| 322 | |
| 323 | // read the rest to buffer -- this is the "packet" itself |
| 324 | buf, err := io.ReadAll(conn) |
| 325 | if err != nil { |
| 326 | t.receivedPacketsErrors.Inc() |
| 327 | level.Warn(t.logger).Log("msg", "error while reading packet data", "err", err, "remote", conn.RemoteAddr()) |
| 328 | return |
| 329 | } |
| 330 | |
| 331 | if len(buf) < md5.Size { |
| 332 | t.receivedPacketsErrors.Inc() |
no test coverage detected