a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 4.4 kB view raw
1package sftp 2 3import ( 4 "encoding" 5 "fmt" 6) 7 8// all incoming packets 9type requestPacket interface { 10 encoding.BinaryUnmarshaler 11 id() uint32 12} 13 14type responsePacket interface { 15 encoding.BinaryMarshaler 16 id() uint32 17} 18 19// interfaces to group types 20type hasPath interface { 21 requestPacket 22 getPath() string 23} 24 25type hasHandle interface { 26 requestPacket 27 getHandle() string 28} 29 30type notReadOnly interface { 31 notReadOnly() 32} 33 34// // define types by adding methods 35// hasPath 36func (p *sshFxpLstatPacket) getPath() string { return p.Path } 37func (p *sshFxpStatPacket) getPath() string { return p.Path } 38func (p *sshFxpRmdirPacket) getPath() string { return p.Path } 39func (p *sshFxpReadlinkPacket) getPath() string { return p.Path } 40func (p *sshFxpRealpathPacket) getPath() string { return p.Path } 41func (p *sshFxpMkdirPacket) getPath() string { return p.Path } 42func (p *sshFxpSetstatPacket) getPath() string { return p.Path } 43func (p *sshFxpStatvfsPacket) getPath() string { return p.Path } 44func (p *sshFxpRemovePacket) getPath() string { return p.Filename } 45func (p *sshFxpRenamePacket) getPath() string { return p.Oldpath } 46func (p *sshFxpSymlinkPacket) getPath() string { return p.Targetpath } 47func (p *sshFxpOpendirPacket) getPath() string { return p.Path } 48func (p *sshFxpOpenPacket) getPath() string { return p.Path } 49 50func (p *sshFxpExtendedPacketPosixRename) getPath() string { return p.Oldpath } 51func (p *sshFxpExtendedPacketHardlink) getPath() string { return p.Oldpath } 52 53// getHandle 54func (p *sshFxpFstatPacket) getHandle() string { return p.Handle } 55func (p *sshFxpFsetstatPacket) getHandle() string { return p.Handle } 56func (p *sshFxpReadPacket) getHandle() string { return p.Handle } 57func (p *sshFxpWritePacket) getHandle() string { return p.Handle } 58func (p *sshFxpReaddirPacket) getHandle() string { return p.Handle } 59func (p *sshFxpClosePacket) getHandle() string { return p.Handle } 60 61// notReadOnly 62func (p *sshFxpWritePacket) notReadOnly() {} 63func (p *sshFxpSetstatPacket) notReadOnly() {} 64func (p *sshFxpFsetstatPacket) notReadOnly() {} 65func (p *sshFxpRemovePacket) notReadOnly() {} 66func (p *sshFxpMkdirPacket) notReadOnly() {} 67func (p *sshFxpRmdirPacket) notReadOnly() {} 68func (p *sshFxpRenamePacket) notReadOnly() {} 69func (p *sshFxpSymlinkPacket) notReadOnly() {} 70func (p *sshFxpExtendedPacketPosixRename) notReadOnly() {} 71func (p *sshFxpExtendedPacketHardlink) notReadOnly() {} 72 73// some packets with ID are missing id() 74func (p *sshFxpDataPacket) id() uint32 { return p.ID } 75func (p *sshFxpStatusPacket) id() uint32 { return p.ID } 76func (p *sshFxpStatResponse) id() uint32 { return p.ID } 77func (p *sshFxpNamePacket) id() uint32 { return p.ID } 78func (p *sshFxpHandlePacket) id() uint32 { return p.ID } 79func (p *StatVFS) id() uint32 { return p.ID } 80func (p *sshFxVersionPacket) id() uint32 { return 0 } 81 82// take raw incoming packet data and build packet objects 83func makePacket(p rxPacket) (requestPacket, error) { 84 var pkt requestPacket 85 switch p.pktType { 86 case sshFxpInit: 87 pkt = &sshFxInitPacket{} 88 case sshFxpLstat: 89 pkt = &sshFxpLstatPacket{} 90 case sshFxpOpen: 91 pkt = &sshFxpOpenPacket{} 92 case sshFxpClose: 93 pkt = &sshFxpClosePacket{} 94 case sshFxpRead: 95 pkt = &sshFxpReadPacket{} 96 case sshFxpWrite: 97 pkt = &sshFxpWritePacket{} 98 case sshFxpFstat: 99 pkt = &sshFxpFstatPacket{} 100 case sshFxpSetstat: 101 pkt = &sshFxpSetstatPacket{} 102 case sshFxpFsetstat: 103 pkt = &sshFxpFsetstatPacket{} 104 case sshFxpOpendir: 105 pkt = &sshFxpOpendirPacket{} 106 case sshFxpReaddir: 107 pkt = &sshFxpReaddirPacket{} 108 case sshFxpRemove: 109 pkt = &sshFxpRemovePacket{} 110 case sshFxpMkdir: 111 pkt = &sshFxpMkdirPacket{} 112 case sshFxpRmdir: 113 pkt = &sshFxpRmdirPacket{} 114 case sshFxpRealpath: 115 pkt = &sshFxpRealpathPacket{} 116 case sshFxpStat: 117 pkt = &sshFxpStatPacket{} 118 case sshFxpRename: 119 pkt = &sshFxpRenamePacket{} 120 case sshFxpReadlink: 121 pkt = &sshFxpReadlinkPacket{} 122 case sshFxpSymlink: 123 pkt = &sshFxpSymlinkPacket{} 124 case sshFxpExtended: 125 pkt = &sshFxpExtendedPacket{} 126 default: 127 return nil, fmt.Errorf("unhandled packet type: %s", p.pktType) 128 } 129 if err := pkt.UnmarshalBinary(p.pktBytes); err != nil { 130 // Return partially unpacked packet to allow callers to return 131 // error messages appropriately with necessary id() method. 132 return pkt, err 133 } 134 return pkt, nil 135}