es.tid.bgp.bgp4.messages.BGP4Open Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of network-protocols Show documentation
Show all versions of network-protocols Show documentation
BGP-LS,OSPF-TE,PCEP and RSVP-TE protocol encodings.
The newest version!
package es.tid.bgp.bgp4.messages;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.LinkedList;
import es.tid.bgp.bgp4.open.BGP4CapabilitiesOptionalParameter;
import es.tid.bgp.bgp4.open.BGP4OptionalParameter;
import es.tid.bgp.bgp4.open.BGP4OptionalParametersTypes;
/**
* BGP Open Message.
*
* BGP Open Message Format (RFC 4271). From
* {link RFC 4271}
* Section 4.2
* 4.2. OPEN Message Format
*
* After a TCP connection is established, the first message sent by each
side is an OPEN message. If the OPEN message is acceptable, a
KEEPALIVE message confirming the OPEN is sent back.
In addition to the fixed-size BGP header, the OPEN message contains
the following fields:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
| Version |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| My Autonomous System |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Hold Time |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| BGP Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Opt Parm Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Optional Parameters (variable) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Version:
This 1-octet unsigned integer indicates the protocol version
number of the message. The current BGP version number is 4.
My Autonomous System:
This 2-octet unsigned integer indicates the Autonomous System
number of the sender.
Hold Time:
This 2-octet unsigned integer indicates the number of seconds
the sender proposes for the value of the Hold Timer. Upon
receipt of an OPEN message, a BGP speaker MUST calculate the
value of the Hold Timer by using the smaller of its configured
Hold Time and the Hold Time received in the OPEN message. The
Hold Time MUST be either zero or at least three seconds. An
implementation MAY reject connections on the basis of the Hold
Time. The calculated value indicates the maximum number of
seconds that may elapse between the receipt of successive
KEEPALIVE and/or UPDATE messages from the sender.
BGP Identifier:
This 4-octet unsigned integer indicates the BGP Identifier of
the sender. A given BGP speaker sets the value of its BGP
Identifier to an IP address that is assigned to that BGP
speaker. The value of the BGP Identifier is determined upon
startup and is the same for every local interface and BGP peer.
Optional Parameters Length:
This 1-octet unsigned integer indicates the total length of the
Optional Parameters field in octets. If the value of this
field is zero, no Optional Parameters are present.
Optional Parameters:
This field contains a list of optional parameters, in which
each parameter is encoded as a Parameter Type, Parameter
Length, Parameter Value triplet.
0 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...
| Parm. Type | Parm. Length | Parameter Value (variable)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...
Parameter Type is a one octet field that unambiguously
identifies individual parameters. Parameter Length is a one
octet field that contains the length of the Parameter Value
field in octets. Parameter Value is a variable length field
that is interpreted according to the value of the Parameter
Type field.
[RFC3392] defines the Capabilities Optional Parameter.
The minimum length of the OPEN message is 29 octets (including the
message header).
* @author mcs
*
*/
public class BGP4Open extends BGP4Message {
/**
* version indicates the protocol version number of the message (current 4)
*/
private int version=0x04;//Default value
/**
* myAutonomousSystem indicates the Autonomous System number of the sender
*/
private int myAutonomousSystem;
/**
* hold Time the number of seconds the sender proposes for the value of the Hold Timer.
* The calculated value indicates the maximum number of seconds that may elapse between the receipt of successive
* KEEPALIVE and/or UPDATE messages from the sender.
*/
private int holdTime=0x03;/** By default 3s */
/**
* BGPIdentifier indicates the BGP Identifier of the sender
* IP address that is assigned to that BGP speaker
*/
private Inet4Address BGPIdentifier;
/**
* optional parameters
*/
private LinkedList parametersList;
/**
* Construct new PCEP Open message from scratch
*/
public BGP4Open () {
super();
this.setMessageType(BGP4MessageTypes.MESSAGE_OPEN);
parametersList = new LinkedList();
}
/**
* Construct new BGP4 Open message from scratch
* @param bytes bytes of the BGP4 Open message to decode
*/
public BGP4Open (byte[] bytes) {//throws PCEPProtocolViolationException {
super(bytes);
parametersList = new LinkedList();
decode();
}
public void encode(){// throws PCEPProtocolViolationException {
//BGPOpenMessageMandatoryFileds=10;
int len=BGPHeaderLength+10;
int optionalParameterLength = 0;
int num_parameters = parametersList.size();
for (int i=0;i>>8 & 0xFF);
messageBytes[offset+1]=(byte)(myAutonomousSystem & 0xFF);
offset=offset+2;
messageBytes[offset]=(byte)(holdTime>>>8 & 0xFF);
messageBytes[offset+1]=(byte)(holdTime & 0xFF);
offset=offset+2;
System.arraycopy(BGPIdentifier.getAddress(),0, messageBytes, offset, 4);
offset=offset+4;
messageBytes[offset] = (byte)(optionalParameterLength & 0xff);
offset++;
for (int i=0;i this.getLength()){
log.warn("Empty BGP OPEN message");
//throw new PCEPProtocolViolationException();
}
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getMyAutonomousSystem() {
return myAutonomousSystem;
}
public void setMyAutonomousSystem(int myAutonomousSystem) {
this.myAutonomousSystem = myAutonomousSystem;
}
public int getHoldTime() {
return holdTime;
}
public void setHoldTime(int holdTime) {
this.holdTime = holdTime;
}
public Inet4Address getBGPIdentifier() {
return BGPIdentifier;
}
public void setBGPIdentifier(Inet4Address bGPIdentifier) {
BGPIdentifier = bGPIdentifier;
}
public LinkedList getParametersList() {
return parametersList;
}
public void setParametersList(LinkedList parametersList) {
this.parametersList = parametersList;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((BGPIdentifier == null) ? 0 : BGPIdentifier.hashCode());
result = prime * result + holdTime;
result = prime * result + myAutonomousSystem;
result = prime * result + ((parametersList == null) ? 0 : parametersList.hashCode());
result = prime * result + version;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
BGP4Open other = (BGP4Open) obj;
if (BGPIdentifier == null) {
if (other.BGPIdentifier != null)
return false;
} else if (!BGPIdentifier.equals(other.BGPIdentifier))
return false;
if (holdTime != other.holdTime)
return false;
if (myAutonomousSystem != other.myAutonomousSystem)
return false;
if (parametersList == null) {
if (other.parametersList != null)
return false;
} else if (!parametersList.equals(other.parametersList))
return false;
if (version != other.version)
return false;
return true;
}
@Override
public String toString() {
StringBuffer sb=new StringBuffer((20)*400);
sb.append("BGP Open Message: \n");
sb.append("> Version: "+ version+"\n");
sb.append("> MyAutonomousSystem: "+ myAutonomousSystem +"\n");
sb.append("> HoldTime: "+ holdTime+"\n");
sb.append("> BGPIdentifier: "+ BGPIdentifier+"\n");
if (parametersList != null){
for (int i=0;i "+ parametersList.get(i).toString()+"\n");
}
}
return sb.toString();
}
}