1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.transport.sshd.agent;
11
12 import java.io.IOException;
13 import java.text.MessageFormat;
14 import java.util.Objects;
15
16 import org.apache.sshd.agent.SshAgentConstants;
17 import org.apache.sshd.common.SshException;
18 import org.apache.sshd.common.util.buffer.BufferUtils;
19 import org.eclipse.jgit.internal.transport.sshd.SshdText;
20
21
22
23
24
25
26 public abstract class AbstractConnector implements Connector {
27
28
29 private static final int MIN_REPLY_LENGTH = 8 * 1024;
30
31
32
33
34 protected static final int DEFAULT_MAX_REPLY_LENGTH = 256 * 1024;
35
36 private final int maxReplyLength;
37
38
39
40
41 protected AbstractConnector() {
42 this(DEFAULT_MAX_REPLY_LENGTH);
43 }
44
45
46
47
48
49
50
51 protected AbstractConnector(int maxReplyLength) {
52 if (maxReplyLength < MIN_REPLY_LENGTH) {
53 throw new IllegalArgumentException(
54 "Maximum payload length too small");
55 }
56 this.maxReplyLength = maxReplyLength;
57 }
58
59
60
61
62
63
64
65 protected int getMaximumMessageLength() {
66 return this.maxReplyLength;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 protected void prepareMessage(byte command, byte[] message)
81 throws IllegalArgumentException {
82 Objects.requireNonNull(message);
83 if (message.length < 5) {
84
85 throw new IllegalArgumentException("Message buffer for "
86 + SshAgentConstants.getCommandMessageName(command)
87 + " must have at least 5 bytes; have only "
88 + message.length);
89 }
90 BufferUtils.putUInt(message.length - 4, message);
91 message[4] = command;
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105 protected int toLength(byte command, byte[] length)
106 throws IOException {
107 long l = BufferUtils.getUInt(length);
108 if (l <= 0 || l > maxReplyLength - 4) {
109 throw new SshException(MessageFormat.format(
110 SshdText.get().sshAgentReplyLengthError,
111 Long.toString(l),
112 SshAgentConstants.getCommandMessageName(command)));
113 }
114 return (int) l;
115 }
116 }