dnsp: Parse TXT records
[Samba/gebeck_regimport.git] / librpc / ndr / ndr_dnsp.c
blob256638aed3399e585aec6f44fd451cc1c28bfddf
1 /*
2 Unix SMB/CIFS implementation.
4 Manually parsed structures found in DNSP
6 Copyright (C) Andrew Tridgell 2010
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.
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.
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/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_dnsp.h"
26 print a dnsp_name
28 _PUBLIC_ void ndr_print_dnsp_name(struct ndr_print *ndr, const char *name,
29 const char *dns_name)
31 ndr->print(ndr, "%-25s: %s", name, dns_name);
35 pull a dnsp_name
37 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_name(struct ndr_pull *ndr, int ndr_flags, const char **name)
39 uint8_t len, count;
40 int i;
41 uint32_t total_len;
42 char *ret;
44 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
45 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &count));
47 ret = talloc_strdup(ndr->current_mem_ctx, "");
48 if (!ret) {
49 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp");
51 total_len = 1;
53 for (i=0; i<count; i++) {
54 uint8_t sublen, newlen;
55 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &sublen));
56 newlen = total_len + sublen;
57 if (i != count-1) {
58 newlen++; /* for the '.' */
60 ret = talloc_realloc(ndr->current_mem_ctx, ret, char, newlen);
61 if (!ret) {
62 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp");
64 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], sublen));
65 if (i != count-1) {
66 ret[newlen-2] = '.';
68 ret[newlen-1] = 0;
69 total_len = newlen;
71 (*name) = ret;
72 NDR_PULL_ALIGN(ndr, 2);
73 return NDR_ERR_SUCCESS;
76 enum ndr_err_code ndr_push_dnsp_name(struct ndr_push *ndr, int ndr_flags, const char *name)
78 int count, total_len, i;
79 /* count the dots */
80 for (count=i=0; name[i]; i++) {
81 if (name[i] == '.') count++;
83 total_len = strlen(name) + 1;
84 if (total_len > 255 || count > 255) {
85 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
86 "dns_name of length %d larger than 255", total_len);
88 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
89 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)count));
90 for (i=0; i<count; i++) {
91 const char *p = strchr(name, '.');
92 size_t sublen = p?(p-name):strlen(name);
93 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)sublen));
94 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)name, sublen));
95 name += sublen + 1;
97 NDR_PUSH_ALIGN(ndr, 2);
99 return NDR_ERR_SUCCESS;
103 print a dnsp_string
105 _PUBLIC_ void ndr_print_dnsp_string(struct ndr_print *ndr, const char *name,
106 const char *dns_string)
108 ndr->print(ndr, "%-25s: %s", name, dns_string);
112 pull a dnsp_string
114 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string(struct ndr_pull *ndr, int ndr_flags, const char **string)
116 uint8_t len;
117 uint32_t total_len;
118 char *ret;
120 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
122 ret = talloc_strdup(ndr->current_mem_ctx, "");
123 if (!ret) {
124 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp");
126 total_len = 1;
127 ret = talloc_zero_array(ndr->current_mem_ctx, char, len+1);
128 if (!ret) {
129 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp");
131 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], len));
132 total_len = len;
134 (*string) = ret;
135 NDR_PULL_ALIGN(ndr, 1);
136 return NDR_ERR_SUCCESS;
139 enum ndr_err_code ndr_push_dnsp_string(struct ndr_push *ndr, int ndr_flags, const char *string)
141 int total_len;
142 total_len = strlen(string) + 1;
143 if (total_len > 255) {
144 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
145 "dns_name of length %d larger than 255", total_len);
147 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
148 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)string, total_len - 1));
149 NDR_PUSH_ALIGN(ndr, 1);
151 return NDR_ERR_SUCCESS;