blob: 6dbe2bf3d7ecdd2ff52bd0a1b8b7b94d2bb4d535 (
plain) (
blame)
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
|
package com.btr.proxy.search.wpad.dhcp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
/**
* This class represents a Socket for sending DHCP Messages
*
* @author Jason Goldschmidt and Simon Frankenberger
*
* @see java.net.DatagramSocket
*/
public class DHCPSocket extends DatagramSocket {
/**
* Default socket timeout (1 second)
*/
private int SOCKET_TIMEOUT = 5000;
/**
* Default MTU (Maximum Transmission Unit) for ethernet (in bytes)
*/
private int mtu = 1500;
/**
* Constructor for creating DHCPSocket on a specific port on the local
* machine.
*
* @param inPort The port for the application to bind.
*
* @throws SocketException As thrown by the {@link Socket} constructor
*/
public DHCPSocket(int inPort) throws SocketException {
super(inPort);
setSoTimeout(this.SOCKET_TIMEOUT);
}
/**
* Sets the Maximum Transfer Unit for the UDP DHCP Packets to be set.
*
* @param inSize Integer representing desired MTU
*/
public void setMTU(int inSize) {
this.mtu = inSize;
}
/**
* Returns the set MTU for this socket
*
* @return The Maximum Transfer Unit set for this socket
*/
public int getMTU() {
return this.mtu;
}
/**
* Sends a DHCPMessage object to a predifined host.
*
* @param inMessage Well-formed DHCPMessage to be sent to a server
*
* @throws IOException If the message could not be sent.
*/
public synchronized void send(DHCPMessage inMessage) throws IOException {
byte data[] = new byte[this.mtu];
data = inMessage.externalize();
InetAddress dest = null;
try {
dest = InetAddress.getByName(inMessage.getDestinationAddress());
}
catch (UnknownHostException e) {
}
DatagramPacket outgoing = new DatagramPacket(data, data.length, dest,
inMessage.getPort());
send(outgoing); // send outgoing message
}
/**
* Receives a datagram packet containing a DHCP Message into
* a DHCPMessage object.
*
* @return <code>true</code> if message is received,
* <code>false</code> if timeout occurs.
* @param outMessage DHCPMessage object to receive new message into
*/
public synchronized boolean receive(DHCPMessage outMessage) {
try {
DatagramPacket incoming = new DatagramPacket(new byte[this.mtu],
this.mtu);
//gSocket.
receive(incoming); // block on receive for SOCKET_TIMEOUT
outMessage.internalize(incoming.getData());
}
catch (java.io.IOException e) {
e.printStackTrace();
return false;
} // end catch
return true;
}
}
|