Exemplo n.º 1
0
func (c *FrontendConnection) sendNotification(n *pq.Notification) error {
	var message fbcore.Message

	buf := &bytes.Buffer{}
	fbbuf.WriteInt32(buf, int32(n.BePid))
	fbbuf.WriteCString(buf, n.Channel)
	fbbuf.WriteCString(buf, n.Extra)
	message.InitFromBytes(fbproto.MsgNotificationResponseA, buf.Bytes())

	return c.WriteAndFlush(&message)
}
Exemplo n.º 2
0
func initFatalMessage(message *fbcore.Message, sqlstate, errorMessage string) {
	buf := &bytes.Buffer{}
	buf.WriteByte('S')
	fbbuf.WriteCString(buf, "FATAL")
	buf.WriteByte('C')
	fbbuf.WriteCString(buf, sqlstate)
	buf.WriteByte('M')
	fbbuf.WriteCString(buf, errorMessage)
	buf.WriteByte('\x00')

	message.InitFromBytes(fbproto.MsgErrorResponseE, buf.Bytes())
}
Exemplo n.º 3
0
func (qr errorResponse) Respond(f Frontend) error {
	var message fbcore.Message

	buf := &bytes.Buffer{}
	buf.WriteByte('S')
	fbbuf.WriteCString(buf, "ERROR")
	buf.WriteByte('C')
	fbbuf.WriteCString(buf, qr.sqlstate)
	buf.WriteByte('M')
	fbbuf.WriteCString(buf, qr.errorMessage)
	buf.WriteByte('\x00')

	message.InitFromBytes(fbproto.MsgErrorResponseE, buf.Bytes())

	return f.WriteMessage(&message)
}
func TestVersionCheck(t *testing.T) {
	for i, tt := range versionCheckTests {
		msgInit := func(dst *core.Message, exit exitFn) {
			b := bytes.Buffer{}
			buf.WriteCString(&b, tt.Version)
			dst.InitFromBytes('V', b.Bytes())
		}

		ok := true
		onBadVersion := func(args ...interface{}) {
			ok = false
		}
		processVerMsg(msgInit, onBadVersion)
		if ok != tt.Ok {
			t.Errorf("%d: Ver Message well formed: %v; want %v",
				i, ok, tt.Ok)
		}
	}
}
Exemplo n.º 5
0
func (c *FrontendConnection) startup(startupParameters map[string]string, dbcfg VirtualDatabaseConfiguration) bool {
	var message fbcore.Message
	var err error

	for {
		err = c.stream.Next(&message)
		if err != nil {
			elog.Logf("error while reading startup packet: %s", err)
			return false
		}
		if fbproto.IsStartupMessage(&message) {
			break
		} else if fbproto.IsSSLRequest(&message) {
			_, err = message.Force()
			if err != nil {
				elog.Logf("error while reading SSLRequest: %s", err)
				return false
			}
			err = c.stream.SendSSLRequestResponse(fbcore.RejectSSLRequest)
			if err != nil {
				elog.Logf("error during startup sequence: %s", err)
				return false
			}
			err = c.FlushStream()
			if err != nil {
				elog.Logf("error during startup sequence: %s", err)
			}
		} else if fbproto.IsCancelRequest(&message) {
			_ = c.stream.Close()
			return false
		} else {
			elog.Warningf("unrecognized frontend message type 0x%x during startup", message.MsgType())
			return false
		}
	}
	sm, err := fbproto.ReadStartupMessage(&message)
	if err != nil {
		elog.Logf("error while reading startup packet: %s", err)
		return false
	}

	if !c.auth(dbcfg, sm) {
		// error already logged
		_ = c.stream.Close()
		return false
	}

	fbproto.InitAuthenticationOk(&message)
	err = c.WriteMessage(&message)
	if err != nil {
		elog.Logf("error during startup sequence: %s", err)
		return false
	}

	for k, v := range startupParameters {
		buf := &bytes.Buffer{}
		fbbuf.WriteCString(buf, k)
		fbbuf.WriteCString(buf, v)
		message.InitFromBytes(fbproto.MsgParameterStatusS, buf.Bytes())
		err = c.WriteMessage(&message)
		if err != nil {
			elog.Logf("error during startup sequence: %s", err)
			return false
		}
	}

	fbproto.InitReadyForQuery(&message, fbproto.RfqIdle)
	err = c.WriteMessage(&message)
	if err != nil {
		elog.Logf("error during startup sequence: %s", err)
		return false
	}

	err = c.FlushStream()
	if err != nil {
		elog.Logf("error during startup sequence: %s", err)
		return false
	}
	return true
}