///** // * Generate the DHCPv6 Server's DUID-LLT. See sections 9 and 22.3 of RFC 3315. // * // * @return the opaque opaqueData // */ public static opaqueData GenerateDUID_LLT() { opaqueData opaque = null; try { var intfs = NetworkInterface.GetAllNetworkInterfaces().GetEnumerator(); if (intfs != null) { while (intfs.MoveNext()) { NetworkInterface intf = intfs.Current as NetworkInterface; if (intf.OperationalStatus == OperationalStatus.Up && !(intf.NetworkInterfaceType == NetworkInterfaceType.Loopback) && !(intf.NetworkInterfaceType == NetworkInterfaceType.Ppp) // && !intf.isVirtual() ) { opaque = new opaqueData(); ByteBuffer bb = ByteBuffer.allocate(intf.GetPhysicalAddress().GetAddressBytes().Length + 8); bb.putShort((short)1); // DUID based on LLT bb.putShort((short)1); // assume ethernet bb.putInt((int)(GetCurrentMilli() / 1000)); // seconds since the Epoch bb.put(intf.GetPhysicalAddress().GetAddressBytes()); opaque.hexValue = bb.getAllBytes(); break; } } } } catch (Exception ex) { log.Error("Failed to generate DUID-LLT: " + ex.Message); } return(opaque); }
public BaseOpaqueData(opaqueData opaqueData) { if (opaqueData != null) { SetAscii(opaqueData.asciiValue); SetHex(opaqueData.hexValue); } }
/** * Matches. * * @param expression the expression * @param myOpaque the my opaque * * @return true, if successful */ public static bool Matches(optionExpression expression, BaseOpaqueData myOpaque) { if (expression == null) { return(false); } opaqueData data = (opaqueData)expression.Item; if (data == null) { return(false); } @operator op = expression.@operator; return(Matches(myOpaque, data, op)); }
/** * To string. * * @param opaque the opaque * * @return the string */ public static string ToString(opaqueData opaque) { if (opaque == null) { return(null); } string ascii = opaque.asciiValue; if (ascii != null) { return(ascii); } else { return(Util.ToHexString(opaque.hexValue)); } }
public bool Matches(optionExpression expression) { if (expression == null) { return(false); } if (expression.code != this.code) { return(false); } unsignedShortOptionType exprOption = (unsignedShortOptionType)expression.Item; if (exprOption != null) { int exprUshort = exprOption.unsignedShort; @operator op = expression.@operator; if (op.Equals(@operator.equals)) { return(unsignedShort == exprUshort); } else if (op.Equals(@operator.lessThan)) { return(unsignedShort < exprUshort); } else if (op.Equals(@operator.lessThanOrEqual)) { return(unsignedShort <= exprUshort); } else if (op.Equals(@operator.greaterThan)) { return(unsignedShort > exprUshort); } else if (op.Equals(@operator.greaterThanOrEqual)) { return(unsignedShort >= exprUshort); } else { log.Warn("Unsupported expression operator: " + op); } } // then see if we have an opaque option opaqueDataOptionType opaqueOption = (opaqueDataOptionType)expression.Item; if (opaqueOption != null) { opaqueData opaque = opaqueOption.opaqueData; if (opaque != null) { string ascii = opaque.asciiValue; if (ascii != null) { try { // need an Integer to handle unsigned short if (unsignedShort == int.Parse(ascii)) { return(true); } } catch (Exception ex) { log.Error("Invalid unsigned short ASCII value for OpaqueData: " + ascii); } } else { byte[] hex = opaque.hexValue; if ((hex != null) && (hex.Length >= 1) && (hex.Length <= 2)) { int hexUnsignedShort = Convert.ToInt32(Util.ToHexString(hex), 16); if (unsignedShort == hexUnsignedShort) { return(true); } } } } } return(false); }
//public void decodeLengthAndData(ByteBuffer buf) //{ // int len = Util.getUnsignedShort(buf); // if (len > 0) // { // decode(buf, len); // } //} // for expression matching public bool Matches(opaqueData that, @operator op) { //if (that != null) //{ // String expAscii = that.getAsciiValue(); // String myAscii = getAscii(); // if ((expAscii != null) && (myAscii != null)) // { // if (op.equals(Operator.EQUALS)) // { // return myAscii.equalsIgnoreCase(expAscii); // } // else if (op.equals(Operator.STARTS_WITH)) // { // return myAscii.startsWith(expAscii); // } // else if (op.equals(Operator.CONTAINS)) // { // return myAscii.contains(expAscii); // } // else if (op.equals(Operator.ENDS_WITH)) // { // return myAscii.endsWith(expAscii); // } // else if (op.equals(Operator.REG_EXP)) // { // return myAscii.matches(expAscii); // } // else // { // log.error("Unsupported expression operator: " + op); // return false; // } // } // else if ((expAscii == null) && (myAscii == null)) // { // byte[] expHex = that.getHexValue(); // byte[] myHex = getHex(); // if ((expHex != null) && (myHex != null)) // { // if (op.equals(Operator.EQUALS)) // { // return Arrays.equals(myHex, expHex); // } // else if (op.equals(Operator.STARTS_WITH)) // { // if (myHex.length >= expHex.length) // { // for (int i = 0; i < expHex.length; i++) // { // if (myHex[i] != expHex[i]) // { // return false; // } // } // return true; // if we get here, it matches // } // else // { // return false; // exp length too long // } // } // else if (op.equals(Operator.CONTAINS)) // { // if (myHex.length >= expHex.length) // { // int j = 0; // for (int i = 0; i < myHex.length; i++) // { // if (myHex[i] == expHex[j]) // { // // found a potential match // j++; // boolean matches = true; // for (int ii = i + 1; ii < myHex.length; ii++) // { // if (myHex[ii] != expHex[j++]) // { // matches = false; // break; // } // } // if (matches) // { // return true; // } // j = 0; // reset to start of exp // } // } // return false; // if we get here, it didn't match // } // else // { // return false; // exp length too long // } // } // else if (op.equals(Operator.ENDS_WITH)) // { // if (myHex.length >= expHex.length) // { // for (int i = myHex.length - 1; // i >= myHex.length - expHex.length; // i--) // { // if (myHex[i] != expHex[i]) // { // return false; // } // } // return true; // if we get here, it matches // } // else // { // return false; // exp length too long // } // } // else if (op.equals(Operator.REG_EXP)) // { // log.Error("Regular expression operator not valid for hex opaque opaqueData"); // return false; // } // else // { // log.Error("Unsupported expression operator: " + op); // return false; // } // } // } //} return(false); }
public static bool Matches(BaseOpaqueData myOpaque, opaqueData that, @operator op) { if (that != null) { string expAscii = that.asciiValue; string myAscii = myOpaque.GetAscii(); if ((expAscii != null) && (myAscii != null)) { if (op.Equals(@operator.equals)) { return(myAscii.equalsIgnoreCase(expAscii)); } else if (op.Equals(@operator.startsWith)) { return(myAscii.startsWith(expAscii)); } else if (op.Equals(@operator.contains)) { return(myAscii.@operator(expAscii)); } else if (op.Equals(@operator.endsWith)) { return(myAscii.endsWith(expAscii)); } else if (op.Equals(@operator.regExp)) { return(myAscii.matches(expAscii)); } else { log.error("Unsupported expression operator: " + op); return(false); } } else if ((expAscii == null) && (myAscii == null)) { byte[] expHex = that.getHexValue(); byte[] myHex = myOpaque.getHex(); if ((expHex != null) && (myHex != null)) { if (op.equals(Operator.EQUALS)) { return(Arrays.equals(myHex, expHex)); } else if (op.equals(Operator.STARTS_WITH)) { if (myHex.length >= expHex.length) { for (int i = 0; i < expHex.length; i++) { if (myHex[i] != expHex[i]) { return(false); } } return(true); // if we get here, it matches } else { return(false); // exp length too long } } else if (op.equals(Operator.CONTAINS)) { if (myHex.length >= expHex.length) { int j = 0; for (int i = 0; i < myHex.length; i++) { if (myHex[i] == expHex[j]) { // found a potential match j++; boolean matches = true; for (int ii = i + 1; ii < myHex.length; ii++) { if (myHex[ii] != expHex[j++]) { matches = false; break; } } if (matches) { return(true); } j = 0; // reset to start of exp } } return(false); // if we get here, it didn't match } else { return(false); // exp length too long } } else if (op.equals(Operator.ENDS_WITH)) { if (myHex.length >= expHex.length) { for (int i = myHex.length - 1; i >= myHex.length - expHex.length; i--) { if (myHex[i] != expHex[i]) { return(false); } } return(true); // if we get here, it matches } else { return(false); // exp length too long } } else if (op.equals(Operator.REG_EXP)) { log.error("Regular expression operator not valid for hex opaque opaqueData"); return(false); } else { log.error("Unsupported expression operator: " + op); return(false); } } } } return(false); }
public static bool Matches(BaseOpaqueData myOpaque, opaqueData that, @operator op) { if (that != null) { string expAscii = that.asciiValue; string myAscii = myOpaque.GetAscii(); if ((expAscii != null) && (myAscii != null)) { if (op.Equals(@operator.equals)) { return(myAscii.ToUpper() == expAscii.ToUpper()); } else if (op.Equals(@operator.startsWith)) { return(myAscii.StartsWith(expAscii)); } else if (op.Equals(@operator.contains)) { return(myAscii.Contains(expAscii)); } else if (op.Equals(@operator.endsWith)) { return(myAscii.EndsWith(expAscii)); } else if (op.Equals(@operator.regExp)) { Match m = Regex.Match(myAscii, expAscii); return(m.Success); } else { log.Error("Unsupported expression operator: " + op); return(false); } } else if ((expAscii == null) && (myAscii == null)) { byte[] expHex = that.hexValue; byte[] myHex = myOpaque.GetHex(); if ((expHex != null) && (myHex != null)) { if (op.Equals(@operator.equals)) { return(Array.Equals(myHex, expHex)); } else if (op.Equals(@operator.startsWith)) { if (myHex.Length >= expHex.Length) { for (int i = 0; i < expHex.Length; i++) { if (myHex[i] != expHex[i]) { return(false); } } return(true); // if we get here, it matches } else { return(false); // exp length too long } } else if (op.Equals(@operator.contains)) { if (myHex.Length >= expHex.Length) { int j = 0; for (int i = 0; i < myHex.Length; i++) { if (myHex[i] == expHex[j]) { // found a potential match j++; bool matches = true; for (int ii = i + 1; ii < myHex.Length; ii++) { if (myHex[ii] != expHex[j++]) { matches = false; break; } } if (matches) { return(true); } j = 0; // reset to start of exp } } return(false); // if we get here, it didn't match } else { return(false); // exp length too long } } else if (op.Equals(@operator.endsWith)) { if (myHex.Length >= expHex.Length) { for (int i = myHex.Length - 1; i >= myHex.Length - expHex.Length; i--) { if (myHex[i] != expHex[i]) { return(false); } } return(true); // if we get here, it matches } else { return(false); // exp length too long } } else if (op.Equals(@operator.regExp)) { log.Error("Regular expression operator not valid for hex opaque opaqueData"); return(false); } else { log.Error("Unsupported expression operator: " + op); return(false); } } } } return(false); }