Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | package org.onosproject.ospf.protocol.ospfpacket; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onosproject.ospf.exceptions.OspfErrorType; |
| 22 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 23 | import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket; |
| 24 | import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket; |
| 25 | import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge; |
| 26 | import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest; |
| 27 | import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate; |
| 28 | import org.onosproject.ospf.protocol.util.OspfParameters; |
| 29 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | /** |
| 34 | * A message reader which reads OSPF messages from ChannelBuffer and converts to OspfMessage instances. |
| 35 | */ |
| 36 | public class OspfMessageReader { |
| 37 | private static final Logger log = LoggerFactory.getLogger(OspfMessageReader.class); |
| 38 | |
| 39 | /** |
| 40 | * Reads and Converts the channel buffer to OspfMessage instance. |
| 41 | * |
| 42 | * @param channelBuffer channel buffer instance. |
| 43 | * @return OSPF message instance. |
| 44 | * @throws Exception might throws exception while parsing buffer |
| 45 | */ |
| 46 | public OspfMessage readFromBuffer(ChannelBuffer channelBuffer) |
| 47 | throws Exception { |
| 48 | |
| 49 | if (channelBuffer.readableBytes() < OspfUtil.PACKET_MINIMUM_LENGTH) { |
| 50 | log.error("Packet should have minimum length..."); |
| 51 | throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer); |
| 56 | int len = ospfHeader.ospfPacLength() - OspfUtil.OSPF_HEADER_LENGTH; |
| 57 | |
| 58 | OspfMessage ospfMessage = null; |
| 59 | switch (ospfHeader.ospfType()) { |
| 60 | case OspfParameters.HELLO: |
| 61 | ospfMessage = new HelloPacket(ospfHeader); |
| 62 | break; |
| 63 | case OspfParameters.DD: |
| 64 | ospfMessage = new DdPacket(ospfHeader); |
| 65 | break; |
| 66 | case OspfParameters.LSREQUEST: |
| 67 | ospfMessage = new LsRequest(ospfHeader); |
| 68 | break; |
| 69 | case OspfParameters.LSUPDATE: |
| 70 | ospfMessage = new LsUpdate(ospfHeader); |
| 71 | break; |
| 72 | case OspfParameters.LSACK: |
| 73 | ospfMessage = new LsAcknowledge(ospfHeader); |
| 74 | break; |
| 75 | default: |
| 76 | log.debug("Message Reader[Decoder] - Unknown LSA type..!!!"); |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | if (ospfMessage != null) { |
| 81 | try { |
| 82 | log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(), |
| 83 | ospfHeader.ospfPacLength()); |
| 84 | ospfMessage.readFrom(channelBuffer.readBytes(len)); |
| 85 | } catch (Exception e) { |
| 86 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 87 | OspfErrorType.BAD_MESSAGE); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | return ospfMessage; |
| 93 | } catch (Exception e) { |
| 94 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 95 | OspfErrorType.BAD_MESSAGE); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the OSPF packet Header. |
| 101 | * |
| 102 | * @param channelBuffer channel buffer instance. |
| 103 | * @return Ospf Header instance. |
| 104 | */ |
| 105 | private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception { |
| 106 | OspfPacketHeader ospfPacketHeader = new OspfPacketHeader(); |
| 107 | |
| 108 | byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES]; |
| 109 | channelBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES); |
| 110 | Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes); |
| 111 | |
| 112 | // Determine ospf version & Packet Type |
| 113 | int version = channelBuffer.readByte(); //byte 1 is ospf version |
| 114 | int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type |
| 115 | |
| 116 | // byte 3 & 4 combine is packet length. |
| 117 | int packetLength = channelBuffer.readShort(); |
| 118 | |
| 119 | if (packetLength > channelBuffer.readableBytes() + OspfUtil.FOUR_BYTES) { |
| 120 | log.error("Packet should have minimum length..."); |
| 121 | throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH); |
| 122 | } |
| 123 | |
| 124 | byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 125 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 126 | Ip4Address routerId = Ip4Address.valueOf(tempByteArray); |
| 127 | |
| 128 | tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 129 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 130 | Ip4Address areaId = Ip4Address.valueOf(tempByteArray); |
| 131 | |
| 132 | int checkSum = channelBuffer.readUnsignedShort(); |
| 133 | int auType = channelBuffer.readUnsignedShort(); |
| 134 | int authentication = (int) channelBuffer.readLong(); |
| 135 | |
| 136 | ospfPacketHeader.setSourceIp(sourceIP); |
| 137 | ospfPacketHeader.setOspfVer(version); |
| 138 | ospfPacketHeader.setOspftype(packetType); |
| 139 | ospfPacketHeader.setOspfPacLength(packetLength); |
| 140 | ospfPacketHeader.setRouterId(routerId); |
| 141 | ospfPacketHeader.setAreaId(areaId); |
| 142 | ospfPacketHeader.setChecksum(checkSum); |
| 143 | ospfPacketHeader.setAuthType(auType); |
| 144 | ospfPacketHeader.setAuthentication(authentication); |
| 145 | |
| 146 | return ospfPacketHeader; |
| 147 | } |
| 148 | } |