Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/wpad/dhcp/DHCPSocket.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.wpad.dhcp;
2
3 import java.io.IOException;
4 import java.net.DatagramPacket;
5 import java.net.DatagramSocket;
6 import java.net.InetAddress;
7 import java.net.Socket;
8 import java.net.SocketException;
9 import java.net.UnknownHostException;
10
11 /**
12 * This class represents a Socket for sending DHCP Messages
13 *
14 * @author Markus Bernhardt, Copyright 2016
15 * @author Jason Goldschmidt and Simon Frankenberger
16 * @author Bernd Rosstauscher, Copyright 2009
17 *
18 * @see java.net.DatagramSocket
19 */
20
21 public class DHCPSocket extends DatagramSocket {
22 /**
23 * Default socket timeout (1 second)
24 */
25 private int SOCKET_TIMEOUT = 1000;
26
27 /**
28 * Default MTU (Maximum Transmission Unit) for ethernet (in bytes)
29 */
30 private int mtu = 1500;
31
32 /**
33 * Constructor for creating DHCPSocket on a specific port on the local
34 * machine.
35 *
36 * @param inPort
37 * The port for the application to bind.
38 *
39 * @throws SocketException
40 * As thrown by the {@link Socket} constructor
41 */
42 public DHCPSocket(int inPort) throws SocketException {
43 super(inPort);
44 setSoTimeout(this.SOCKET_TIMEOUT);
45 }
46
47 /**
48 * Sets the Maximum Transfer Unit for the UDP DHCP Packets to be set.
49 *
50 * @param inSize
51 * Integer representing desired MTU
52 */
53 public void setMTU(int inSize) {
54 this.mtu = inSize;
55 }
56
57 /**
58 * Returns the set MTU for this socket
59 *
60 * @return The Maximum Transfer Unit set for this socket
61 */
62 public int getMTU() {
63 return this.mtu;
64 }
65
66 /**
67 * Sends a DHCPMessage object to a predifined host.
68 *
69 * @param inMessage
70 * Well-formed DHCPMessage to be sent to a server
71 *
72 * @throws IOException
73 * If the message could not be sent.
74 */
75 public synchronized void send(DHCPMessage inMessage) throws IOException {
76 byte data[] = new byte[this.mtu];
77 data = inMessage.externalize();
78 InetAddress dest = null;
79 try {
80 dest = InetAddress.getByName(inMessage.getDestinationAddress());
81 } catch (UnknownHostException e) {
82 }
83
84 DatagramPacket outgoing = new DatagramPacket(data, data.length, dest, inMessage.getPort());
85
86 send(outgoing); // send outgoing message
87 }
88
89 /**
90 * Receives a datagram packet containing a DHCP Message into a DHCPMessage
91 * object.
92 *
93 * @return <code>true</code> if message is received, <code>false</code> if
94 * timeout occurs.
95 * @param outMessage
96 * DHCPMessage object to receive new message into
97 */
98 public synchronized boolean receive(DHCPMessage outMessage) {
99 try {
100 DatagramPacket incoming = new DatagramPacket(new byte[this.mtu], this.mtu);
101 // gSocket.
102 receive(incoming); // block on receive for SOCKET_TIMEOUT
103
104 outMessage.internalize(incoming.getData());
105 } catch (java.io.IOException e) {
106 return false;
107 } // end catch
108 return true;
109 }
110 }