alshabib

adding packet types

Showing 34 changed files with 2504 additions and 60 deletions
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
21 <groupId>com.google.guava</groupId> 21 <groupId>com.google.guava</groupId>
22 <artifactId>guava-testlib</artifactId> 22 <artifactId>guava-testlib</artifactId>
23 </dependency> 23 </dependency>
24 + <dependency>
25 + <groupId>org.onlab.onos</groupId>
26 + <artifactId>onlab-misc</artifactId>
27 + </dependency>
24 </dependencies> 28 </dependencies>
25 29
26 </project> 30 </project>
......
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
56 </execution> 56 </execution>
57 </executions> 57 </executions>
58 </plugin> 58 </plugin>
59 -
60 <plugin> 59 <plugin>
61 <groupId>org.apache.felix</groupId> 60 <groupId>org.apache.felix</groupId>
62 <artifactId>maven-bundle-plugin</artifactId> 61 <artifactId>maven-bundle-plugin</artifactId>
......
1 +package org.onlab.onos.of.controller;
2 +
3 +import org.onlab.packet.Ethernet;
4 +import org.projectfloodlight.openflow.protocol.OFPacketIn;
5 +import org.projectfloodlight.openflow.protocol.OFPacketOut;
6 +import org.projectfloodlight.openflow.types.OFPort;
7 +
8 +public class DefaultPacketContext implements PacketContext {
9 +
10 + private boolean free = true;
11 + private boolean isBuilt = false;
12 + private final OpenFlowSwitch sw;
13 + private final OFPacketIn pktin;
14 + private final OFPacketOut pktout = null;
15 +
16 + private DefaultPacketContext(OpenFlowSwitch s, OFPacketIn pkt) {
17 + this.sw = s;
18 + this.pktin = pkt;
19 + }
20 +
21 + @Override
22 + public void block() {
23 + free = false;
24 + }
25 +
26 + @Override
27 + public void send() {
28 + if (free && isBuilt) {
29 + sw.sendMsg(pktout);
30 + }
31 +
32 + }
33 +
34 + @Override
35 + public void build(OFPort outPort) {
36 + isBuilt = true;
37 +
38 + }
39 +
40 + @Override
41 + public void build(Ethernet ethFrame, OFPort outPort) {
42 + // TODO Auto-generated method stub
43 +
44 + }
45 +
46 + @Override
47 + public Ethernet parsed() {
48 + // TODO Auto-generated method stub
49 + return null;
50 + }
51 +
52 + @Override
53 + public Dpid dpid() {
54 + // TODO Auto-generated method stub
55 + return null;
56 + }
57 +
58 + public static PacketContext PacketContextFromPacketIn(OpenFlowSwitch s, OFPacketIn pkt) {
59 + return new DefaultPacketContext(s, pkt);
60 + }
61 +
62 +}
...@@ -87,9 +87,10 @@ public interface OpenFlowController { ...@@ -87,9 +87,10 @@ public interface OpenFlowController {
87 /** 87 /**
88 * Process a message and notify the appropriate listeners. 88 * Process a message and notify the appropriate listeners.
89 * 89 *
90 + * @param dpid the dpid the message arrived on
90 * @param msg the message to process. 91 * @param msg the message to process.
91 */ 92 */
92 - public void processPacket(OFMessage msg); 93 + public void processPacket(Dpid dpid, OFMessage msg);
93 94
94 /** 95 /**
95 * Sets the role for a given switch. 96 * Sets the role for a given switch.
......
1 package org.onlab.onos.of.controller; 1 package org.onlab.onos.of.controller;
2 2
3 +import org.onlab.packet.Ethernet;
3 import org.projectfloodlight.openflow.types.OFPort; 4 import org.projectfloodlight.openflow.types.OFPort;
4 5
5 /** 6 /**
...@@ -34,13 +35,13 @@ public interface PacketContext { ...@@ -34,13 +35,13 @@ public interface PacketContext {
34 * @param ethFrame the actual packet to send out. 35 * @param ethFrame the actual packet to send out.
35 * @param outPort the out port to send to packet out of. 36 * @param outPort the out port to send to packet out of.
36 */ 37 */
37 - public void build(Object ethFrame, OFPort outPort); 38 + public void build(Ethernet ethFrame, OFPort outPort);
38 39
39 /** 40 /**
40 * Provided a handle onto the parsed payload. 41 * Provided a handle onto the parsed payload.
41 * @return the parsed form of the payload. 42 * @return the parsed form of the payload.
42 */ 43 */
43 - public Object parsed(); 44 + public Ethernet parsed();
44 45
45 /** 46 /**
46 * Provide the dpid of the switch where the packet in arrived. 47 * Provide the dpid of the switch where the packet in arrived.
......
...@@ -159,7 +159,7 @@ public abstract class AbstractOpenFlowSwitch implements OpenFlowSwitchDriver { ...@@ -159,7 +159,7 @@ public abstract class AbstractOpenFlowSwitch implements OpenFlowSwitchDriver {
159 */ 159 */
160 @Override 160 @Override
161 public final void handleMessage(OFMessage m) { 161 public final void handleMessage(OFMessage m) {
162 - this.agent.processMessage(m); 162 + this.agent.processMessage(dpid, m);
163 } 163 }
164 164
165 @Override 165 @Override
......
...@@ -69,7 +69,9 @@ public interface OpenFlowAgent { ...@@ -69,7 +69,9 @@ public interface OpenFlowAgent {
69 69
70 /** 70 /**
71 * Process a message coming from a switch. 71 * Process a message coming from a switch.
72 + *
73 + * @param dpid the dpid the message came on.
72 * @param m the message to process 74 * @param m the message to process
73 */ 75 */
74 - public void processMessage(OFMessage m); 76 + public void processMessage(Dpid dpid, OFMessage m);
75 } 77 }
......
1 -# See: http://rolf-engelhard.de/2011/04/using-the-same-suppression-filter-for-checkstyle-in-eclipse-and-maven/
2 -config_loc=conf/checkstyle
This diff is collapsed. Click to expand it.
1 -<?xml version="1.0" encoding="UTF-8"?>
2 -<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
3 -
4 -<suppressions>
5 - <!--
6 - Note: Exclusion definition exists in multiple places.
7 - - In file ${findbugs.excludeFilterFile} defined at top of pom.xml
8 - - In file conf/checkstyle/onos_suppressions.xml (this file)
9 - - maven-pmd-plugin configuration in pom.xml
10 - (under build and reporting)
11 - -->
12 -
13 - <suppress files=".*" checks="FinalParametersCheck"/>
14 - <suppress files=".*" checks="MagicNumbersCheck"/>
15 - <suppress files=".*" checks="DesignForExtensionCheck"/>
16 - <suppress files=".*" checks="TodoCommentCheck"/>
17 - <suppress files=".*" checks="AvoidInlineConditionalsCheck"/>
18 - <suppress files=".*" checks="OperatorWrapCheck"/>
19 -</suppressions>
20 -
1 -<FindBugsFilter>
2 - <!--
3 - Note: Exclusion definition exists in multiple places.
4 - - In file ${findbugs.excludeFilterFile} defined at top of pom.xml (this file)
5 - - In file conf/checkstyle/onos_suppressions.xml
6 - - maven-pmd-plugin configuration in pom.xml
7 - (under build and reporting)
8 - -->
9 - <Match>
10 - <Class name="~net\.onrc\.onos\.core\.datastore\.serializers\..*" />
11 - </Match>
12 - <Match>
13 - <Class name="~.*edu\.stanford\..*"/>
14 - </Match>
15 - <Match>
16 - <Class name="~.org\.projectfloodlight\..*"/>
17 - </Match>
18 -</FindBugsFilter>
...@@ -6,9 +6,7 @@ import java.util.concurrent.locks.Lock; ...@@ -6,9 +6,7 @@ import java.util.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock; 6 import java.util.concurrent.locks.ReentrantLock;
7 7
8 import org.apache.felix.scr.annotations.Activate; 8 import org.apache.felix.scr.annotations.Activate;
9 -import org.apache.felix.scr.annotations.Component;
10 import org.apache.felix.scr.annotations.Deactivate; 9 import org.apache.felix.scr.annotations.Deactivate;
11 -import org.apache.felix.scr.annotations.Service;
12 import org.onlab.onos.of.controller.Dpid; 10 import org.onlab.onos.of.controller.Dpid;
13 import org.onlab.onos.of.controller.OpenFlowController; 11 import org.onlab.onos.of.controller.OpenFlowController;
14 import org.onlab.onos.of.controller.OpenFlowSwitch; 12 import org.onlab.onos.of.controller.OpenFlowSwitch;
...@@ -17,11 +15,10 @@ import org.onlab.onos.of.controller.PacketListener; ...@@ -17,11 +15,10 @@ import org.onlab.onos.of.controller.PacketListener;
17 import org.onlab.onos.of.controller.RoleState; 15 import org.onlab.onos.of.controller.RoleState;
18 import org.onlab.onos.of.controller.driver.OpenFlowAgent; 16 import org.onlab.onos.of.controller.driver.OpenFlowAgent;
19 import org.projectfloodlight.openflow.protocol.OFMessage; 17 import org.projectfloodlight.openflow.protocol.OFMessage;
18 +import org.projectfloodlight.openflow.protocol.OFPortStatus;
20 import org.slf4j.Logger; 19 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory; 20 import org.slf4j.LoggerFactory;
22 21
23 -@Component(immediate = true)
24 -@Service
25 public class OpenFlowControllerImpl implements OpenFlowController { 22 public class OpenFlowControllerImpl implements OpenFlowController {
26 23
27 private static final Logger log = 24 private static final Logger log =
...@@ -38,6 +35,9 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -38,6 +35,9 @@ public class OpenFlowControllerImpl implements OpenFlowController {
38 protected ArrayList<OpenFlowSwitchListener> ofEventListener = 35 protected ArrayList<OpenFlowSwitchListener> ofEventListener =
39 new ArrayList<OpenFlowSwitchListener>(); 36 new ArrayList<OpenFlowSwitchListener>();
40 37
38 + protected ArrayList<PacketListener> ofPacketListener =
39 + new ArrayList<PacketListener>();
40 +
41 private final Controller ctrl = new Controller(); 41 private final Controller ctrl = new Controller();
42 42
43 @Activate 43 @Activate
...@@ -94,14 +94,12 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -94,14 +94,12 @@ public class OpenFlowControllerImpl implements OpenFlowController {
94 94
95 @Override 95 @Override
96 public void addPacketListener(int priority, PacketListener listener) { 96 public void addPacketListener(int priority, PacketListener listener) {
97 - // TODO Auto-generated method stub 97 + ofPacketListener.add(priority, listener);
98 -
99 } 98 }
100 99
101 @Override 100 @Override
102 public void removePacketListener(PacketListener listener) { 101 public void removePacketListener(PacketListener listener) {
103 - // TODO Auto-generated method stub 102 + ofPacketListener.remove(listener);
104 -
105 } 103 }
106 104
107 @Override 105 @Override
...@@ -110,8 +108,22 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -110,8 +108,22 @@ public class OpenFlowControllerImpl implements OpenFlowController {
110 } 108 }
111 109
112 @Override 110 @Override
113 - public void processPacket(OFMessage msg) { 111 + public void processPacket(Dpid dpid, OFMessage msg) {
114 - log.info("Got message {}", msg); 112 + switch (msg.getType()) {
113 + case PORT_STATUS:
114 + for (OpenFlowSwitchListener l : ofEventListener) {
115 + l.portChanged(dpid, (OFPortStatus) msg);
116 + }
117 + break;
118 + case PACKET_IN:
119 + for (PacketListener p : ofPacketListener) {
120 + //TODO fix me!
121 + p.handlePacket(null);
122 + }
123 + break;
124 + default:
125 + log.warn("Handling message type {} not yet implemented", msg.getType());
126 + }
115 } 127 }
116 128
117 @Override 129 @Override
...@@ -252,8 +264,8 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -252,8 +264,8 @@ public class OpenFlowControllerImpl implements OpenFlowController {
252 } 264 }
253 265
254 @Override 266 @Override
255 - public void processMessage(OFMessage m) { 267 + public void processMessage(Dpid dpid, OFMessage m) {
256 - processPacket(m); 268 + processPacket(dpid, m);
257 } 269 }
258 } 270 }
259 271
......
...@@ -22,6 +22,13 @@ ...@@ -22,6 +22,13 @@
22 <module>drivers</module> 22 <module>drivers</module>
23 </modules> 23 </modules>
24 24
25 + <dependencies>
26 + <dependency>
27 + <groupId>org.onlab.onos</groupId>
28 + <artifactId>onlab-misc</artifactId>
29 + </dependency>
30 + </dependencies>
31 +
25 <build> 32 <build>
26 <plugins> 33 <plugins>
27 <plugin> 34 <plugin>
......
1 package org.onlab.onos.provider.of.link.impl; 1 package org.onlab.onos.provider.of.link.impl;
2 2
3 +import static org.slf4j.LoggerFactory.getLogger;
4 +
3 import org.apache.felix.scr.annotations.Activate; 5 import org.apache.felix.scr.annotations.Activate;
4 import org.apache.felix.scr.annotations.Component; 6 import org.apache.felix.scr.annotations.Component;
5 import org.apache.felix.scr.annotations.Deactivate; 7 import org.apache.felix.scr.annotations.Deactivate;
...@@ -11,10 +13,10 @@ import org.onlab.onos.net.link.LinkProviderService; ...@@ -11,10 +13,10 @@ import org.onlab.onos.net.link.LinkProviderService;
11 import org.onlab.onos.net.provider.AbstractProvider; 13 import org.onlab.onos.net.provider.AbstractProvider;
12 import org.onlab.onos.net.provider.ProviderId; 14 import org.onlab.onos.net.provider.ProviderId;
13 import org.onlab.onos.of.controller.OpenFlowController; 15 import org.onlab.onos.of.controller.OpenFlowController;
16 +import org.onlab.onos.of.controller.PacketContext;
17 +import org.onlab.onos.of.controller.PacketListener;
14 import org.slf4j.Logger; 18 import org.slf4j.Logger;
15 19
16 -import static org.slf4j.LoggerFactory.getLogger;
17 -
18 /** 20 /**
19 * Provider which uses an OpenFlow controller to detect network 21 * Provider which uses an OpenFlow controller to detect network
20 * infrastructure links. 22 * infrastructure links.
...@@ -32,6 +34,8 @@ public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvid ...@@ -32,6 +34,8 @@ public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvid
32 34
33 private LinkProviderService providerService; 35 private LinkProviderService providerService;
34 36
37 + private final PacketListener listener = new InternalLinkProvider();
38 +
35 /** 39 /**
36 * Creates an OpenFlow link provider. 40 * Creates an OpenFlow link provider.
37 */ 41 */
...@@ -42,14 +46,26 @@ public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvid ...@@ -42,14 +46,26 @@ public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvid
42 @Activate 46 @Activate
43 public void activate() { 47 public void activate() {
44 providerService = providerRegistry.register(this); 48 providerService = providerRegistry.register(this);
49 + controller.addPacketListener(0, listener);
45 log.info("Started"); 50 log.info("Started");
46 } 51 }
47 52
48 @Deactivate 53 @Deactivate
49 public void deactivate() { 54 public void deactivate() {
50 providerRegistry.unregister(this); 55 providerRegistry.unregister(this);
56 + controller.removePacketListener(listener);
51 providerService = null; 57 providerService = null;
52 log.info("Stopped"); 58 log.info("Stopped");
53 } 59 }
54 60
61 +
62 + private class InternalLinkProvider implements PacketListener {
63 +
64 + @Override
65 + public void handlePacket(PacketContext pktCtx) {
66 +
67 + }
68 +
69 + }
70 +
55 } 71 }
......
...@@ -25,5 +25,7 @@ ...@@ -25,5 +25,7 @@
25 <suppress files=".*" checks="TodoCommentCheck"/> 25 <suppress files=".*" checks="TodoCommentCheck"/>
26 <suppress files=".*" checks="AvoidInlineConditionalsCheck"/> 26 <suppress files=".*" checks="AvoidInlineConditionalsCheck"/>
27 <suppress files=".*" checks="OperatorWrapCheck"/> 27 <suppress files=".*" checks="OperatorWrapCheck"/>
28 + <suppress files=".*" checks="HiddenField"/>
29 +
28 </suppressions> 30 </suppressions>
29 31
......
This diff is collapsed. Click to expand it.
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +/**
36 + *
37 + * @author David Erickson (daviderickson@cs.stanford.edu)
38 + */
39 +public abstract class BasePacket implements IPacket {
40 + protected IPacket parent;
41 + protected IPacket payload;
42 +
43 + /**
44 + * @return the parent
45 + */
46 + @Override
47 + public IPacket getParent() {
48 + return this.parent;
49 + }
50 +
51 + /**
52 + * @param parent
53 + * the parent to set
54 + */
55 + @Override
56 + public IPacket setParent(final IPacket parent) {
57 + this.parent = parent;
58 + return this;
59 + }
60 +
61 + /**
62 + * @return the payload
63 + */
64 + @Override
65 + public IPacket getPayload() {
66 + return this.payload;
67 + }
68 +
69 + /**
70 + * @param payload
71 + * the payload to set
72 + */
73 + @Override
74 + public IPacket setPayload(final IPacket payload) {
75 + this.payload = payload;
76 + return this;
77 + }
78 +
79 + @Override
80 + public void resetChecksum() {
81 + if (this.parent != null) {
82 + this.parent.resetChecksum();
83 + }
84 + }
85 +
86 + /*
87 + * (non-Javadoc)
88 + *
89 + * @see java.lang.Object#hashCode()
90 + */
91 + @Override
92 + public int hashCode() {
93 + final int prime = 6733;
94 + int result = 1;
95 + result = prime * result
96 + + (this.payload == null ? 0 : this.payload.hashCode());
97 + return result;
98 + }
99 +
100 + /*
101 + * (non-Javadoc)
102 + *
103 + * @see java.lang.Object#equals(java.lang.Object)
104 + */
105 + @Override
106 + public boolean equals(final Object obj) {
107 + if (this == obj) {
108 + return true;
109 + }
110 + if (obj == null) {
111 + return false;
112 + }
113 + if (!(obj instanceof BasePacket)) {
114 + return false;
115 + }
116 + final BasePacket other = (BasePacket) obj;
117 + if (this.payload == null) {
118 + if (other.payload != null) {
119 + return false;
120 + }
121 + } else if (!this.payload.equals(other.payload)) {
122 + return false;
123 + }
124 + return true;
125 + }
126 +
127 + @Override
128 + public Object clone() {
129 + IPacket pkt;
130 + try {
131 + pkt = this.getClass().newInstance();
132 + } catch (final Exception e) {
133 + throw new RuntimeException("Could not clone packet");
134 + }
135 + // TODO: we are using serialize()/deserialize() to perform the
136 + // cloning. Not the most efficient way but simple. We can revisit
137 + // if we hit performance problems.
138 + final byte[] data = this.serialize();
139 + pkt.deserialize(this.serialize(), 0, data.length);
140 + pkt.setParent(this.parent);
141 + return pkt;
142 + }
143 +}
This diff is collapsed. Click to expand it.
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.util.Arrays;
36 +
37 +/**
38 + *
39 + * @author David Erickson (daviderickson@cs.stanford.edu)
40 + */
41 +public class DHCPOption {
42 + protected byte code;
43 + protected byte length;
44 + protected byte[] data;
45 +
46 + /**
47 + * @return the code
48 + */
49 + public byte getCode() {
50 + return this.code;
51 + }
52 +
53 + /**
54 + * @param code
55 + * the code to set
56 + */
57 + public DHCPOption setCode(final byte code) {
58 + this.code = code;
59 + return this;
60 + }
61 +
62 + /**
63 + * @return the length
64 + */
65 + public byte getLength() {
66 + return this.length;
67 + }
68 +
69 + /**
70 + * @param length
71 + * the length to set
72 + */
73 + public DHCPOption setLength(final byte length) {
74 + this.length = length;
75 + return this;
76 + }
77 +
78 + /**
79 + * @return the data
80 + */
81 + public byte[] getData() {
82 + return this.data;
83 + }
84 +
85 + /**
86 + * @param data
87 + * the data to set
88 + */
89 + public DHCPOption setData(final byte[] data) {
90 + this.data = data;
91 + return this;
92 + }
93 +
94 + /*
95 + * (non-Javadoc)
96 + *
97 + * @see java.lang.Object#hashCode()
98 + */
99 + @Override
100 + public int hashCode() {
101 + final int prime = 31;
102 + int result = 1;
103 + result = prime * result + this.code;
104 + result = prime * result + Arrays.hashCode(this.data);
105 + result = prime * result + this.length;
106 + return result;
107 + }
108 +
109 + /*
110 + * (non-Javadoc)
111 + *
112 + * @see java.lang.Object#equals(java.lang.Object)
113 + */
114 + @Override
115 + public boolean equals(final Object obj) {
116 + if (this == obj) {
117 + return true;
118 + }
119 + if (obj == null) {
120 + return false;
121 + }
122 + if (!(obj instanceof DHCPOption)) {
123 + return false;
124 + }
125 + final DHCPOption other = (DHCPOption) obj;
126 + if (this.code != other.code) {
127 + return false;
128 + }
129 + if (!Arrays.equals(this.data, other.data)) {
130 + return false;
131 + }
132 + if (this.length != other.length) {
133 + return false;
134 + }
135 + return true;
136 + }
137 +
138 + /*
139 + * (non-Javadoc)
140 + *
141 + * @see java.lang.Object#toString()
142 + */
143 + @Override
144 + public String toString() {
145 + return "DHCPOption [code=" + this.code + ", length=" + this.length
146 + + ", data=" + Arrays.toString(this.data) + "]";
147 + }
148 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +public enum DHCPPacketType {
36 + // From RFC 1533
37 + DHCPDISCOVER(1), DHCPOFFER(2), DHCPREQUEST(3), DHCPDECLINE(4), DHCPACK(5), DHCPNAK(
38 + 6), DHCPRELEASE(7),
39 +
40 + // From RFC2132
41 + DHCPINFORM(8),
42 +
43 + // From RFC3203
44 + DHCPFORCERENEW(9),
45 +
46 + // From RFC4388
47 + DHCPLEASEQUERY(10), DHCPLEASEUNASSIGNED(11), DHCPLEASEUNKNOWN(12), DHCPLEASEACTIVE(
48 + 13);
49 +
50 + protected int value;
51 +
52 + private DHCPPacketType(final int value) {
53 + this.value = value;
54 + }
55 +
56 + public int getValue() {
57 + return this.value;
58 + }
59 +
60 + @Override
61 + public String toString() {
62 + switch (this.value) {
63 + case 1:
64 + return "DHCPDISCOVER";
65 + case 2:
66 + return "DHCPOFFER";
67 + case 3:
68 + return "DHCPREQUEST";
69 + case 4:
70 + return "DHCPDECLINE";
71 + case 5:
72 + return "DHCPACK";
73 + case 6:
74 + return "DHCPNAK";
75 + case 7:
76 + return "DHCPRELEASE";
77 + case 8:
78 + return "DHCPINFORM";
79 + case 9:
80 + return "DHCPFORCERENEW";
81 + case 10:
82 + return "DHCPLEASEQUERY";
83 + case 11:
84 + return "DHCPLEASEUNASSIGNED";
85 + case 12:
86 + return "DHCPLEASEUNKNOWN";
87 + case 13:
88 + return "DHCPLEASEACTIVE";
89 + default:
90 + break;
91 + }
92 +
93 + return null;
94 + }
95 +
96 + public static DHCPPacketType getType(final int value) {
97 + switch (value) {
98 + case 1:
99 + return DHCPDISCOVER;
100 + case 2:
101 + return DHCPOFFER;
102 + case 3:
103 + return DHCPREQUEST;
104 + case 4:
105 + return DHCPDECLINE;
106 + case 5:
107 + return DHCPACK;
108 + case 6:
109 + return DHCPNAK;
110 + case 7:
111 + return DHCPRELEASE;
112 + case 8:
113 + return DHCPINFORM;
114 + case 9:
115 + return DHCPFORCERENEW;
116 + case 10:
117 + return DHCPLEASEQUERY;
118 + case 11:
119 + return DHCPLEASEUNASSIGNED;
120 + case 12:
121 + return DHCPLEASEUNKNOWN;
122 + case 13:
123 + return DHCPLEASEACTIVE;
124 + default:
125 + break;
126 + }
127 +
128 + return null;
129 + }
130 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.util.Arrays;
36 +
37 +/**
38 + *
39 + * @author David Erickson (daviderickson@cs.stanford.edu)
40 + */
41 +public class Data extends BasePacket {
42 + protected byte[] data;
43 +
44 + /**
45 + *
46 + */
47 + public Data() {
48 + }
49 +
50 + /**
51 + * @param data
52 + */
53 + public Data(final byte[] data) {
54 + this.data = data;
55 + }
56 +
57 + /**
58 + * @return the data
59 + */
60 + public byte[] getData() {
61 + return this.data;
62 + }
63 +
64 + /**
65 + * @param data
66 + * the data to set
67 + */
68 + public Data setData(final byte[] data) {
69 + this.data = data;
70 + return this;
71 + }
72 +
73 + @Override
74 + public byte[] serialize() {
75 + return this.data;
76 + }
77 +
78 + @Override
79 + public IPacket deserialize(final byte[] data, final int offset,
80 + final int length) {
81 + this.data = Arrays.copyOfRange(data, offset, data.length);
82 + return this;
83 + }
84 +
85 + /*
86 + * (non-Javadoc)
87 + *
88 + * @see java.lang.Object#hashCode()
89 + */
90 + @Override
91 + public int hashCode() {
92 + final int prime = 1571;
93 + int result = super.hashCode();
94 + result = prime * result + Arrays.hashCode(this.data);
95 + return result;
96 + }
97 +
98 + /*
99 + * (non-Javadoc)
100 + *
101 + * @see java.lang.Object#equals(java.lang.Object)
102 + */
103 + @Override
104 + public boolean equals(final Object obj) {
105 + if (this == obj) {
106 + return true;
107 + }
108 + if (!super.equals(obj)) {
109 + return false;
110 + }
111 + if (!(obj instanceof Data)) {
112 + return false;
113 + }
114 + final Data other = (Data) obj;
115 + if (!Arrays.equals(this.data, other.data)) {
116 + return false;
117 + }
118 + return true;
119 + }
120 +}
This diff is collapsed. Click to expand it.
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.nio.ByteBuffer;
36 +
37 +/**
38 + * Implements ICMP packet format.
39 + *
40 + * @author shudong.zhou@bigswitch.com
41 + */
42 +public class ICMP extends BasePacket {
43 + protected byte icmpType;
44 + protected byte icmpCode;
45 + protected short checksum;
46 +
47 + /**
48 + * @return the icmpType
49 + */
50 + public byte getIcmpType() {
51 + return this.icmpType;
52 + }
53 +
54 + /**
55 + * @param icmpType
56 + * to set
57 + */
58 + public ICMP setIcmpType(final byte icmpType) {
59 + this.icmpType = icmpType;
60 + return this;
61 + }
62 +
63 + /**
64 + * @return the icmp code
65 + */
66 + public byte getIcmpCode() {
67 + return this.icmpCode;
68 + }
69 +
70 + /**
71 + * @param icmpCode
72 + * code to set
73 + */
74 + public ICMP setIcmpCode(final byte icmpCode) {
75 + this.icmpCode = icmpCode;
76 + return this;
77 + }
78 +
79 + /**
80 + * @return the checksum
81 + */
82 + public short getChecksum() {
83 + return this.checksum;
84 + }
85 +
86 + /**
87 + * @param checksum
88 + * the checksum to set
89 + */
90 + public ICMP setChecksum(final short checksum) {
91 + this.checksum = checksum;
92 + return this;
93 + }
94 +
95 + /**
96 + * Serializes the packet. Will compute and set the following fields if they
97 + * are set to specific values at the time serialize is called: -checksum : 0
98 + * -length : 0
99 + */
100 + @Override
101 + public byte[] serialize() {
102 + int length = 4;
103 + byte[] payloadData = null;
104 + if (this.payload != null) {
105 + this.payload.setParent(this);
106 + payloadData = this.payload.serialize();
107 + length += payloadData.length;
108 + }
109 +
110 + final byte[] data = new byte[length];
111 + final ByteBuffer bb = ByteBuffer.wrap(data);
112 +
113 + bb.put(this.icmpType);
114 + bb.put(this.icmpCode);
115 + bb.putShort(this.checksum);
116 + if (payloadData != null) {
117 + bb.put(payloadData);
118 + }
119 +
120 + if (this.parent != null && this.parent instanceof IPv4) {
121 + ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP);
122 + }
123 +
124 + // compute checksum if needed
125 + if (this.checksum == 0) {
126 + bb.rewind();
127 + int accumulation = 0;
128 +
129 + for (int i = 0; i < length / 2; ++i) {
130 + accumulation += 0xffff & bb.getShort();
131 + }
132 + // pad to an even number of shorts
133 + if (length % 2 > 0) {
134 + accumulation += (bb.get() & 0xff) << 8;
135 + }
136 +
137 + accumulation = (accumulation >> 16 & 0xffff)
138 + + (accumulation & 0xffff);
139 + this.checksum = (short) (~accumulation & 0xffff);
140 + bb.putShort(2, this.checksum);
141 + }
142 + return data;
143 + }
144 +
145 + /*
146 + * (non-Javadoc)
147 + *
148 + * @see java.lang.Object#hashCode()
149 + */
150 + @Override
151 + public int hashCode() {
152 + final int prime = 5807;
153 + int result = super.hashCode();
154 + result = prime * result + this.icmpType;
155 + result = prime * result + this.icmpCode;
156 + result = prime * result + this.checksum;
157 + return result;
158 + }
159 +
160 + /*
161 + * (non-Javadoc)
162 + *
163 + * @see java.lang.Object#equals(java.lang.Object)
164 + */
165 + @Override
166 + public boolean equals(final Object obj) {
167 + if (this == obj) {
168 + return true;
169 + }
170 + if (!super.equals(obj)) {
171 + return false;
172 + }
173 + if (!(obj instanceof ICMP)) {
174 + return false;
175 + }
176 + final ICMP other = (ICMP) obj;
177 + if (this.icmpType != other.icmpType) {
178 + return false;
179 + }
180 + if (this.icmpCode != other.icmpCode) {
181 + return false;
182 + }
183 + if (this.checksum != other.checksum) {
184 + return false;
185 + }
186 + return true;
187 + }
188 +
189 + @Override
190 + public IPacket deserialize(final byte[] data, final int offset,
191 + final int length) {
192 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
193 + this.icmpType = bb.get();
194 + this.icmpCode = bb.get();
195 + this.checksum = bb.getShort();
196 +
197 + this.payload = new Data();
198 + this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
199 + - bb.position());
200 + this.payload.setParent(this);
201 + return this;
202 + }
203 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +/**
36 + *
37 + * @author David Erickson (daviderickson@cs.stanford.edu)
38 + */
39 +public interface IPacket {
40 + /**
41 + *
42 + * @return
43 + */
44 + public IPacket getPayload();
45 +
46 + /**
47 + *
48 + * @param packet
49 + * @return
50 + */
51 + public IPacket setPayload(IPacket packet);
52 +
53 + /**
54 + *
55 + * @return
56 + */
57 + public IPacket getParent();
58 +
59 + /**
60 + *
61 + * @param packet
62 + * @return
63 + */
64 + public IPacket setParent(IPacket packet);
65 +
66 + /**
67 + * Reset any checksums as needed, and call resetChecksum on all parents.
68 + */
69 + public void resetChecksum();
70 +
71 + /**
72 + * Sets all payloads parent packet if applicable, then serializes this
73 + * packet and all payloads.
74 + *
75 + * @return a byte[] containing this packet and payloads
76 + */
77 + public byte[] serialize();
78 +
79 + /**
80 + * Deserializes this packet layer and all possible payloads.
81 + *
82 + * @param data
83 + * @param offset
84 + * offset to start deserializing from
85 + * @param length
86 + * length of the data to deserialize
87 + * @return the deserialized data
88 + */
89 + public IPacket deserialize(byte[] data, int offset, int length);
90 +
91 + /**
92 + * Clone this packet and its payload packet but not its parent.
93 + *
94 + * @return
95 + */
96 + public Object clone();
97 +}
This diff is collapsed. Click to expand it.
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.nio.ByteBuffer;
36 +
37 +/**
38 + * This class represents an Link Local Control header that is used in Ethernet
39 + * 802.3.
40 + *
41 + * @author alexreimers
42 + *
43 + */
44 +public class LLC extends BasePacket {
45 + private byte dsap = 0;
46 + private byte ssap = 0;
47 + private byte ctrl = 0;
48 +
49 + public byte getDsap() {
50 + return this.dsap;
51 + }
52 +
53 + public void setDsap(final byte dsap) {
54 + this.dsap = dsap;
55 + }
56 +
57 + public byte getSsap() {
58 + return this.ssap;
59 + }
60 +
61 + public void setSsap(final byte ssap) {
62 + this.ssap = ssap;
63 + }
64 +
65 + public byte getCtrl() {
66 + return this.ctrl;
67 + }
68 +
69 + public void setCtrl(final byte ctrl) {
70 + this.ctrl = ctrl;
71 + }
72 +
73 + @Override
74 + public byte[] serialize() {
75 + final byte[] data = new byte[3];
76 + final ByteBuffer bb = ByteBuffer.wrap(data);
77 + bb.put(this.dsap);
78 + bb.put(this.ssap);
79 + bb.put(this.ctrl);
80 + return data;
81 + }
82 +
83 + @Override
84 + public IPacket deserialize(final byte[] data, final int offset,
85 + final int length) {
86 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
87 + this.dsap = bb.get();
88 + this.ssap = bb.get();
89 + this.ctrl = bb.get();
90 + return this;
91 + }
92 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +/**
34 + *
35 + */
36 +package org.onlab.packet;
37 +
38 +import java.nio.ByteBuffer;
39 +import java.util.ArrayList;
40 +import java.util.List;
41 +
42 +/**
43 + * @author David Erickson (daviderickson@cs.stanford.edu)
44 + *
45 + */
46 +public class LLDP extends BasePacket {
47 + protected LLDPTLV chassisId;
48 + protected LLDPTLV portId;
49 + protected LLDPTLV ttl;
50 + protected List<LLDPTLV> optionalTLVList;
51 + protected short ethType;
52 +
53 + public LLDP() {
54 + this.optionalTLVList = new ArrayList<LLDPTLV>();
55 + this.ethType = Ethernet.TYPE_LLDP;
56 + }
57 +
58 + /**
59 + * @return the chassisId
60 + */
61 + public LLDPTLV getChassisId() {
62 + return this.chassisId;
63 + }
64 +
65 + /**
66 + * @param chassisId
67 + * the chassisId to set
68 + */
69 + public LLDP setChassisId(final LLDPTLV chassis) {
70 + this.chassisId = chassis;
71 + return this;
72 + }
73 +
74 + /**
75 + * @return the portId
76 + */
77 + public LLDPTLV getPortId() {
78 + return this.portId;
79 + }
80 +
81 + /**
82 + * @param portId
83 + * the portId to set
84 + */
85 + public LLDP setPortId(final LLDPTLV portId) {
86 + this.portId = portId;
87 + return this;
88 + }
89 +
90 + /**
91 + * @return the ttl
92 + */
93 + public LLDPTLV getTtl() {
94 + return this.ttl;
95 + }
96 +
97 + /**
98 + * @param ttl
99 + * the ttl to set
100 + */
101 + public LLDP setTtl(final LLDPTLV ttl) {
102 + this.ttl = ttl;
103 + return this;
104 + }
105 +
106 + /**
107 + * @return the optionalTLVList
108 + */
109 + public List<LLDPTLV> getOptionalTLVList() {
110 + return this.optionalTLVList;
111 + }
112 +
113 + /**
114 + * @param optionalTLVList
115 + * the optionalTLVList to set
116 + */
117 + public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
118 + this.optionalTLVList = optionalTLVList;
119 + return this;
120 + }
121 +
122 + @Override
123 + public byte[] serialize() {
124 + int length = 2 + this.chassisId.getLength() + 2
125 + + this.portId.getLength() + 2 + this.ttl.getLength() + 2;
126 + for (final LLDPTLV tlv : this.optionalTLVList) {
127 + length += 2 + tlv.getLength();
128 + }
129 +
130 + final byte[] data = new byte[length];
131 + final ByteBuffer bb = ByteBuffer.wrap(data);
132 + bb.put(this.chassisId.serialize());
133 + bb.put(this.portId.serialize());
134 + bb.put(this.ttl.serialize());
135 + for (final LLDPTLV tlv : this.optionalTLVList) {
136 + bb.put(tlv.serialize());
137 + }
138 + bb.putShort((short) 0); // End of LLDPDU
139 +
140 + /*
141 + * if (this.parent != null && this.parent instanceof Ethernet) {
142 + * ((Ethernet) this.parent).setEtherType(this.ethType); }
143 + */
144 +
145 + return data;
146 + }
147 +
148 + @Override
149 + public IPacket deserialize(final byte[] data, final int offset,
150 + final int length) {
151 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
152 + LLDPTLV tlv;
153 + do {
154 + tlv = new LLDPTLV().deserialize(bb);
155 +
156 + // if there was a failure to deserialize stop processing TLVs
157 + if (tlv == null) {
158 + break;
159 + }
160 + switch (tlv.getType()) {
161 + case 0x0:
162 + // can throw this one away, its just an end delimiter
163 + break;
164 + case 0x1:
165 + this.chassisId = tlv;
166 + break;
167 + case 0x2:
168 + this.portId = tlv;
169 + break;
170 + case 0x3:
171 + this.ttl = tlv;
172 + break;
173 + default:
174 + this.optionalTLVList.add(tlv);
175 + break;
176 + }
177 + } while (tlv.getType() != 0 && bb.hasRemaining());
178 + return this;
179 + }
180 +
181 + /*
182 + * (non-Javadoc)
183 + *
184 + * @see java.lang.Object#hashCode()
185 + */
186 + @Override
187 + public int hashCode() {
188 + final int prime = 883;
189 + int result = super.hashCode();
190 + result = prime * result
191 + + (this.chassisId == null ? 0 : this.chassisId.hashCode());
192 + result = prime * result + this.optionalTLVList.hashCode();
193 + result = prime * result
194 + + (this.portId == null ? 0 : this.portId.hashCode());
195 + result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode());
196 + return result;
197 + }
198 +
199 + /*
200 + * (non-Javadoc)
201 + *
202 + * @see java.lang.Object#equals(java.lang.Object)
203 + */
204 + @Override
205 + public boolean equals(final Object obj) {
206 + if (this == obj) {
207 + return true;
208 + }
209 + if (!super.equals(obj)) {
210 + return false;
211 + }
212 + if (!(obj instanceof LLDP)) {
213 + return false;
214 + }
215 + final LLDP other = (LLDP) obj;
216 + if (this.chassisId == null) {
217 + if (other.chassisId != null) {
218 + return false;
219 + }
220 + } else if (!this.chassisId.equals(other.chassisId)) {
221 + return false;
222 + }
223 + if (!this.optionalTLVList.equals(other.optionalTLVList)) {
224 + return false;
225 + }
226 + if (this.portId == null) {
227 + if (other.portId != null) {
228 + return false;
229 + }
230 + } else if (!this.portId.equals(other.portId)) {
231 + return false;
232 + }
233 + if (this.ttl == null) {
234 + if (other.ttl != null) {
235 + return false;
236 + }
237 + } else if (!this.ttl.equals(other.ttl)) {
238 + return false;
239 + }
240 + return true;
241 + }
242 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 + * not use this file except in compliance with the License. You may obtain
19 + * a copy of the License at
20 + *
21 + * http://www.apache.org/licenses/LICENSE-2.0
22 + *
23 + * Unless required by applicable law or agreed to in writing, software
24 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26 + * License for the specific language governing permissions and limitations
27 + * under the License.
28 + **/
29 +
30 +package org.onlab.packet;
31 +
32 +import java.nio.ByteBuffer;
33 +import java.nio.charset.Charset;
34 +import java.util.Arrays;
35 +
36 +/**
37 + * The class representing LLDP Organizationally Specific TLV.
38 + *
39 + * @author Sho Shimizu (sho.shimizu@gmail.com)
40 + */
41 +public class LLDPOrganizationalTLV extends LLDPTLV {
42 + public static final int OUI_LENGTH = 3;
43 + public static final int SUBTYPE_LENGTH = 1;
44 + public static final byte ORGANIZATIONAL_TLV_TYPE = 127;
45 + public static final int MAX_INFOSTRING_LENGTH = 507;
46 +
47 + protected byte[] oui;
48 + protected byte subType;
49 + private byte[] infoString;
50 +
51 + public LLDPOrganizationalTLV() {
52 + this.type = LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE;
53 + }
54 +
55 + /**
56 + * Set the value of OUI.
57 + *
58 + * @param oui
59 + * The value of OUI to be set.
60 + * @return This LLDP Organizationally Specific TLV.
61 + */
62 + public LLDPOrganizationalTLV setOUI(final byte[] oui) {
63 + if (oui.length != LLDPOrganizationalTLV.OUI_LENGTH) {
64 + throw new IllegalArgumentException("The length of OUI must be "
65 + + LLDPOrganizationalTLV.OUI_LENGTH + ", but it is "
66 + + oui.length);
67 + }
68 + this.oui = Arrays.copyOf(oui, oui.length);
69 + return this;
70 + }
71 +
72 + /**
73 + * Returns the value of the OUI.
74 + *
75 + * @return The value of the OUI .
76 + */
77 + public byte[] getOUI() {
78 + return Arrays.copyOf(this.oui, this.oui.length);
79 + }
80 +
81 + /**
82 + * Set the value of sub type.
83 + *
84 + * @param subType
85 + * The value of sub type to be set.
86 + * @return This LLDP Organizationally Specific TLV.
87 + */
88 + public LLDPOrganizationalTLV setSubType(final byte subType) {
89 + this.subType = subType;
90 + return this;
91 + }
92 +
93 + /**
94 + * Returns the value of the sub type.
95 + *
96 + * @return The value of the sub type.
97 + */
98 + public byte getSubType() {
99 + return this.subType;
100 + }
101 +
102 + /**
103 + * Set the value of information string.
104 + *
105 + * @param infoString
106 + * the byte array of the value of information string.
107 + * @return This LLDP Organizationally Specific TLV.
108 + */
109 + public LLDPOrganizationalTLV setInfoString(final byte[] infoString) {
110 + if (infoString.length > LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH) {
111 + throw new IllegalArgumentException(
112 + "The length of infoString cannot exceed "
113 + + LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH);
114 + }
115 + this.infoString = Arrays.copyOf(infoString, infoString.length);
116 + return this;
117 + }
118 +
119 + /**
120 + * Set the value of information string. The String value is automatically
121 + * converted into byte array with UTF-8 encoding.
122 + *
123 + * @param infoString
124 + * the String value of information string.
125 + * @return This LLDP Organizationally Specific TLV.
126 + */
127 + public LLDPOrganizationalTLV setInfoString(final String infoString) {
128 + final byte[] infoStringBytes = infoString.getBytes(Charset
129 + .forName("UTF-8"));
130 + return this.setInfoString(infoStringBytes);
131 + }
132 +
133 + /**
134 + * Returns the value of information string.
135 + *
136 + * @return the value of information string.
137 + */
138 + public byte[] getInfoString() {
139 + return Arrays.copyOf(this.infoString, this.infoString.length);
140 + }
141 +
142 + @Override
143 + public byte[] serialize() {
144 + final int valueLength = LLDPOrganizationalTLV.OUI_LENGTH
145 + + LLDPOrganizationalTLV.SUBTYPE_LENGTH + this.infoString.length;
146 + this.value = new byte[valueLength];
147 + final ByteBuffer bb = ByteBuffer.wrap(this.value);
148 + bb.put(this.oui);
149 + bb.put(this.subType);
150 + bb.put(this.infoString);
151 + return super.serialize();
152 + }
153 +
154 + @Override
155 + public LLDPTLV deserialize(final ByteBuffer bb) {
156 + super.deserialize(bb);
157 + final ByteBuffer optionalField = ByteBuffer.wrap(this.value);
158 +
159 + final byte[] oui = new byte[LLDPOrganizationalTLV.OUI_LENGTH];
160 + optionalField.get(oui);
161 + this.setOUI(oui);
162 +
163 + this.setSubType(optionalField.get());
164 +
165 + final byte[] infoString = new byte[this.getLength()
166 + - LLDPOrganizationalTLV.OUI_LENGTH
167 + - LLDPOrganizationalTLV.SUBTYPE_LENGTH];
168 + optionalField.get(infoString);
169 + this.setInfoString(infoString);
170 + return this;
171 + }
172 +
173 + @Override
174 + public int hashCode() {
175 + final int prime = 1423;
176 + int result = 1;
177 + result = prime * result + this.type;
178 + result = prime * result + this.length;
179 + result = prime * result + Arrays.hashCode(this.oui);
180 + result = prime * result + this.subType;
181 + result = prime * result + Arrays.hashCode(this.infoString);
182 + return result;
183 + }
184 +
185 + @Override
186 + public boolean equals(final Object o) {
187 + if (o == this) {
188 + return true;
189 + }
190 +
191 + if (!(o instanceof LLDPOrganizationalTLV)) {
192 + return false;
193 + }
194 +
195 + final LLDPOrganizationalTLV other = (LLDPOrganizationalTLV) o;
196 + if (this.type != other.type) {
197 + return false;
198 + }
199 + if (this.length != other.length) {
200 + return false;
201 + }
202 + if (!Arrays.equals(this.oui, other.oui)) {
203 + return false;
204 + }
205 + if (this.subType != other.subType) {
206 + return false;
207 + }
208 + if (!Arrays.equals(this.infoString, other.infoString)) {
209 + return false;
210 + }
211 +
212 + return true;
213 + }
214 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.nio.ByteBuffer;
36 +import java.util.Arrays;
37 +
38 +/**
39 + *
40 + *
41 + * @author David Erickson (daviderickson@cs.stanford.edu)
42 + */
43 +public class LLDPTLV {
44 + protected byte type;
45 + protected short length;
46 + protected byte[] value;
47 +
48 + /**
49 + * @return the type
50 + */
51 + public byte getType() {
52 + return this.type;
53 + }
54 +
55 + /**
56 + * @param type
57 + * the type to set
58 + */
59 + public LLDPTLV setType(final byte type) {
60 + this.type = type;
61 + return this;
62 + }
63 +
64 + /**
65 + * @return the length
66 + */
67 + public short getLength() {
68 + return this.length;
69 + }
70 +
71 + /**
72 + * @param length
73 + * the length to set
74 + */
75 + public LLDPTLV setLength(final short length) {
76 + this.length = length;
77 + return this;
78 + }
79 +
80 + /**
81 + * @return the value
82 + */
83 + public byte[] getValue() {
84 + return this.value;
85 + }
86 +
87 + /**
88 + * @param value
89 + * the value to set
90 + */
91 + public LLDPTLV setValue(final byte[] value) {
92 + this.value = value;
93 + return this;
94 + }
95 +
96 + public byte[] serialize() {
97 + // type = 7 bits
98 + // info string length 9 bits, each value == byte
99 + // info string
100 + final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
101 + final byte[] data = new byte[2 + this.length];
102 + final ByteBuffer bb = ByteBuffer.wrap(data);
103 + bb.putShort(scratch);
104 + if (this.value != null) {
105 + bb.put(this.value);
106 + }
107 + return data;
108 + }
109 +
110 + public LLDPTLV deserialize(final ByteBuffer bb) {
111 + short sscratch;
112 + sscratch = bb.getShort();
113 + this.type = (byte) (sscratch >> 9 & 0x7f);
114 + this.length = (short) (sscratch & 0x1ff);
115 + if (this.length > 0) {
116 + this.value = new byte[this.length];
117 +
118 + // if there is an underrun just toss the TLV
119 + if (bb.remaining() < this.length) {
120 + return null;
121 + }
122 + bb.get(this.value);
123 + }
124 + return this;
125 + }
126 +
127 + /*
128 + * (non-Javadoc)
129 + *
130 + * @see java.lang.Object#hashCode()
131 + */
132 + @Override
133 + public int hashCode() {
134 + final int prime = 1423;
135 + int result = 1;
136 + result = prime * result + this.length;
137 + result = prime * result + this.type;
138 + result = prime * result + Arrays.hashCode(this.value);
139 + return result;
140 + }
141 +
142 + /*
143 + * (non-Javadoc)
144 + *
145 + * @see java.lang.Object#equals(java.lang.Object)
146 + */
147 + @Override
148 + public boolean equals(final Object obj) {
149 + if (this == obj) {
150 + return true;
151 + }
152 + if (obj == null) {
153 + return false;
154 + }
155 + if (!(obj instanceof LLDPTLV)) {
156 + return false;
157 + }
158 + final LLDPTLV other = (LLDPTLV) obj;
159 + if (this.length != other.length) {
160 + return false;
161 + }
162 + if (this.type != other.type) {
163 + return false;
164 + }
165 + if (!Arrays.equals(this.value, other.value)) {
166 + return false;
167 + }
168 + return true;
169 + }
170 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +package org.onlab.packet;
17 +
18 +import java.util.Arrays;
19 +
20 +/**
21 + * The class representing MAC address.
22 + *
23 + * @author Sho Shimizu (sho.shimizu@gmail.com)
24 + */
25 +public class MACAddress {
26 + public static final int MAC_ADDRESS_LENGTH = 6;
27 + private byte[] address = new byte[MACAddress.MAC_ADDRESS_LENGTH];
28 +
29 + public MACAddress(final byte[] address) {
30 + this.address = Arrays.copyOf(address, MACAddress.MAC_ADDRESS_LENGTH);
31 + }
32 +
33 + /**
34 + * Returns a MAC address instance representing the value of the specified
35 + * {@code String}.
36 + *
37 + * @param address
38 + * the String representation of the MAC Address to be parsed.
39 + * @return a MAC Address instance representing the value of the specified
40 + * {@code String}.
41 + * @throws IllegalArgumentException
42 + * if the string cannot be parsed as a MAC address.
43 + */
44 + public static MACAddress valueOf(final String address) {
45 + final String[] elements = address.split(":");
46 + if (elements.length != MACAddress.MAC_ADDRESS_LENGTH) {
47 + throw new IllegalArgumentException(
48 + "Specified MAC Address must contain 12 hex digits"
49 + + " separated pairwise by :'s.");
50 + }
51 +
52 + final byte[] addressInBytes = new byte[MACAddress.MAC_ADDRESS_LENGTH];
53 + for (int i = 0; i < MACAddress.MAC_ADDRESS_LENGTH; i++) {
54 + final String element = elements[i];
55 + addressInBytes[i] = (byte) Integer.parseInt(element, 16);
56 + }
57 +
58 + return new MACAddress(addressInBytes);
59 + }
60 +
61 + /**
62 + * Returns a MAC address instance representing the specified {@code byte}
63 + * array.
64 + *
65 + * @param address
66 + * the byte array to be parsed.
67 + * @return a MAC address instance representing the specified {@code byte}
68 + * array.
69 + * @throws IllegalArgumentException
70 + * if the byte array cannot be parsed as a MAC address.
71 + */
72 + public static MACAddress valueOf(final byte[] address) {
73 + if (address.length != MACAddress.MAC_ADDRESS_LENGTH) {
74 + throw new IllegalArgumentException("the length is not "
75 + + MACAddress.MAC_ADDRESS_LENGTH);
76 + }
77 +
78 + return new MACAddress(address);
79 + }
80 +
81 + /**
82 + * Returns a MAC address instance representing the specified {@code long}
83 + * value. The lower 48 bits of the long value are used to parse as a MAC
84 + * address.
85 + *
86 + * @param address
87 + * the long value to be parsed. The lower 48 bits are used for a
88 + * MAC address.
89 + * @return a MAC address instance representing the specified {@code long}
90 + * value.
91 + * @throws IllegalArgumentException
92 + * if the long value cannot be parsed as a MAC address.
93 + */
94 + public static MACAddress valueOf(final long address) {
95 + final byte[] addressInBytes = new byte[] {
96 + (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
97 + (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
98 + (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff) };
99 +
100 + return new MACAddress(addressInBytes);
101 + }
102 +
103 + /**
104 + * Returns the length of the {@code MACAddress}.
105 + *
106 + * @return the length of the {@code MACAddress}.
107 + */
108 + public int length() {
109 + return this.address.length;
110 + }
111 +
112 + /**
113 + * Returns the value of the {@code MACAddress} as a {@code byte} array.
114 + *
115 + * @return the numeric value represented by this object after conversion to
116 + * type {@code byte} array.
117 + */
118 + public byte[] toBytes() {
119 + return Arrays.copyOf(this.address, this.address.length);
120 + }
121 +
122 + /**
123 + * Returns the value of the {@code MACAddress} as a {@code long}.
124 + *
125 + * @return the numeric value represented by this object after conversion to
126 + * type {@code long}.
127 + */
128 + public long toLong() {
129 + long mac = 0;
130 + for (int i = 0; i < 6; i++) {
131 + final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
132 + mac |= t;
133 + }
134 + return mac;
135 + }
136 +
137 + /**
138 + * Returns {@code true} if the MAC address is the broadcast address.
139 + *
140 + * @return {@code true} if the MAC address is the broadcast address.
141 + */
142 + public boolean isBroadcast() {
143 + for (final byte b : this.address) {
144 + if (b != -1) {
145 + return false;
146 + }
147 + }
148 + return true;
149 + }
150 +
151 + /**
152 + * Returns {@code true} if the MAC address is the multicast address.
153 + *
154 + * @return {@code true} if the MAC address is the multicast address.
155 + */
156 + public boolean isMulticast() {
157 + if (this.isBroadcast()) {
158 + return false;
159 + }
160 + return (this.address[0] & 0x01) != 0;
161 + }
162 +
163 + @Override
164 + public boolean equals(final Object o) {
165 + if (o == this) {
166 + return true;
167 + }
168 +
169 + if (!(o instanceof MACAddress)) {
170 + return false;
171 + }
172 +
173 + final MACAddress other = (MACAddress) o;
174 + return Arrays.equals(this.address, other.address);
175 + }
176 +
177 + @Override
178 + public int hashCode() {
179 + return Arrays.hashCode(this.address);
180 + }
181 +
182 + @Override
183 + public String toString() {
184 + final StringBuilder builder = new StringBuilder();
185 + for (final byte b : this.address) {
186 + if (builder.length() > 0) {
187 + builder.append(":");
188 + }
189 + builder.append(String.format("%02X", b & 0xFF));
190 + }
191 + return builder.toString();
192 + }
193 +
194 + /**
195 + * @return MAC address in string representation without colons (useful for
196 + * radix tree storage)
197 + */
198 + public String toStringNoColon() {
199 + final StringBuilder builder = new StringBuilder();
200 + for (final byte b : this.address) {
201 + builder.append(String.format("%02X", b & 0xFF));
202 + }
203 + return builder.toString();
204 + }
205 +
206 + public byte[] getAddress() {
207 + return this.address;
208 + }
209 +}
This diff is collapsed. Click to expand it.
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.nio.ByteBuffer;
36 +
37 +/**
38 + *
39 + * @author shudong.zhou@bigswitch.com
40 + */
41 +
42 +public class TCP extends BasePacket {
43 + protected short sourcePort;
44 + protected short destinationPort;
45 + protected int sequence;
46 + protected int acknowledge;
47 + protected byte dataOffset;
48 + protected short flags;
49 + protected short windowSize;
50 + protected short checksum;
51 + protected short urgentPointer;
52 + protected byte[] options;
53 +
54 + /**
55 + * @return the sourcePort
56 + */
57 + public short getSourcePort() {
58 + return this.sourcePort;
59 + }
60 +
61 + /**
62 + * @param sourcePort
63 + * the sourcePort to set
64 + */
65 + public TCP setSourcePort(final short sourcePort) {
66 + this.sourcePort = sourcePort;
67 + return this;
68 + }
69 +
70 + /**
71 + * @return the destinationPort
72 + */
73 + public short getDestinationPort() {
74 + return this.destinationPort;
75 + }
76 +
77 + /**
78 + * @param destinationPort
79 + * the destinationPort to set
80 + */
81 + public TCP setDestinationPort(final short destinationPort) {
82 + this.destinationPort = destinationPort;
83 + return this;
84 + }
85 +
86 + /**
87 + * @return the checksum
88 + */
89 + public short getChecksum() {
90 + return this.checksum;
91 + }
92 +
93 + public int getSequence() {
94 + return this.sequence;
95 + }
96 +
97 + public TCP setSequence(final int seq) {
98 + this.sequence = seq;
99 + return this;
100 + }
101 +
102 + public int getAcknowledge() {
103 + return this.acknowledge;
104 + }
105 +
106 + public TCP setAcknowledge(final int ack) {
107 + this.acknowledge = ack;
108 + return this;
109 + }
110 +
111 + public byte getDataOffset() {
112 + return this.dataOffset;
113 + }
114 +
115 + public TCP setDataOffset(final byte offset) {
116 + this.dataOffset = offset;
117 + return this;
118 + }
119 +
120 + public short getFlags() {
121 + return this.flags;
122 + }
123 +
124 + public TCP setFlags(final short flags) {
125 + this.flags = flags;
126 + return this;
127 + }
128 +
129 + public short getWindowSize() {
130 + return this.windowSize;
131 + }
132 +
133 + public TCP setWindowSize(final short windowSize) {
134 + this.windowSize = windowSize;
135 + return this;
136 + }
137 +
138 + public short getTcpChecksum() {
139 + return this.checksum;
140 + }
141 +
142 + public TCP setTcpChecksum(final short checksum) {
143 + this.checksum = checksum;
144 + return this;
145 + }
146 +
147 + @Override
148 + public void resetChecksum() {
149 + this.checksum = 0;
150 + super.resetChecksum();
151 + }
152 +
153 + public short getUrgentPointer(final short urgentPointer) {
154 + return this.urgentPointer;
155 + }
156 +
157 + public TCP setUrgentPointer(final short urgentPointer) {
158 + this.urgentPointer = urgentPointer;
159 + return this;
160 + }
161 +
162 + public byte[] getOptions() {
163 + return this.options;
164 + }
165 +
166 + public TCP setOptions(final byte[] options) {
167 + this.options = options;
168 + this.dataOffset = (byte) (20 + options.length + 3 >> 2);
169 + return this;
170 + }
171 +
172 + /**
173 + * @param checksum
174 + * the checksum to set
175 + */
176 + public TCP setChecksum(final short checksum) {
177 + this.checksum = checksum;
178 + return this;
179 + }
180 +
181 + /**
182 + * Serializes the packet. Will compute and set the following fields if they
183 + * are set to specific values at the time serialize is called: -checksum : 0
184 + * -length : 0
185 + */
186 + @Override
187 + public byte[] serialize() {
188 + int length;
189 + if (this.dataOffset == 0) {
190 + this.dataOffset = 5; // default header length
191 + }
192 + length = this.dataOffset << 2;
193 + byte[] payloadData = null;
194 + if (this.payload != null) {
195 + this.payload.setParent(this);
196 + payloadData = this.payload.serialize();
197 + length += payloadData.length;
198 + }
199 +
200 + final byte[] data = new byte[length];
201 + final ByteBuffer bb = ByteBuffer.wrap(data);
202 +
203 + bb.putShort(this.sourcePort);
204 + bb.putShort(this.destinationPort);
205 + bb.putInt(this.sequence);
206 + bb.putInt(this.acknowledge);
207 + bb.putShort((short) (this.flags | this.dataOffset << 12));
208 + bb.putShort(this.windowSize);
209 + bb.putShort(this.checksum);
210 + bb.putShort(this.urgentPointer);
211 + if (this.dataOffset > 5) {
212 + int padding;
213 + bb.put(this.options);
214 + padding = (this.dataOffset << 2) - 20 - this.options.length;
215 + for (int i = 0; i < padding; i++) {
216 + bb.put((byte) 0);
217 + }
218 + }
219 + if (payloadData != null) {
220 + bb.put(payloadData);
221 + }
222 +
223 + if (this.parent != null && this.parent instanceof IPv4) {
224 + ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_TCP);
225 + }
226 +
227 + // compute checksum if needed
228 + if (this.checksum == 0) {
229 + bb.rewind();
230 + int accumulation = 0;
231 +
232 + // compute pseudo header mac
233 + if (this.parent != null && this.parent instanceof IPv4) {
234 + final IPv4 ipv4 = (IPv4) this.parent;
235 + accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
236 + + (ipv4.getSourceAddress() & 0xffff);
237 + accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
238 + + (ipv4.getDestinationAddress() & 0xffff);
239 + accumulation += ipv4.getProtocol() & 0xff;
240 + accumulation += length & 0xffff;
241 + }
242 +
243 + for (int i = 0; i < length / 2; ++i) {
244 + accumulation += 0xffff & bb.getShort();
245 + }
246 + // pad to an even number of shorts
247 + if (length % 2 > 0) {
248 + accumulation += (bb.get() & 0xff) << 8;
249 + }
250 +
251 + accumulation = (accumulation >> 16 & 0xffff)
252 + + (accumulation & 0xffff);
253 + this.checksum = (short) (~accumulation & 0xffff);
254 + bb.putShort(16, this.checksum);
255 + }
256 + return data;
257 + }
258 +
259 + /*
260 + * (non-Javadoc)
261 + *
262 + * @see java.lang.Object#hashCode()
263 + */
264 + @Override
265 + public int hashCode() {
266 + final int prime = 5807;
267 + int result = super.hashCode();
268 + result = prime * result + this.checksum;
269 + result = prime * result + this.destinationPort;
270 + result = prime * result + this.sourcePort;
271 + return result;
272 + }
273 +
274 + /*
275 + * (non-Javadoc)
276 + *
277 + * @see java.lang.Object#equals(java.lang.Object)
278 + */
279 + @Override
280 + public boolean equals(final Object obj) {
281 + if (this == obj) {
282 + return true;
283 + }
284 + if (!super.equals(obj)) {
285 + return false;
286 + }
287 + if (!(obj instanceof TCP)) {
288 + return false;
289 + }
290 + final TCP other = (TCP) obj;
291 + // May want to compare fields based on the flags set
292 + return this.checksum == other.checksum
293 + && this.destinationPort == other.destinationPort
294 + && this.sourcePort == other.sourcePort
295 + && this.sequence == other.sequence
296 + && this.acknowledge == other.acknowledge
297 + && this.dataOffset == other.dataOffset
298 + && this.flags == other.flags
299 + && this.windowSize == other.windowSize
300 + && this.urgentPointer == other.urgentPointer
301 + && (this.dataOffset == 5 || this.options.equals(other.options));
302 + }
303 +
304 + @Override
305 + public IPacket deserialize(final byte[] data, final int offset,
306 + final int length) {
307 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
308 + this.sourcePort = bb.getShort();
309 + this.destinationPort = bb.getShort();
310 + this.sequence = bb.getInt();
311 + this.acknowledge = bb.getInt();
312 + this.flags = bb.getShort();
313 + this.dataOffset = (byte) (this.flags >> 12 & 0xf);
314 + this.flags = (short) (this.flags & 0x1ff);
315 + this.windowSize = bb.getShort();
316 + this.checksum = bb.getShort();
317 + this.urgentPointer = bb.getShort();
318 + if (this.dataOffset > 5) {
319 + int optLength = (this.dataOffset << 2) - 20;
320 + if (bb.limit() < bb.position() + optLength) {
321 + optLength = bb.limit() - bb.position();
322 + }
323 + try {
324 + this.options = new byte[optLength];
325 + bb.get(this.options, 0, optLength);
326 + } catch (final IndexOutOfBoundsException e) {
327 + this.options = null;
328 + }
329 + }
330 +
331 + this.payload = new Data();
332 + this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
333 + - bb.position());
334 + this.payload.setParent(this);
335 + return this;
336 + }
337 +}
1 +/*******************************************************************************
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + ******************************************************************************/
16 +/**
17 + * Copyright 2011, Big Switch Networks, Inc.
18 + * Originally created by David Erickson, Stanford University
19 + *
20 + * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 + * not use this file except in compliance with the License. You may obtain
22 + * a copy of the License at
23 + *
24 + * http://www.apache.org/licenses/LICENSE-2.0
25 + *
26 + * Unless required by applicable law or agreed to in writing, software
27 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 + * License for the specific language governing permissions and limitations
30 + * under the License.
31 + **/
32 +
33 +package org.onlab.packet;
34 +
35 +import java.nio.ByteBuffer;
36 +import java.util.HashMap;
37 +import java.util.Map;
38 +
39 +/**
40 + *
41 + * @author David Erickson (daviderickson@cs.stanford.edu)
42 + */
43 +
44 +public class UDP extends BasePacket {
45 + public static Map<Short, Class<? extends IPacket>> decodeMap;
46 + public static final short DHCP_SERVER_PORT = (short) 67;
47 + public static final short DHCP_CLIENT_PORT = (short) 68;
48 +
49 + static {
50 + UDP.decodeMap = new HashMap<Short, Class<? extends IPacket>>();
51 + /*
52 + * Disable DHCP until the deserialize code is hardened to deal with
53 + * garbage input
54 + */
55 + UDP.decodeMap.put(UDP.DHCP_SERVER_PORT, DHCP.class);
56 + UDP.decodeMap.put(UDP.DHCP_CLIENT_PORT, DHCP.class);
57 +
58 + }
59 +
60 + protected short sourcePort;
61 + protected short destinationPort;
62 + protected short length;
63 + protected short checksum;
64 +
65 + /**
66 + * @return the sourcePort
67 + */
68 + public short getSourcePort() {
69 + return this.sourcePort;
70 + }
71 +
72 + /**
73 + * @param sourcePort
74 + * the sourcePort to set
75 + */
76 + public UDP setSourcePort(final short sourcePort) {
77 + this.sourcePort = sourcePort;
78 + return this;
79 + }
80 +
81 + /**
82 + * @return the destinationPort
83 + */
84 + public short getDestinationPort() {
85 + return this.destinationPort;
86 + }
87 +
88 + /**
89 + * @param destinationPort
90 + * the destinationPort to set
91 + */
92 + public UDP setDestinationPort(final short destinationPort) {
93 + this.destinationPort = destinationPort;
94 + return this;
95 + }
96 +
97 + /**
98 + * @return the length
99 + */
100 + public short getLength() {
101 + return this.length;
102 + }
103 +
104 + /**
105 + * @return the checksum
106 + */
107 + public short getChecksum() {
108 + return this.checksum;
109 + }
110 +
111 + /**
112 + * @param checksum
113 + * the checksum to set
114 + */
115 + public UDP setChecksum(final short checksum) {
116 + this.checksum = checksum;
117 + return this;
118 + }
119 +
120 + @Override
121 + public void resetChecksum() {
122 + this.checksum = 0;
123 + super.resetChecksum();
124 + }
125 +
126 + /**
127 + * Serializes the packet. Will compute and set the following fields if they
128 + * are set to specific values at the time serialize is called: -checksum : 0
129 + * -length : 0
130 + */
131 + @Override
132 + public byte[] serialize() {
133 + byte[] payloadData = null;
134 + if (this.payload != null) {
135 + this.payload.setParent(this);
136 + payloadData = this.payload.serialize();
137 + }
138 +
139 + this.length = (short) (8 + (payloadData == null ? 0
140 + : payloadData.length));
141 +
142 + final byte[] data = new byte[this.length];
143 + final ByteBuffer bb = ByteBuffer.wrap(data);
144 +
145 + bb.putShort(this.sourcePort);
146 + bb.putShort(this.destinationPort);
147 + bb.putShort(this.length);
148 + bb.putShort(this.checksum);
149 + if (payloadData != null) {
150 + bb.put(payloadData);
151 + }
152 +
153 + if (this.parent != null && this.parent instanceof IPv4) {
154 + ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP);
155 + }
156 +
157 + // compute checksum if needed
158 + if (this.checksum == 0) {
159 + bb.rewind();
160 + int accumulation = 0;
161 +
162 + // compute pseudo header mac
163 + if (this.parent != null && this.parent instanceof IPv4) {
164 + final IPv4 ipv4 = (IPv4) this.parent;
165 + accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
166 + + (ipv4.getSourceAddress() & 0xffff);
167 + accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
168 + + (ipv4.getDestinationAddress() & 0xffff);
169 + accumulation += ipv4.getProtocol() & 0xff;
170 + accumulation += this.length & 0xffff;
171 + }
172 +
173 + for (int i = 0; i < this.length / 2; ++i) {
174 + accumulation += 0xffff & bb.getShort();
175 + }
176 + // pad to an even number of shorts
177 + if (this.length % 2 > 0) {
178 + accumulation += (bb.get() & 0xff) << 8;
179 + }
180 +
181 + accumulation = (accumulation >> 16 & 0xffff)
182 + + (accumulation & 0xffff);
183 + this.checksum = (short) (~accumulation & 0xffff);
184 + bb.putShort(6, this.checksum);
185 + }
186 + return data;
187 + }
188 +
189 + /*
190 + * (non-Javadoc)
191 + *
192 + * @see java.lang.Object#hashCode()
193 + */
194 + @Override
195 + public int hashCode() {
196 + final int prime = 5807;
197 + int result = super.hashCode();
198 + result = prime * result + this.checksum;
199 + result = prime * result + this.destinationPort;
200 + result = prime * result + this.length;
201 + result = prime * result + this.sourcePort;
202 + return result;
203 + }
204 +
205 + /*
206 + * (non-Javadoc)
207 + *
208 + * @see java.lang.Object#equals(java.lang.Object)
209 + */
210 + @Override
211 + public boolean equals(final Object obj) {
212 + if (this == obj) {
213 + return true;
214 + }
215 + if (!super.equals(obj)) {
216 + return false;
217 + }
218 + if (!(obj instanceof UDP)) {
219 + return false;
220 + }
221 + final UDP other = (UDP) obj;
222 + if (this.checksum != other.checksum) {
223 + return false;
224 + }
225 + if (this.destinationPort != other.destinationPort) {
226 + return false;
227 + }
228 + if (this.length != other.length) {
229 + return false;
230 + }
231 + if (this.sourcePort != other.sourcePort) {
232 + return false;
233 + }
234 + return true;
235 + }
236 +
237 + @Override
238 + public IPacket deserialize(final byte[] data, final int offset,
239 + final int length) {
240 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
241 + this.sourcePort = bb.getShort();
242 + this.destinationPort = bb.getShort();
243 + this.length = bb.getShort();
244 + this.checksum = bb.getShort();
245 +
246 + if (UDP.decodeMap.containsKey(this.destinationPort)) {
247 + try {
248 + this.payload = UDP.decodeMap.get(this.destinationPort)
249 + .getConstructor().newInstance();
250 + } catch (final Exception e) {
251 + throw new RuntimeException("Failure instantiating class", e);
252 + }
253 + } else if (UDP.decodeMap.containsKey(this.sourcePort)) {
254 + try {
255 + this.payload = UDP.decodeMap.get(this.sourcePort)
256 + .getConstructor().newInstance();
257 + } catch (final Exception e) {
258 + throw new RuntimeException("Failure instantiating class", e);
259 + }
260 + } else {
261 + this.payload = new Data();
262 + }
263 + this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
264 + - bb.position());
265 + this.payload.setParent(this);
266 + return this;
267 + }
268 +}
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
27 <groupId>com.google.guava</groupId> 27 <groupId>com.google.guava</groupId>
28 <artifactId>guava</artifactId> 28 <artifactId>guava</artifactId>
29 </dependency> 29 </dependency>
30 + <dependency>
31 + <groupId>commons-lang</groupId>
32 + <artifactId>commons-lang</artifactId>
33 + <version>2.3</version>
34 + </dependency>
30 </dependencies> 35 </dependencies>
31 36
32 <build> 37 <build>
......