func (s *Startup) FillMessage(m *femebe.Message) { buf := bytes.NewBuffer(make([]byte, 0, 1024)) // Startup-message type word buf.Write([]byte{0x00, 0x03, 0x00, 0x00}) for name, value := range s.Params { femebe.WriteCString(buf, name) femebe.WriteCString(buf, value) } buf.Write([]byte{'\000'}) m.InitFromBytes(femebe.MSG_TYPE_FIRST, buf.Bytes()) }
func TestBackendKeyReading(t *testing.T) { buf := bytes.Buffer{} const PID = 1234 const KEY = 5768 femebe.WriteInt32(&buf, PID) femebe.WriteInt32(&buf, KEY) var m femebe.Message m.InitFromBytes(MSG_BACKEND_KEY_DATA_K, buf.Bytes()) kd, err := ReadBackendKeyData(&m) if err != nil { t.Fatal() } if kd.pid != PID { t.Fatal() } if kd.key != KEY { t.Fatal() } }
// Process a log message, sending it to the client. func processLogMsg(die dieCh, lpc *logplexc.Client, msgInit msgInit, sr *serveRecord, exit exitFn) { var m femebe.Message for { // Poll request to exit select { case <-die: return default: break } msgInit(&m, exit) // Refuse to handle any log message above an arbitrary // size. Furthermore, exit the worker, closing the0 // connection, so that the client doesn't even bother // to wait for this process to drain the oversized // item and anything following it; these will be // dropped. It's on the client to gracefully handle // the error and re-connect after this happens. if m.Size() > 1*MB { exit("client %q sent oversized log record") } payload, err := m.Force() if err != nil { exit("could not retrieve payload of message: %v", err) } var lr logRecord parseLogRecord(&lr, payload, exit) processLogRec(&lr, lpc, sr, exit) } }
func TestStartupSerDes(t *testing.T) { ms, _ := newTestClientStream(t) var m femebe.Message s := Startup{Params: make(map[string]string)} s.Params["hello"] = "world" s.Params["goodbye"] = "world" s.Params["glory"] = "spite" s.FillMessage(&m) ms.Send(&m) var deserM femebe.Message ms.Next(&deserM) serBytes, _ := m.Force() deserBytes, _ := deserM.Force() if !bytes.Equal(serBytes, deserBytes) { t.Fatal() } }
// Process the identity ('I') message, reporting the identity therein. func processIdentMsg(msgInit msgInit, exit exitFn) string { var m femebe.Message msgInit(&m, exit) // Read the remote system identifier string if m.MsgType() != 'I' { exit("expected identification ('I') message, "+ "but received %c", m.MsgType()) } // hard-coded lengh limit, but it's very generous if m.Size() > 10*KB { log.Printf("oversized message string, msg size is %d", m.Size()) } s, err := femebe.ReadCString(m.Payload()) if err != nil { exit("couldn't read identification string: %v", err) } return s }
// Read the version message, calling exit if this is not a supported // version. func processVerMsg(msgInit msgInit, exit exitFn) { var m femebe.Message msgInit(&m, exit) if m.MsgType() != 'V' { exit("expected version ('V') message, "+ "but received %c", m.MsgType()) } // hard-coded lengh limit, but it's very generous if m.Size() > 10*KB { log.Printf("oversized message string, msg size is %d", m.Size()) } s, err := femebe.ReadCString(m.Payload()) if err != nil { exit("couldn't read version string: %v", err) } if !strings.HasPrefix(s, "PG-9.2.") || !strings.HasSuffix(s, "/logfebe-1") { exit("protocol version not supported: %s", s) } }
func ReadStartupMessage(m *femebe.Message) (*Startup, error) { var err error if remainingSz := m.Size() - 4; remainingSz > 10000 { // Startup packets longer than this are considered // invalid. Copied from the PostgreSQL source code. err = ErrTooBig{fmt.Errorf( "Rejecting oversized startup packet: got %v", m.Size())} return nil, err } else if remainingSz < 4 { // We expect all initialization messages to // have at least a 4-byte header err = ErrWrongSize{ fmt.Errorf( "Expected message of at least 4 bytes; got %v", remainingSz)} return nil, err } body, err := m.Force() if err != nil { return nil, err } var b femebe.Reader b.InitReader(body) protoVer, _ := femebe.ReadInt32(&b) const SUPPORTED_PROTOVER = 0x00030000 if protoVer != SUPPORTED_PROTOVER { err = ErrStartupVersion{ fmt.Errorf("bad version: got %x expected %x", protoVer, SUPPORTED_PROTOVER)} return nil, err } params := make(map[string]string) for remaining := b.Len(); remaining > 1; { key, err := femebe.ReadCString(&b) if err != nil { return nil, err } val, err := femebe.ReadCString(&b) if err != nil { return nil, err } remaining -= len(key) + len(val) + 2 /* null bytes */ params[key] = val } // Fidelity check on the startup packet, whereby the last byte // must be a NUL. if d, _ := femebe.ReadByte(&b); d != '\000' { return nil, ErrStartupFmt{ errors.New("malformed startup packet")} } return &Startup{params}, nil }