1 | /* $Id: ClientId.cpp 79563 2019-07-06 01:22:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - client identifier
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <algorithm>
|
---|
23 | #include "ClientId.h"
|
---|
24 |
|
---|
25 |
|
---|
26 | /*********************************************************************************************************************************
|
---|
27 | * Global Variables *
|
---|
28 | *********************************************************************************************************************************/
|
---|
29 | /** Indiciates wherther ClientId::rtStrFormat was already registered. */
|
---|
30 | bool ClientId::g_fFormatRegistered = false;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Registers the ClientId format type callback ("%R[id]").
|
---|
35 | */
|
---|
36 | void ClientId::registerFormat() RT_NOEXCEPT
|
---|
37 | {
|
---|
38 | if (!g_fFormatRegistered)
|
---|
39 | {
|
---|
40 | int rc = RTStrFormatTypeRegister("id", rtStrFormat, NULL);
|
---|
41 | AssertRC(rc);
|
---|
42 | g_fFormatRegistered = RT_SUCCESS(rc);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @callback_method_impl{FNRTSTRFORMATTYPE, Formats ClientId via "%R[id]". }
|
---|
49 | */
|
---|
50 | DECLCALLBACK(size_t)
|
---|
51 | ClientId::rtStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
52 | const char *pszType, void const *pvValue,
|
---|
53 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
54 | void *pvUser)
|
---|
55 | {
|
---|
56 | RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
|
---|
57 | Assert(strcmp(pszType, "id") == 0);
|
---|
58 |
|
---|
59 | const ClientId *pThis = static_cast<const ClientId *>(pvValue);
|
---|
60 | if (pThis == NULL)
|
---|
61 | return pfnOutput(pvArgOutput, RT_STR_TUPLE("<NULL>"));
|
---|
62 |
|
---|
63 | size_t cb = 0;
|
---|
64 | if (pThis->m_id.present())
|
---|
65 | {
|
---|
66 | cb += pfnOutput(pvArgOutput, RT_STR_TUPLE("["));
|
---|
67 |
|
---|
68 | const OptClientId::value_t &idopt = pThis->m_id.value();
|
---|
69 | for (size_t i = 0; i < idopt.size(); ++i)
|
---|
70 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%02x", (i == 0 ? "" : ":"), idopt[i]);
|
---|
71 |
|
---|
72 | cb += pfnOutput(pvArgOutput, RT_STR_TUPLE("] ("));
|
---|
73 | }
|
---|
74 |
|
---|
75 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%RTmac", &pThis->m_mac);
|
---|
76 |
|
---|
77 | if (pThis->m_id.present())
|
---|
78 | cb += pfnOutput(pvArgOutput, RT_STR_TUPLE(")"));
|
---|
79 |
|
---|
80 | return cb;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | bool operator==(const ClientId &l, const ClientId &r) RT_NOEXCEPT
|
---|
85 | {
|
---|
86 | if (l.m_id.present())
|
---|
87 | {
|
---|
88 | if (r.m_id.present())
|
---|
89 | return l.m_id.value() == r.m_id.value();
|
---|
90 | }
|
---|
91 | else
|
---|
92 | {
|
---|
93 | if (!r.m_id.present())
|
---|
94 | return l.m_mac == r.m_mac;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | bool operator<(const ClientId &l, const ClientId &r) RT_NOEXCEPT
|
---|
102 | {
|
---|
103 | if (l.m_id.present())
|
---|
104 | {
|
---|
105 | if (r.m_id.present())
|
---|
106 | return l.m_id.value() < r.m_id.value();
|
---|
107 | return false; /* the one with id comes last */
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | if (r.m_id.present())
|
---|
112 | return true; /* the one with id comes last */
|
---|
113 | return l.m_mac < r.m_mac;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|