|
| 1 | +// Copyright 2015 runtime.js project authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +var ip4header = require('./ip4-header'); |
| 18 | +var ip4receive = require('./ip4-receive'); |
| 19 | +var timeNow = require('../../utils').timeNow; |
| 20 | +var FRAGMENT_QUEUE_MAX_AGE_MS = 30000; |
| 21 | +var FRAGMENT_QUEUE_MAX_COUNT = 100; |
| 22 | + |
| 23 | +function fragmentHash(srcIP, destIP, protocolId, packetId) { |
| 24 | + return srcIP.toInteger() + '-' + destIP.toInteger() + '-' + (packetId + (protocolId << 16)); |
| 25 | +} |
| 26 | + |
| 27 | +function dropFragmentQueue(intf, hash) { |
| 28 | + intf.fragments.delete(hash); |
| 29 | +} |
| 30 | + |
| 31 | +exports.addFragment = function(intf, u8, headerOffset, fragmentOffset, isMoreFragments) { |
| 32 | + var headerLength = ip4header.getHeaderLength(u8, headerOffset); |
| 33 | + var protocolId = ip4header.getProtocolId(u8, headerOffset); |
| 34 | + var srcIP = ip4header.getSrcIP(u8, headerOffset); |
| 35 | + var destIP = ip4header.getDestIP(u8, headerOffset); |
| 36 | + var packetId = ip4header.getIdentification(u8, headerOffset); |
| 37 | + var nextOffset = headerOffset + headerLength; |
| 38 | + |
| 39 | + var hash = fragmentHash(srcIP, destIP, packetId, protocolId); |
| 40 | + |
| 41 | + var firstFragment = false; |
| 42 | + var fragmentQueue = intf.fragments.get(hash); |
| 43 | + if (!fragmentQueue) { |
| 44 | + if (intf.fragments.size >= FRAGMENT_QUEUE_MAX_COUNT) { |
| 45 | + // too many fragment queues |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + firstFragment = true; |
| 50 | + fragmentQueue = { |
| 51 | + receivedLength: 0, |
| 52 | + totalLength: 0, |
| 53 | + createdAt: timeNow(), |
| 54 | + fragments: [] |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + var fragmentLength = u8.length - nextOffset; |
| 59 | + if (fragmentLength <= 0) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + var fragmentEnd = fragmentOffset + fragmentLength; |
| 64 | + |
| 65 | + if (fragmentEnd > 0xffff) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Locate non overlapping portion of new fragment |
| 70 | + var newOffset = fragmentOffset; |
| 71 | + var newEnd = fragmentEnd; |
| 72 | + var newNextOffset = nextOffset; |
| 73 | + for (var i = 0, l = fragmentQueue.fragments.length; i < l; ++i) { |
| 74 | + var fragment = fragmentQueue.fragments[i]; |
| 75 | + if (!fragment) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + var fragBegin = fragment[0]; |
| 80 | + var fragEnd = fragBegin + fragment[1]; |
| 81 | + |
| 82 | + var overlapOffset = newOffset >= fragBegin && newOffset <= fragEnd; |
| 83 | + var overlapEnd = newEnd >= fragBegin && newEnd <= fragEnd; |
| 84 | + |
| 85 | + // New fragment is fully contained within another fragment, |
| 86 | + // just ignore it |
| 87 | + if (overlapOffset && overlapEnd) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // First fragment byte is somewhere withing existing fragment |
| 92 | + if (overlapOffset && newOffset < fragEnd) { |
| 93 | + newNextOffset += fragEnd - newOffset; |
| 94 | + newOffset = fragEnd; |
| 95 | + } |
| 96 | + |
| 97 | + // Last fragment byte is somewhere withing existing fragment |
| 98 | + if (overlapEnd && newEnd > fragBegin) { |
| 99 | + newEnd = fragBegin; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Remove old fragments fully contained within the new one |
| 104 | + // By doing this we can avoid splitting big new fragments into |
| 105 | + // smaller chunks |
| 106 | + var removedIndex = -1; |
| 107 | + for (var i = 0, l = fragmentQueue.fragments.length; i < l; ++i) { |
| 108 | + var fragment = fragmentQueue.fragments[i]; |
| 109 | + if (!fragment) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + var fragBegin = fragment[0]; |
| 114 | + var fragEnd = fragBegin + fragment[1]; |
| 115 | + |
| 116 | + if (fragBegin >= newOffset && fragBegin <= newEnd && |
| 117 | + fragEnd >= newOffset && fragEnd <= newEnd) { |
| 118 | + // remove this old fragment |
| 119 | + fragmentQueue.fragments[i] = null; |
| 120 | + fragmentQueue.receivedLength -= fragment[1]; |
| 121 | + removedIndex = i; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + var newLength = newEnd - newOffset; |
| 126 | + |
| 127 | + // fragment offset - fragement length - buffer data offset - buffer |
| 128 | + var newFragment = [newOffset, newLength, newNextOffset, u8]; |
| 129 | + |
| 130 | + if (removedIndex >= 0) { |
| 131 | + fragmentQueue.fragments[removedIndex] = newFragment; |
| 132 | + } else { |
| 133 | + fragmentQueue.fragments.push(newFragment); |
| 134 | + } |
| 135 | + |
| 136 | + fragmentQueue.receivedLength += newLength; |
| 137 | + |
| 138 | + // Last fragment? |
| 139 | + if (!isMoreFragments) { |
| 140 | + |
| 141 | + // Another last fragment? |
| 142 | + if (fragmentQueue.totalLength > fragmentEnd) { |
| 143 | + // Wrong len |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + fragmentQueue.totalLength = fragmentEnd; |
| 148 | + } |
| 149 | + |
| 150 | + if (firstFragment) { |
| 151 | + intf.fragments.set(hash, fragmentQueue); |
| 152 | + } |
| 153 | + |
| 154 | + if (fragmentQueue.totalLength === fragmentQueue.receivedLength) { |
| 155 | + var u8asm = new Uint8Array(fragmentQueue.totalLength); |
| 156 | + for (var i = 0, l = fragmentQueue.fragments.length; i < l; ++i) { |
| 157 | + var fragment = fragmentQueue.fragments[i]; |
| 158 | + if (!fragment) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + |
| 162 | + var itemOffset = fragment[0]; |
| 163 | + var itemLength = fragment[1]; |
| 164 | + var itemNextOffset = fragment[2]; |
| 165 | + var itemBuffer = fragment[3]; |
| 166 | + u8asm.set(itemBuffer.subarray(itemNextOffset, itemNextOffset + itemLength), itemOffset); |
| 167 | + } |
| 168 | + |
| 169 | + dropFragmentQueue(intf, hash); |
| 170 | + ip4receive(intf, srcIP, destIP, protocolId, u8asm, 0); |
| 171 | + return; |
| 172 | + } |
| 173 | +}; |
| 174 | + |
| 175 | +/** |
| 176 | + * Timer tick |
| 177 | + * |
| 178 | + * @param {Interface} intf Network interface |
| 179 | + */ |
| 180 | +exports.tick = function(intf) { |
| 181 | + var time = timeNow(); |
| 182 | + |
| 183 | + for (var pair of intf.fragments) { |
| 184 | + var hash = pair[0]; |
| 185 | + var fragmentQueue = pair[1]; |
| 186 | + if (fragmentQueue.createdAt + FRAGMENT_QUEUE_MAX_AGE_MS <= time) { |
| 187 | + dropFragmentQueue(intf, hash); |
| 188 | + } |
| 189 | + } |
| 190 | +}; |
0 commit comments