Debian Codegraph @

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

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.wpad.dhcp;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.net.InetAddress;
9 import java.net.UnknownHostException;
10
11 /**
12 * This class represents a DHCP Message.
13 *
14 * @author Markus Bernhardt, Copyright 2016
15 * @author Jason Goldschmidt, Nick Stone and Simon Frankenberger
16 * @author Bernd Rosstauscher, Copyright 2009
17 */
18 public class DHCPMessage {
19 // -----------------------------------------------------------
20 // Constants
21 // -----------------------------------------------------------
22 /**
23 * Operation for a request
24 */
25 public static final byte OP_REQUEST = 1;
26
27 /**
28 * Operation for a reply
29 */
30 public static final byte OP_REPLY = 2;
31
32 /**
33 * Message Code representing a DHCPDISCOVER message
34 */
35 public static final byte DHCPDISCOVER = 1;
36
37 /**
38 * Message Code representing a DHCPOFFER message
39 */
40 public static final byte DHCPOFFER = 2;
41
42 /**
43 * Message Code representing a DHCPREQUEST message
44 */
45 public static final byte DHCPREQUEST = 3;
46
47 /**
48 * Message Code representing a DHCPDECLINE message
49 */
50 public static final byte DHCPDECLINE = 4;
51
52 /**
53 * Message Code representing a DHCPACK message
54 */
55 public static final byte DHCPACK = 5;
56
57 /**
58 * Message Code representing a DHCPNAK message
59 */
60 public static final byte DHCPNAK = 6;
61
62 /**
63 * Message Code representing a DHCPRELEASE message
64 */
65 public static final byte DHCPRELEASE = 7;
66
67 /**
68 * Message Code representing a DHCPINFORM message
69 */
70 public static final byte DHCPINFORM = 8;
71
72 /**
73 * Default DHCP client port
74 */
75 public static final int CLIENT_PORT = 68; // client port (by default)
76
77 /**
78 * Default DHCP server port
79 */
80 public static final int SERVER_PORT = 67; // server port (by default)
81
82 /**
83 * Broadcast Adress to send packets to
84 */
85 public static InetAddress BROADCAST_ADDR = null;
86
87 // -----------------------------------------------------------
88 // Fields defining a dhcp message
89 // -----------------------------------------------------------
90 /**
91 * Operation Code.<br>
92 * <br>
93 * Can either be {@link #OP_REQUEST} or {@link #OP_REPLY}.
94 */
95 private byte op;
96
97 /**
98 * Networktype as defined by
99 * <a href="http://tools.ietf.org/html/rfc1340#page-54">RFC1340 page 54</a>.
100 */
101 private byte htype;
102
103 /**
104 * Hardware address length (e.g. '6' for ethernet).
105 */
106 private byte hlen;
107
108 /**
109 * Client sets to zero, optionally used by relay-agents when booting via a
110 * relay-agent.
111 */
112 private byte hops;
113
114 /**
115 * Transaction ID, a random number chosen by the client, used by the client
116 * and server to associate messages and responses between a client and a
117 * server.
118 */
119 private int xid;
120
121 /**
122 * Filled in by client, seconds elapsed since client started trying to boot.
123 */
124 private short secs;
125
126 /**
127 * Flags for this message.<br>
128 * The leftmost bit is defined as the BROADCAST (B) flag.
129 */
130 private short flags;
131
132 /**
133 * Client IP address; filled in by client in DHCPREQUEST if verifying
134 * previously allocated configuration parameters.
135 */
136 private byte ciaddr[] = new byte[4];
137
138 /**
139 * 'your' (client) IP address.
140 */
141 private byte yiaddr[] = new byte[4];
142
143 /**
144 * IP address of next server to use in bootstrap; returned in DHCPOFFER,
145 * DHCPACK and DHCPNAK by server.
146 */
147 private byte siaddr[] = new byte[4];
148
149 /**
150 * Relay agent IP address, used in booting via a relay-agent.
151 */
152 private byte giaddr[] = new byte[4];
153
154 /**
155 * Client hardware address.
156 */
157 private byte chaddr[] = new byte[16];
158
159 /**
160 * Optional server host name, null terminated string.
161 */
162 private byte sname[] = new byte[64];
163
164 /**
165 * Boot file name, null terminated string; "generic" name or null in
166 * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
167 */
168 private byte file[] = new byte[128];
169
170 /**
171 * Internal representation of the given DHCP options.
172 */
173 private DHCPOptions optionsList = null;
174
175 /**
176 * global port variable for this message
177 */
178 private int gPort;
179
180 /**
181 * The destination IP-Adress of this message
182 */
183 private InetAddress destination_IP;
184
185 static {
186 try {
187 BROADCAST_ADDR = InetAddress.getByName("255.255.255.255");
188 // broadcast address(by default)
189 } catch (UnknownHostException e) {
190 // Broadcast address must always exist
191 }
192 }
193
194 // -----------------------------------------------------------
195 // Constructors
196 // -----------------------------------------------------------
197
198 /**
199 * Creates empty DHCPMessage object, initializes the object, sets the host
200 * to the broadcast address, the local subnet, binds to the default server
201 * port.
202 */
203 public DHCPMessage() {
204 initialize();
205
206 this.destination_IP = BROADCAST_ADDR;
207 this.gPort = SERVER_PORT;
208 }
209
210 /**
211 * Copy constructor creates DHCPMessage from inMessage
212 *
213 * @param inMessage
214 * The message to be copied
215 */
216 public DHCPMessage(DHCPMessage inMessage) {
217 initialize();
218
219 this.destination_IP = BROADCAST_ADDR;
220 this.gPort = SERVER_PORT;
221 this.op = inMessage.getOp();
222 this.htype = inMessage.getHtype();
223 this.hlen = inMessage.getHlen();
224 this.hops = inMessage.getHops();
225 this.xid = inMessage.getXid();
226 this.secs = inMessage.getSecs();
227 this.flags = inMessage.getFlags();
228 this.ciaddr = inMessage.getCiaddr();
229 this.yiaddr = inMessage.getYiaddr();
230 this.siaddr = inMessage.getSiaddr();
231 this.giaddr = inMessage.getGiaddr();
232 this.chaddr = inMessage.getChaddr();
233 this.sname = inMessage.getSname();
234 this.file = inMessage.getFile();
235 this.optionsList.internalize(inMessage.getOptions());
236 }
237
238 /**
239 * Copy constructor creates DHCPMessage from inMessage and sets server and
240 * port.
241 *
242 * @param inMessage
243 * The message to be copied
244 * @param inServername
245 * The host name
246 * @param inPort
247 * The port number
248 */
249 public DHCPMessage(DHCPMessage inMessage, InetAddress inServername, int inPort) {
250 initialize();
251
252 this.destination_IP = inServername;
253 this.gPort = inPort;
254
255 this.op = inMessage.getOp();
256 this.htype = inMessage.getHtype();
257 this.hlen = inMessage.getHlen();
258 this.hops = inMessage.getHops();
259 this.xid = inMessage.getXid();
260 this.secs = inMessage.getSecs();
261 this.flags = inMessage.getFlags();
262 this.ciaddr = inMessage.getCiaddr();
263 this.yiaddr = inMessage.getYiaddr();
264 this.siaddr = inMessage.getSiaddr();
265 this.giaddr = inMessage.getGiaddr();
266 this.chaddr = inMessage.getChaddr();
267 this.sname = inMessage.getSname();
268 this.file = inMessage.getFile();
269 this.optionsList.internalize(inMessage.getOptions());
270 }
271
272 /**
273 * Copy constructor creates DHCPMessage from inMessage and sets server name.
274 *
275 * @param inMessage
276 * The message to be copied
277 * @param inServername
278 * The host name
279 */
280 public DHCPMessage(DHCPMessage inMessage, InetAddress inServername) {
281 initialize();
282
283 this.destination_IP = inServername;
284 this.gPort = SERVER_PORT;
285
286 this.op = inMessage.getOp();
287 this.htype = inMessage.getHtype();
288 this.hlen = inMessage.getHlen();
289 this.hops = inMessage.getHops();
290 this.xid = inMessage.getXid();
291 this.secs = inMessage.getSecs();
292 this.flags = inMessage.getFlags();
293 this.ciaddr = inMessage.getCiaddr();
294 this.yiaddr = inMessage.getYiaddr();
295 this.siaddr = inMessage.getSiaddr();
296 this.giaddr = inMessage.getGiaddr();
297 this.chaddr = inMessage.getChaddr();
298 this.sname = inMessage.getSname();
299 this.file = inMessage.getFile();
300 this.optionsList.internalize(inMessage.getOptions());
301 }
302
303 /**
304 * Creates an empty DHCPMessage object, initializes the object, sets the
305 * host to a specified host name, and binds to a specified port.
306 *
307 * @param inServername
308 * The host name
309 * @param inPort
310 * The port number
311 */
312 public DHCPMessage(InetAddress inServername, int inPort) {
313 initialize();
314
315 this.destination_IP = inServername;
316 this.gPort = inPort;
317 }
318
319 /**
320 * Creates an empty DHCPMessage object, initializes the object, sets the
321 * host to a specified host name, and binds to the default port.
322 *
323 * @param inServername
324 * The host name
325 */
326 public DHCPMessage(InetAddress inServername) {
327 initialize();
328
329 this.destination_IP = inServername;
330 this.gPort = SERVER_PORT;
331 }
332
333 /**
334 * Creates an empty DHCPMessage object, initializes the object, sets the
335 * host to the broadcast address, and binds to a specified port.
336 *
337 * @param inPort
338 * The port number
339 */
340 public DHCPMessage(int inPort) {
341 initialize();
342
343 this.destination_IP = BROADCAST_ADDR;
344 this.gPort = inPort;
345 }
346
347 /**
348 * Creates an empty DHCPMessage object, initializes the object with a
349 * specified byte array containing DHCP message information, sets the host
350 * to default host name, the local subnet, and bind to the default server
351 * port.
352 *
353 * @param ibuf
354 * The byte array to initialize DHCPMessage object
355 */
356 public DHCPMessage(byte[] ibuf) {
357 initialize();
358 internalize(ibuf);
359
360 this.destination_IP = BROADCAST_ADDR;
361 this.gPort = SERVER_PORT;
362 }
363
364 /**
365 * Creates an empty DHCPMessage object, initializes the object with a
366 * specified byte array containing DHCP message information, sets the host
367 * to specified host name, and binds to the specified port.
368 *
369 * @param ibuf
370 * The byte array to initialize DHCPMessage object
371 * @param inServername
372 * The hostname
373 * @param inPort
374 * The port number
375 */
376 public DHCPMessage(byte[] ibuf, InetAddress inServername, int inPort) {
377 initialize();
378 internalize(ibuf);
379
380 this.destination_IP = inServername;
381 this.gPort = inPort;
382 }
383
384 /**
385 * Creates an empty DHCPMessage object, initializes the object with a
386 * specified byte array containing DHCP message information, sets the host
387 * to broadcast address, and binds to the specified port.
388 *
389 * @param ibuf
390 * The byte array to initialize DHCPMessage object
391 * @param inPort
392 * The port number
393 */
394 public DHCPMessage(byte ibuf[], int inPort) {
395 initialize();
396 internalize(ibuf);
397
398 this.destination_IP = BROADCAST_ADDR;
399 this.gPort = inPort;
400 }
401
402 /**
403 * Creates an empty DHCPMessage object, initializes the object with a
404 * specified byte array containing DHCP message information, sets the host
405 * to specified host name, and binds to the specified port.
406 *
407 * @param ibuf
408 * The byte array to initialize DHCPMessage object
409 * @param inServername
410 * The hostname
411 */
412 public DHCPMessage(byte[] ibuf, InetAddress inServername) {
413 initialize();
414 internalize(ibuf);
415
416 this.destination_IP = inServername;
417 this.gPort = SERVER_PORT;
418 }
419
420 /**
421 * Creates a new DHCPMessage object from the giben DataInputStream.
422 *
423 * @param inStream
424 * The stream to read from
425 */
426 public DHCPMessage(DataInputStream inStream) {
427 initialize();
428
429 try {
430 this.op = inStream.readByte();
431 this.htype = inStream.readByte();
432 this.hlen = inStream.readByte();
433 this.hops = inStream.readByte();
434 this.xid = inStream.readInt();
435 this.secs = inStream.readShort();
436 this.flags = inStream.readShort();
437 inStream.readFully(this.ciaddr, 0, 4);
438 inStream.readFully(this.yiaddr, 0, 4);
439 inStream.readFully(this.siaddr, 0, 4);
440 inStream.readFully(this.giaddr, 0, 4);
441 inStream.readFully(this.chaddr, 0, 16);
442 inStream.readFully(this.sname, 0, 64);
443 inStream.readFully(this.file, 0, 128);
444 byte[] options = new byte[312];
445 inStream.readFully(options, 0, 312);
446 this.optionsList.internalize(options);
447 } catch (IOException e) {
448 System.err.println(e);
449 }
450 }
451
452 // -----------------------------------------------------------
453 // Methods
454 // -----------------------------------------------------------
455 /**
456 * Initializes datamembers in the constructors every empty DHCPMessage
457 * object will by default contain these params. Initializes options array
458 * from linked list form.
459 */
460 private void initialize() {
461 this.optionsList = new DHCPOptions();
462 }
463
464 /**
465 * Converts a DHCPMessage object to a byte array.
466 *
467 * @return A byte array with information from DHCPMessage object, ready to
468 * send.
469 */
470 public synchronized byte[] externalize() {
471 ByteArrayOutputStream outBStream = new ByteArrayOutputStream();
472 DataOutputStream outStream = new DataOutputStream(outBStream);
473
474 try {
475 outStream.writeByte(this.op);
476 outStream.writeByte(this.htype);
477 outStream.writeByte(this.hlen);
478 outStream.writeByte(this.hops);
479 outStream.writeInt(this.xid);
480 outStream.writeShort(this.secs);
481 outStream.writeShort(this.flags);
482 outStream.write(this.ciaddr, 0, 4);
483 outStream.write(this.yiaddr, 0, 4);
484 outStream.write(this.siaddr, 0, 4);
485 outStream.write(this.giaddr, 0, 4);
486 outStream.write(this.chaddr, 0, 16);
487 outStream.write(this.sname, 0, 64);
488 outStream.write(this.file, 0, 128);
489
490 byte[] options = new byte[312];
491 if (this.optionsList == null) {
492 initialize();
493 }
494
495 options = this.optionsList.externalize();
496 outStream.write(options, 0, 312);
497 } catch (IOException e) {
498 System.err.println(e);
499 }
500
501 // extract the byte array from the Stream
502 byte data[] = outBStream.toByteArray();
503
504 return data;
505 }
506
507 /**
508 * Convert a specified byte array containing a DHCP message into a
509 * DHCPMessage object.
510 *
511 * @param ibuff
512 * Byte array to convert to a DHCPMessage object
513 * @return A DHCPMessage object with information from byte array.
514 */
515
516 public synchronized DHCPMessage internalize(byte[] ibuff) {
517 ByteArrayInputStream inBStream = new ByteArrayInputStream(ibuff, 0, ibuff.length);
518 DataInputStream inStream = new DataInputStream(inBStream);
519
520 try {
521 this.op = inStream.readByte();
522 this.htype = inStream.readByte();
523 this.hlen = inStream.readByte();
524 this.hops = inStream.readByte();
525 this.xid = inStream.readInt();
526 this.secs = inStream.readShort();
527 this.flags = inStream.readShort();
528 inStream.readFully(this.ciaddr, 0, 4);
529 inStream.readFully(this.yiaddr, 0, 4);
530 inStream.readFully(this.siaddr, 0, 4);
531 inStream.readFully(this.giaddr, 0, 4);
532 inStream.readFully(this.chaddr, 0, 16);
533 inStream.readFully(this.sname, 0, 64);
534 inStream.readFully(this.file, 0, 128);
535
536 byte[] options = new byte[312];
537 inStream.readFully(options, 0, 312);
538 if (this.optionsList == null) {
539 initialize();
540 }
541
542 this.optionsList.internalize(options);
543 } catch (IOException e) {
544 System.err.println(e);
545 } // end catch
546
547 return this;
548 }
549
550 /**
551 * Set message Op code / message type.
552 *
553 * @param inOp
554 * message Op code / message type
555 */
556 public void setOp(byte inOp) {
557 this.op = inOp;
558 }
559
560 /**
561 * Set hardware address type.
562 *
563 * @param inHtype
564 * hardware address type
565 */
566 public void setHtype(byte inHtype) {
567 this.htype = inHtype;
568 }
569
570 /**
571 * Set hardware address length.
572 *
573 * @param inHlen
574 * hardware address length
575 */
576 public void setHlen(byte inHlen) {
577 this.hlen = inHlen;
578 }
579
580 /**
581 * Set hops field.
582 *
583 * @param inHops
584 * hops field
585 */
586 public void setHops(byte inHops) {
587 this.hops = inHops;
588 }
589
590 /**
591 * Set transaction ID.
592 *
593 * @param inXid
594 * transactionID
595 */
596 public void setXid(int inXid) {
597 this.xid = inXid;
598 }
599
600 /**
601 * Set seconds elapsed since client began address acquisition or renewal
602 * process.
603 *
604 * @param inSecs
605 * Seconds elapsed since client began address acquisition or
606 * renewal process
607 */
608 public void setSecs(short inSecs) {
609 this.secs = inSecs;
610 }
611
612 /**
613 * Set flags field.
614 *
615 * @param inFlags
616 * flags field
617 */
618 public void setFlags(short inFlags) {
619 this.flags = inFlags;
620 }
621
622 /**
623 * Set client IP address.
624 *
625 * @param inCiaddr
626 * client IP address
627 */
628 public void setCiaddr(byte[] inCiaddr) {
629 this.ciaddr = inCiaddr;
630 }
631
632 /**
633 * Set 'your' (client) IP address.
634 *
635 * @param inYiaddr
636 * 'your' (client) IP address
637 */
638 public void setYiaddr(byte[] inYiaddr) {
639 this.yiaddr = inYiaddr;
640 }
641
642 /**
643 * Set address of next server to use in bootstrap.
644 *
645 * @param inSiaddr
646 * address of next server to use in bootstrap
647 */
648 public void setSiaddr(byte[] inSiaddr) {
649 this.siaddr = inSiaddr;
650 }
651
652 /**
653 * Set relay agent IP address.
654 *
655 * @param inGiaddr
656 * relay agent IP address
657 */
658 public void setGiaddr(byte[] inGiaddr) {
659 this.giaddr = inGiaddr;
660 }
661
662 /**
663 * Set client harware address.
664 *
665 * @param inChaddr
666 * client hardware address
667 */
668 public void setChaddr(byte[] inChaddr) {
669 this.chaddr = inChaddr;
670 }
671
672 /**
673 * Set optional server host name.
674 *
675 * @param inSname
676 * server host name
677 */
678 public void setSname(byte[] inSname) {
679 this.sname = inSname;
680 }
681
682 /**
683 * Set boot file name.
684 *
685 * @param inFile
686 * boot file name
687 */
688 public void setFile(byte[] inFile) {
689 this.file = inFile;
690 }
691
692 /**
693 * Set message destination port.
694 *
695 * @param inPortNum
696 * port on message destination host
697 */
698 public void setPort(int inPortNum) {
699 this.gPort = inPortNum;
700 }
701
702 /**
703 * Set message destination IP
704 *
705 * @param inHost
706 * string representation of message destination IP or hostname
707 */
708 public void setDestinationHost(String inHost) {
709 try {
710 this.destination_IP = InetAddress.getByName(inHost);
711 } catch (Exception e) {
712 System.err.println(e);
713 }
714 }
715
716 /**
717 * @return message Op code / message type.
718 */
719 public byte getOp() {
720 return this.op;
721 }
722
723 /**
724 * @return hardware address type.
725 */
726 public byte getHtype() {
727 return this.htype;
728 }
729
730 /**
731 * @return hardware address length.
732 */
733 public byte getHlen() {
734 return this.hlen;
735 }
736
737 /**
738 * @return hops field.
739 */
740 public byte getHops() {
741 return this.hops;
742 }
743
744 /**
745 * @return transaction ID.
746 */
747 public int getXid() {
748 return this.xid;
749 }
750
751 /**
752 * @return seconds elapsed since client began address acquisition or renewal
753 * process.
754 */
755 public short getSecs() {
756 return this.secs;
757 }
758
759 /**
760 * @return flags field.
761 */
762 public short getFlags() {
763 return this.flags;
764 }
765
766 /**
767 * @return client IP address.
768 */
769 public byte[] getCiaddr() {
770 return this.ciaddr;
771 }
772
773 /**
774 * @return 'your' (client) IP address.
775 */
776 public byte[] getYiaddr() {
777 return this.yiaddr;
778 }
779
780 /**
781 * @return address of next server to use in bootstrap.
782 */
783 public byte[] getSiaddr() {
784 return this.siaddr;
785 }
786
787 /**
788 * @return relay agent IP address.
789 */
790 public byte[] getGiaddr() {
791 return this.giaddr;
792 }
793
794 /**
795 * @return client harware address.
796 */
797 public byte[] getChaddr() {
798 return this.chaddr;
799 }
800
801 /**
802 * @return optional server host name.
803 */
804 public byte[] getSname() {
805 return this.sname;
806 }
807
808 /**
809 * @return boot file name.
810 */
811 public byte[] getFile() {
812 return this.file;
813 }
814
815 /**
816 * @return a byte array containing options
817 */
818 public byte[] getOptions() {
819 if (this.optionsList == null) {
820 initialize();
821 }
822 return this.optionsList.externalize();
823 }
824
825 /**
826 * @return An interger representation of the message destination port
827 */
828 public int getPort() {
829 return this.gPort;
830 }
831
832 /**
833 * Get message destination hostname
834 *
835 * @return A string representing the hostname of the message destination
836 * server
837 */
838 public String getDestinationAddress() {
839 return this.destination_IP.getHostAddress();
840 }
841
842 /**
843 * Sets DHCP options in DHCPMessage. If option already exists then remove
844 * old option and insert a new one.
845 *
846 * @param inOptNum
847 * option number
848 * @param inOptionData
849 * option data
850 */
851 public void setOption(int inOptNum, byte[] inOptionData) {
852 this.optionsList.setOption((byte) inOptNum, inOptionData);
853 }
854
855 /**
856 * Returns specified DHCP option that matches the input code. Null is
857 * returned if option is not set.
858 *
859 * @param inOptNum
860 * option number
861 *
862 * @return the option matching input code
863 */
864 public byte[] getOption(int inOptNum) {
865 if (this.optionsList == null) {
866 initialize();
867 }
868 return this.optionsList.getOption((byte) inOptNum);
869 }
870
871 /**
872 * Removes the specified DHCP option that matches the input code.
873 *
874 * @param inOptNum
875 * option number
876 */
877 public void removeOption(int inOptNum) {
878 if (this.optionsList == null) {
879 initialize();
880 }
881 this.optionsList.removeOption((byte) inOptNum);
882 }
883
884 /**
885 * Report whether or not the input option is set.
886 *
887 * @param inOptNum
888 * option number
889 *
890 * @return is the given option set?
891 */
892 public boolean IsOptSet(int inOptNum) {
893 if (this.optionsList == null) {
894 initialize();
895 }
896
897 return this.optionsList.contains((byte) inOptNum);
898 }
899 }