0

I want to know how I can generate a Session-Id. Some time Diameter Accept Request that do not have session id so, I want to create Session-Id for those request like Cancel location request.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
raman
  • 25
  • 4
  • We use `UUID.randomUUID().toString()` which the easiest was to generate a unique identity. If performance is critical then you can build a numerical one using internal components of `UUID.randomUUID()`. – Kedar Joshi Feb 19 '20 at 10:25

1 Answers1

-1
public static String CreateSessionId()
{
    /*<DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]
      <high 32 bits> and <low 32 bits> are decimal representations of the
       high and low 32 bits of a monotonically increasing 64-bit value.  The
       64-bit value is rendered in two part to simplify formatting by 32-bit
       processors.  At startup, the high 32 bits of the 64-bit value MAY be
       initialized to the time, and the low 32 bits MAY be initialized to
       zero.  This will for practical purposes eliminate the possibility of
       overlapping Session-Ids after a reboot, assuming the reboot process
       takes longer than a second.  Alternatively, an implementation MAY
       keep track of the increasing value in non-volatile memory.

     * <optional value> is implementation specific but may include a modem's
       device Id, a layer 2 address, timestamp, etc.
       Example, in which there is no optional value:
          accesspoint7.acme.com;1876543210;523
       Example, in which there is an optional value:
          accesspoint7.acme.com;1876543210;523;mobile@200.1.1.88*/

    String SessionID = "";

    String hostIdentity;


    int low32 = Sequence.getNext();

    int high32 = 0;

    long id = ((long)low32) << 32 | high32;

    String optional = String.valueOf(Sequence.getNext());

    SessionID = hostIdentity + ";" + id + ";" + optional;

    return SessionID;
}

public final class Sequence
{
    private static int _value = -1;
    private static final Object m_lock = new Object();

    public static int getNext()
    {
        synchronized (m_lock)
        {
            if (_value == Integer.MAX_VALUE)
            {
                _value = -1;
            }
            return ++_value;
        }
    }
}

you can try this one

Kamal Kumar
  • 194
  • 12
  • If i understand correctly this solution might repeat session-IDs after restart or after _value ==Integer.MAX_VALUE. You might have big problems if you have the same session-ID twice for different sessions. Also please read RFC 3588: "The Session-Id MUST be globally and eternally unique, as it is meant to uniquely identify a user session without reference to any other information, and may be needed to correlate historical authentication information with accounting information." – Oded Itzhaky Jul 25 '19 at 14:15
  • It is better to use DATE instead of a counter – Oded Itzhaky Jul 25 '19 at 14:16