Skip to content

Commit

Permalink
msgq in all responses & ipversion formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gauzy-gossamer committed Jul 29, 2023
1 parent 1249474 commit 143233c
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 62 deletions.
56 changes: 40 additions & 16 deletions registry/epp/dbreg/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,63 @@ func CreatePollMessage(db *server.DBConn, registrar_id uint, msg_type int) (uint
return poll_msg_id, err
}

func GetPollMessageCount(db *server.DBConn, regid uint) (uint, error) {
type PollMsg struct {
db *server.DBConn
regid uint
extended bool
}

func NewPollMsg(db *server.DBConn, regid uint) *PollMsg {
return &PollMsg{
db:db,
regid:regid,
}
}

func (p *PollMsg) SetExtended(extended bool) *PollMsg {
p.extended = extended
return p
}

func (p *PollMsg) GetPollMessageCount() (uint, error) {
query := "SELECT count(*) FROM message WHERE seen='f' " +
"and clid=$1::bigint and exdate > now() and msgtype in(22,1)"
row := db.QueryRow(query, regid)
row := p.db.QueryRow(query, p.regid)

var count uint
err := row.Scan(&count)
return count, err
}

func getPollTransferObject(db *server.DBConn, msgid uint) (*PollMessage, error) {
func (p *PollMsg) getPollTransferObject(msgid uint) (*PollMessage, error) {
var poll_msg PollMessage
row := db.QueryRow("SELECT request_id, status FROM epp_transfer_request_state_change "+
row := p.db.QueryRow("SELECT request_id, status FROM epp_transfer_request_state_change "+
"WHERE msgid=$1::integer", msgid)
var requestid, status uint
err := row.Scan(&requestid, &status)
if err != nil {
return &poll_msg, err
}
find_transfer := FindTransferRequest{TrID:requestid}
transfer_obj, err := find_transfer.Exec(db)
if err != nil {
return &poll_msg, err

if p.extended {
find_transfer := FindTransferRequest{TrID:requestid}
transfer_obj, err := find_transfer.Exec(p.db)
if err != nil {
return &poll_msg, err
}
poll_msg.Content = transfer_obj
}

poll_msg.Msgid = msgid
poll_msg.Msg = "changed state"
poll_msg.Content = transfer_obj
poll_msg.Msg = GetTransferMsg(status)

return &poll_msg, nil
}

func GetFirstUnreadPollMessage(db *server.DBConn, regid uint) (*PollMessage, error) {
func (p *PollMsg) GetFirstUnreadPollMessage() (*PollMessage, error) {
query := "SELECT id, msgtype, crdate, exdate FROM message WHERE seen='f' " +
"and clid=$1::bigint and exdate > now() and msgtype in(22,1) ORDER BY id LIMIT 1"
row := db.QueryRow(query, regid)
row := p.db.QueryRow(query, p.regid)

var msgtype, msgid uint
var crdate, exdate pgtype.Timestamp
Expand All @@ -69,14 +90,18 @@ func GetFirstUnreadPollMessage(db *server.DBConn, regid uint) (*PollMessage, err
var poll_msg *PollMessage
switch msgtype {
case POLL_LOW_CREDIT:
poll_msg = &PollMessage{Msgid:msgid, Msg:"Credit balance low."}
poll_msg = &PollMessage{}
poll_msg.Msgid = msgid
poll_msg.Msg = "Credit balance low."
case POLL_TRANSFER_REQUEST:
poll_msg, err = getPollTransferObject(db, msgid)
poll_msg, err = p.getPollTransferObject(msgid)
if err != nil {
return poll_msg, err
}
default:
poll_msg = &PollMessage{Msgid:msgid, Msg:"unsupported"}
poll_msg = &PollMessage{}
poll_msg.Msgid = msgid
poll_msg.Msg = "unsupported"
}
poll_msg.MsgType = msgtype
poll_msg.QDate = crdate
Expand All @@ -89,4 +114,3 @@ func MarkMessageRead(db *server.DBConn, regid uint, msgid uint64) error {
_, err := db.Exec(query, msgid, regid)
return err
}

20 changes: 20 additions & 0 deletions registry/epp/epp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ func (e *EPPContext) ResolveErrorMsg(db *server.DBConn, epp_result *EPPResult, l
}
}

func (e *EPPContext) FillMsgQ(epp_result *EPPResult) error {
if e.session == nil {
return nil
}
/* could already be filled */
if epp_result.MsgQ != nil {
return nil
}
poll_msg, err := get_poll_msg(e)
if err != nil {
return err
}
epp_result.MsgQ = &poll_msg.MsgQ
return nil
}


func (e *EPPContext) SetLogger(logger server.Logger) {
e.logger = logger
}
Expand Down Expand Up @@ -177,6 +194,9 @@ func (ctx *EPPContext) ExecuteEPPCommand(ctx_ context.Context, cmd *xml.XMLComma
epp_result = &EPPResult{CmdType:EPP_UNKNOWN_CMD, RetCode:EPP_UNKNOWN_ERR}
}
epp_result.CmdType = cmd.CmdType
if err := ctx.FillMsgQ(epp_result); err != nil {
ctx.logger.Error(err)
}

ctx.ResolveErrorMsg(ctx.dbconn, epp_result, Lang)
return epp_result
Expand Down
38 changes: 27 additions & 11 deletions registry/epp/epp_poll.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
package epp

import (
// "registry/xml"
"strconv"
"registry/epp/dbreg"
. "registry/epp/eppcom"
)

func epp_poll_req_impl(ctx *EPPContext) *EPPResult {
ctx.logger.Info("Poll req")
func get_poll_msg(ctx *EPPContext) (*PollMessage, error) {
var poll_msg *PollMessage

count, err := dbreg.GetPollMessageCount(ctx.dbconn, ctx.session.Regid)
ctx.logger.Trace(ctx.session.Regid)
poll := dbreg.NewPollMsg(ctx.dbconn, ctx.session.Regid)
count, err := poll.GetPollMessageCount()
if err != nil {
ctx.logger.Error(err)
return &EPPResult{RetCode:EPP_FAILED}
return poll_msg, err
}

if count == 0 {
return &EPPResult{RetCode:EPP_POLL_NO_MSG}
poll_msg = &PollMessage{}
poll_msg.Count = count
return poll_msg, nil
}
poll_msg, err := dbreg.GetFirstUnreadPollMessage(ctx.dbconn, ctx.session.Regid)
poll_msg, err = poll.SetExtended(true).GetFirstUnreadPollMessage()
if err != nil {
return poll_msg, err
}
poll_msg.Count = count

return poll_msg, nil
}

func epp_poll_req_impl(ctx *EPPContext) *EPPResult {
ctx.logger.Info("Poll req")

poll_msg, err := get_poll_msg(ctx)
if err != nil {
ctx.logger.Error(err)
return &EPPResult{RetCode:EPP_FAILED}
}
poll_msg.Count = count

if poll_msg.Count == 0 {
return &EPPResult{RetCode:EPP_POLL_NO_MSG}
}

var res = EPPResult{RetCode:EPP_POLL_ACK_MSG}
res.Content = poll_msg
res.Content = poll_msg.Content
return &res
}

Expand All @@ -46,6 +63,5 @@ func epp_poll_ack_impl(ctx *EPPContext, msgid string) *EPPResult {
}

var res = EPPResult{RetCode:EPP_OK}
// res.Content = host_data
return &res
}
23 changes: 14 additions & 9 deletions registry/epp/eppcom/eppstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ type SecDNSUpdate struct {
RemAll bool
}

type MsgQ struct {
Msgid uint
Msg string
Count uint
MsgType uint
QDate pgtype.Timestamp /* when poll message was created */
}

type PollMessage struct {
MsgQ
Content interface{}
}

/* main structure returned by ExecuteEPPCommand */
type EPPResult struct {
CmdType int
Expand All @@ -86,6 +99,7 @@ type EPPResult struct {
Content interface{}
// EPP Extensions
Ext []EPPExt
MsgQ *MsgQ
}

type CheckResult struct {
Expand Down Expand Up @@ -229,12 +243,3 @@ type CreateDomainResult struct {
CreateObjectResult
Exdate pgtype.Timestamp
}

type PollMessage struct {
MsgType uint
Msgid uint
Msg string
Count uint
QDate pgtype.Timestamp /* when poll message was created */
Content interface{}
}
8 changes: 5 additions & 3 deletions registry/tests/epptests/epp_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func TestEPPDomainTransfer(t *testing.T) {

/* access to a second registrar should not be allowed without a transfer */
_ = infoDomain(t, eppc, test_domain, EPP_AUTHORIZATION_ERR, sessionid2)
_ = infoContact(t, eppc, test_contact, EPP_AUTHORIZATION_ERR, sessionid2)

transferDomain(t, eppc, test_domain, reg_handle2, TR_REQUEST, EPP_OK, sessionid)
transferDomain(t, eppc, test_domain, reg_handle2, TR_QUERY, EPP_OK, sessionid)
Expand All @@ -351,11 +350,11 @@ func TestEPPDomainTransfer(t *testing.T) {
if epp_res.RetCode != EPP_POLL_ACK_MSG {
t.Error("should be ", EPP_POLL_ACK_MSG, epp_res.RetCode)
}
poll_msg, ok := epp_res.Content.(*PollMessage)
_, ok := epp_res.Content.(*TransferRequestObject)
if !ok {
t.Error("should be ok")
}
pollAck(t, eppc, poll_msg.Msgid, EPP_OK, sessionid2)
pollAck(t, eppc, epp_res.MsgQ.Msgid, EPP_OK, sessionid2)

transferDomain(t, eppc, test_domain, "", TR_REJECT, EPP_OK, sessionid2)

Expand Down Expand Up @@ -409,6 +408,9 @@ func TestEPPDomainTransfer2(t *testing.T) {
createHost(t, eppc, test_host1, []string{}, EPP_OK, sessionid)
createHost(t, eppc, test_host2, []string{}, EPP_OK, sessionid)

/* access to a second registrar should not be allowed without a transfer */
_ = infoContact(t, eppc, test_contact, EPP_AUTHORIZATION_ERR, sessionid2)

createDomain(t, eppc, test_domain, test_contact, EPP_OK, sessionid)
updateDomainHosts(t, eppc, test_domain, []string{test_host1, test_host2}, []string{}, EPP_OK, sessionid)

Expand Down
5 changes: 2 additions & 3 deletions registry/tests/epptests/epp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ func TestEPPPoll(t *testing.T) {
if epp_res.RetCode != EPP_POLL_ACK_MSG {
t.Error("should be ", EPP_POLL_ACK_MSG, epp_res.RetCode)
}
poll_msg, ok := epp_res.Content.(*PollMessage)
if !ok {
if epp_res.Content == nil {
t.Error("should be ok")
}
pollAck(t, eppc, poll_msg.Msgid, EPP_OK, sessionid)
pollAck(t, eppc, epp_res.MsgQ.Msgid, EPP_OK, sessionid)
}

func infoContact(t *testing.T, eppc *epp.EPPContext, name string, retcode int, sessionid uint64) *InfoContactData {
Expand Down
Loading

0 comments on commit 143233c

Please sign in to comment.