@cto.af/pcap-ng-parser 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.mjs +1 -1
- package/lib/options.d.mts +0 -1
- package/lib/options.mjs +1 -1
- package/package.json +4 -4
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{decryptionSecretsFormat,enhancedPacketBlockFormat,interfaceDescriptionBlockFormat,interfaceStatisticsFormat,nameResolutionFormat,optionBlock,pcapHeaderFormat,pcapPacketFormat,sectionHeaderBlock,timestampFormat}from"./blocks.mjs";import{CUSTOM_COPY,CUSTOM_NOCOPY,DECRYPTION_SECRETS,ENHANCED_PACKET,INTERFACE_DESCRIPTION,INTERFACE_STATISTICS,NAME_RESOLUTION,OPTION_NAMES,SECTION_HEADER,SIMPLE_PACKET}from"./options.mjs";import{EPB_FLAGS_DIRECTIONS,EPB_FLAGS_LINK_LAYER_ERRORS,EPB_FLAGS_RECEPTION,LocalErrorEvent}from"./events.mjs";import{colons,pad4}from"./utils.mjs";import{LINK_TYPE_NAMES}from"./linkTypes.mjs";import{DataViewReader,DataViewWritableStream,TruncationError}from"dataview-stream";import{assert}from"@cto.af/utils";import{decode}from"@leichtgewicht/ip-codec";const TD=new TextDecoder;var RecoverableError=class extends Error{constructor(err){super(err instanceof Error?err.message:`Recoverable Error`,{cause:err})}},PCAPNGParser=class extends WritableStream{interfaces=[];#listeners=new WeakMap;#et=new EventTarget;#dv;#ng=!0;#opts;constructor(opts={}){let dv=new DataViewWritableStream,writer=dv.getWriter(),self;super({write(chunk){return writer.write(chunk)},async abort(reason){await writer.abort(reason),self?.dispatchEvent(new LocalErrorEvent(`error`,{error:reason}))},async close(){await writer.close()}}),self=this,this.#opts={rejectOld:!1,rejectNG:!1,...opts},this.#dv=dv,this.#readFile()}get ng(){return this.#ng}get endianess(){return this.#dv.littleEndian?`LE`:`BE`}addEventListener(type,callback,options){this.#et.addEventListener(type,callback,options)}dispatchEvent(event){return this.#et.dispatchEvent(event)}removeEventListener(type,callback,options){this.#et.removeEventListener(type,callback,options)}on(eventName,listener){let wrapper=ev=>{eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper),this}once(eventName,listener){let wrapper=ev=>{this.#listeners.delete(listener),eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper,{once:!0}),this}off(eventName,listener){let wrapper=this.#listeners.get(listener);if(!wrapper)throw Error(`Unknown listener`);return this.removeEventListener(eventName,wrapper),this.#listeners.delete(listener),this}#timestamp(stampHigh,stampLow,interfaceId){let stamp=BigInt(stampHigh)<<32n|BigInt(stampLow),int=this.interfaces[interfaceId];assert(int,`Interface always checked before calling`);let off=int.tsoffset+stamp/int.tsresol;return new Date(Number(off))}#dvr(bytes){return new DataViewReader(bytes,{littleEndian:this.#dv.littleEndian})}async#readFile(){try{let magicBuf=await this.#dv.peek(8),magic=this.#dvr(magicBuf).u32();if(magic===SECTION_HEADER)await this.#readSections();else if(magic===2712847316)await this.#readPCAP(!1,!1);else if(magic===3569595041)await this.#readPCAP(!0,!1);else if(magic===2712812621)await this.#readPCAP(!1,!0);else if(magic===1295823521)await this.#readPCAP(!0,!0);else throw Error(`Invalid file format: magic = ${magic}`)}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error}))}this.dispatchEvent(new Event(`close`))}async#readPCAP(littleEndian,nano){if(this.#opts.rejectOld)throw Error(`Old PCAP format detected and rejected`);littleEndian&&(this.#dv.littleEndian=!this.#dv.littleEndian),this.#ng=!1;let{linkType,snapLen}=await this.#dv.struct(pcapHeaderFormat),tsresol=nano?1000000n:1000n,int={linkType:linkType&65535,snapLen,tsresol,tsoffset:0n,options:[]},ltName=LINK_TYPE_NAMES.get(int.linkType);if(ltName&&(int.linkTypeName=ltName),linkType&67108864){let FCSlen=linkType>>28;int.options.push({optionType:13,name:`if_fcslen`,data:new Uint8Array([FCSlen*16])})}for(this.interfaces.push(int),this.dispatchEvent(new CustomEvent(`interface`,{detail:int}));;){try{await this.#dv.waitFor(4)}catch(er){if(er instanceof TruncationError&&er.requested===4)return;throw er}let{timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=await this.#dv.struct(pcapPacketFormat),data=await this.#dv.bytes(capturedPacketLength),pkt={interfaceId:0,timestamp:new Date(timestampHigh*1e3+timestampLow/Number(tsresol)),originalPacketLength,data,options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}}async#readSections(){if(this.#opts.rejectNG)throw Error(`PCAPng format detected and rejected`);for(;;){let block=await this.#readBlock();if(!block)break;try{switch(block.blockType){case SECTION_HEADER:await this.#processSectionHeader(block);break;case INTERFACE_DESCRIPTION:await this.#processInterface(block);break;case SIMPLE_PACKET:await this.#processSimplePacket(block);break;case NAME_RESOLUTION:await this.#processNameResolution(block);break;case INTERFACE_STATISTICS:await this.#processInterfaceStatistics(block);break;case ENHANCED_PACKET:await this.#processEnhancedPacket(block);break;case DECRYPTION_SECRETS:await this.#processDecryptionSecrets(block);break;case CUSTOM_COPY:case CUSTOM_NOCOPY:await this.#processCustom(block);break;default:this.dispatchEvent(new CustomEvent(`unknown`,{detail:block.blockType}));break}}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error:new RecoverableError(error)}))}}}async#readBlock(){let blockType;try{blockType=await this.#dv.u32()}catch(er){assert(er instanceof TruncationError),assert(er.requested===4);return}if(blockType===SECTION_HEADER){let prelude=await this.#dv.peek(8),dv=this.#dvr(prelude);dv.skip(4);let bom=dv.u32();if(bom!==439041101)if(bom===1295788826)this.#dv.littleEndian=!this.#dv.littleEndian;else throw Error(`Unable to determine endian from 0x${bom.toString(16)}`)}let blockTotalLength=await this.#dv.u32(),dataLen=blockTotalLength-12,data=await this.#dv.bytes(dataLen),block={blockType,blockTotalLength,data:this.#dvr(data)};await this.#dv.bytes(pad4(dataLen));let endTotalLength=await this.#dv.u32();if(endTotalLength!==blockTotalLength)throw Error(`Length mismatch, ${endTotalLength} != ${blockTotalLength}`);return block}#readOptions(block,interfaceId){let foundEndOption=!1,options=[];for(;!block.data.finished&&!foundEndOption;){let rb=block.data.struct(optionBlock);if(rb.optionType===0)foundEndOption=!0;else{let pen,name,optLen=rb.dataLength,typ,desc=OPTION_NAMES.get(block.blockType)?.get(rb.optionType);desc&&(name={name:desc[0]},typ=desc[1],desc[2]&&(pen={pen:block.data.u32()},optLen-=4));let opt={optionType:rb.optionType,...name,...pen};switch(rb.optionType&32768&&(opt.private=!0),typ){case`string`:{let left=block.data.bytes(optLen);opt.str=TD.decode(left).replaceAll(`\0`,``).trim();break}case`ipv4`:case`ipv6`:opt.str=decode(block.data.bytes(rb.dataLength));break;case`ipv4mask`:if(rb.dataLength!==8)throw Error(`Invalid ipv4mask option`);opt.str=`${decode(block.data.bytes(4))}/${decode(block.data.bytes(4))}`;break;case`ipv6prefix`:if(rb.dataLength!==17)throw Error(`Invalid ipv6prefix option`);opt.str=`${decode(block.data.bytes(16))}/${block.data.u8()}`;break;case`eui`:opt.str=colons(block.data.bytes(rb.dataLength));break;case`u32`:opt.bigint=BigInt(block.data.u32());break;case`u64`:opt.bigint=block.data.u64();break;case`timestamp`:{let{timestampHigh,timestampLow}=block.data.struct(timestampFormat);opt.date=this.#timestamp(timestampHigh,timestampLow,interfaceId);break}default:opt.data=block.data.bytes(rb.dataLength);break}block.data.skip(pad4(rb.dataLength)),options.push(opt)}}return options}#processSectionHeader(block){let detail={...block.data.struct(sectionHeaderBlock),endianess:this.endianess,options:this.#readOptions(block)};this.interfaces=[],this.dispatchEvent(new CustomEvent(`section`,{detail}))}#processInterface(block){let idRes=block.data.struct(interfaceDescriptionBlockFormat),iData={linkType:idRes.linkType,snapLen:idRes.snapLen,tsoffset:0n,tsresol:1000n,options:this.#readOptions(block)};for(let opt of iData.options)switch(opt.optionType){case 2:iData.name=opt.str;break;case 14:iData.tsoffset=opt.bigint*1000n;break;case 9:{let tsresol=opt.data[0];tsresol&128?iData.tsresol=(1n<<BigInt(tsresol&127))/1000n:iData.tsresol=10n**(BigInt(tsresol)-3n);break}}this.interfaces.push(iData),this.dispatchEvent(new CustomEvent(`interface`,{detail:iData}))}#processSimplePacket(block){let[int]=this.interfaces;if(!int)throw Error(`No interface for simple packet`);let originalPacketLength=block.data.u32(),len=Math.min(originalPacketLength,int.snapLen),pkt={interfaceId:0,originalPacketLength,data:block.data.bytes(len),options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processNameResolution(block){let res={records:[],options:[]};for(;;){let{recordType,recordValueLength}=block.data.struct(nameResolutionFormat);if(recordType===0)break;let val=block.data.bytes(recordValueLength);switch(block.data.skip(pad4(recordValueLength)),recordType){case 1:if(recordValueLength<6)throw Error(`Invalid nrb_record_ipv4 record`);res.records.push({name:`nrb_record_ipv4`,ipv4:decode(val.subarray(0,4)),entries:TD.decode(val.subarray(4)).split(`\0`).slice(0,-1)});break;case 2:if(recordValueLength<18)throw Error(`Invalid nrb_record_ipv6 record`);res.records.push({name:`nrb_record_ipv6`,ipv6:decode(val.subarray(0,16)),entries:TD.decode(val.subarray(16)).split(`\0`).slice(0,-1)});break;case 3:if(recordValueLength<8)throw Error(`Invalid nrb_record_eui48 record`);res.records.push({name:`nrb_record_eui48`,eui48:colons(val.subarray(0,6)),entries:TD.decode(val.subarray(6)).split(`\0`).slice(0,-1)});break;case 4:if(recordValueLength<10)throw Error(`Invalid nrb_record_eui64 record`);res.records.push({name:`nrb_record_eui64`,eui64:colons(val.subarray(0,8)),entries:TD.decode(val.subarray(8)).split(`\0`).slice(0,-1)});break}}res.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`names`,{detail:res}))}#processInterfaceStatistics(block){let{interfaceId,timestampHigh,timestampLow}=block.data.struct(interfaceStatisticsFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface id: ${interfaceId}`);let stats={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),options:this.#readOptions(block,interfaceId)};this.dispatchEvent(new CustomEvent(`stats`,{detail:stats}))}#processEnhancedPacket(block){let{interfaceId,timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=block.data.struct(enhancedPacketBlockFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface ID: ${interfaceId} >= ${this.interfaces.length}`);let data=block.data.bytes(capturedPacketLength),pkt={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),originalPacketLength,data,options:[]};block.data.skip(pad4(capturedPacketLength)),pkt.options=this.#readOptions(block,interfaceId);for(let o of pkt.options)if(o.optionType===2){assert(o.data);let flags=this.#dvr(o.data).u32();pkt.flags={direction:EPB_FLAGS_DIRECTIONS[flags&3],reception:EPB_FLAGS_RECEPTION[flags>>2&7],FCSlen:flags>>5&15,noChecksum:!!(flags>>9&1),checksumValid:!!(flags>>10&1),TCPsegmentationOffload:!!(flags>>11&1),linkLayerErrors:[]},flags>>=16;for(let i=0;i<8;i++)flags&1&&pkt.flags.linkLayerErrors?.push(EPB_FLAGS_LINK_LAYER_ERRORS[i]),flags>>=1}this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processDecryptionSecrets(block){let{secretsType,secretsLength}=block.data.struct(decryptionSecretsFormat),detail={secretsType,data:block.data.bytes(secretsLength),options:[]};block.data.skip(pad4(secretsLength)),detail.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`secrets`,{detail}))}#processCustom(block){let detail={pen:block.data.u32(),data:block.data.unused(),copy:block.blockType===CUSTOM_COPY};this.dispatchEvent(new CustomEvent(`custom`,{detail}))}},src_default=PCAPNGParser;export{PCAPNGParser,RecoverableError,src_default as default};
|
|
1
|
+
import{decryptionSecretsFormat,enhancedPacketBlockFormat,interfaceDescriptionBlockFormat,interfaceStatisticsFormat,nameResolutionFormat,optionBlock,pcapHeaderFormat,pcapPacketFormat,sectionHeaderBlock,timestampFormat}from"./blocks.mjs";import{CUSTOM_COPY,CUSTOM_NOCOPY,OPTION_NAMES,SECTION_HEADER}from"./options.mjs";import{EPB_FLAGS_DIRECTIONS,EPB_FLAGS_LINK_LAYER_ERRORS,EPB_FLAGS_RECEPTION,LocalErrorEvent}from"./events.mjs";import{colons,pad4}from"./utils.mjs";import{LINK_TYPE_NAMES}from"./linkTypes.mjs";import{DataViewReader,DataViewWritableStream,TruncationError}from"dataview-stream";import{assert}from"@cto.af/utils";import{decode}from"@leichtgewicht/ip-codec";const TD=new TextDecoder;var RecoverableError=class extends Error{constructor(err){super(err instanceof Error?err.message:`Recoverable Error`,{cause:err})}},PCAPNGParser=class extends WritableStream{interfaces=[];#listeners=new WeakMap;#et=new EventTarget;#dv;#ng=!0;#opts;constructor(opts={}){let dv=new DataViewWritableStream,writer=dv.getWriter(),self;super({write(chunk){return writer.write(chunk)},async abort(reason){await writer.abort(reason),self?.dispatchEvent(new LocalErrorEvent(`error`,{error:reason}))},async close(){await writer.close()}}),self=this,this.#opts={rejectOld:!1,rejectNG:!1,...opts},this.#dv=dv,this.#readFile()}get ng(){return this.#ng}get endianess(){return this.#dv.littleEndian?`LE`:`BE`}addEventListener(type,callback,options){this.#et.addEventListener(type,callback,options)}dispatchEvent(event){return this.#et.dispatchEvent(event)}removeEventListener(type,callback,options){this.#et.removeEventListener(type,callback,options)}on(eventName,listener){let wrapper=ev=>{eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper),this}once(eventName,listener){let wrapper=ev=>{this.#listeners.delete(listener),eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper,{once:!0}),this}off(eventName,listener){let wrapper=this.#listeners.get(listener);if(!wrapper)throw Error(`Unknown listener`);return this.removeEventListener(eventName,wrapper),this.#listeners.delete(listener),this}#timestamp(stampHigh,stampLow,interfaceId){let stamp=BigInt(stampHigh)<<32n|BigInt(stampLow),int=this.interfaces[interfaceId];assert(int,`Interface always checked before calling`);let off=int.tsoffset+stamp/int.tsresol;return new Date(Number(off))}#dvr(bytes){return new DataViewReader(bytes,{littleEndian:this.#dv.littleEndian})}async#readFile(){try{let magicBuf=await this.#dv.peek(8),magic=this.#dvr(magicBuf).u32();if(magic===168627466)await this.#readSections();else if(magic===2712847316)await this.#readPCAP(!1,!1);else if(magic===3569595041)await this.#readPCAP(!0,!1);else if(magic===2712812621)await this.#readPCAP(!1,!0);else if(magic===1295823521)await this.#readPCAP(!0,!0);else throw Error(`Invalid file format: magic = ${magic}`)}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error}))}this.dispatchEvent(new Event(`close`))}async#readPCAP(littleEndian,nano){if(this.#opts.rejectOld)throw Error(`Old PCAP format detected and rejected`);littleEndian&&(this.#dv.littleEndian=!this.#dv.littleEndian),this.#ng=!1;let{linkType,snapLen}=await this.#dv.struct(pcapHeaderFormat),tsresol=nano?1000000n:1000n,int={linkType:linkType&65535,snapLen,tsresol,tsoffset:0n,options:[]},ltName=LINK_TYPE_NAMES.get(int.linkType);if(ltName&&(int.linkTypeName=ltName),linkType&67108864){let FCSlen=linkType>>28;int.options.push({optionType:13,name:`if_fcslen`,data:new Uint8Array([FCSlen*16])})}for(this.interfaces.push(int),this.dispatchEvent(new CustomEvent(`interface`,{detail:int}));;){try{await this.#dv.waitFor(4)}catch(er){if(er instanceof TruncationError&&er.requested===4)return;throw er}let{timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=await this.#dv.struct(pcapPacketFormat),data=await this.#dv.bytes(capturedPacketLength),pkt={interfaceId:0,timestamp:new Date(timestampHigh*1e3+timestampLow/Number(tsresol)),originalPacketLength,data,options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}}async#readSections(){if(this.#opts.rejectNG)throw Error(`PCAPng format detected and rejected`);for(;;){let block=await this.#readBlock();if(!block)break;try{switch(block.blockType){case SECTION_HEADER:await this.#processSectionHeader(block);break;case 1:await this.#processInterface(block);break;case 3:await this.#processSimplePacket(block);break;case 4:await this.#processNameResolution(block);break;case 5:await this.#processInterfaceStatistics(block);break;case 6:await this.#processEnhancedPacket(block);break;case 10:await this.#processDecryptionSecrets(block);break;case CUSTOM_COPY:case CUSTOM_NOCOPY:await this.#processCustom(block);break;default:this.dispatchEvent(new CustomEvent(`unknown`,{detail:block.blockType}));break}}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error:new RecoverableError(error)}))}}}async#readBlock(){let blockType;try{blockType=await this.#dv.u32()}catch(er){assert(er instanceof TruncationError),assert(er.requested===4);return}if(blockType===168627466){let prelude=await this.#dv.peek(8),dv=this.#dvr(prelude);dv.skip(4);let bom=dv.u32();if(bom!==439041101)if(bom===1295788826)this.#dv.littleEndian=!this.#dv.littleEndian;else throw Error(`Unable to determine endian from 0x${bom.toString(16)}`)}let blockTotalLength=await this.#dv.u32(),dataLen=blockTotalLength-12,data=await this.#dv.bytes(dataLen),block={blockType,blockTotalLength,data:this.#dvr(data)};await this.#dv.bytes(pad4(dataLen));let endTotalLength=await this.#dv.u32();if(endTotalLength!==blockTotalLength)throw Error(`Length mismatch, ${endTotalLength} != ${blockTotalLength}`);return block}#readOptions(block,interfaceId){let foundEndOption=!1,options=[];for(;!block.data.finished&&!foundEndOption;){let rb=block.data.struct(optionBlock);if(rb.optionType===0)foundEndOption=!0;else{let pen,name,optLen=rb.dataLength,typ,desc=OPTION_NAMES.get(block.blockType)?.get(rb.optionType);desc&&(name={name:desc[0]},typ=desc[1],desc[2]&&(pen={pen:block.data.u32()},optLen-=4));let opt={optionType:rb.optionType,...name,...pen};switch(rb.optionType&32768&&(opt.private=!0),typ){case`string`:{let left=block.data.bytes(optLen);opt.str=TD.decode(left).replaceAll(`\0`,``).trim();break}case`ipv4`:case`ipv6`:opt.str=decode(block.data.bytes(rb.dataLength));break;case`ipv4mask`:if(rb.dataLength!==8)throw Error(`Invalid ipv4mask option`);opt.str=`${decode(block.data.bytes(4))}/${decode(block.data.bytes(4))}`;break;case`ipv6prefix`:if(rb.dataLength!==17)throw Error(`Invalid ipv6prefix option`);opt.str=`${decode(block.data.bytes(16))}/${block.data.u8()}`;break;case`eui`:opt.str=colons(block.data.bytes(rb.dataLength));break;case`u32`:opt.bigint=BigInt(block.data.u32());break;case`u64`:opt.bigint=block.data.u64();break;case`timestamp`:{let{timestampHigh,timestampLow}=block.data.struct(timestampFormat);opt.date=this.#timestamp(timestampHigh,timestampLow,interfaceId);break}default:opt.data=block.data.bytes(rb.dataLength);break}block.data.skip(pad4(rb.dataLength)),options.push(opt)}}return options}#processSectionHeader(block){let detail={...block.data.struct(sectionHeaderBlock),endianess:this.endianess,options:this.#readOptions(block)};this.interfaces=[],this.dispatchEvent(new CustomEvent(`section`,{detail}))}#processInterface(block){let idRes=block.data.struct(interfaceDescriptionBlockFormat),iData={linkType:idRes.linkType,snapLen:idRes.snapLen,tsoffset:0n,tsresol:1000n,options:this.#readOptions(block)};for(let opt of iData.options)switch(opt.optionType){case 2:iData.name=opt.str;break;case 14:iData.tsoffset=opt.bigint*1000n;break;case 9:{let tsresol=opt.data[0];tsresol&128?iData.tsresol=(1n<<BigInt(tsresol&127))/1000n:iData.tsresol=10n**(BigInt(tsresol)-3n);break}}this.interfaces.push(iData),this.dispatchEvent(new CustomEvent(`interface`,{detail:iData}))}#processSimplePacket(block){let[int]=this.interfaces;if(!int)throw Error(`No interface for simple packet`);let originalPacketLength=block.data.u32(),len=Math.min(originalPacketLength,int.snapLen),pkt={interfaceId:0,originalPacketLength,data:block.data.bytes(len),options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processNameResolution(block){let res={records:[],options:[]};for(;;){let{recordType,recordValueLength}=block.data.struct(nameResolutionFormat);if(recordType===0)break;let val=block.data.bytes(recordValueLength);switch(block.data.skip(pad4(recordValueLength)),recordType){case 1:if(recordValueLength<6)throw Error(`Invalid nrb_record_ipv4 record`);res.records.push({name:`nrb_record_ipv4`,ipv4:decode(val.subarray(0,4)),entries:TD.decode(val.subarray(4)).split(`\0`).slice(0,-1)});break;case 2:if(recordValueLength<18)throw Error(`Invalid nrb_record_ipv6 record`);res.records.push({name:`nrb_record_ipv6`,ipv6:decode(val.subarray(0,16)),entries:TD.decode(val.subarray(16)).split(`\0`).slice(0,-1)});break;case 3:if(recordValueLength<8)throw Error(`Invalid nrb_record_eui48 record`);res.records.push({name:`nrb_record_eui48`,eui48:colons(val.subarray(0,6)),entries:TD.decode(val.subarray(6)).split(`\0`).slice(0,-1)});break;case 4:if(recordValueLength<10)throw Error(`Invalid nrb_record_eui64 record`);res.records.push({name:`nrb_record_eui64`,eui64:colons(val.subarray(0,8)),entries:TD.decode(val.subarray(8)).split(`\0`).slice(0,-1)});break}}res.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`names`,{detail:res}))}#processInterfaceStatistics(block){let{interfaceId,timestampHigh,timestampLow}=block.data.struct(interfaceStatisticsFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface id: ${interfaceId}`);let stats={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),options:this.#readOptions(block,interfaceId)};this.dispatchEvent(new CustomEvent(`stats`,{detail:stats}))}#processEnhancedPacket(block){let{interfaceId,timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=block.data.struct(enhancedPacketBlockFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface ID: ${interfaceId} >= ${this.interfaces.length}`);let data=block.data.bytes(capturedPacketLength),pkt={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),originalPacketLength,data,options:[]};block.data.skip(pad4(capturedPacketLength)),pkt.options=this.#readOptions(block,interfaceId);for(let o of pkt.options)if(o.optionType===2){assert(o.data);let flags=this.#dvr(o.data).u32();pkt.flags={direction:EPB_FLAGS_DIRECTIONS[flags&3],reception:EPB_FLAGS_RECEPTION[flags>>2&7],FCSlen:flags>>5&15,noChecksum:!!(flags>>9&1),checksumValid:!!(flags>>10&1),TCPsegmentationOffload:!!(flags>>11&1),linkLayerErrors:[]},flags>>=16;for(let i=0;i<8;i++)flags&1&&pkt.flags.linkLayerErrors?.push(EPB_FLAGS_LINK_LAYER_ERRORS[i]),flags>>=1}this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processDecryptionSecrets(block){let{secretsType,secretsLength}=block.data.struct(decryptionSecretsFormat),detail={secretsType,data:block.data.bytes(secretsLength),options:[]};block.data.skip(pad4(secretsLength)),detail.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`secrets`,{detail}))}#processCustom(block){let detail={pen:block.data.u32(),data:block.data.unused(),copy:block.blockType===CUSTOM_COPY};this.dispatchEvent(new CustomEvent(`custom`,{detail}))}};export{PCAPNGParser,PCAPNGParser as default,RecoverableError};
|
package/lib/options.d.mts
CHANGED
package/lib/options.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const SECTION_HEADER=168627466,
|
|
1
|
+
const SECTION_HEADER=168627466,CUSTOM_COPY=2989,CUSTOM_NOCOPY=1073744813,OPTION_NAMES=new Map([[SECTION_HEADER,new Map([[1,[`opt_comment`,`string`]],[2,[`shb_hardware`,`string`]],[3,[`shb_os`,`string`]],[4,[`shb_userappl`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[1,new Map([[1,[`opt_comment`,`string`]],[2,[`if_name`,`string`]],[3,[`if_description`,`string`]],[4,[`if_IPv4addr`,`ipv4mask`]],[5,[`if_IPv6addr`,`ipv6prefix`]],[6,[`if_MACaddr`,`eui`]],[7,[`if_EUIaddr`,`eui`]],[8,[`if_speed`]],[9,[`if_tsresol`]],[10,[`if_tzone`]],[11,[`if_filter`]],[12,[`if_os`,`string`]],[13,[`if_fcslen`]],[14,[`if_tsoffset`,`u64`]],[15,[`if_hardware`,`string`]],[16,[`if_txspeed`,`u64`]],[17,[`if_rxspeed`,`u64`]],[18,[`if_iana_tzname`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[4,new Map([[1,[`opt_comment`,`string`]],[2,[`ns_dnsname`,`string`]],[3,[`ns_dnsIP4addr`,`ipv4`]],[4,[`ns_dnsIP6addr`,`ipv6`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[5,new Map([[1,[`opt_comment`,`string`]],[2,[`isb_starttime`,`timestamp`]],[3,[`isb_endtime`,`timestamp`]],[4,[`isb_ifrecv`,`u64`]],[5,[`isb_ifdrop`,`u64`]],[6,[`isb_filteraccept`,`u64`]],[7,[`isb_osdrop`,`u64`]],[8,[`isb_usrdeliv`,`u64`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[6,new Map([[1,[`opt_comment`,`string`]],[2,[`epb_flags`]],[3,[`epb_hash`]],[4,[`epb_dropcount`,`u64`]],[5,[`epb_packetid`,`u64`]],[6,[`epb_queue`,`u32`]],[7,[`epb_verdict`]],[8,[`epb_processid_threadid`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[10,new Map([[1,[`opt_comment`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])]]);export{CUSTOM_COPY,CUSTOM_NOCOPY,OPTION_NAMES,SECTION_HEADER};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cto.af/pcap-ng-parser",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Node.js Module for parsing .pcapng files and tcpdump output.",
|
|
5
5
|
"main": "./lib/index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
],
|
|
36
36
|
"license": "BSD-3",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@cto.af/utils": "1.
|
|
38
|
+
"@cto.af/utils": "1.4.2",
|
|
39
39
|
"@leichtgewicht/ip-codec": "2.0.5",
|
|
40
|
-
"dataview-stream": "^2.0.
|
|
40
|
+
"dataview-stream": "^2.0.3"
|
|
41
41
|
},
|
|
42
|
-
"packageManager": "pnpm@10.
|
|
42
|
+
"packageManager": "pnpm@10.31.0",
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=20"
|
|
45
45
|
}
|