$71 GRAYBYTE WORDPRESS FILE MANAGER $94

SERVER : in-mum-web1330.main-hosting.eu #1 SMP Mon Feb 10 22:45:17 UTC 2025
SERVER IP : 93.127.173.63 | ADMIN IP 216.73.216.215
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/go/pkg/mod/github.com/hashicorp/memberlist@v0.5.0/

HOME
Current File : /opt/go/pkg/mod/github.com/hashicorp/memberlist@v0.5.0//label.go
package memberlist

import (
	"bufio"
	"fmt"
	"io"
	"net"
)

// General approach is to prefix all packets and streams with the same structure:
//
// magic type byte (244): uint8
// length of label name:  uint8 (because labels can't be longer than 255 bytes)
// label name:            []uint8

// LabelMaxSize is the maximum length of a packet or stream label.
const LabelMaxSize = 255

// AddLabelHeaderToPacket prefixes outgoing packets with the correct header if
// the label is not empty.
func AddLabelHeaderToPacket(buf []byte, label string) ([]byte, error) {
	if label == "" {
		return buf, nil
	}
	if len(label) > LabelMaxSize {
		return nil, fmt.Errorf("label %q is too long", label)
	}

	return makeLabelHeader(label, buf), nil
}

// RemoveLabelHeaderFromPacket removes any label header from the provided
// packet and returns it along with the remaining packet contents.
func RemoveLabelHeaderFromPacket(buf []byte) (newBuf []byte, label string, err error) {
	if len(buf) == 0 {
		return buf, "", nil // can't possibly be labeled
	}

	// [type:byte] [size:byte] [size bytes]

	msgType := messageType(buf[0])
	if msgType != hasLabelMsg {
		return buf, "", nil
	}

	if len(buf) < 2 {
		return nil, "", fmt.Errorf("cannot decode label; packet has been truncated")
	}

	size := int(buf[1])
	if size < 1 {
		return nil, "", fmt.Errorf("label header cannot be empty when present")
	}

	if len(buf) < 2+size {
		return nil, "", fmt.Errorf("cannot decode label; packet has been truncated")
	}

	label = string(buf[2 : 2+size])
	newBuf = buf[2+size:]

	return newBuf, label, nil
}

// AddLabelHeaderToStream prefixes outgoing streams with the correct header if
// the label is not empty.
func AddLabelHeaderToStream(conn net.Conn, label string) error {
	if label == "" {
		return nil
	}
	if len(label) > LabelMaxSize {
		return fmt.Errorf("label %q is too long", label)
	}

	header := makeLabelHeader(label, nil)

	_, err := conn.Write(header)
	return err
}

// RemoveLabelHeaderFromStream removes any label header from the beginning of
// the stream if present and returns it along with an updated conn with that
// header removed.
//
// Note that on error it is the caller's responsibility to close the
// connection.
func RemoveLabelHeaderFromStream(conn net.Conn) (net.Conn, string, error) {
	br := bufio.NewReader(conn)

	// First check for the type byte.
	peeked, err := br.Peek(1)
	if err != nil {
		if err == io.EOF {
			// It is safe to return the original net.Conn at this point because
			// it never contained any data in the first place so we don't have
			// to splice the buffer into the conn because both are empty.
			return conn, "", nil
		}
		return nil, "", err
	}

	msgType := messageType(peeked[0])
	if msgType != hasLabelMsg {
		conn, err = newPeekedConnFromBufferedReader(conn, br, 0)
		return conn, "", err
	}

	// We are guaranteed to get a size byte as well.
	peeked, err = br.Peek(2)
	if err != nil {
		if err == io.EOF {
			return nil, "", fmt.Errorf("cannot decode label; stream has been truncated")
		}
		return nil, "", err
	}

	size := int(peeked[1])
	if size < 1 {
		return nil, "", fmt.Errorf("label header cannot be empty when present")
	}
	// NOTE: we don't have to check this against LabelMaxSize because a byte
	// already has a max value of 255.

	// Once we know the size we can peek the label as well. Note that since we
	// are using the default bufio.Reader size of 4096, the entire label header
	// fits in the initial buffer fill so this should be free.
	peeked, err = br.Peek(2 + size)
	if err != nil {
		if err == io.EOF {
			return nil, "", fmt.Errorf("cannot decode label; stream has been truncated")
		}
		return nil, "", err
	}

	label := string(peeked[2 : 2+size])

	conn, err = newPeekedConnFromBufferedReader(conn, br, 2+size)
	if err != nil {
		return nil, "", err
	}

	return conn, label, nil
}

// newPeekedConnFromBufferedReader will splice the buffer contents after the
// offset into the provided net.Conn and return the result so that the rest of
// the buffer contents are returned first when reading from the returned
// peekedConn before moving on to the unbuffered conn contents.
func newPeekedConnFromBufferedReader(conn net.Conn, br *bufio.Reader, offset int) (*peekedConn, error) {
	// Extract any of the readahead buffer.
	peeked, err := br.Peek(br.Buffered())
	if err != nil {
		return nil, err
	}

	return &peekedConn{
		Peeked: peeked[offset:],
		Conn:   conn,
	}, nil
}

func makeLabelHeader(label string, rest []byte) []byte {
	newBuf := make([]byte, 2, 2+len(label)+len(rest))
	newBuf[0] = byte(hasLabelMsg)
	newBuf[1] = byte(len(label))
	newBuf = append(newBuf, []byte(label)...)
	if len(rest) > 0 {
		newBuf = append(newBuf, []byte(rest)...)
	}
	return newBuf
}

func labelOverhead(label string) int {
	if label == "" {
		return 0
	}
	return 2 + len(label)
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
25 Jan 2024 4.43 PM
root / root
0755
.github
--
25 Jan 2024 4.43 PM
root / root
0555
internal
--
25 Jan 2024 4.43 PM
root / root
0555
test
--
25 Jan 2024 4.43 PM
root / root
0555
.gitignore
0.264 KB
25 Jan 2024 4.43 PM
root / root
0444
.golangci.yml
2.646 KB
25 Jan 2024 4.43 PM
root / root
0444
LICENSE
15.603 KB
25 Jan 2024 4.43 PM
root / root
0444
Makefile
0.654 KB
25 Jan 2024 4.43 PM
root / root
0444
README.md
3.12 KB
25 Jan 2024 4.43 PM
root / root
0444
alive_delegate.go
0.577 KB
25 Jan 2024 4.43 PM
root / root
0444
awareness.go
1.9 KB
25 Jan 2024 4.43 PM
root / root
0444
awareness_test.go
0.915 KB
25 Jan 2024 4.43 PM
root / root
0444
broadcast.go
3.233 KB
25 Jan 2024 4.43 PM
root / root
0444
broadcast_test.go
0.564 KB
25 Jan 2024 4.43 PM
root / root
0444
config.go
15.32 KB
25 Jan 2024 4.43 PM
root / root
0444
config_test.go
2.171 KB
25 Jan 2024 4.43 PM
root / root
0444
conflict_delegate.go
0.367 KB
25 Jan 2024 4.43 PM
root / root
0444
delegate.go
1.808 KB
25 Jan 2024 4.43 PM
root / root
0444
event_delegate.go
1.873 KB
25 Jan 2024 4.43 PM
root / root
0444
go.mod
0.714 KB
25 Jan 2024 4.43 PM
root / root
0444
go.sum
4.714 KB
25 Jan 2024 4.43 PM
root / root
0444
integ_test.go
1.972 KB
25 Jan 2024 4.43 PM
root / root
0444
keyring.go
4.399 KB
25 Jan 2024 4.43 PM
root / root
0444
keyring_test.go
3.626 KB
25 Jan 2024 4.43 PM
root / root
0444
label.go
4.724 KB
25 Jan 2024 4.43 PM
root / root
0444
label_test.go
9.289 KB
25 Jan 2024 4.43 PM
root / root
0444
logging.go
0.443 KB
25 Jan 2024 4.43 PM
root / root
0444
logging_test.go
0.782 KB
25 Jan 2024 4.43 PM
root / root
0444
memberlist.go
22.886 KB
25 Jan 2024 4.43 PM
root / root
0444
memberlist_test.go
49.766 KB
25 Jan 2024 4.43 PM
root / root
0444
merge_delegate.go
0.557 KB
25 Jan 2024 4.43 PM
root / root
0444
mock_transport.go
4.287 KB
25 Jan 2024 4.43 PM
root / root
0444
net.go
38.072 KB
25 Jan 2024 4.43 PM
root / root
0444
net_test.go
20.005 KB
25 Jan 2024 4.43 PM
root / root
0444
net_transport.go
10.031 KB
25 Jan 2024 4.43 PM
root / root
0444
peeked_conn.go
1.558 KB
25 Jan 2024 4.43 PM
root / root
0444
ping_delegate.go
0.63 KB
25 Jan 2024 4.43 PM
root / root
0444
queue.go
11.117 KB
25 Jan 2024 4.43 PM
root / root
0444
queue_test.go
6.806 KB
25 Jan 2024 4.43 PM
root / root
0444
security.go
5.315 KB
25 Jan 2024 4.43 PM
root / root
0444
security_test.go
1.474 KB
25 Jan 2024 4.43 PM
root / root
0444
state.go
37.886 KB
25 Jan 2024 4.43 PM
root / root
0444
state_test.go
62.422 KB
25 Jan 2024 4.43 PM
root / root
0444
suspicion.go
4.197 KB
25 Jan 2024 4.43 PM
root / root
0444
suspicion_test.go
4.392 KB
25 Jan 2024 4.43 PM
root / root
0444
tag.sh
0.39 KB
25 Jan 2024 4.43 PM
root / root
0444
todo.md
0.206 KB
25 Jan 2024 4.43 PM
root / root
0444
transport.go
5.372 KB
25 Jan 2024 4.43 PM
root / root
0444
transport_test.go
5.192 KB
25 Jan 2024 4.43 PM
root / root
0444
util.go
8.501 KB
25 Jan 2024 4.43 PM
root / root
0444
util_test.go
8.381 KB
25 Jan 2024 4.43 PM
root / root
0444

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF