@netkitty/codec 1.2.1 → 2.0.0

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.
Files changed (176) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/README.md +58 -10
  3. package/README.zh-CN.md +52 -9
  4. package/dist/Codec.d.ts +3 -2
  5. package/dist/Codec.js +106 -10
  6. package/dist/abstracts/BaseHeader.d.ts +28 -2
  7. package/dist/abstracts/BaseHeader.js +132 -43
  8. package/dist/errors/CodecSchemaValidateError.d.ts +34 -1
  9. package/dist/errors/CodecSchemaValidateError.js +26 -3
  10. package/dist/headers/ADSAMS.js +2 -0
  11. package/dist/headers/AH.js +1 -0
  12. package/dist/headers/AMQP.js +2 -1
  13. package/dist/headers/ARP.js +16 -10
  14. package/dist/headers/BACnetIP.js +1 -0
  15. package/dist/headers/BGP.js +1 -0
  16. package/dist/headers/Babel.js +2 -1
  17. package/dist/headers/BitTorrent.d.ts +2 -0
  18. package/dist/headers/BitTorrent.js +4 -2
  19. package/dist/headers/C37118.js +1 -0
  20. package/dist/headers/CAPWAP.js +1 -0
  21. package/dist/headers/CDP.js +1 -1
  22. package/dist/headers/CMS.js +2 -2
  23. package/dist/headers/COTP.d.ts +2 -0
  24. package/dist/headers/COTP.js +6 -1
  25. package/dist/headers/CassandraCQL.js +1 -0
  26. package/dist/headers/Codesys.js +1 -0
  27. package/dist/headers/Collectd.js +1 -1
  28. package/dist/headers/DCCP.js +1 -0
  29. package/dist/headers/DHCPFailover.js +1 -0
  30. package/dist/headers/DNS.js +3 -3
  31. package/dist/headers/DTLS.js +1 -0
  32. package/dist/headers/Diameter.js +3 -2
  33. package/dist/headers/EAPOL.js +1 -1
  34. package/dist/headers/EIGRP.js +1 -1
  35. package/dist/headers/ENIP.js +1 -0
  36. package/dist/headers/Elasticsearch.js +1 -0
  37. package/dist/headers/EtherCAT.js +1 -0
  38. package/dist/headers/EthernetII.js +6 -0
  39. package/dist/headers/FTP.js +6 -6
  40. package/dist/headers/Finger.js +3 -3
  41. package/dist/headers/FoundationFieldbusHSE.js +1 -0
  42. package/dist/headers/GELF.js +7 -1
  43. package/dist/headers/GENEVE.js +1 -1
  44. package/dist/headers/GLBP.js +1 -1
  45. package/dist/headers/GTPU.js +1 -0
  46. package/dist/headers/GTPv2C.js +1 -0
  47. package/dist/headers/GitProtocol.js +2 -2
  48. package/dist/headers/Goose.js +20 -8
  49. package/dist/headers/Gopher.js +6 -6
  50. package/dist/headers/HARTIP.js +1 -0
  51. package/dist/headers/HSR.js +2 -0
  52. package/dist/headers/HTTP.js +6 -6
  53. package/dist/headers/HTTP2.js +1 -0
  54. package/dist/headers/ICMP.js +1 -0
  55. package/dist/headers/ICMPv6.js +1 -0
  56. package/dist/headers/IEC104_I_Frame.js +26 -3
  57. package/dist/headers/IEC104_S_Frame.js +7 -2
  58. package/dist/headers/IEC104_U_Frame.js +14 -3
  59. package/dist/headers/IEC61850SampledValues.js +2 -1
  60. package/dist/headers/IGMP.js +1 -0
  61. package/dist/headers/IMAP.js +5 -5
  62. package/dist/headers/IPFIX.js +2 -1
  63. package/dist/headers/IPv4.js +18 -1
  64. package/dist/headers/IPv6.js +1 -0
  65. package/dist/headers/IPv6HopByHopOptions.js +12 -11
  66. package/dist/headers/IRC.js +4 -4
  67. package/dist/headers/ISAKMP.js +2 -1
  68. package/dist/headers/ISCSI.js +2 -0
  69. package/dist/headers/ISIS.js +2 -1
  70. package/dist/headers/ISOSession.d.ts +2 -0
  71. package/dist/headers/ISOSession.js +14 -10
  72. package/dist/headers/Ident.js +8 -8
  73. package/dist/headers/KNXnetIP.js +1 -0
  74. package/dist/headers/Kafka.js +1 -0
  75. package/dist/headers/Kerberos.js +1 -0
  76. package/dist/headers/L2TP.js +2 -2
  77. package/dist/headers/LACP.js +1 -1
  78. package/dist/headers/LDAP.js +2 -3
  79. package/dist/headers/LDP.js +1 -0
  80. package/dist/headers/LLC.d.ts +2 -0
  81. package/dist/headers/LLC.js +2 -0
  82. package/dist/headers/LPD.js +3 -3
  83. package/dist/headers/MGCP.js +7 -7
  84. package/dist/headers/MMS.d.ts +2 -0
  85. package/dist/headers/MMS.js +10 -6
  86. package/dist/headers/MQTT.js +1 -0
  87. package/dist/headers/MQTTSN.js +1 -0
  88. package/dist/headers/MarkerProtocol.js +1 -1
  89. package/dist/headers/Megaco.js +3 -3
  90. package/dist/headers/Memcached.js +3 -2
  91. package/dist/headers/ModbusTCP.js +1 -0
  92. package/dist/headers/ModbusUDP.js +1 -0
  93. package/dist/headers/MongoDB.js +1 -0
  94. package/dist/headers/MySQL.js +1 -0
  95. package/dist/headers/NATS.js +2 -2
  96. package/dist/headers/NBDS.js +1 -0
  97. package/dist/headers/NBSS.js +1 -0
  98. package/dist/headers/NFS.js +1 -0
  99. package/dist/headers/NHRP.js +1 -0
  100. package/dist/headers/NNTP.js +6 -6
  101. package/dist/headers/NetFlowV5.js +1 -0
  102. package/dist/headers/OLSR.js +2 -1
  103. package/dist/headers/OPCUA.js +1 -0
  104. package/dist/headers/OpenFlow.js +1 -0
  105. package/dist/headers/OpenVPN.js +1 -0
  106. package/dist/headers/PCWorx.js +1 -0
  107. package/dist/headers/POP3.js +5 -5
  108. package/dist/headers/PPPoEDiscovery.js +2 -1
  109. package/dist/headers/PPPoESession.js +1 -0
  110. package/dist/headers/PPTP.js +1 -0
  111. package/dist/headers/PTP.js +1 -0
  112. package/dist/headers/PostgreSQL.js +2 -0
  113. package/dist/headers/QUIC.js +2 -0
  114. package/dist/headers/RADIUS.js +1 -0
  115. package/dist/headers/RDP.d.ts +2 -0
  116. package/dist/headers/RDP.js +4 -1
  117. package/dist/headers/RFB.js +4 -4
  118. package/dist/headers/RGoose.js +6 -1
  119. package/dist/headers/RSVP.js +3 -2
  120. package/dist/headers/RTCP.js +1 -0
  121. package/dist/headers/RTP.js +2 -1
  122. package/dist/headers/RTPS.d.ts +2 -0
  123. package/dist/headers/RTPS.js +4 -1
  124. package/dist/headers/RTSP.js +7 -7
  125. package/dist/headers/RawData.js +1 -1
  126. package/dist/headers/Redis.js +4 -4
  127. package/dist/headers/Rlogin.js +5 -5
  128. package/dist/headers/Rsync.js +2 -2
  129. package/dist/headers/S7comm.d.ts +2 -0
  130. package/dist/headers/S7comm.js +5 -1
  131. package/dist/headers/SCTP.js +2 -2
  132. package/dist/headers/SFlow.js +2 -1
  133. package/dist/headers/SIP.js +6 -6
  134. package/dist/headers/SLMP.js +1 -0
  135. package/dist/headers/SLP.js +2 -0
  136. package/dist/headers/SLPv1.js +1 -0
  137. package/dist/headers/SMB1.js +1 -0
  138. package/dist/headers/SMB2.js +1 -0
  139. package/dist/headers/SMPP.js +1 -0
  140. package/dist/headers/SMTP.js +6 -6
  141. package/dist/headers/SNAP.js +18 -5
  142. package/dist/headers/SNMP.js +4 -4
  143. package/dist/headers/SOCKS4.js +11 -5
  144. package/dist/headers/SSDP.js +7 -7
  145. package/dist/headers/SSH.js +5 -4
  146. package/dist/headers/STOMP.js +5 -5
  147. package/dist/headers/STUN.js +2 -0
  148. package/dist/headers/Skinny.js +1 -0
  149. package/dist/headers/StatsD.js +4 -4
  150. package/dist/headers/SunRPC.js +1 -0
  151. package/dist/headers/Syslog.js +2 -2
  152. package/dist/headers/TCP.js +18 -6
  153. package/dist/headers/TLS_Alert.js +23 -11
  154. package/dist/headers/TLS_ApplicationData.js +9 -4
  155. package/dist/headers/TLS_ChangeCipherSpec.js +10 -5
  156. package/dist/headers/TLS_Handshake.js +41 -26
  157. package/dist/headers/TLS_Heartbeat.js +10 -1
  158. package/dist/headers/TPKT.js +3 -1
  159. package/dist/headers/TacacsPlus.js +1 -0
  160. package/dist/headers/Telnet.js +2 -1
  161. package/dist/headers/Teredo.js +2 -2
  162. package/dist/headers/UDP.js +8 -0
  163. package/dist/headers/VLAN_802dot1Q.js +7 -0
  164. package/dist/headers/WCCP.js +1 -0
  165. package/dist/headers/WHOIS.js +3 -3
  166. package/dist/headers/WSDiscovery.js +2 -2
  167. package/dist/headers/XMPP.js +3 -3
  168. package/dist/headers/Zabbix.js +1 -0
  169. package/dist/index.d.ts +3 -3
  170. package/dist/lib/SchemaValidator.d.ts +31 -0
  171. package/dist/lib/SchemaValidator.js +137 -0
  172. package/dist/schema/ProtocolFieldJSONSchema.d.ts +4 -1
  173. package/dist/schema/ProtocolJSONSchema.d.ts +1 -1
  174. package/dist/types/CodecErrorInfo.d.ts +11 -0
  175. package/dist/types/LayerGraph.d.ts +14 -1
  176. package/package.json +2 -2
@@ -153,6 +153,7 @@ class TCP extends BaseHeader_1.BaseHeader {
153
153
  },
154
154
  hdrLen: {
155
155
  type: 'integer',
156
+ autoCalculated: true,
156
157
  label: 'Header Length',
157
158
  minimum: 0,
158
159
  maximum: 60,
@@ -333,6 +334,7 @@ class TCP extends BaseHeader_1.BaseHeader {
333
334
  },
334
335
  checksum: {
335
336
  type: 'integer',
337
+ autoCalculated: true,
336
338
  label: 'Checksum',
337
339
  minimum: 0,
338
340
  maximum: 65535,
@@ -388,6 +390,7 @@ class TCP extends BaseHeader_1.BaseHeader {
388
390
  },
389
391
  options: {
390
392
  type: 'array',
393
+ byteAlignment: 4,
391
394
  label: 'Options',
392
395
  items: {
393
396
  anyOf: [
@@ -790,7 +793,7 @@ class TCP extends BaseHeader_1.BaseHeader {
790
793
  const mssBuffer = Buffer.concat([
791
794
  (0, NumberToBuffer_1.UInt8ToBuffer)(2),
792
795
  (0, NumberToBuffer_1.UInt8ToBuffer)(4),
793
- (0, NumberToBuffer_1.UInt16ToBuffer)(optionMSS.mss)
796
+ (0, NumberToBuffer_1.UInt16ToBuffer)(optionMSS.mss ?? 0)
794
797
  ]);
795
798
  this.writeBytes(optionOffset, mssBuffer);
796
799
  optionOffset += mssBuffer.length;
@@ -801,7 +804,7 @@ class TCP extends BaseHeader_1.BaseHeader {
801
804
  const wsBuffer = Buffer.concat([
802
805
  (0, NumberToBuffer_1.UInt8ToBuffer)(3),
803
806
  (0, NumberToBuffer_1.UInt8ToBuffer)(3),
804
- (0, NumberToBuffer_1.UInt8ToBuffer)(optionWS.shift)
807
+ (0, NumberToBuffer_1.UInt8ToBuffer)(optionWS.shift ?? 0)
805
808
  ]);
806
809
  this.writeBytes(optionOffset, wsBuffer);
807
810
  optionOffset += wsBuffer.length;
@@ -822,7 +825,8 @@ class TCP extends BaseHeader_1.BaseHeader {
822
825
  const kindBuffer = (0, NumberToBuffer_1.UInt8ToBuffer)(5);
823
826
  const length = 2;
824
827
  let blocksBuffer = Buffer.from([]);
825
- optionSack.blocks.forEach((block) => {
828
+ const sackBlocks = optionSack.blocks ?? [];
829
+ sackBlocks.forEach((block) => {
826
830
  blocksBuffer = Buffer.concat([blocksBuffer, (0, NumberToBuffer_1.UInt64ToBuffer)((0, HexToNumber_1.HexToUInt64)(block))]);
827
831
  });
828
832
  const sackBuffer = Buffer.concat([kindBuffer, (0, NumberToBuffer_1.UInt8ToBuffer)(length + blocksBuffer.length), blocksBuffer]);
@@ -860,7 +864,7 @@ class TCP extends BaseHeader_1.BaseHeader {
860
864
  case TCPOption.TCP_Authentication_Option: {
861
865
  const optionTcpAo = optionItem;
862
866
  const kindBuffer = (0, NumberToBuffer_1.UInt8ToBuffer)(29);
863
- const dataBuffer = Buffer.from(optionTcpAo.data, 'hex');
867
+ const dataBuffer = Buffer.from(optionTcpAo.data ?? '', 'hex');
864
868
  const lengthBuffer = (0, NumberToBuffer_1.UInt8ToBuffer)(dataBuffer.length + 2);
865
869
  const tcpAoBuffer = Buffer.concat([kindBuffer, lengthBuffer, dataBuffer]);
866
870
  this.writeBytes(optionOffset, tcpAoBuffer);
@@ -869,8 +873,16 @@ class TCP extends BaseHeader_1.BaseHeader {
869
873
  }
870
874
  default: {
871
875
  const defaultOptionItem = optionItem;
872
- const kind = defaultOptionItem.kind;
873
- const hexBuffer = Buffer.from(defaultOptionItem.data.toString(), 'hex');
876
+ const kind = defaultOptionItem.kind ?? 0;
877
+ //Kinds 0 (EOL) and 1 (NOP) are single-octet options with no length/data
878
+ //(RFC 9293 §3.1), so a generic {kind: 0|1} emits just the kind octet.
879
+ if (kind === 0 || kind === 1) {
880
+ this.writeBytes(optionOffset, (0, NumberToBuffer_1.UInt8ToBuffer)(kind));
881
+ optionOffset += 1;
882
+ return;
883
+ }
884
+ //Data is optional: a generic option without data is the bare kind+length pair.
885
+ const hexBuffer = Buffer.from(defaultOptionItem.data === undefined || defaultOptionItem.data === null ? '' : defaultOptionItem.data.toString(), 'hex');
874
886
  const length = hexBuffer.length + 2;
875
887
  const defaultOptionBuffer = Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(kind), (0, NumberToBuffer_1.UInt8ToBuffer)(length), hexBuffer]);
876
888
  this.writeBytes(optionOffset, defaultOptionBuffer);
@@ -77,13 +77,15 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
77
77
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
78
78
  },
79
79
  encode: () => {
80
- const contentTyppe = this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found'));
80
+ const contentTyppe = this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
81
81
  this.writeBytes(0, (0, NumberToBuffer_1.UInt8ToBuffer)(contentTyppe));
82
82
  }
83
83
  },
84
84
  version: {
85
85
  type: 'string',
86
86
  label: 'Legacy Version',
87
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
88
+ enum: [...Object.values(TLSver), '0'],
87
89
  minimum: 0,
88
90
  maximum: 65535,
89
91
  decode: () => {
@@ -121,7 +123,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
121
123
  }
122
124
  },
123
125
  encode: () => {
124
- const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
126
+ const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
125
127
  switch (version) {
126
128
  case TLSver.SSL_3_0:
127
129
  {
@@ -154,6 +156,9 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
154
156
  }
155
157
  break;
156
158
  default: {
159
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
160
+ if (`${version}` !== '0')
161
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
157
162
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
158
163
  }
159
164
  }
@@ -161,14 +166,14 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
161
166
  },
162
167
  length: {
163
168
  type: 'integer',
164
- label: 'Legacy Version',
169
+ label: 'Length',
165
170
  minimum: 0,
166
- maximum: 255,
171
+ maximum: 65535,
167
172
  decode: () => {
168
173
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
169
174
  },
170
175
  encode: () => {
171
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
176
+ const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
172
177
  this.writeBytes(3, length);
173
178
  }
174
179
  },
@@ -179,7 +184,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
179
184
  maximum: 255,
180
185
  decode: () => {
181
186
  this.instance.levelType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(5, 1)));
182
- const type = this.instance.levelType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found'));
187
+ const type = this.instance.levelType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
183
188
  switch (type) {
184
189
  case 1:
185
190
  {
@@ -197,7 +202,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
197
202
  }
198
203
  },
199
204
  encode: () => {
200
- const type = this.instance.levelType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
205
+ const type = this.instance.levelType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
201
206
  switch (type) {
202
207
  case 'Warning':
203
208
  {
@@ -212,8 +217,11 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
212
217
  }
213
218
  break;
214
219
  default: {
220
+ //Not a known name: a numeric level (what decode stores for an unnamed one).
215
221
  const type1 = parseInt(type, 10);
216
- this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(type1));
222
+ if (!/^\d+$/.test(`${type}`) || type1 > 255)
223
+ this.recordError(this.instance.levelType.getPath(), `Unknown alert level '${type}', encoded as 0`, 'error');
224
+ this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(/^\d+$/.test(`${type}`) && type1 <= 255 ? type1 : 0));
217
225
  }
218
226
  }
219
227
  }
@@ -225,7 +233,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
225
233
  maximum: 255,
226
234
  decode: () => {
227
235
  this.instance.descriptionType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(6, 1)));
228
- const type = this.instance.descriptionType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found'));
236
+ const type = this.instance.descriptionType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
229
237
  switch (type) {
230
238
  case 0:
231
239
  {
@@ -403,7 +411,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
403
411
  }
404
412
  },
405
413
  encode: () => {
406
- const type = this.instance.descriptionType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
414
+ const type = this.instance.descriptionType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
407
415
  switch (type) {
408
416
  case Description_type.code_0:
409
417
  {
@@ -610,8 +618,12 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
610
618
  }
611
619
  break;
612
620
  default: {
621
+ //Not a known name: a numeric description (what decode stores for an unnamed one).
613
622
  const type1 = parseInt(type, 10);
614
- const type2 = (0, NumberToBuffer_1.UInt8ToBuffer)(type1);
623
+ const known = /^\d+$/.test(`${type}`) && type1 <= 255;
624
+ if (!known)
625
+ this.recordError(this.instance.descriptionType.getPath(), `Unknown alert description '${type}', encoded as 0`, 'error');
626
+ const type2 = (0, NumberToBuffer_1.UInt8ToBuffer)(known ? type1 : 0);
615
627
  this.writeBytes(6, type2);
616
628
  }
617
629
  }
@@ -42,13 +42,15 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
42
42
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
43
43
  },
44
44
  encode: () => {
45
- const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
45
+ const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
46
46
  this.writeBytes(0, contentType);
47
47
  }
48
48
  },
49
49
  version: {
50
50
  type: 'string',
51
51
  label: 'Legacy Version',
52
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
53
+ enum: [...Object.values(TLSver), '0'],
52
54
  minimum: 0,
53
55
  maximum: 65535,
54
56
  decode: () => {
@@ -85,7 +87,7 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
85
87
  }
86
88
  },
87
89
  encode: () => {
88
- const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
90
+ const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
89
91
  switch (version) {
90
92
  case TLSver.SSL_3_0:
91
93
  {
@@ -118,6 +120,9 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
118
120
  }
119
121
  break;
120
122
  default: {
123
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
124
+ if (`${version}` !== '0')
125
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
121
126
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
122
127
  }
123
128
  }
@@ -132,7 +137,7 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
132
137
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
133
138
  },
134
139
  encode: () => {
135
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
140
+ const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
136
141
  this.writeBytes(3, length);
137
142
  }
138
143
  },
@@ -145,7 +150,7 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
145
150
  this.instance.appData.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(5, this.instance.length.getValue())));
146
151
  },
147
152
  encode: () => {
148
- const appData = (0, HexToBuffer_1.HexToBuffer)(this.instance.appData.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found')));
153
+ const appData = (0, HexToBuffer_1.HexToBuffer)(this.instance.appData.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found')));
149
154
  this.writeBytes(5, appData);
150
155
  }
151
156
  }
@@ -41,13 +41,15 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
41
41
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
42
42
  },
43
43
  encode: () => {
44
- const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
44
+ const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
45
45
  this.writeBytes(0, contentType);
46
46
  }
47
47
  },
48
48
  version: {
49
49
  type: 'string',
50
50
  label: 'Legacy Version',
51
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
52
+ enum: [...Object.values(TLSver), '0'],
51
53
  minimum: 0,
52
54
  maximum: 65535,
53
55
  decode: () => {
@@ -84,7 +86,7 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
84
86
  }
85
87
  },
86
88
  encode: () => {
87
- const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
89
+ const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
88
90
  switch (version) {
89
91
  case TLSver.SSL_3_0:
90
92
  {
@@ -117,6 +119,9 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
117
119
  }
118
120
  break;
119
121
  default: {
122
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
123
+ if (`${version}` !== '0')
124
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
120
125
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
121
126
  }
122
127
  }
@@ -131,7 +136,7 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
131
136
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
132
137
  },
133
138
  encode: () => {
134
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
139
+ const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
135
140
  this.writeBytes(3, length);
136
141
  }
137
142
  },
@@ -139,11 +144,11 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
139
144
  type: 'string',
140
145
  label: 'Change Cipher Spec Message',
141
146
  decode: () => {
142
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found'));
147
+ const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
143
148
  this.instance.change_cipher_spec_message.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(5, length)));
144
149
  },
145
150
  encode: () => {
146
- const message = (0, HexToBuffer_1.HexToBuffer)(this.instance.change_cipher_spec_message.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found')));
151
+ const message = (0, HexToBuffer_1.HexToBuffer)(this.instance.change_cipher_spec_message.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found')));
147
152
  this.writeBytes(5, message);
148
153
  }
149
154
  }
@@ -44,13 +44,15 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
44
44
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
45
45
  },
46
46
  encode: () => {
47
- const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
47
+ const contentType = (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.contentType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
48
48
  this.writeBytes(0, contentType);
49
49
  }
50
50
  },
51
51
  version: {
52
52
  type: 'string',
53
53
  label: 'Legacy Version',
54
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
55
+ enum: [...Object.values(TLSver), '0'],
54
56
  minimum: 0,
55
57
  maximum: 65535,
56
58
  decode: () => {
@@ -87,7 +89,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
87
89
  }
88
90
  },
89
91
  encode: () => {
90
- const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'));
92
+ const version = this.instance.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
91
93
  switch (version) {
92
94
  case TLSver.SSL_3_0:
93
95
  {
@@ -120,6 +122,9 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
120
122
  }
121
123
  break;
122
124
  default: {
125
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
126
+ if (`${version}` !== '0')
127
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
123
128
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
124
129
  }
125
130
  }
@@ -134,7 +139,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
134
139
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
135
140
  },
136
141
  encode: () => {
137
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
142
+ const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
138
143
  this.writeBytes(3, length);
139
144
  }
140
145
  },
@@ -287,8 +292,12 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
287
292
  }
288
293
  break;
289
294
  default: {
295
+ //Not a known name: a numeric type (what decode stores for an unnamed one).
290
296
  const handshakeType1 = parseInt(handshakeType);
291
- this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(handshakeType1));
297
+ const known = /^\d+$/.test(`${handshakeType}`) && handshakeType1 <= 255;
298
+ if (!known)
299
+ this.recordError(this.instance.handshakeType.getPath(), `Unknown handshake type '${handshakeType}', encoded as 0`, 'error');
300
+ this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(known ? handshakeType1 : 0));
292
301
  }
293
302
  }
294
303
  }
@@ -297,12 +306,12 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
297
306
  type: 'integer',
298
307
  label: 'Length',
299
308
  minimum: 0,
300
- maximum: 1118481,
309
+ maximum: 16777215,
301
310
  decode: () => {
302
311
  this.instance.handshakeLength.setValue((0, BufferToNumber_1.BufferToUInt32)(this.readBytes(6, 3)));
303
312
  },
304
313
  encode: () => {
305
- const length1 = (this.instance.handshakeLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
314
+ const length1 = (this.instance.handshakeLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
306
315
  const length2 = length1.toString(16).padStart(6, '0');
307
316
  const length3 = (0, HexToBuffer_1.HexToBuffer)(length2);
308
317
  this.writeBytes(6, length3);
@@ -326,7 +335,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
326
335
  type: 'object',
327
336
  properties: {
328
337
  type: { type: 'string', label: 'Type' },
329
- name: { type: 'string', label: 'Name' },
338
+ name: { type: 'string', label: 'Name', readOnly: true },
330
339
  //Present only for extensions we cannot structure (unknown type or
331
340
  //malformed): the raw extension data, kept as hex so it round-trips.
332
341
  data: { type: 'string', contentEncoding: 'hex', label: 'Data' },
@@ -356,7 +365,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
356
365
  raw: { type: 'string', contentEncoding: 'hex', label: 'Unparsed Body' }
357
366
  },
358
367
  decode: () => {
359
- const handshakeLength = (this.instance.handshakeLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not found')));
368
+ const handshakeLength = (this.instance.handshakeLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
360
369
  //handshakeLength describes the whole (possibly reassembled) message, which may
361
370
  //span multiple TLS records. The body carried by THIS record is bounded by the
362
371
  //record-layer length minus the 4-byte handshake header (type + 3-byte length);
@@ -391,7 +400,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
391
400
  }
392
401
  const handshakeType = this.instance.handshakeType.getValue();
393
402
  const parts = [];
394
- parts.push(this.tlsVersionStringToBuffer(md.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not found'))));
403
+ parts.push(this.tlsVersionStringToBuffer(md.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
395
404
  parts.push((0, HexToBuffer_1.HexToBuffer)(md.random.getValue('')));
396
405
  const sessionId = (0, HexToBuffer_1.HexToBuffer)(md.sessionId.getValue(''));
397
406
  parts.push((0, NumberToBuffer_1.UInt8ToBuffer)(sessionId.length), sessionId);
@@ -407,7 +416,12 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
407
416
  }
408
417
  if (!md.extensions.isUndefined()) {
409
418
  const extensionList = md.extensions.getValue([]);
410
- const extensions = Buffer.concat(extensionList.map((extension) => {
419
+ const extensions = Buffer.concat(extensionList.map((extension, index) => {
420
+ //An extension without its 2-byte type cannot be framed: skip it, reported.
421
+ if (typeof extension.type !== 'string' || !extension.type) {
422
+ this.recordError(`${md.extensions.getPath()}[${index}].type`, 'Not Found, extension ignored');
423
+ return Buffer.alloc(0);
424
+ }
411
425
  //Structurally decoded extensions carry their specific fields (no `data`);
412
426
  //rebuild those from the fields. Hex-preserved extensions carry `data`.
413
427
  const data = extension.data !== undefined ? (0, HexToBuffer_1.HexToBuffer)(extension.data) : this.encodeExtensionData(extension, handshakeType);
@@ -791,9 +805,9 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
791
805
  encodeExtensionData(extension, handshakeType) {
792
806
  switch (extension.type.toLowerCase()) {
793
807
  case '0000': { //server_name
794
- const entries = Buffer.concat(extension.serverNames.map((serverName) => {
795
- const host = Buffer.from(serverName.hostName, 'ascii');
796
- const nameType = serverName.nameType === 'host_name' ? 0 : parseInt(serverName.nameType);
808
+ const entries = Buffer.concat((extension.serverNames ?? []).map((serverName) => {
809
+ const host = Buffer.from(serverName.hostName ?? '', 'ascii');
810
+ const nameType = serverName.nameType === undefined || serverName.nameType === 'host_name' ? 0 : parseInt(serverName.nameType) || 0;
797
811
  return Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(nameType), (0, NumberToBuffer_1.UInt16ToBuffer)(host.length), host]);
798
812
  }));
799
813
  if (entries.length === 0)
@@ -801,38 +815,39 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
801
815
  return Buffer.concat([(0, NumberToBuffer_1.UInt16ToBuffer)(entries.length), entries]);
802
816
  }
803
817
  case '000a':
804
- return this.encodeCodeList16(extension.groups);
818
+ return this.encodeCodeList16(extension.groups ?? []);
805
819
  case '000d':
806
- return this.encodeCodeList16(extension.signatureAlgorithms);
820
+ return this.encodeCodeList16(extension.signatureAlgorithms ?? []);
807
821
  case '000b': {
808
- const list = Buffer.concat(extension.ecPointFormats.map((format) => (0, HexToBuffer_1.HexToBuffer)(format)));
822
+ const list = Buffer.concat((extension.ecPointFormats ?? []).map((format) => (0, HexToBuffer_1.HexToBuffer)(format)));
809
823
  return Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(list.length), list]);
810
824
  }
811
825
  case '002d': {
812
- const list = Buffer.concat(extension.modes.map((mode) => (0, HexToBuffer_1.HexToBuffer)(mode)));
826
+ const list = Buffer.concat((extension.modes ?? []).map((mode) => (0, HexToBuffer_1.HexToBuffer)(mode)));
813
827
  return Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(list.length), list]);
814
828
  }
815
829
  case '0010': {
816
- const entries = Buffer.concat(extension.protocols.map((protocol) => {
817
- const bytes = Buffer.from(protocol, 'ascii');
830
+ const entries = Buffer.concat((extension.protocols ?? []).map((protocol) => {
831
+ const bytes = Buffer.from(protocol ?? '', 'ascii');
818
832
  return Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(bytes.length), bytes]);
819
833
  }));
820
834
  return Buffer.concat([(0, NumberToBuffer_1.UInt16ToBuffer)(entries.length), entries]);
821
835
  }
822
836
  case '002b': {
823
837
  if (handshakeType === HandshakeType.Handshake_3)
824
- return this.tlsVersionStringToBuffer(extension.selectedVersion);
825
- const list = Buffer.concat(extension.versions.map((version) => this.tlsVersionStringToBuffer(version)));
838
+ return this.tlsVersionStringToBuffer(extension.selectedVersion ?? '0');
839
+ const list = Buffer.concat((extension.versions ?? []).map((version) => this.tlsVersionStringToBuffer(version)));
826
840
  return Buffer.concat([(0, NumberToBuffer_1.UInt8ToBuffer)(list.length), list]);
827
841
  }
828
842
  case '0033': {
829
843
  if (handshakeType === HandshakeType.Handshake_3) {
830
- const key = (0, HexToBuffer_1.HexToBuffer)(extension.keyShare.keyExchange);
831
- return Buffer.concat([(0, HexToBuffer_1.HexToBuffer)(extension.keyShare.group), (0, NumberToBuffer_1.UInt16ToBuffer)(key.length), key]);
844
+ const keyShare = extension.keyShare ?? {};
845
+ const key = (0, HexToBuffer_1.HexToBuffer)(keyShare.keyExchange ?? '');
846
+ return Buffer.concat([(0, HexToBuffer_1.HexToBuffer)(keyShare.group ?? '0000'), (0, NumberToBuffer_1.UInt16ToBuffer)(key.length), key]);
832
847
  }
833
- const entries = Buffer.concat(extension.keyShares.map((keyShare) => {
834
- const key = (0, HexToBuffer_1.HexToBuffer)(keyShare.keyExchange);
835
- return Buffer.concat([(0, HexToBuffer_1.HexToBuffer)(keyShare.group), (0, NumberToBuffer_1.UInt16ToBuffer)(key.length), key]);
848
+ const entries = Buffer.concat((extension.keyShares ?? []).map((keyShare) => {
849
+ const key = (0, HexToBuffer_1.HexToBuffer)(keyShare.keyExchange ?? '');
850
+ return Buffer.concat([(0, HexToBuffer_1.HexToBuffer)(keyShare.group ?? '0000'), (0, NumberToBuffer_1.UInt16ToBuffer)(key.length), key]);
836
851
  }));
837
852
  return Buffer.concat([(0, NumberToBuffer_1.UInt16ToBuffer)(entries.length), entries]);
838
853
  }
@@ -52,6 +52,8 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
52
52
  version: {
53
53
  type: 'string',
54
54
  label: 'Version',
55
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
56
+ enum: [...Object.values(TLSver), '0'],
55
57
  minimum: 0,
56
58
  maximum: 65535,
57
59
  decode: () => {
@@ -116,6 +118,9 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
116
118
  }
117
119
  break;
118
120
  default: {
121
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
122
+ if (`${version}` !== '0')
123
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
119
124
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
120
125
  }
121
126
  }
@@ -171,8 +176,12 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
171
176
  }
172
177
  break;
173
178
  default: {
179
+ //Not a known name: a numeric type (what decode stores for an unnamed one).
174
180
  const heartbeatType1 = parseInt(heartbeatType);
175
- this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(heartbeatType1));
181
+ const known = /^\d+$/.test(`${heartbeatType}`) && heartbeatType1 <= 255;
182
+ if (!known)
183
+ this.recordError(this.instance.heartbeatType.getPath(), `Unknown heartbeat type '${heartbeatType}', encoded as 0`, 'error');
184
+ this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(known ? heartbeatType1 : 0));
176
185
  }
177
186
  }
178
187
  }
@@ -38,10 +38,12 @@ class TPKT extends BaseHeader_1.BaseHeader {
38
38
  type: 'object',
39
39
  summary: 'TPKT v${version} len=${length}',
40
40
  properties: {
41
- version: this.fieldUInt('version', 0, 1, 'Version'),
41
+ //Default 3: the only TPKT version (RFC 1006), and what match() requires.
42
+ version: { ...this.fieldUInt('version', 0, 1, 'Version'), default: 3 },
42
43
  reserved: this.fieldUInt('reserved', 1, 1, 'Reserved'),
43
44
  length: {
44
45
  type: 'integer',
46
+ autoCalculated: true,
45
47
  label: 'Length',
46
48
  minimum: 0,
47
49
  maximum: 65535,
@@ -69,6 +69,7 @@ class TacacsPlus extends BaseHeader_1.BaseHeader {
69
69
  sessionId: this.fieldUInt('sessionId', 4, 4, 'Session Id'),
70
70
  length: {
71
71
  type: 'integer',
72
+ autoCalculated: true,
72
73
  label: 'Length',
73
74
  minimum: 0,
74
75
  maximum: 4294967295,
@@ -137,9 +137,10 @@ class Telnet extends BaseHeader_1.BaseHeader {
137
137
  //Display-only metadata parsed from the leading IAC command run on decode (no encode —
138
138
  //populated by the `message` field below, never read back). isNegotiation flags a payload
139
139
  //that opens with an IAC byte; commands lists the leading command sequences for the UI.
140
- isNegotiation: { type: 'boolean', label: 'Is Negotiation', default: false },
140
+ isNegotiation: { type: 'boolean', label: 'Is Negotiation', readOnly: true, default: false },
141
141
  commands: {
142
142
  type: 'array',
143
+ readOnly: true,
143
144
  label: 'IAC Commands',
144
145
  items: {
145
146
  type: 'object',
@@ -106,8 +106,8 @@ class Teredo extends BaseHeader_1.BaseHeader {
106
106
  type: 'object',
107
107
  label: 'Authentication Indication',
108
108
  properties: {
109
- clientIdLength: { type: 'integer', label: 'Client ID Length', minimum: 0, maximum: 255 },
110
- authDataLength: { type: 'integer', label: 'Authentication Value Length', minimum: 0, maximum: 255 },
109
+ clientIdLength: { type: 'integer', label: 'Client ID Length', readOnly: true, minimum: 0, maximum: 255 },
110
+ authDataLength: { type: 'integer', label: 'Authentication Value Length', readOnly: true, minimum: 0, maximum: 255 },
111
111
  clientId: { type: 'string', label: 'Client Identifier', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
112
112
  authData: { type: 'string', label: 'Authentication Value', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
113
113
  nonce: { type: 'string', label: 'Nonce', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
@@ -25,6 +25,12 @@ class UDP extends BaseHeader_1.BaseHeader {
25
25
  */
26
26
  calculateUDPChecksum(udpHeaderBuffer) {
27
27
  const udpHeaderLength = udpHeaderBuffer.length;
28
+ //Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack, so record
29
+ //an error and leave the checksum 0 instead of dereferencing a missing previous layer.
30
+ if (!this.prevCodecModule) {
31
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute UDP checksum: no IP layer beneath UDP');
32
+ return 0;
33
+ }
28
34
  const ipVersion = this.prevCodecModule.instance.version.getValue();
29
35
  let pseudoHeaderBuffer = Buffer.from([]);
30
36
  const sourceIp = this.prevCodecModule.instance.sip.getValue();
@@ -90,6 +96,7 @@ class UDP extends BaseHeader_1.BaseHeader {
90
96
  dstport: this.fieldUInt('dstport', 2, 2, 'Destination Port'),
91
97
  length: {
92
98
  type: 'integer',
99
+ autoCalculated: true,
93
100
  label: 'Length',
94
101
  minimum: 0,
95
102
  maximum: 65535,
@@ -122,6 +129,7 @@ class UDP extends BaseHeader_1.BaseHeader {
122
129
  },
123
130
  checksum: {
124
131
  type: 'integer',
132
+ autoCalculated: true,
125
133
  label: 'Checksum',
126
134
  minimum: 0,
127
135
  maximum: 65535,
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VLAN_802dot1Q = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
+ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
5
6
  const NumberToHex_1 = require("../helper/NumberToHex");
6
7
  const BufferToHex_1 = require("../helper/BufferToHex");
7
8
  /**
@@ -68,10 +69,13 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
68
69
  minLength: 4,
69
70
  maxLength: 4,
70
71
  label: 'EtherType',
72
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
71
73
  decode: () => {
72
74
  this.instance.etherType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2)));
73
75
  },
74
76
  encode: () => {
77
+ if (this.instance.etherType.isUndefined())
78
+ return this.encodeOmittedEtherType(this.instance.etherType, 2);
75
79
  const etherType = this.instance.etherType.getValue((0, NumberToHex_1.UInt16ToHex)(0x0000), (nodePath) => this.recordError(nodePath, 'Not Found'));
76
80
  const typeBuffer = Buffer.from(etherType, 'hex');
77
81
  if (typeBuffer.length < 2)
@@ -90,6 +94,9 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
90
94
  match() {
91
95
  if (!this.prevCodecModule)
92
96
  return false;
97
+ //Fewer bytes than the 4-octet tag cannot be reproduced by these fields: leave them to raw.
98
+ if (this.packet.length - this.startPos < 4)
99
+ return false;
93
100
  return this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x8100);
94
101
  }
95
102
  }