source: vendor/current/librpc/ndr/ndr_dnsp.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 6.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Manually parsed structures found in DNSP
5
6 Copyright (C) Andrew Tridgell 2010
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "librpc/gen_ndr/ndr_dnsp.h"
24
25/*
26 print a dnsp_name
27*/
28_PUBLIC_ void ndr_print_dnsp_name(struct ndr_print *ndr, const char *name,
29 const char *dns_name)
30{
31 ndr->print(ndr, "%-25s: %s", name, dns_name);
32}
33
34/*
35 pull a dnsp_name
36*/
37_PUBLIC_ enum ndr_err_code ndr_pull_dnsp_name(struct ndr_pull *ndr, int ndr_flags, const char **name)
38{
39 uint8_t len, count, termination;
40 int i;
41 uint32_t total_len, raw_offset;
42 char *ret;
43
44 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
45 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &count));
46
47 raw_offset = ndr->offset;
48
49 ret = talloc_strdup(ndr->current_mem_ctx, "");
50 if (!ret) {
51 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
52 }
53 total_len = 1;
54
55 for (i=0; i<count; i++) {
56 uint8_t sublen, newlen;
57 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &sublen));
58 newlen = total_len + sublen;
59 if (i != count-1) {
60 newlen++; /* for the '.' */
61 }
62 ret = talloc_realloc(ndr->current_mem_ctx, ret, char, newlen);
63 if (!ret) {
64 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
65 }
66 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], sublen));
67 if (i != count-1) {
68 ret[newlen-2] = '.';
69 }
70 ret[newlen-1] = 0;
71 total_len = newlen;
72 }
73 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &termination));
74 if (termination != 0) {
75 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - not NUL terminated");
76 }
77 if (ndr->offset > raw_offset + len) {
78 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - overrun by %u bytes",
79 ndr->offset - (raw_offset + len));
80 }
81 /* there could be additional pad bytes */
82 while (ndr->offset < raw_offset + len) {
83 uint8_t pad;
84 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &pad));
85 }
86 (*name) = ret;
87 return NDR_ERR_SUCCESS;
88}
89
90enum ndr_err_code ndr_push_dnsp_name(struct ndr_push *ndr, int ndr_flags, const char *name)
91{
92 int count, total_len, i;
93
94 /* count the dots */
95 for (count=i=0; name[i]; i++) {
96 if (name[i] == '.') count++;
97 }
98 total_len = strlen(name) + 1;
99
100 /* cope with names ending in '.' */
101 if (name[strlen(name)-1] != '.') {
102 total_len++;
103 count++;
104 }
105 if (total_len > 255 || count > 255) {
106 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
107 "dns_name of length %d larger than 255", total_len);
108 }
109 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
110 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)count));
111 for (i=0; i<count; i++) {
112 const char *p = strchr(name, '.');
113 size_t sublen = p?(p-name):strlen(name);
114 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)sublen));
115 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)name, sublen));
116 name += sublen + 1;
117 }
118 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, 0));
119
120 return NDR_ERR_SUCCESS;
121}
122
123/*
124 print a dnsp_string
125*/
126_PUBLIC_ void ndr_print_dnsp_string(struct ndr_print *ndr, const char *name,
127 const char *dns_string)
128{
129 ndr->print(ndr, "%-25s: %s", name, dns_string);
130}
131
132/*
133 pull a dnsp_string
134*/
135_PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string(struct ndr_pull *ndr, int ndr_flags, const char **string)
136{
137 uint8_t len;
138 char *ret;
139
140 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
141
142 ret = talloc_strdup(ndr->current_mem_ctx, "");
143 if (!ret) {
144 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
145 }
146 ret = talloc_zero_array(ndr->current_mem_ctx, char, len + 1);
147 if (!ret) {
148 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
149 }
150 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)ret, len));
151
152 (*string) = ret;
153 NDR_PULL_ALIGN(ndr, 1);
154 return NDR_ERR_SUCCESS;
155}
156
157enum ndr_err_code ndr_push_dnsp_string(struct ndr_push *ndr, int ndr_flags, const char *string)
158{
159 int total_len;
160 total_len = strlen(string);
161 if (total_len > 255) {
162 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
163 "dns_name of length %d larger than 255", total_len);
164 }
165 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
166 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)string, total_len));
167
168 return NDR_ERR_SUCCESS;
169}
170
171/*
172 * print a dnsp_string_list
173 */
174_PUBLIC_ void ndr_print_dnsp_string_list(struct ndr_print *ndr, const char *name,
175 const struct dnsp_string_list *list)
176{
177 uint32_t i;
178
179 ndr->no_newline = true;
180 for (i=0; i<ndr->depth; i++) {
181 ndr->print(ndr, " ");
182 }
183 ndr->print(ndr, "%-25s:", name);
184 for (i=0; i<list->count; i++) {
185 ndr->print(ndr, " \"%s\"", list->str[i]);
186 }
187 ndr->print(ndr, "\n");
188 ndr->no_newline = false;
189}
190
191/*
192 * pull a dnsp_string_list
193 */
194_PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string_list(struct ndr_pull *ndr, int ndr_flags, struct dnsp_string_list *list)
195{
196 list->count = 0;
197 list->str = talloc_array(ndr->current_mem_ctx, const char *,
198 list->count);
199 if (! list->str) {
200 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
201 }
202
203 while (ndr->offset < ndr->data_size) {
204 list->str = talloc_realloc(ndr->current_mem_ctx, list->str,
205 const char *, list->count+1);
206 if (! list->str) {
207 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
208 }
209 NDR_CHECK(ndr_pull_dnsp_string(ndr, ndr_flags, &list->str[list->count]));
210 list->count++;
211 }
212
213 return NDR_ERR_SUCCESS;
214}
215
216enum ndr_err_code ndr_push_dnsp_string_list(struct ndr_push *ndr, int ndr_flags, const struct dnsp_string_list *list)
217{
218 uint8_t i;
219
220 for (i=0; i<list->count; i++) {
221 NDR_CHECK(ndr_push_dnsp_string(ndr, ndr_flags, list->str[i]));
222 }
223 return NDR_ERR_SUCCESS;
224}
225
226enum ndr_err_code ndr_dnsp_string_list_copy(TALLOC_CTX *mem_ctx,
227 const struct dnsp_string_list *src,
228 struct dnsp_string_list *dst)
229{
230 size_t i;
231
232 dst->count = 0;
233 dst->str = talloc_zero_array(mem_ctx, const char *, src->count);
234 if (dst->str == NULL) {
235 return NDR_ERR_ALLOC;
236 }
237
238 for (i = 0; i < src->count; i++) {
239 dst->str[i] = talloc_strdup(dst->str, src->str[i]);
240 if (dst->str[i] == NULL) {
241 TALLOC_FREE(dst->str);
242 return NDR_ERR_ALLOC;
243 }
244 }
245
246 dst->count = src->count;
247 return NDR_ERR_SUCCESS;
248}
Note: See TracBrowser for help on using the repository browser.