1/*
2 * Copyright (c) 2000-2001,2003-2004,2006,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25#ifndef _DBNAME_H_
26#define _DBNAME_H_  1
27
28#include <security_utilities/utilities.h>
29#include <security_cdsa_utilities/walkers.h>
30#include <Security/cssmtype.h>
31#include <string>
32
33#ifdef _CPP_DBNAME
34# pragma export on
35#endif
36
37// @@@ Should not use using in headers.
38using namespace std;
39
40namespace Security
41{
42
43//----------------------------------------------------------------
44//typedef struct cssm_net_address {
45//    CSSM_NET_ADDRESS_TYPE AddressType;
46//    CSSM_DATA Address;
47//} CSSM_NET_ADDRESS, *CSSM_NET_ADDRESS_PTR;
48//----------------------------------------------------------------
49
50// XXX TODO: Make CssmNetAddress use a factory to constuct netadrress objects based on CSSM_NET_ADDRESS_TYPE!
51class CssmNetAddress : public PodWrapper<CssmNetAddress, CSSM_NET_ADDRESS>
52{
53public:
54    // Create a CssmNetAddress wrapper.  Copies inAddress.Data
55    CssmNetAddress(CSSM_DB_RECORDTYPE inAddressType, const CssmData &inAddress);
56    CssmNetAddress(const CSSM_NET_ADDRESS &other);
57    ~CssmNetAddress();
58    CSSM_DB_RECORDTYPE addressType() const { return AddressType; }
59    const CssmData &address() const { return CssmData::overlay(Address); }
60    bool operator <(const CssmNetAddress &other) const
61    {
62        return AddressType != other.AddressType ? AddressType < other.AddressType : address() < other.address();
63    }
64};
65
66class DbName
67{
68public:
69    DbName (const char *inDbName = NULL, const CSSM_NET_ADDRESS *inDbLocation = NULL);
70    DbName(const DbName &other);
71    DbName &operator =(const DbName &other);
72    ~DbName ();
73	const char *dbName() const { return mDbNameValid ? mDbName.c_str() : NULL; }
74	const char *canonicalName() const { return mDbNameValid ? mCanonicalName.c_str() : NULL; }
75    const CssmNetAddress *dbLocation() const { return mDbLocation; }
76    bool operator <(const DbName &other) const
77    {
78		// invalid is always smaller than valid
79		if (!mDbNameValid || !other.mDbNameValid)
80			return mDbNameValid < other.mDbNameValid;
81
82        // If mDbNames are not equal return whether our mDbName is less than others mDbName.
83        if (canonicalName() != other.canonicalName())
84            return mDbName < other.mDbName;
85
86        // DbNames are equal so check for pointer equality of DbLocations
87        if (mDbLocation == other.mDbLocation)
88            return false;
89
90        // If either DbLocations is nil the one that is nil is less than the other.
91        if (mDbLocation == nil || other.mDbLocation == nil)
92            return mDbLocation < other.mDbLocation;
93
94        // Return which mDbLocation is smaller.
95        return *mDbLocation < *other.mDbLocation;
96    }
97	bool operator ==(const DbName &other) const
98	{ return (!(*this < other)) && (!(other < *this)); }
99	bool operator !=(const DbName &other) const
100	{ return *this < other || other < *this; }
101
102private:
103	void CanonicalizeName();
104
105    string mDbName;
106	string mCanonicalName;
107	bool mDbNameValid;
108    CssmNetAddress *mDbLocation;
109};
110
111
112namespace DataWalkers
113{
114
115template<class Action>
116CssmNetAddress *walk(Action &operate, CssmNetAddress * &addr)
117{
118    operate(addr);
119    walk(operate, addr->Address);
120    return addr;
121}
122
123} // end namespace DataWalkers
124
125} // end namespace Security
126
127#ifdef _CPP_DBNAME
128# pragma export off
129#endif
130
131#endif //_DBNAME_H_
132