Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 757x 757x 757x 757x 757x 757x 757x 757x 757x 757x 757x 757x 757x 757x 756x 756x 756x 756x 756x 756x 756x 756x 756x 1x 1x 1x 1x 1x 1x 279x 279x 279x 279x 279x 279x 279x 279x 757x 757x 757x 757x 757x 757x 757x 753x 753x 753x | import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {Nfsv4Decoder} from '../Nfsv4Decoder';
import {Nfsv4FullEncoder} from '../Nfsv4FullEncoder';
import {RmRecordDecoder, type RmRecordEncoder} from '../../../rm';
import {
RpcAcceptStat,
RpcAcceptedReplyMessage,
RpcAuthFlavor,
RpcCallMessage,
type RpcMessage,
RpcMessageDecoder,
type RpcMessageEncoder,
RpcOpaqueAuth,
RpcRejectedReplyMessage,
} from '../../../rpc';
import * as msg from '../messages';
import {EMPTY_READER, Nfsv4Proc, Nfsv4Stat} from '../constants';
import {Nfsv4CompoundProcCtx} from './Nfsv4CompoundProcCtx';
import type {Duplex} from 'node:stream';
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib/types';
import type {Nfsv4Operations} from './operations/Nfsv4Operations';
const EMPTY_AUTH = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NONE, EMPTY_READER);
export interface Nfsv4ConnectionOpts {
/**
* Normally this is a TCP socket, but any Duplex stream will do.
*/
duplex: Duplex;
ops: Nfsv4Operations;
encoder?: Nfsv4FullEncoder;
decoder?: Nfsv4Decoder;
debug?: boolean;
logger?: Pick<typeof console, 'log' | 'error'>;
}
export class Nfsv4Connection {
public closed = false;
public maxIncomingMessage: number = 2 * 1024 * 1024;
public maxBackpressure: number = 2 * 1024 * 1024;
/** Last known RPC transaction ID. Used to emit fatal connection errors. */
protected lastXid = 0;
public readonly duplex: Duplex;
protected readonly rmDecoder: RmRecordDecoder;
protected readonly rpcDecoder: RpcMessageDecoder;
protected readonly nfsDecoder: Nfsv4Decoder;
protected readonly writer: IWriter & IWriterGrowable;
protected readonly rmEncoder: RmRecordEncoder;
protected readonly rpcEncoder: RpcMessageEncoder;
protected readonly nfsEncoder: Nfsv4FullEncoder;
public debug: boolean;
public logger: Pick<typeof console, 'log' | 'error'>;
public readonly ops: Nfsv4Operations;
constructor(opts: Nfsv4ConnectionOpts) {
this.debug = !!opts.debug;
this.logger = opts.logger || console;
const duplex = (this.duplex = opts.duplex);
this.ops = opts.ops;
this.rmDecoder = new RmRecordDecoder();
this.rpcDecoder = new RpcMessageDecoder();
this.nfsDecoder = new Nfsv4Decoder();
const nfsEncoder = (this.nfsEncoder = new Nfsv4FullEncoder());
this.writer = nfsEncoder.writer;
this.rmEncoder = nfsEncoder.rmEncoder;
this.rpcEncoder = nfsEncoder.rpcEncoder;
duplex.on('data', this.onData.bind(this));
duplex.on('timeout', () => this.close());
duplex.on('close', (hadError: boolean): void => {
this.close();
});
duplex.on('error', (err: Error) => {
this.logger.error('SOCKET ERROR:', err);
this.close();
});
}
protected onData(data: Uint8Array): void {
const {rmDecoder, rpcDecoder} = this;
rmDecoder.push(data);
let record = rmDecoder.readRecord();
while (record) {
if (record.size()) {
const rpcMessage = rpcDecoder.decodeMessage(record);
if (rpcMessage) this.onRpcMessage(rpcMessage);
else E{
this.close();
return;
}
}
record = rmDecoder.readRecord();
}
}
protected onRpcMessage(msg: RpcMessage): void {
if (msg instanceof RpcCallMessage) {
this.lastXid = msg.xid;
this.onRpcCallMessage(msg);
} else Eif (msg instanceof RpcAcceptedReplyMessage) {
throw new Error('Not implemented RpcAcceptedReplyMessage');
} else Iif (msg instanceof RpcRejectedReplyMessage) {
throw new Error('Not implemented RpcRejectedReplyMessage');
}
}
protected onRpcCallMessage(procedure: RpcCallMessage): void {
const {debug, logger, writer, rmEncoder} = this;
const {xid, proc} = procedure;
switch (proc) {
case Nfsv4Proc.COMPOUND: {
Iif (debug) logger.log(`\n<COMPOUND{${xid}}>`);
Iif (!(procedure.params instanceof Reader)) return;
const compound = this.nfsDecoder.decodeCompoundRequest(procedure.params);
if (compound instanceof msg.Nfsv4CompoundRequest) {
new Nfsv4CompoundProcCtx(this, compound)
.exec()
.then((procResponse) => {
Iif (debug) logger.log(`</COMPOUND{${xid}}>`);
this.nfsEncoder.writeAcceptedCompoundReply(xid, EMPTY_AUTH, procResponse);
this.write(writer.flush());
})
.catch((err) => {
logger.error('NFS COMPOUND error:', xid, err);
this.nfsEncoder.writeRejectedReply(xid, Nfsv4Stat.NFS4ERR_SERVERFAULT);
});
} else Ethis.closeWithError(RpcAcceptStat.GARBAGE_ARGS);
break;
}
case Nfsv4Proc.NULL: {
Iif (debug) logger.log('NULL', procedure);
const state = rmEncoder.startRecord();
this.rpcEncoder.writeAcceptedReply(xid, EMPTY_AUTH, RpcAcceptStat.SUCCESS);
rmEncoder.endRecord(state);
this.write(writer.flush());
break;
}
default: {
Iif (debug) logger.error(`Unknown procedure: ${proc}`);
}
}
}
private closeWithError(
error:
| RpcAcceptStat.PROG_UNAVAIL
| RpcAcceptStat.PROC_UNAVAIL
| RpcAcceptStat.GARBAGE_ARGS
| RpcAcceptStat.SYSTEM_ERR,
): void {
Iif (this.debug) this.logger.log(`Closing with error: RpcAcceptStat = ${error}, xid = ${this.lastXid}`);
const xid = this.lastXid;
Iif (xid) {
const state = this.rmEncoder.startRecord();
const verify = new RpcOpaqueAuth(RpcAuthFlavor.AUTH_NONE, EMPTY_READER);
this.rpcEncoder.writeAcceptedReply(xid, verify, error);
this.rmEncoder.endRecord(state);
const bin = this.writer.flush();
this.duplex.write(bin);
}
this.close();
}
public close(): void {
Iif (this.closed) return;
this.closed = true;
clearImmediate(this.__uncorkTimer);
this.__uncorkTimer = null;
const duplex = this.duplex;
duplex.removeAllListeners();
if (!duplex.destroyed) duplex.destroy();
}
// ---------------------------------------------------------- Write to socket
private __uncorkTimer: any = null;
public write(buf: Uint8Array): void {
Iif (this.closed) return;
const duplex = this.duplex;
Iif (duplex.writableLength > this.maxBackpressure) {
this.closeWithError(RpcAcceptStat.SYSTEM_ERR);
return;
}
const __uncorkTimer = this.__uncorkTimer;
if (!__uncorkTimer) duplex.cork();
duplex.write(buf);
if (!__uncorkTimer)
this.__uncorkTimer = setImmediate(() => {
this.__uncorkTimer = null;
duplex.uncork();
});
}
// TODO: Execute NFS Callback...
public send(): void {}
}
|