Skip to content

Commit

Permalink
Handle non-DIMSE network activity
Browse files Browse the repository at this point in the history
  • Loading branch information
PantelisGeorgiadis committed Oct 26, 2024
1 parent a98654b commit 26d257b
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 40 deletions.
9 changes: 9 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ts-node": {
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
"target": "es2021"
}
}
}
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dcmjs-dimse",
"version": "0.1.29",
"version": "0.1.30",
"description": "DICOM DIMSE implementation for Node.js using dcmjs",
"main": "build/dcmjs-dimse.min.js",
"module": "build/dcmjs-dimse.min.js",
Expand Down Expand Up @@ -51,12 +51,12 @@
},
"devDependencies": {
"@types/async-eventemitter": "^0.2.4",
"@types/node": "^22.7.5",
"@types/node": "^22.8.1",
"c8": "^9.1.0",
"chai": "^4.3.10",
"docdash": "^2.0.2",
"eslint": "^8.57.0",
"jsdoc": "^4.0.3",
"jsdoc": "^4.0.4",
"mocha": "^10.7.3",
"mock-fs": "^5.4.0",
"nodemon": "^3.1.7",
Expand Down
18 changes: 18 additions & 0 deletions src/Constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
//#region RawPduType
/**
* Raw PDU types.
* @constant {Object}
*/
const RawPduType = {
AAbort: 0x07,
AAssociateAC: 0x02,
AAssociateRJ: 0x03,
AAssociateRQ: 0x01,
AReleaseRP: 0x06,
AReleaseRQ: 0x05,
PDataTF: 0x04,
};
Object.freeze(RawPduType);
//#endregion

//#region CommandFieldType
/**
* Command field types.
Expand Down Expand Up @@ -350,6 +367,7 @@ module.exports = {
DefaultImplementation,
PresentationContextResult,
Priority,
RawPduType,
RejectReason,
RejectResult,
RejectSource,
Expand Down
43 changes: 34 additions & 9 deletions src/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ const {
Pdv,
RawPdu,
} = require('./Pdu');
const { CommandFieldType, Status, TranscodableTransferSyntaxes } = require('./Constants');
const {
CommandFieldType,
RawPduType,
Status,
TranscodableTransferSyntaxes,
} = require('./Constants');
const {
CCancelRequest,
CEchoRequest,
Expand Down Expand Up @@ -406,7 +411,7 @@ class Network extends AsyncEventEmitter {
raw.readPdu();

switch (raw.getType()) {
case 0x01: {
case RawPduType.AAssociateRQ: {
this.association = new Association();
const pdu = new AAssociateRQ(this.association);
pdu.read(raw);
Expand All @@ -415,15 +420,15 @@ class Network extends AsyncEventEmitter {
this.emit('associationRequested', this.association);
break;
}
case 0x02: {
case RawPduType.AAssociateAC: {
const pdu = new AAssociateAC(this.association);
pdu.read(raw);
this.logId = this.association.getCalledAeTitle();
log.info(`${this.logId} <- Association accept:${EOL}${this.association.toString()}`);
this.emit('associationAccepted', this.association);
break;
}
case 0x03: {
case RawPduType.AAssociateRJ: {
const pdu = new AAssociateRJ();
pdu.read(raw);
log.info(`${this.logId} <- Association reject ${pdu.toString()}`);
Expand All @@ -434,27 +439,27 @@ class Network extends AsyncEventEmitter {
});
break;
}
case 0x04: {
case RawPduType.PDataTF: {
const pdu = new PDataTF();
pdu.read(raw);
this._processPDataTf(pdu);
break;
}
case 0x05: {
case RawPduType.AReleaseRQ: {
const pdu = new AReleaseRQ();
pdu.read(raw);
log.info(`${this.logId} <- Association release request`);
this.emit('associationReleaseRequested');
break;
}
case 0x06: {
case RawPduType.AReleaseRP: {
const pdu = new AReleaseRP();
pdu.read(raw);
log.info(`${this.logId} <- Association release response`);
this.emit('associationReleaseResponse');
break;
}
case 0x07: {
case RawPduType.AAbort: {
const pdu = new AAbort();
pdu.read(raw);
log.info(`${this.logId} <- Association abort ${pdu.toString()}`);
Expand Down Expand Up @@ -704,6 +709,12 @@ class Network extends AsyncEventEmitter {
this.lastPduTime = Date.now();
this._processPdu(data);
});
pduAccumulator.on('error', (err) => {
this._reset();
const error = `${this.logId} -> Connection error: ${err.message}`;
log.error(error);
this.emit('networkError', new Error(error));
});
this.socket.on('data', (data) => {
pduAccumulator.accumulate(data);
this.statistics.addBytesReceived(data.length);
Expand Down Expand Up @@ -816,7 +827,21 @@ class PduAccumulator extends AsyncEventEmitter {
}

const buffer = SmartBuffer.fromBuffer(data, 'ascii');
buffer.readUInt8();
const pduType = buffer.readUInt8();
if (
pduType !== RawPduType.AAssociateRQ &&
pduType !== RawPduType.AAssociateAC &&
pduType !== RawPduType.AAssociateRJ &&
pduType !== RawPduType.PDataTF &&
pduType !== RawPduType.AReleaseRQ &&
pduType !== RawPduType.AReleaseRP &&
pduType !== RawPduType.AAbort
) {
this.emit('error', new Error(`Unknown PDU type: ${pduType}`));

return undefined;
}

buffer.readUInt8();
const pduLength = buffer.readUInt32BE();
let dataNeeded = data.length - 6;
Expand Down
15 changes: 8 additions & 7 deletions src/Pdu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
AbortReason,
AbortSource,
RawPduType,
RejectReason,
RejectResult,
RejectSource,
Expand Down Expand Up @@ -365,7 +366,7 @@ class AAssociateRQ {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x01);
const pdu = new RawPdu(RawPduType.AAssociateRQ);

pdu.writeUInt16('Version', 0x0001);
pdu.writeByteMultiple('Reserved', 0x00, 2);
Expand Down Expand Up @@ -608,7 +609,7 @@ class AAssociateAC {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x02);
const pdu = new RawPdu(RawPduType.AAssociateAC);

pdu.writeUInt16('Version', 0x0001);
pdu.writeByteMultiple('Reserved', 0x00, 2);
Expand Down Expand Up @@ -864,7 +865,7 @@ class AAssociateRJ {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x03);
const pdu = new RawPdu(RawPduType.AAssociateRJ);

pdu.writeByte('Reserved', 0x00);
pdu.writeByte('Result', this.result);
Expand Down Expand Up @@ -1001,7 +1002,7 @@ class AReleaseRQ {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x05);
const pdu = new RawPdu(RawPduType.AReleaseRQ);

pdu.writeUInt32('Reserved', 0x00000000);

Expand Down Expand Up @@ -1041,7 +1042,7 @@ class AReleaseRP {
* @returns {RawPdu} PDU buffer.
*/
write() {
const pdu = new RawPdu(0x06);
const pdu = new RawPdu(RawPduType.AReleaseRP);

pdu.writeUInt32('Reserved', 0x00000000);

Expand Down Expand Up @@ -1104,7 +1105,7 @@ class AAbort {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x07);
const pdu = new RawPdu(RawPduType.AAbort);

pdu.writeByte('Reserved', 0x00);
pdu.writeByte('Reserved', 0x00);
Expand Down Expand Up @@ -1356,7 +1357,7 @@ class PDataTF {
* @returns {RawPdu} PDU.
*/
write() {
const pdu = new RawPdu(0x04);
const pdu = new RawPdu(RawPduType.PDataTF);
this.pdvs.forEach((pdv) => {
pdv.write(pdu);
});
Expand Down
4 changes: 4 additions & 0 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ class Server extends AsyncEventEmitter {
client.on('close', () => {
this.statistics.addFromOtherStatistics(client.getStatistics());
});
client.on('networkError', (err) => {
socket.end();
this.emit('networkError', err);
});
this.clients.push(client);

this.clients = this.clients.filter((item) => item.connected);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
CommandFieldType,
PresentationContextResult,
Priority,
RawPduType,
RejectReason,
RejectResult,
RejectSource,
Expand Down Expand Up @@ -95,6 +96,7 @@ const constants = {
CommandFieldType,
PresentationContextResult,
Priority,
RawPduType,
RejectReason,
RejectResult,
RejectSource,
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = '0.1.29';
module.exports = '0.1.30';
16 changes: 16 additions & 0 deletions test/Network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Client = require('./../src/Client');
const Dataset = require('./../src/Dataset');
const log = require('./../src/log');

const http = require('http');
const selfSigned = require('selfsigned');
const chai = require('chai');
const expect = chai.expect;
Expand Down Expand Up @@ -713,4 +714,19 @@ describe('Network', () => {
});
client.send('127.0.0.1', 2112, 'CALLINGAET', 'CALLEDAET');
});

it('should reject non-DIMSE communication (HTTP)', () => {
let error = false;

const server = new Server(AbortingScp);
server.on('networkError', () => {
error = true;
server.close();
});
server.listen(2113);

http.get('http://localhost:2113').on('error', () => {
expect(error).to.be.true;
});
});
});
Loading

0 comments on commit 26d257b

Please sign in to comment.