blob: 81b06e06b14b229c15041b09a0407d6fe4438328 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart2a655752015-04-07 16:46:33 -07003 *
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
17package org.onlab.packet;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21/**
22 * Utilities for working with packet headers.
23 */
24public final class PacketUtils {
25
26 private PacketUtils() {
27 }
28
29 /**
30 * Check the length of the input buffer is appropriate given the offset and
31 * length parameters.
32 *
33 * @param byteLength length of the input buffer array
34 * @param offset offset given to begin reading bytes from
35 * @param length length given to read up until
36 * @throws DeserializationException if the input parameters don't match up (i.e
37 * we can't read that many bytes from the buffer at the given offest)
38 */
39 public static void checkBufferLength(int byteLength, int offset, int length)
40 throws DeserializationException {
41 boolean ok = (offset >= 0 && offset < byteLength);
Ray Milkeye559bcf2017-12-15 15:58:12 -080042 ok = ok && (length >= 0 && offset + length <= byteLength);
Jonathan Hart2a655752015-04-07 16:46:33 -070043
44 if (!ok) {
45 throw new DeserializationException("Unable to read " + length + " bytes from a "
46 + byteLength + " byte array starting at offset " + offset);
47 }
48 }
49
50 /**
51 * Check that there are enough bytes in the buffer to read some number of
52 * bytes that we need to read a full header.
53 *
54 * @param givenLength given size of the buffer
55 * @param requiredLength number of bytes we need to read some header fully
56 * @throws DeserializationException if there aren't enough bytes
57 */
58 public static void checkHeaderLength(int givenLength, int requiredLength)
59 throws DeserializationException {
60 if (requiredLength > givenLength) {
61 throw new DeserializationException(requiredLength
62 + " bytes are needed to continue deserialization, however only "
63 + givenLength + " remain in buffer");
64 }
65 }
66
67 /**
68 * Check the input parameters are sane and there's enough bytes to read
69 * the required length.
70 *
71 * @param data input byte buffer
72 * @param offset offset of the start of the header
73 * @param length length given to deserialize the header
74 * @param requiredLength length needed to deserialize header
75 * @throws DeserializationException if we're unable to deserialize the
76 * packet based on the input parameters
77 */
78 public static void checkInput(byte[] data, int offset, int length, int requiredLength)
79 throws DeserializationException {
80 checkNotNull(data);
81 checkBufferLength(data.length, offset, length);
82 checkHeaderLength(length, requiredLength);
83 }
84}