r6731: add a useful function for getting a guid with all bits to 0
[samba.git] / source / librpc / ndr / ndr_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID/policy_handle functions
5
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9    Copyright (C) Stefan (metze) Metzmacher         2004.
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "system/network.h"
28
29 void ndr_print_ipv4_addr(struct ndr_print *ndr, const char *name, const struct ipv4_addr *_ip)
30 {
31         struct ipv4_addr ip;
32
33         ip.addr = htonl(_ip->addr);
34
35         ndr->print(ndr, "%-25s: %s", name, sys_inet_ntoa(ip));
36 }
37
38 /*
39   build a GUID from a string
40 */
41 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
42 {
43         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
44         uint32_t time_low;
45         uint32_t time_mid, time_hi_and_version;
46         uint32_t clock_seq[2];
47         uint32_t node[6];
48         int i;
49
50         if (s == NULL) {
51                 return NT_STATUS_INVALID_PARAMETER;
52         }
53
54         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
55                          &time_low, &time_mid, &time_hi_and_version, 
56                          &clock_seq[0], &clock_seq[1],
57                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
58                 status = NT_STATUS_OK;
59         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
60                                 &time_low, &time_mid, &time_hi_and_version, 
61                                 &clock_seq[0], &clock_seq[1],
62                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
63                 status = NT_STATUS_OK;
64         }
65
66         if (!NT_STATUS_IS_OK(status)) {
67                 return status;
68         }
69
70         guid->time_low = time_low;
71         guid->time_mid = time_mid;
72         guid->time_hi_and_version = time_hi_and_version;
73         guid->clock_seq[0] = clock_seq[0];
74         guid->clock_seq[1] = clock_seq[1];
75         for (i=0;i<6;i++) {
76                 guid->node[i] = node[i];
77         }
78
79         return NT_STATUS_OK;
80 }
81
82 /* generate a random GUID */
83 struct GUID GUID_random(void)
84 {
85         struct GUID guid;
86
87         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
88         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
89         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
90
91         return guid;
92 }
93
94 /* generate a random GUID */
95 struct GUID GUID_zero(void)
96 {
97         struct GUID guid;
98
99         ZERO_STRUCT(guid);
100
101         return guid;
102 }
103
104 BOOL GUID_all_zero(const struct GUID *u)
105 {
106         if (u->time_low != 0 ||
107             u->time_mid != 0 ||
108             u->time_hi_and_version != 0 ||
109             u->clock_seq[0] != 0 ||
110             u->clock_seq[1] != 0 ||
111             !all_zero(u->node, 6)) {
112                 return False;
113         }
114         return True;
115 }
116
117 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
118 {
119         if (u1->time_low != u2->time_low ||
120             u1->time_mid != u2->time_mid ||
121             u1->time_hi_and_version != u2->time_hi_and_version ||
122             u1->clock_seq[0] != u2->clock_seq[0] ||
123             u1->clock_seq[1] != u2->clock_seq[1] ||
124             memcmp(u1->node, u2->node, 6) != 0) {
125                 return False;
126         }
127         return True;
128 }
129
130 /*
131   its useful to be able to display these in debugging messages
132 */
133 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
134 {
135         return talloc_asprintf(mem_ctx, 
136                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
137                                guid->time_low, guid->time_mid,
138                                guid->time_hi_and_version,
139                                guid->clock_seq[0],
140                                guid->clock_seq[1],
141                                guid->node[0], guid->node[1],
142                                guid->node[2], guid->node[3],
143                                guid->node[4], guid->node[5]);
144 }
145
146 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
147 {
148         return talloc_asprintf(mem_ctx, 
149                                "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
150                                guid->time_low, guid->time_mid,
151                                guid->time_hi_and_version,
152                                guid->clock_seq[0],
153                                guid->clock_seq[1],
154                                guid->node[0], guid->node[1],
155                                guid->node[2], guid->node[3],
156                                guid->node[4], guid->node[5]);
157 }
158
159 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
160 {
161         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
162 }
163
164 BOOL policy_handle_empty(struct policy_handle *h) 
165 {
166         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
167 }