MCPcopy
hub / github.com/hashicorp/memberlist / rawSendMsgPacket

Method rawSendMsgPacket

net.go:825–888  ·  view source on GitHub ↗

rawSendMsgPacket is used to send message via packet to another host without modification, other than compression or encryption if enabled.

(a Address, node *Node, msg []byte)

Source from the content-addressed store, hash-verified

823// rawSendMsgPacket is used to send message via packet to another host without
824// modification, other than compression or encryption if enabled.
825func (m *Memberlist) rawSendMsgPacket(a Address, node *Node, msg []byte) error {
826 if a.Name == "" && m.config.RequireNodeNames {
827 return errNodeNamesAreRequired
828 }
829
830 // Check if we have compression enabled
831 if m.config.EnableCompression {
832 buf, err := compressPayload(msg, m.config.MsgpackUseNewTimeFormat)
833 if err != nil {
834 m.logger.Printf("[WARN] memberlist: Failed to compress payload: %v", err)
835 } else {
836 // Only use compression if it reduced the size
837 if buf.Len() < len(msg) {
838 msg = buf.Bytes()
839 }
840 }
841 }
842
843 // Try to look up the destination node. Note this will only work if the
844 // bare ip address is used as the node name, which is not guaranteed.
845 if node == nil {
846 toAddr, _, err := net.SplitHostPort(a.Addr)
847 if err != nil {
848 m.logger.Printf("[ERR] memberlist: Failed to parse address %q: %v", a.Addr, err)
849 return err
850 }
851 m.nodeLock.RLock()
852 nodeState, ok := m.nodeMap[toAddr]
853 m.nodeLock.RUnlock()
854 if ok {
855 node = &nodeState.Node
856 }
857 }
858
859 // Add a CRC to the end of the payload if the recipient understands
860 // ProtocolVersion >= 5
861 if node != nil && node.PMax >= 5 {
862 crc := crc32.ChecksumIEEE(msg)
863 header := make([]byte, 5, 5+len(msg))
864 header[0] = byte(hasCrcMsg)
865 binary.BigEndian.PutUint32(header[1:], crc)
866 msg = append(header, msg...)
867 }
868
869 // Check if we have encryption enabled
870 if m.config.EncryptionEnabled() && m.config.GossipVerifyOutgoing {
871 // Encrypt the payload
872 var (
873 primaryKey = m.config.Keyring.GetPrimaryKey()
874 packetLabel = []byte(m.config.Label)
875 buf bytes.Buffer
876 )
877 err := encryptPayload(m.encryptionVersion(), primaryKey, msg, packetLabel, &buf)
878 if err != nil {
879 m.logger.Printf("[ERR] memberlist: Encryption of message failed: %v", err)
880 return err
881 }
882 msg = buf.Bytes()

Callers 7

SendToAddressMethod · 0.95
SendBestEffortMethod · 0.95
probeNodeMethod · 0.95
gossipMethod · 0.95
sendMsgMethod · 0.95
TestRawSendUdp_CRCFunction · 0.80
TestIngestPacket_CRCFunction · 0.80

Calls 6

encryptionVersionMethod · 0.95
compressPayloadFunction · 0.85
encryptPayloadFunction · 0.85
EncryptionEnabledMethod · 0.80
GetPrimaryKeyMethod · 0.80
WriteToAddressMethod · 0.65

Tested by 2

TestRawSendUdp_CRCFunction · 0.64
TestIngestPacket_CRCFunction · 0.64