1414
1515'use strict' ;
1616
17- var BufferBuilder = require ( './buffer-builder' ) ;
18- var IP4Address = require ( '../../../core/net/ip4-address' ) ;
19- var MACAddress = require ( '../../../core/net/mac-address' ) ;
20- var checksum = require ( '../../../core/net/checksum' ) ;
21-
22- function cksum ( u8 ) {
23- return checksum ( u8 , 0 , u8 . length , 0 ) ;
24- }
25-
26- exports . createEthernetIP4 = function ( protocol , payload , opts ) {
27- opts = opts || { } ;
28- var srcIP = IP4Address . parse ( opts . srcIP ) || new IP4Address ( 127 , 0 , 0 , 1 ) ;
29- var destIP = IP4Address . parse ( opts . destIP ) || IP4Address . ANY ;
30- var srcMAC = MACAddress . parse ( opts . srcMAC ) || MACAddress . ZERO ;
31- var destMAC = MACAddress . parse ( opts . destMAC ) || MACAddress . ZERO ;
32- var etherType = opts . etherType || 0x0800 ;
33- var tos = opts . tos || 0 ;
34- var id = opts . id || 0 ;
35- var dontFragment = opts . dontFragment || false ;
36- var moreFragments = opts . moreFragments || false ;
37- var fragmentOffsetBytes = opts . fragmentOffsetBytes || 0 ;
38- var ttl = opts . ttl || 64 ;
39-
40- var fragmentData = ( opts . fragmentOffsetBytes >>> 3 ) & 0x1fff ;
17+ const BufferBuilder = require ( './buffer-builder' ) ;
18+ const IP4Address = require ( '../../../core/net/ip4-address' ) ;
19+ const MACAddress = require ( '../../../core/net/mac-address' ) ;
20+ const checksum = require ( '../../../core/net/checksum' ) ;
21+
22+ const cksum = u8 => checksum ( u8 , 0 , u8 . length , 0 ) ;
23+
24+ exports . createEthernetIP4 = ( protocol , payload , opts = { } ) => {
25+ const srcIP = IP4Address . parse ( opts . srcIP ) || new IP4Address ( 127 , 0 , 0 , 1 ) ;
26+ const destIP = IP4Address . parse ( opts . destIP ) || IP4Address . ANY ;
27+ const srcMAC = MACAddress . parse ( opts . srcMAC ) || MACAddress . ZERO ;
28+ const destMAC = MACAddress . parse ( opts . destMAC ) || MACAddress . ZERO ;
29+ const etherType = opts . etherType || 0x0800 ;
30+ const tos = opts . tos || 0 ;
31+ const id = opts . id || 0 ;
32+ const dontFragment = opts . dontFragment || false ;
33+ const moreFragments = opts . moreFragments || false ;
34+ const fragmentOffsetBytes = opts . fragmentOffsetBytes || 0 ;
35+ const ttl = opts . ttl || 64 ;
36+
37+ let fragmentData = ( opts . fragmentOffsetBytes >>> 3 ) & 0x1fff ;
4138
4239 if ( ( fragmentData << 3 ) !== fragmentOffsetBytes ) {
43- throw new Error ( ' invalid fragment offset ' + fragmentOffsetBytes + ' byte(s)' ) ;
40+ throw new Error ( ` invalid fragment offset ${ fragmentOffsetBytes } byte(s)` ) ;
4441 }
4542
46- if ( dontFragment ) {
47- fragmentData |= ( 1 << 14 ) ;
48- }
49-
50- if ( moreFragments ) {
51- fragmentData |= ( 1 << 13 ) ;
52- }
43+ if ( dontFragment ) fragmentData |= ( 1 << 14 ) ;
44+ if ( moreFragments ) fragmentData |= ( 1 << 13 ) ;
5345
54- var protocolId = 0 ;
46+ let protocolId = 0 ;
5547 switch ( protocol ) {
56- case 'icmp' : protocolId = 0x01 ; break ;
57- case 'tcp' : protocolId = 0x06 ; break ;
58- case 'udp' : protocolId = 0x11 ; break ;
59- default : throw new Error ( 'unknown protocol' ) ;
48+ case 'icmp' : protocolId = 0x01 ; break ;
49+ case 'tcp' : protocolId = 0x06 ; break ;
50+ case 'udp' : protocolId = 0x11 ; break ;
51+ default : throw new Error ( 'unknown protocol' ) ;
6052 }
6153
6254 return new BufferBuilder ( )
@@ -95,10 +87,9 @@ exports.createEthernetIP4 = function(protocol, payload, opts) {
9587 . buffer ( ) ;
9688} ;
9789
98- exports . createUDP = function ( payload , opts ) {
99- opts = opts || { } ;
100- var srcPort = opts . srcPort || 1 ;
101- var destPort = opts . destPort || 1 ;
90+ exports . createUDP = ( payload , opts = { } ) => {
91+ const srcPort = opts . srcPort || 1 ;
92+ const destPort = opts . destPort || 1 ;
10293
10394 return new BufferBuilder ( )
10495 . uint16 ( srcPort )
@@ -109,71 +100,55 @@ exports.createUDP = function(payload, opts) {
109100 . buffer ( ) ;
110101} ;
111102
112- exports . splitBuffer = function ( u8 , chunks ) {
113- var results = [ ] ;
114- for ( var i = 0 ; i < chunks . length ; ++ i ) {
115- var chunkLength = chunks [ i ] ;
103+ exports . splitBuffer = ( u8Opt , chunks ) => {
104+ let u8 = u8Opt ;
105+ const results = [ ] ;
106+ for ( const chunkLength of chunks ) {
116107 if ( u8 . length < chunkLength ) {
117108 throw new Error ( 'need bigger buffer to produce chunks' ) ;
118109 }
119110 results . push ( u8 . subarray ( 0 , chunkLength ) ) ;
120111 u8 = u8 . subarray ( chunkLength ) ;
121112 }
122113
123- if ( u8 . length > 0 ) {
124- results . push ( u8 ) ;
125- }
114+ if ( u8 . length > 0 ) results . push ( u8 ) ;
126115
127116 return results ;
128117} ;
129118
130- exports . makeBuffer = function ( length , firstValue ) {
131- firstValue = firstValue || 0 ;
132- var u8 = new Uint8Array ( length ) ;
133- for ( var i = 0 ; i < u8 . length ; ++ i ) {
134- u8 [ i ] = firstValue ++ ;
135- }
119+ exports . makeBuffer = ( length , firstValueOpt = 0 ) => {
120+ let firstValue = firstValueOpt ;
121+ const u8 = new Uint8Array ( length ) ;
122+ for ( let i = 0 ; i < u8 . length ; ++ i ) u8 [ i ] = firstValue ++ ;
136123 return u8 ;
137124} ;
138125
139- exports . buffersEqual = function ( a , b ) {
140- if ( ! ( a instanceof Uint8Array ) || ! ( b instanceof Uint8Array ) ) {
141- return false ;
142- }
143-
144- if ( a . length !== b . length ) {
145- return false ;
146- }
126+ exports . buffersEqual = ( a , b ) => {
127+ if ( ! ( a instanceof Uint8Array ) || ! ( b instanceof Uint8Array ) ) return false ;
128+ if ( a . length !== b . length ) return false ;
147129
148- for ( var i = 0 ; i < a . length ; ++ i ) {
149- if ( a [ i ] !== b [ i ] ) {
150- return false ;
151- }
152- }
130+ for ( let i = 0 ; i < a . length ; ++ i ) if ( a [ i ] !== b [ i ] ) return false ;
153131
154132 return true ;
155133} ;
156134
157- exports . makeBufferSlices = function ( u8 , slices ) {
158- var results = [ ] ;
159- for ( var i = 0 ; i < slices . length ; ++ i ) {
160- results . push ( u8 . subarray ( slices [ i ] . offset , slices [ i ] . offset + slices [ i ] . len ) ) ;
161- }
135+ exports . makeBufferSlices = ( u8 , slices ) => {
136+ const results = [ ] ;
137+ for ( const slice of slices ) results . push ( u8 . subarray ( slice . offset , slice . offset + slice . len ) ) ;
162138 return results ;
163139} ;
164140
165- exports . createFragmentedIP4 = function ( opts , payloadLength , slices ) {
166- if ( payloadLength < 8 ) {
167- throw new Error ( 'no space for udp header in fragmented buffers' ) ;
168- }
141+ exports . createFragmentedIP4 = ( optsOpt , payloadLength , slices ) => {
142+ const opts = optsOpt ;
143+ if ( payloadLength < 8 ) throw new Error ( 'no space for udp header in fragmented buffers' ) ;
169144
170- var dataBuffer = exports . makeBuffer ( payloadLength - 8 ) ;
171- var udp = exports . createUDP ( dataBuffer , opts ) ;
172- var fragments = exports . makeBufferSlices ( udp , slices ) ;
145+ const dataBuffer = exports . makeBuffer ( payloadLength - 8 ) ;
146+ const udp = exports . createUDP ( dataBuffer , opts ) ;
147+ const fragments = exports . makeBufferSlices ( udp , slices ) ;
173148
174- var ip4fragments = [ ] ;
175- for ( var i = 0 ; i < fragments . length ; ++ i ) {
176- var fragment = fragments [ i ] ;
149+ const ip4fragments = [ ] ;
150+ for ( let i = 0 ; i < fragments . length ; ++ i ) {
151+ const fragment = fragments [ i ] ;
177152 opts . fragmentOffsetBytes = slices [ i ] . offset ;
178153 opts . moreFragments = payloadLength !== slices [ i ] . offset + slices [ i ] . len ;
179154 ip4fragments . push ( exports . createEthernetIP4 ( 'udp' , fragment , opts ) ) ;
0 commit comments