| 1 | package sftp |
| 2 | |
| 3 | import ( |
| 4 | "encoding" |
| 5 | "sort" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | type packetManager struct { |
| 13 | requests chan orderedPacket |
| 14 | responses chan orderedPacket |
| 15 | fini chan struct{} |
| 16 | incoming orderedPackets |
| 17 | outgoing orderedPackets |
| 18 | sender packetSender |
| 19 | working *sync.WaitGroup |
| 20 | packetCount uint32 |
| 21 | |
| 22 | alloc *allocator |
| 23 | |
| 24 | sequential bool |
| 25 | } |
| 26 | |
| 27 | type packetSender interface { |
| 28 | sendPacket(encoding.BinaryMarshaler) error |
| 29 | } |
| 30 | |
| 31 | func newPktMgr(sender packetSender) *packetManager { |
| 32 | s := &packetManager{ |
| 33 | requests: make(chan orderedPacket, SftpServerWorkerCount), |
| 34 | responses: make(chan orderedPacket, SftpServerWorkerCount), |
| 35 | fini: make(chan struct{}), |
| 36 | incoming: make([]orderedPacket, 0, SftpServerWorkerCount), |
| 37 | outgoing: make([]orderedPacket, 0, SftpServerWorkerCount), |
| 38 | sender: sender, |
| 39 | working: &sync.WaitGroup{}, |
| 40 | } |
| 41 | go s.controller() |
| 42 | return s |
| 43 | } |
| 44 | |
| 45 | |
| 46 | func (s *packetManager) newOrderID() uint32 { |
| 47 | s.packetCount++ |
| 48 | return s.packetCount |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | func (s *packetManager) getNextOrderID() uint32 { |
| 56 | return s.packetCount + 1 |
| 57 | } |
| 58 | |
| 59 | type orderedRequest struct { |
| 60 | requestPacket |
| 61 | orderid uint32 |
| 62 | } |
| 63 | |
| 64 | func (s *packetManager) newOrderedRequest(p requestPacket) orderedRequest { |
| 65 | return orderedRequest{requestPacket: p, orderid: s.newOrderID()} |
| 66 | } |
| 67 | func (p orderedRequest) orderID() uint32 { return p.orderid } |
| 68 | |
| 69 | type orderedResponse struct { |
| 70 | responsePacket |
| 71 | orderid uint32 |
| 72 | } |
| 73 | |
| 74 | func (s *packetManager) newOrderedResponse(p responsePacket, id uint32, |
| 75 | ) orderedResponse { |
| 76 | return orderedResponse{responsePacket: p, orderid: id} |
| 77 | } |
| 78 | func (p orderedResponse) orderID() uint32 { return p.orderid } |
| 79 | |
| 80 | type orderedPacket interface { |
| 81 | id() uint32 |
| 82 | orderID() uint32 |
| 83 | } |
| 84 | type orderedPackets []orderedPacket |
| 85 | |
| 86 | func (o orderedPackets) Sort() { |
| 87 | sort.Slice(o, func(i, j int) bool { |
| 88 | |
| 89 | return int32(o[i].orderID()-o[j].orderID()) < 0 |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | func (s *packetManager) incomingPacket(pkt orderedRequest) { |
| 96 | s.working.Add(1) |
| 97 | s.requests <- pkt |
| 98 | } |
| 99 | |
| 100 | |
| 101 | func (s *packetManager) readyPacket(pkt orderedResponse) { |
| 102 | s.responses <- pkt |
| 103 | s.working.Done() |
| 104 | } |
| 105 | |
| 106 | |
| 107 | func (s *packetManager) close() { |
| 108 | |
| 109 | s.working.Wait() |
| 110 | close(s.fini) |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | func (s *packetManager) workerChan(runWorker func(chan orderedRequest), |
| 117 | ) chan orderedRequest { |
| 118 | if s.sequential { |
| 119 | ch := make(chan orderedRequest) |
| 120 | runWorker(ch) |
| 121 | pktChan := make(chan orderedRequest, SftpServerWorkerCount) |
| 122 | go func() { |
| 123 | for pkt := range pktChan { |
| 124 | s.incomingPacket(pkt) |
| 125 | ch <- pkt |
| 126 | } |
| 127 | close(ch) |
| 128 | s.close() |
| 129 | }() |
| 130 | return pktChan |
| 131 | } |
| 132 | |
| 133 | |
| 134 | rwChan := make(chan orderedRequest, SftpServerWorkerCount) |
| 135 | for i := 0; i < SftpServerWorkerCount; i++ { |
| 136 | runWorker(rwChan) |
| 137 | } |
| 138 | |
| 139 | |
| 140 | cmdChan := make(chan orderedRequest) |
| 141 | runWorker(cmdChan) |
| 142 | |
| 143 | pktChan := make(chan orderedRequest, SftpServerWorkerCount) |
| 144 | go func() { |
| 145 | for pkt := range pktChan { |
| 146 | switch p := pkt.requestPacket.(type) { |
| 147 | case *sshFxpReadPacket, *sshFxpWritePacket: |
| 148 | s.incomingPacket(pkt) |
| 149 | rwChan <- pkt |
| 150 | continue |
| 151 | case *sshFxpClosePacket: |
| 152 | |
| 153 | |
| 154 | s.working.Wait() |
| 155 | case *sshFxpExtendedPacket: |
| 156 | |
| 157 | if _, ok := p.SpecificPacket.(*sshFxpFsyncPacket); ok { |
| 158 | s.working.Wait() |
| 159 | } |
| 160 | } |
| 161 | s.incomingPacket(pkt) |
| 162 | |
| 163 | cmdChan <- pkt |
| 164 | } |
| 165 | close(rwChan) |
| 166 | close(cmdChan) |
| 167 | s.close() |
| 168 | }() |
| 169 | |
| 170 | return pktChan |
| 171 | } |
| 172 | |
| 173 | |
| 174 | func (s *packetManager) controller() { |
| 175 | for { |
| 176 | select { |
| 177 | case pkt := <-s.requests: |
| 178 | debug("incoming id (oid): %v (%v)", pkt.id(), pkt.orderID()) |
| 179 | s.incoming = append(s.incoming, pkt) |
| 180 | s.incoming.Sort() |
| 181 | case pkt := <-s.responses: |
| 182 | debug("outgoing id (oid): %v (%v)", pkt.id(), pkt.orderID()) |
| 183 | s.outgoing = append(s.outgoing, pkt) |
| 184 | s.outgoing.Sort() |
| 185 | case <-s.fini: |
| 186 | return |
| 187 | } |
| 188 | s.maybeSendPackets() |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | func (s *packetManager) maybeSendPackets() { |
| 194 | for { |
| 195 | if len(s.outgoing) == 0 || len(s.incoming) == 0 { |
| 196 | debug("break! -- outgoing: %v; incoming: %v", |
| 197 | len(s.outgoing), len(s.incoming)) |
| 198 | break |
| 199 | } |
| 200 | out := s.outgoing[0] |
| 201 | in := s.incoming[0] |
| 202 | |
| 203 | |
| 204 | if in.orderID() == out.orderID() { |
| 205 | debug("Sending packet: %v", out.id()) |
| 206 | s.sender.sendPacket(out.(encoding.BinaryMarshaler)) |
| 207 | if s.alloc != nil { |
| 208 | |
| 209 | s.alloc.ReleasePages(in.orderID()) |
| 210 | } |
| 211 | |
| 212 | copy(s.incoming, s.incoming[1:]) |
| 213 | s.incoming[len(s.incoming)-1] = nil |
| 214 | s.incoming = s.incoming[:len(s.incoming)-1] |
| 215 | copy(s.outgoing, s.outgoing[1:]) |
| 216 | s.outgoing[len(s.outgoing)-1] = nil |
| 217 | s.outgoing = s.outgoing[:len(s.outgoing)-1] |
| 218 | } else { |
| 219 | break |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |