-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
190 lines (156 loc) · 5.5 KB
/
Copy pathServer.java
File metadata and controls
190 lines (156 loc) · 5.5 KB
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
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Basic TFTP Server, adhering to RFC1350.
*
* @author James Li
*/
public class Server {
public static void main(String[] args) {
IncomingPacket requestPacket;
List<ServerSession> activeSessions = new ArrayList<ServerSession>();
Iterator<ServerSession> activeSessionsIterator;
boolean addrTIDClash;
String addrTIDPair;
InetAddress addr;
int port;
// Arguments parser
ArgumentsParser argsParser = new ArgumentsParser()
.register("port", ArgumentsParser.type.INT, Integer.toString(Constants.defaultServerTID)) // Port
.register("timeout", ArgumentsParser.type.INT, Integer.toString(Constants.defaultOperationTimeout)) // Timeout
.register("attempts", ArgumentsParser.type.INT, Integer.toString(Constants.defaultMaxOperationAttempts)) // Max
// attempts
.registerBoolean("enable-error-message-delivery") // Error msg
// delivery
.registerBoolean("disable-block-messages"); // Disables standard
// block ack
// messages
// Parse what we can
args = argsParser.munch(args);
try {
// Start
System.out.println("TFTP Server starting");
// Set up port
DatagramSocket welcomeSocket;
try {
welcomeSocket = new DatagramSocket(argsParser.readInt("port"));
} catch (SocketException e) {
System.err.println(String.format(Constants.strings.SOCKET_PORT_X_SETUP_FAILED, argsParser.readInt("port")));
return;
}
System.out.println(String.format(Constants.strings.SOCKET_PORT_X_LISTENING, welcomeSocket.getLocalPort()));
// Main loop
while (true) {
// Receive initial request
requestPacket = new IncomingPacket(welcomeSocket).receive().process();
// See if same addr-TID pair is active
activeSessionsIterator = activeSessions.iterator();
addrTIDClash = false;
addr = requestPacket.getDatagramPacket().getAddress();
port = requestPacket.getDatagramPacket().getPort();
addrTIDPair = addr.getHostAddress() + ":" + port;
// Go over the list to see if the same addr-TID pair is in there
while (activeSessionsIterator.hasNext()) {
ServerSession activeSession = activeSessionsIterator.next();
// Cleanup expired sessions from the list
if (!activeSession.isSessionActive()) {
activeSessionsIterator.remove();
continue;
}
// Check
if (!addrTIDClash && addrTIDPair.equals(activeSession.getAddrTID())) {
addrTIDClash = true;
continue; // Continue so that we go through the whole
// list to clean up any old ones
}
}
// If clash, respond with ERROR
if (addrTIDClash) {
System.err.println(
"Address-TID pair clashing request from '" + addrTIDPair + "'; replying with ERROR");
new OutgoingPacket(welcomeSocket, addr, port) //
.addOpcode(Packet.opcode.ERROR) //
.addErrorCode(Packet.errcode.NOT_DEFINED) //
.addString("") //
.addNullByte() //
.send();
System.err.println(String.format(Constants.strings.PACKET_TYPE_X_SENT, "ERROR"));
continue;
}
// Create new session to handle requestor if everything is fine
// so far
ServerSession session = new ServerSession(argsParser, requestPacket);
session.start();
activeSessions.add(session);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ServerSession extends Session {
/*
* (non-Javadoc)
*
* @see Session#begin()
*/
@Override
Session begin() throws Exception {
// Interpret the incoming request type, and set up session
short opcode = this.getInPacket().getOpcode();
switch (opcode) {
case Packet.opcode.RRQ:
this.setMode(this.getInPacket().getMode()) //
.setupSessionForReadingFromLocal(true, opcode, this.getInPacket().getFilename());
break;
case Packet.opcode.WRQ:
this.setMode(this.getInPacket().getMode()) //
.setupSessionForWritingToLocal(true, opcode, this.getInPacket().getFilename());
break;
default:
throw new Exception(Constants.strings.INVALID_REQUEST);
}
return this;
}
/*
* (non-Javadoc)
*
* @see Session#processInPacket()
*/
@Override
Session processInPacket() throws Exception {
return this.commonProcessInPacket(Packet.opcode.RRQ, Packet.opcode.WRQ);
}
/**
* @param argsParser
* Arguments passed to the program
* @param incomingPacket
* First incoming packet from the client
* @throws Exception
*/
ServerSession(ArgumentsParser argsParser, IncomingPacket incomingPacket) throws Exception {
this.setInPacket(incomingPacket) // Initial packet
.setDeliveryInfo() // Both address and port are set via. init
// packet
.setAddrTID() // Set the Addr-TID pair
.setTimeout(argsParser.readInt("timeout")) // Timeout
.setMaxAttempts(argsParser.readInt("attempts")) // Max
// attempts
// Encoding is set in #begin() as we need to be able to handle
// exceptions raised from reading the mode string
.print(String.format(Constants.strings.COMM_WITH_CLIENT_OVER_IP_X_PORT_X,
this.getDeliveryAddress().getHostAddress(), this.getDeliveryPort()));
// If error message delivery is enabled
if (argsParser.readBool("enable-error-message-delivery")) {
this.enableExceptionMessageDelivery();
}
// If std block ack messages are enabled
if (argsParser.readBool("disable-block-messages")) {
this.disableBlockAckMessagePrinting();
}
}
}